CTKlib

 view release on metacpan or  search on metacpan

lib/CTK/Skel/Daemon.pm  view on Meta::CPAN

See L<CTK::Daemon/"init">

%PODSIG%head2 run

See L<CTK::Daemon/"run">

%PODSIG%head2 down

See L<CTK::Daemon/"down">

%PODSIG%head1 HISTORY

%PODSIG%over

%PODSIG%item B<%PROJECT_VERSION% %GMT%>

Init version

%PODSIG%back

See C<Changes> file

%PODSIG%head1 SEE ALSO

L<CTK>, L<CTK::Daemon>

%PODSIG%head1 AUTHOR

%AUTHOR% E<lt>%ADMIN%E<gt>

%PODSIG%head1 COPYRIGHT

Copyright (C) %YEAR% %AUTHOR%. All Rights Reserved

%PODSIG%head1 LICENSE

This program is distributed under the GNU GPL v3.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

See C<LICENSE> file

%PODSIG%cut

use vars qw($VERSION);
$VERSION = '%PROJECT_VERSION%';

use base qw/CTK::Daemon/;

use constant {
    FORKS   => 1,
    INTERVAL=> 1,
    MAXITER => 10, # 10 iterations max
};

sub new {
    my $class = shift;
    my $name = shift;
    my %daemon_options = @_;
    $daemon_options{forks} //= FORKS;
    my $self = $class->SUPER::new($name, %daemon_options);
    $self->{interval} = $daemon_options{interval} || INTERVAL;
    $self->logger->log_info("Daemon object created");
    return $self;
}

# Before running
sub init {
    my $self = shift; # Daemon object
    $self->logger->log_info("Init here!");
}

sub run {
    my $self = shift; # Daemon object
    my $ctk = $self->get_ctk; # CTK object
    my $logger = $self->logger; # Logger from daemon object for internal only (in Daemon context)
    return 1 unless $self->ok; # Return with true status while any error occurred

    my $iteration = 0; # Iteration number
    my $interval = abs($self->{interval}) || INTERVAL; # Time interval. 1 op per n sec
    $logger->log_info("Runned (ID=%d; PID=%d; Interval=%d)",
        $self->{workerident},
        $self->{workerpid},
        $interval,
    );

    # Cycle
    while ($self->ok) { # Check it every time
        $iteration++;
        last if $iteration > MAXITER;
        $logger->log_info("=> iteration (ID=%d; PID=%d; Iteration=%d/%d)",
            $self->{workerident},
            $self->{workerpid},
            $iteration, MAXITER
        );

        # If occurred usual error:
        #    $logger->log_error("...");
        #    next;

        # If occurred exception error
        #    $logger->log_crit("...");
        #    $self->exception(1);
        #    last;

        # For skip this loop
        #    $self->skip(1);
        #    next;

        if ($ctk->testmode && $iteration == 3) {
            $ctk->error("Test error, go to next iteration");
            next;
        }



( run in 0.693 second using v1.01-cache-2.11-cpan-96521ef73a4 )