Daemon-Device

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN


1.09      2021-01-10 14:39:41-08:00 America/Los_Angeles
        - Migrate to Test2::V0
        - Improve .gitignore file
        - License migrate to Artistic 2.0
        - Migrate Travis CI and Coveralls to GitHub Actions and Codecov
        - Update copyright year
        - Update Travis YAML

1.08      2019-05-13 18:03:07-07:00 America/Los_Angeles
        - Added parent_alive method; set default child sub to use it
        - Improved tests to expect not-perfectly-uniform signal responses
        - Adding Perl versions to test; annual general update for 2018

1.07      2016-11-11 08:45:38-08:00 America/Los_Angeles
        - Provided accessor to the Daemon::Control sub-object

1.06      2016-08-08 14:35:10-07:00 America/Los_Angeles
        - Fix for bug when installed not as root and a group named $user does not exist

1.05      2015-04-29 11:28:19-07:00 America/Los_Angeles

lib/Daemon/Device.pm  view on Meta::CPAN

            my @messages = map { split(/\r?\n/) } $self->{_io_dn}->getlines;
            $self->{_on_message}->( $self, @messages );
        }
    };

    if ( $self->{_child} ) {
        $self->{_child}->($self);
    }
    else {
        while (1) {
            exit unless ( $self->parent_alive );
            sleep 1;
        }
    }

    return;
}

sub ppid {
    return shift->{_ppid};
}

lib/Daemon/Device.pm  view on Meta::CPAN

    $self->{_replace_children} = $_[0] if (@_);
    return $self->{_replace_children};
}

sub parent_hup_to_child {
    my $self = shift;
    $self->{_parent_hup_to_child} = $_[0] if (@_);
    return $self->{_parent_hup_to_child};
}

sub parent_alive {
    my ($self) = @_;
    return kill( 0, $self->{_ppid} );
}

sub data {
    my $self = shift;
    return $self->{'_data'} unless (@_);

    if ( @_ == 1 ) {
        return $self->{'_data'}{ $_[0] } if ( not ref $_[0] );

lib/Daemon/Device.pm  view on Meta::CPAN

            $device->adjust_spawn(2);
            sleep 5;
        }
    }

    sub child {
        my ($device) = @_;

        while (1) {
            warn "Child $$ exists (heartbeat)\n";
            exit unless ( $device->parent_alive );
            sleep 5;
        }
    }

=head1 DESCRIPTION

This module provides a straight-forward and simple construct to creating
applications that run as daemons and fork some number of child processes.
This module leverages the excellent L<Daemon::Control> to provide the
functionality for the daemon itself, and it manages the spawning and

lib/Daemon/Device.pm  view on Meta::CPAN

This is a reference to a subroutine containing the code that should be executed
in every child process.

    exit Daemon::Device->new(
        daemon => \%daemon_control_settings,
        child  => sub {
            my ($device) = @_;

            while (1) {
                warn "Child $$ exists (heartbeat)\n";
                exit unless ( $device->parent_alive );
                sleep 5;
            }
        },
    )->run;

It's expected that if you need to keep the parent running, you'll implement
something in this subroutine that will do that, like a C<while> loop.
If "child" is not defined, then the child will sit around and wait forever.
Not sure why you'd want to spawn children and then let them be lazy like this
since idle hands are the devil's playthings, though.

lib/Daemon/Device.pm  view on Meta::CPAN

number.

=head2 replace_children, parent_hup_to_child

These are simple get-er/set-er methods for the C<replace_children> and
C<parent_hup_to_child> values, allowing you to change them during runtime.
This should be done in parents. Remember that data values are copied into
children during spawning (i.e. forking), so changing these values in children
is meaningless.

=head2 parent_alive

The C<parent_alive> method returns true if the daemon parent still lives or
false if it doesn't live. This is useful when writing child code, since a child
should periodically check to see if it's an orphan.

    exit Daemon::Device->new(
        daemon => \%daemon_control_settings,
        child  => sub {
            my ($self) = @_;
            while (1) {
                exit unless ( $self->parent_alive );
                sleep 1;
            }
        },
    )->run;

=head1 DATA

Each parent and child have a simple data storage mechanism in them under the
"data" parameter and "data" method, all of which is optional. To use it,
you can, if you elect, pass the "data" parameter to C<new()>.

t/module.t  view on Meta::CPAN

    parent => sub {
        my ($device) = @_;
        warn "PARENT $$ start\n";
        sleep 1 while (1);
    },

    child => sub {
        my ($device) = @_;
        warn "CHILD $$ start\n";
        while (1) {
            exit unless ( $device->parent_alive );
            sleep 1;
        }
    },

    on_startup       => sub { warn "EVENT $$ on_startup\n"       },
    on_shutdown      => sub { warn "EVENT $$ on_shutdown\n"      },
    on_spawn         => sub { warn "EVENT $$ on_spawn\n"         },
    on_parent_hup    => sub { warn "EVENT $$ on_parent_hup\n"    },
    on_child_hup     => sub { warn "EVENT $$ on_child_hup\n"     },
    on_parent_death  => sub { warn "EVENT $$ on_parent_death\n"  },



( run in 1.184 second using v1.01-cache-2.11-cpan-39bf76dae61 )