The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

POE::Component::Cron - Schedule POE Events using a cron spec

SYNOPSIS

    use POE::Component::Cron;
    use DateTime::Event::Crontab;
    use DateTime::Event::Random;

    $s1 = POE::Session->create(
        inline_states => {
            _start => sub {
                $_[KERNEL]->delay( _die_, 120 );
              }

              Tick => sub {
                print 'tick ', scalar localtime, "\n";
              },

            Tock => sub {
                print 'tock ', scalar localtime, "\n";
              }

              _die_ => sub {
                print "_die_";
              }
        }
    );

    # crontab DateTime set the hard way
    $sched1 = POE::Component::Cron->add(
        $s1 => Tick => DateTime::Event::Cron->from_cron('* * * * *')->iterator(
            span => DateTime::Span->from_datetimes(
                start => DateTime->now,
                end   => DateTime::Infinite::Future->new
            )
        ),
    );

    # random stream of events
    $sched2 = POE::Component::Cron->add(
        $s2 => Tock => DateTime::Event::Random->(
            seconds => 5,
            start   => DateTime->now,
          )->iterator
    );

    # crontab schedule the easy way
    $sched3 =
      POE::Component::Cron->from_cron( '* * * * *' => $s2->ID => 'modify' );

    # delete some schedule of events
    $sched2->delete();

DESCRIPTION

This component encapsulates a session that sends events to client sessions on a schedule as defined by a DateTime::Set iterator. The implementation is straight forward if a little limited.

METHODS

spawn

No need to call this in normal use, add, new and from_cron all crank one of these up if it is needed. Start up a poco::cron. returns a handle that can then be added to.

add

Add a set of events to the schedule. the session and event name are passed to POE without even checking to see if they are valid and so have the same warnnigs as ->post() itself.

    $cron->add( 
        session,
        'event_name',
        DateTime::Set->iterator,
        @other_args_to_event\@session
    );

new

new is an alias for add

from_cron

Add a schedule using a simple syntax for plain old cron spec.

    POE::Component::Cron-> from_cron('*/5 */2 * * 1' => session => event);

Accepts the cron syntax as defined by DateTime::Event::Cron which is pretty the same as that used by common linux cron.

delete

remove a schedule using the handle returned from ->add, ->new or ->from_cron;

SEE ALSO

POE, perl, DateTime::Set, DateTime::Event::Cron.

AUTHOR

Chris Fedde, <cfedde@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2008 by Chris Fedde

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.3 or, at your option, any later version of Perl 5 you may have available.