NAME

Krang::Schedule::Daemon - Module to handle scheduled tasks in Krang.


SYNOPSIS

  use Krang::ClassLoader 'Schedule::Daemon';
  pkg('Schedule::Daemon')->run();


DESCRIPTION

This module is responsible for creating a daemon whose task is to run all scheduled tasks in Krang.

When started (using the run() method), Krang::Schedule::Daemon creates a persistant daemon (whose PID file can be found at KRANG_ROOT/tmp/schedule_daemon.pid). Once started, the daemon will poll Krang::Scheduler at a set interval (defaults to 1 second --- Is this something we want settable in KrangConf??), looking for jobs that need to be executed. If none are found, the daemon will sleep for another interval, wake up, and repeat the process.

If jobs are found, Krang::Schedule::Daemon will sort and group them based on priority, next_run time, and resource usage. Once sorted and grouped, jobs will be handed off to child processes that will actually handle execution.

When a child process exits (all tasks have been completed, or a fatal error occurred), the parent daemon will make a note of the child exiting and clear its entry from the internal table tracking all work being done.

There is a limit to the number of children that can run simultaneously, determined by the Krang::Conf config file directive ScheduleMaxChildren.

Multiple schedulers (on multiple machines) can be run without interfering with each other.

Job Execution

When a group of jobs is ready to be executed, a child process is spawned to handle the work. The workload assigned to the child process is determined by the parent daemon, and the work is completed in order of priority.

Once spawned, the parent daemon makes entries in the %active_children and %assigned_jobs hashes. %active_children is keyed by child PID, and each entry contains a list of Krang::Schedule IDs currently being processed by that child. %assigned_jobs is the opposite hash, keyed by Krang::Schedule IDs, where each key points to the child PID of the child process handling the job. These two hashes exist to keep Krang::Schedule::Daemon from assigning work-in-progress to new children.

Priority

The Priority of a job entry determines when it will get executed in relation to all other pending jobs. Priority is stored in the Krang::Schedule object as an integer value.

Priority will be adjusted by Krang::Schedule::Daemon, based on whether or not the next scheduled execution (next_run) of a job was met (e.g. if the value of job->next_run() < now(), the execution was missed).

If scheduled execution was missed, the priority of the job will be bumped up by 1, unless it is more than 1 hour late, when it is bumped by 2. This will not necessarily put the job at the front of the line, but it will get it executed sooner than it would have been.

Cleanup

When a child finishes an individual task, < Krang::Schedule-mark_as_completed($task_id) >> will be called, updating the entry in the database. Krang::Schedule will determine what the new entry in the database will look like, based on whether or not the job is a re-occuring one.

Once a child has completed all tasks assigned to it, the child will exit. The parent daemon will trap the SIG_CHLD signal sent by the exiting child, and remove that child PID and all Krang::Schedule entries from the %active_children and %assigned_jobs hashes.


INTERFACE


TODO


SEE ALSO

Krang::Schedule, Krang::Alert, Krang::Publisher