.\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Catalyst::Plugin::Scheduler 3pm" .TH Catalyst::Plugin::Scheduler 3pm "2018-03-30" "perl v5.26.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Catalyst::Plugin::Scheduler \- Schedule events to run in a cron\-like fashion .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Catalyst qw/Scheduler/; \& \& # run remove_sessions in the Cron controller every hour \& _\|_PACKAGE_\|_\->schedule( \& at => \*(Aq0 * * * *\*(Aq, \& event => \*(Aq/cron/remove_sessions\*(Aq \& ); \& \& # Run a subroutine at 4:05am every Sunday \& _\|_PACKAGE_\|_\->schedule( \& at => \*(Aq5 4 * * sun\*(Aq, \& event => \e&do_stuff, \& ); \& \& # A long\-running scheduled event that must be triggered \& # manually by an authorized user \& _\|_PACKAGE_\|_\->schedule( \& trigger => \*(Aqrebuild_search_index\*(Aq, \& event => \*(Aq/cron/rebuild_search_index\*(Aq, \& ); \& $ wget \-q http://www.myapp.com/?schedule_trigger=rebuild_search_index .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This plugin allows you to schedule events to run at recurring intervals. Events will run during the first request which meets or exceeds the specified time. Depending on the level of traffic to the application, events may or may not run at exactly the correct time, but it should be enough to satisfy many basic scheduling needs. .SH "CONFIGURATION" .IX Header "CONFIGURATION" Configuration is optional and is specified in MyApp\->config\->{scheduler}. .SS "logging" .IX Subsection "logging" Set to 1 to enable logging of events as they are executed. This option is enabled by default when running under \-Debug mode. Errors are always logged regardless of the value of this option. .SS "time_zone" .IX Subsection "time_zone" The time zone of your system. This will be autodetected where possible, or will default to \s-1UTC\s0 (\s-1GMT\s0). You can override the detection by providing a valid DateTime time zone string, such as 'America/New_York'. .SS "state_file" .IX Subsection "state_file" The current state of every event is stored in a file. By default this is \&\f(CW$APP_HOME\fR/scheduler.state. This file is created on the first request if it does not already exist. .SS "yaml_file" .IX Subsection "yaml_file" The location of the optional \s-1YAML\s0 event configuration file. By default this is \f(CW$APP_HOME\fR/scheduler.yml. .SS "hosts_allow" .IX Subsection "hosts_allow" This option specifies \s-1IP\s0 addresses for trusted users. This option defaults to 127.0.0.1. Multiple addresses can be specified by using an array reference. This option is used for both events where auto_run is set to 0 and for manually-triggered events. .PP .Vb 5 \& _\|_PACKAGE_\|_\->config\->{scheduler}\->{hosts_allow} = \*(Aq192.168.1.1\*(Aq; \& _\|_PACKAGE_\|_\->config\->{scheduler}\->{hosts_allow} = [ \& \*(Aq127.0.0.1\*(Aq, \& \*(Aq192.168.1.1\*(Aq \& ]; .Ve .SH "SCHEDULING" .IX Header "SCHEDULING" .SS "\s-1AUTOMATED EVENTS\s0" .IX Subsection "AUTOMATED EVENTS" Events are scheduled by calling the class method \f(CW\*(C`schedule\*(C'\fR. .PP .Vb 4 \& MyApp\->schedule( \& at => \*(Aq0 * * * *\*(Aq, \& event => \*(Aq/cron/remove_sessions\*(Aq, \& ); \& \& package MyApp::Controller::Cron; \& \& sub remove_sessions : Private { \& my ( $self, $c ) = @_; \& \& $c\->delete_expired_sessions; \& } .Ve .PP \fIat\fR .IX Subsection "at" .PP The time to run an event is specified using \fIcrontab\fR\|(5)\-style syntax. .PP .Vb 4 \& 5 0 * * * # 5 minutes after midnight, every day \& 15 14 1 * * # run at 2:15pm on the first of every month \& 0 22 * * 1\-5 # run at 10 pm on weekdays \& 5 4 * * sun # run at 4:05am every Sunday .Ve .PP From \fIcrontab\fR\|(5): .PP .Vb 7 \& field allowed values \& \-\-\-\-\- \-\-\-\-\-\-\-\-\-\-\-\-\-\- \& minute 0\-59 \& hour 0\-23 \& day of month 1\-31 \& month 0\-12 (or names, see below) \& day of week 0\-7 (0 or 7 is Sun, or use names) .Ve .PP Instead of the first five fields, one of seven special strings may appear: .PP .Vb 9 \& string meaning \& \-\-\-\-\-\- \-\-\-\-\-\-\- \& @yearly Run once a year, "0 0 1 1 *". \& @annually (same as @yearly) \& @monthly Run once a month, "0 0 1 * *". \& @weekly Run once a week, "0 0 * * 0". \& @daily Run once a day, "0 0 * * *". \& @midnight (same as @daily) \& @hourly Run once an hour, "0 * * * *". .Ve .PP \fIevent\fR .IX Subsection "event" .PP The event to run at the specified time can be either a Catalyst private action path or a coderef. Both types of event methods will receive the \f(CW$c\fR object from the current request, but you must not rely on any request-specific information present in \f(CW$c\fR as it will be from a random user request at or near the event's specified run time. .PP Important: Methods used for events should be marked \f(CW\*(C`Private\*(C'\fR so that they can not be executed via the browser. .PP \fIauto_run\fR .IX Subsection "auto_run" .PP The auto_run parameter specifies when the event is allowed to be executed. By default this option is set to 1, so the event will be executed during the first request that matches the specified time in \f(CW\*(C`at\*(C'\fR. .PP If set to 0, the event will only run when a request is made by a user from an authorized address. The purpose of this option is to allow long-running tasks to execute only for certain users. .PP .Vb 5 \& MyApp\->schedule( \& at => \*(Aq0 0 * * *\*(Aq, \& event => \*(Aq/cron/rebuild_search_index\*(Aq, \& auto_run => 0, \& ); \& \& package MyApp::Controller::Cron; \& \& sub rebuild_search_index : Private { \& my ( $self, $c ) = @_; \& \& # rebuild the search index, this may take a long time \& } .Ve .PP Now, the search index will only be rebuilt when a request is made from a user whose \s-1IP\s0 address matches the list in the \f(CW\*(C`hosts_allow\*(C'\fR config option. To run this event, you probably want to ping the app from a cron job. .PP .Vb 1 \& 0 0 * * * wget \-q http://www.myapp.com/ .Ve .SS "\s-1MANUAL EVENTS\s0" .IX Subsection "MANUAL EVENTS" To create an event that does not run on a set schedule and must be manually triggered, you can specify the \f(CW\*(C`trigger\*(C'\fR option instead of \f(CW\*(C`at\*(C'\fR. .PP .Vb 4 \& _\|_PACKAGE_\|_\->schedule( \& trigger => \*(Aqsend_email\*(Aq, \& event => \*(Aq/events/send_email\*(Aq, \& ); .Ve .PP The event may then be triggered by a standard web request from an authorized user. The trigger to run is specified by using a special \s-1GET\s0 parameter, \&'schedule_trigger'; the path requested does not matter. .PP .Vb 1 \& http://www.myapp.com/?schedule_trigger=send_email .Ve .PP By default, manual events may only be triggered by requests made from localhost (127.0.0.1). To allow other addresses to run events, use the configuration option \*(L"hosts_allow\*(R". .SH "SCHEDULING USING A YAML FILE" .IX Header "SCHEDULING USING A YAML FILE" As an alternative to using the \fIschedule()\fR method, you may define scheduled events in an external \s-1YAML\s0 file. By default, the plugin looks for the existence of a file called \f(CW\*(C`scheduler.yml\*(C'\fR in your application's home directory. You can change the filename using the configuration option \&\*(L"yaml_file\*(R". .PP Modifications to this file will be re-read once per minute during the normal event checking process. .PP Here's an example \s-1YAML\s0 configuration file with 4 events. Each event is denoted with a '\-' character, followed by the same parameters used by the \&\f(CW\*(C`schedule\*(C'\fR method. Note that coderef events are not supported by the \s-1YAML\s0 file. .PP .Vb 10 \& \-\-\- \& \- at: \*(Aq* * * * *\*(Aq \& event: /cron/delete_sessions \& \- event: /cron/send_email \& trigger: send_email \& \- at: \*(Aq@hourly\*(Aq \& event: /cron/hourly \& \- at: 0 0 * * * \& auto_run: 0 \& event: /cron/rebuild_search_index .Ve .SH "SECURITY" .IX Header "SECURITY" All events are run inside of an eval container. This protects the user from receiving any error messages or page crashes if an event fails to run properly. All event errors are logged, even if logging is disabled. .SH "PLUGIN SUPPORT" .IX Header "PLUGIN SUPPORT" Other plugins may register scheduled events if they need to perform periodic maintenance. Plugin authors, \fBbe sure to inform your users\fR if you do this! Events should be registered from a plugin's \f(CW\*(C`setup\*(C'\fR method. .PP .Vb 3 \& sub setup { \& my $c = shift; \& $c\->maybe::next::method(@_); \& \& if ( $c\->can(\*(Aqschedule\*(Aq) ) { \& $c\->schedule( \& at => \*(Aq0 * * * *\*(Aq, \& event => \e&cleanup, \& ); \& } \& } .Ve .SH "CAVEATS" .IX Header "CAVEATS" The time at which an event will run is determined completely by the requests made to the application. Apps with heavy traffic may have events run at very close to the correct time, whereas apps with low levels of traffic may see events running much later than scheduled. If this is a problem, you can use a real cron entry that simply hits your application at the desired time. .PP .Vb 1 \& 0 * * * * wget \-q http://www.myapp.com/ .Ve .PP Events which consume a lot of time will slow the request processing for the user who triggers the event. For these types of events, you should use auto_run => 0 or manual event triggering. .SH "PERFORMANCE" .IX Header "PERFORMANCE" The plugin only checks once per minute if any events need to be run, so the overhead on each request is minimal. On my test server, the difference between running with Scheduler and without was only around 0.02% (0.004 seconds). .PP Of course, when a scheduled event runs, performance will depend on what's being run in the event. .SH "METHODS" .IX Header "METHODS" .SS "schedule" .IX Subsection "schedule" Schedule is a class method for adding scheduled events. See the \&\*(L"\s-1SCHEDULING\*(R"\*(L"\s0 in \*(R" section for more information. .SS "scheduler_state" .IX Subsection "scheduler_state" The current state of all scheduled events is available in an easy-to-use format by calling \f(CW$c\fR\->scheduler_state. You can use this data to build an admin view into the scheduling engine, for example. This same data is also displayed on the Catalyst debug screen. .PP This method returns an array reference containing a hash reference for each event. .PP .Vb 10 \& [ \& { \& \*(Aqlast_run\*(Aq => \*(Aq2005\-12\-29 16:29:33 EST\*(Aq, \& \*(Aqauto_run\*(Aq => 1, \& \*(Aqlast_output\*(Aq => 1, \& \*(Aqat\*(Aq => \*(Aq0 0 * * *\*(Aq, \& \*(Aqnext_run\*(Aq => \*(Aq2005\-12\-30 00:00:00 EST\*(Aq, \& \*(Aqevent\*(Aq => \*(Aq/cron/session_cleanup\*(Aq \& }, \& { \& \*(Aqauto_run\*(Aq => 1, \& \*(Aqat\*(Aq => \*(Aq0 0 * * *\*(Aq, \& \*(Aqnext_run\*(Aq => \*(Aq2005\-12\-30 00:00:00 EST\*(Aq, \& \*(Aqevent\*(Aq => \*(Aq/cron/build_rss\*(Aq \& }, \& ] .Ve .SH "INTERNAL METHODS" .IX Header "INTERNAL METHODS" The following methods are extended by this plugin. .IP "dispatch" 4 .IX Item "dispatch" The main scheduling logic takes place during the dispatch phase. .IP "dump_these" 4 .IX Item "dump_these" On the Catalyst debug screen, all scheduled events are displayed along with the next time they will be executed. .IP "setup" 4 .IX Item "setup" .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fIcrontab\fR\|(5) .SH "AUTHOR" .IX Header "AUTHOR" Andy Grundman, .SH "COPYRIGHT" .IX Header "COPYRIGHT" This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself.