Krang::Schedule - Module for scheduling events in Krang.
use Krang::ClassLoader 'Schedule';
# publish a story at a specific date
$sched = pkg('Schedule')->new(
object_type => 'story',
object_id => $story_id,
action => 'publish',
repeat => 'never',
date => $date
);
# publish a story at a specific date, specifying the version to be
# published
$sched = pkg('Schedule')->new(
object_type => 'story',
object_id => $story_id,
action => 'publish',
context => [version => $version],
date => $date
);
# publish a story at a specific date, specifying a limit to the number of
# attempts, a delay (in seconds) between each attempt, and the ID for a user
# who should be emailed after failure/success
$sched = pkg('Schedule')->new(
object_type => 'story',
object_id => $story_id,
action => 'publish',
date => $date,
failure_max_tries => 4, # try a maximum of 4 times before giving up
failure_delay_sec => 600, # wait 10 minutes between each try
failure_notify_id => 2, # if all 4 attempts fail, email user 2 to notify him/her
success_notify_id => 2, # if any attempt succeeds, email user 2 to notify him/her
);
# save the schedule entry to the database
$sched->save();
# get the ID for a schedule
$schedule_id = $schedule->schedule_id;
# Create an entry to publish a story every Monday at noon.
$sched = pkg('Schedule')->new(
object_type => 'story',
object_id => $story_id,
action => 'publish',
repeat => 'weekly',
day_of_week => 1,
hour => 12,
minute => 0
);
# Create an entry to publish a story at noon every day.
$sched = pkg('Schedule')->new(
object_type => 'story',
object_id => $story_id,
action => 'publish',
repeat => 'daily',
hour => 12,
minute => 0
);
# Create an entry to publish a story every hour on the hour.
$sched = pkg('Schedule')->new(
object_type => 'story',
object_id => $story_id,
action => 'publish',
repeat => 'hourly',
minute => 0
);
# Create an entry to publish a story every hour on the hour.
$sched = pkg('Schedule')->new(
object_type => 'story',
object_id => $story_id,
action => 'publish',
repeat => 'hourly',
minute => 0
);
# get a list of schedule objects for a given story
@schedules = pkg('Schedule')->find(
object_type => 'story',
object_id => 1
);
# load a schedule object by ID
($schedule) = pkg('Schedule')->find(schedule_id => $schedule_id);
# get the next execution time of a scheduled event
$date = $schedule->next_run;
# get the last execution time of a scheduled event
$date = $schedule->last_run;
# execute the task represented by the schedule object
$success = $schedule->execute();
# update the next_run field in the schedule object.
$schedule->update_execution_time();
# Find the default priority for a Krang::Schedule object
$priority = pkg('Schedule')->determine_priority(schedule => $schedule);
# or for the current object
$priority = $schedule->determine_priority();
This module provides the API into the Krang scheduler. It is responsible for handling events within Krang that have been scheduled by users. At this time, those events fall into one of three categories: sending alerts, publishing content (stories and/or media), and expiring content (stories and/or media).
Krang::Schedule is responsible for entering jobs into the system, and for handling the actual execution of a given job. Determining when to run a job, and the allocation of resources to run that job are handled by Krang::Schedule::Daemon.
$sched = pkg('Schedule')->new()actionobject_typeobject_idrepeatnever, hourly,
daily, weekly, monthly or interval.
The following options are also available:
daterepeat is set to
'never' then you must set this option.
hourdaily and
weekly tasks. 0 indicates 12 midnight.
minutehourly, daily
and weekly tasks.
day_of_weekweekly
tasks. This is an integer from 0 (Sun) to 6 (Sat).
day_of_monthmonthly
tasks. This is an integer set from 1 to 28, or from -1 to -28. Positive values
represent the first 28 days of each month. Negative values count backwards
from the last day of the month, with -1 being the last day, -2 being the second
to last day, etc.
day_intervalinterval tasks. This is
a positive integer.
contexttest_datepriorityNote that you should not worry about priority, except in special cases. Krang::Schedule::Daemon handles how jobs will make adjustments to priority if a scheduled task is running late.
inactivedetermine_priority(schedule => $schedule)Realize that Krang::Schedule::Daemon will raise priority as needed if a scheduled task has not been executed on time.
NOTE: This is not an accessor/mutator - to find the currently-set
priority (or to set the priority manually) of a Krang::Schedule object,
use $schedule->priority().
Priority is determined based primarily on the action being performed,
with repeat and object_type modifying the final result.
If a job is repeated, the default priority is raised 1 for weekly, 2 for daily, and 3 for hourly.
For example, a story published only once will have a priority of 8 (default), but a media object published hourly would have a priority of 4 (default 7 - 3 for hourly repeats = 4).
$sched->deleteKrang::Schedule->delete( $schedule_id )never schedules automatically after they
are run.
find(...)Fields may be matched using SQL matching. Appending ``_like'' to a field name will specify a case-insensitive SQL match.
Available search options are:
Options affecting the search and the results returned:
$sched->save$schedule = Krang::Schedule->deserialize_xml(xml => $xml, set => $set, no_update => 0)If an incoming schedule has matching fields with an existing schedule, it is ignored (not duplicated).
Note that last_run is not imported, next_run is translated to 'date'.
pkg('Schedule')->activate(object_type => $type, object_id => $id)pkg('Schedule')->inactivate(object_type => $type, object_id => $id)