AnyEvent-DateTime-Cron

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

SYNOPSIS
        AnyEvent::DateTime::Cron->new()
            ->add(
                '* * * * *'   => sub { warn "Every minute"},
                '*/2 * * * *' => sub { warn "Every second minute"},
              )
            ->start
            ->recv

        $cron = AnyEvent::DateTime::Cron->new();
        $cron->debug(1)->add(
            '* * * * *', name   => 'job_name', single => 1,  sub {'foo'},
            ...
        );

        $cron->delete($job_id,$job_id...)

        $cv = $cron->start;
        $cv->recv;

        AnyEvent::DateTime::Cron->new(time_zone => 'local');

README  view on Meta::CPAN

  add()
        $cron->add(
            '* * * * *',                                     sub {...},
            '* * * * *', name   => 'job_name', single => 1,  sub {...},
            ...
        );

    Use "add()" to add new cron jobs. It accepts a list of crontab entries,
    optional paremeters and callbacks.

    The "name" parameter is useful for debugging, otherwise the
    auto-assigned "ID" is used instead.

    The "single" parameter, if "true", will only allow a single instance of
    a job to run at any one time.

    New jobs can be added before running, or while running.

    See "CALLBACKS" for more.

  delete()

README  view on Meta::CPAN

        $cron->stop()

    Used to shutdown the cron loop gracefully. You can also shutdown the
    cron loop by sending a "TERM" signal to the process.

  jobs()
        $job = $cron->jobs

    Returns a hashref containing all the current cron jobs.

  debug()
        $cron->debug(1|0)

    Turn on debugging.

CALLBACKS
    A callback is a coderef (eg an anonymous subroutine) which will be
    called every time your job is triggered. Callbacks should use "AnyEvent"
    themselves, so that they run asynchronously, otherwise they can block
    the execution of the cron loop, delaying other jobs.

    Two parameters are passed to your callback: the main $cv of the cron
    loop, and the $job_description which contains various details about the
    current job.

lib/AnyEvent/DateTime/Cron.pm  view on Meta::CPAN

    	die "Unknown param '$key'" unless $key =~ /^(time_zone|quartz)$/;
    }

    $params{time_zone} = DateTime::TimeZone->new(name => $params{time_zone})
        if $params{time_zone};

    $params{quartz} = 0 unless defined $params{quartz};

    return bless {
        _jobs      => {},
        _debug     => 0,
        _id        => 0,
        _running   => 0,
        _time_zone => $params{time_zone},
        _quartz    => $params{quartz},
    }, $class;
}

#===================================
sub add {
#===================================

lib/AnyEvent/DateTime/Cron.pm  view on Meta::CPAN

}

#===================================
sub delete {
#===================================
    my $self = shift;
    my @ids = ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_;

    for (@ids) {
        print STDERR "Deleting job '$_'\n"
            if $self->{_debug};

        if ( my $job = delete $self->{_jobs}{$_} ) {
            $job->{watchers} = {};
        }
        elsif ( $self->{_debug} ) {
            print STDERR "Job '$_' not found\n";
        }
    }
    return $self;
}

#===================================
sub start {
#===================================
    my $self = shift;
    my $cv = $self->{_cv} = AnyEvent->condvar;

    $cv->begin( sub { $self->stop } );

    $self->{_signal} = AnyEvent->signal(
        signal => 'TERM',
        cb     => sub {
            print STDERR "Shutting down\n" if $self->{_debug};
            $cv->end;
        }
    );
    $self->{_running} = 1;
    $self->_schedule( values %{ $self->{_jobs} } );

    return $cv;
}

#===================================

lib/AnyEvent/DateTime/Cron.pm  view on Meta::CPAN

#===================================
sub _schedule {
#===================================
    my $self = shift;

    my $time_zone = $self->{_time_zone};

    AnyEvent->now_update();
    my $now_epoch = AnyEvent->now;
    my $now       = DateTime->from_epoch( epoch => $now_epoch );
    my $debug     = $self->{_debug};

    $now->set_time_zone($time_zone) if $time_zone;

    for my $job (@_) {
        my $name       = $job->{name};

        my $next_run;
        if ($self->{_quartz}) {
            $next_run = $job->{event}->get_next_valid_time_after($now);
        }
        else {
            $next_run = $job->{event}->next($now);
        }

        $next_run->set_time_zone($time_zone) if $time_zone;

        my $next_epoch = $next_run->epoch;
        my $delay      = $next_epoch - $now_epoch;

        print STDERR "Scheduling job '$name' for: $next_run\n"
            if $debug;

        my $run_event = sub {
            print STDERR "Starting job '$name'\n"
                if $debug;

            $self->{_cv}->begin;
            delete $job->{watchers}{$next_epoch};

            $self->_schedule($job);

            if ( $job->{single} && $job->{running}++ ) {
                print STDERR "Skipping job '$name' - still running\n"
                    if $debug;
            }
            else {
                eval { $job->{cb}->( $self->{_cv}, $job ); 1 }
                    or warn $@ || 'Unknown error';
                delete $job->{running};
                print STDERR "Finished job '$name'\n"
                    if $debug;
            }

            $self->{_cv}->end;
        };

        $job->{watchers}{$next_epoch} = AnyEvent->timer(
            after => $delay,
            cb    => $run_event
        );
    }
}

#===================================
sub debug {
#===================================
    my $self = shift;
    $self->{_debug} = shift if @_;
    return $self;
}

#===================================
sub jobs { shift->{_jobs} }
#===================================

1;

=pod

lib/AnyEvent/DateTime/Cron.pm  view on Meta::CPAN


    AnyEvent::DateTime::Cron->new()
        ->add(
            '* * * * *'   => sub { warn "Every minute"},
            '*/2 * * * *' => sub { warn "Every second minute"},
          )
        ->start
        ->recv

    $cron = AnyEvent::DateTime::Cron->new();
    $cron->debug(1)->add(
        '* * * * *', name   => 'job_name', single => 1,  sub {'foo'},
        ...
    );

    $cron->delete($job_id,$job_id...)

    $cv = $cron->start;
    $cv->recv;

    AnyEvent::DateTime::Cron->new(time_zone => 'local');

lib/AnyEvent/DateTime/Cron.pm  view on Meta::CPAN


    $cron->add(
        '* * * * *',                                     sub {...},
        '* * * * *', name   => 'job_name', single => 1,  sub {...},
        ...
    );

Use C<add()> to add new cron jobs.  It accepts a list of crontab entries,
optional paremeters and callbacks.

The C<name> parameter is useful for debugging, otherwise the auto-assigned
C<ID> is used instead.

The C<single> parameter, if C<true>, will only allow a single instance of
a job to run at any one time.

New jobs can be added before running, or while running.

See L</"CALLBACKS"> for more.

=head2 delete()

lib/AnyEvent/DateTime/Cron.pm  view on Meta::CPAN


Used to shutdown the cron loop gracefully. You can also shutdown the cron loop
by sending a C<TERM> signal to the process.

=head2 jobs()

    $job = $cron->jobs

Returns a hashref containing all the current cron jobs.

=head2 debug()

    $cron->debug(1|0)

Turn on debugging.

=head1 CALLBACKS

A callback is a coderef (eg an anonymous subroutine) which will be called
every time your job is triggered. Callbacks should use C<AnyEvent> themselves,
so that they run asynchronously, otherwise they can block the execution
of the cron loop, delaying other jobs.

Two parameters are passed to your callback: the main C<$cv> of the cron loop,
and the C<$job_description> which contains various details about the current



( run in 0.296 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )