Chronic

 view release on metacpan or  search on metacpan

docs/design.txt  view on Meta::CPAN


Chronic constraints-based scheduling algorithm: 

    The scheduler maintains three kinds of timers:

            ``constraint_wait'' - per constraint
            ``task_wait'' - per task
            ``scheduler_wait'' - per scheduler

        constraint_wait is what ::Constraint::wait() returns, immediately
        after the call to met().

        task_wait is the smallest of constraint_wait's for a
        task.

    When a constraint_wait <= 0, it's met() method is called to see if the
    constraint was met. wait() must be called after the met() function to
    determine the wait() before the constraint should be checked again
    (ie. before the next met() call). wait() should be called immediately
    after met(), since the wait() value is actually computed by met(), 
    and wait() just returns it.

    The scheduler also maintains an "active" timer for each constraint.
    This active value is number of seconds for which the constrain has
    been active.

    The algorithm is as follows: 

    1. sleep_for_unit_time()

    2. If scheduler_wait > 0, sleep(scheduler_wait);

    3. Choose the next task from the list.

       If task_wait > 0, goto step 1
        
    4. For every constraint of the task: 

        a. Call ::constraint::met() followed by 
            ::constrain::wait()

        c. set time_wait to wait if wait > time_wait

        c. If met(),
 
            -> reset constraint_active timer if 
                not running
            -> if (constraint_active > reqd_active) 
                constraint_active
                    -> last_ran = time();
                    -> execute the program

        c. If not met(), 
            do nothing.

        d. Goto Slep 1.
                

A tasks consists of: 

    1. A ``frequency'' in seconds (the task should be executed no 
        more than once in ``frequency'' seconds.

    2. A ``command'' to execute.

    3. One or more ``constraints'' that must be met before the task is
       run.

    4. A ``last_ran'' time that is used by the scheduler to determine
       if the task is in or outside the ``frequency'' threshold. If
       ``last_run'' is not provided, Chronic assumes a ``last_time''
       of 0, which means the task should be scheduled soon as
       constraints are met.

    5. A ``hard_limit'' is an optional frequency that tells chronic to 
       schedule the task irrespective of the constraints if the task
       has not been executed in ``hard_limit'' seconds.

Schedule:

Is a set of tasks, represented as hash references. Example of a schedule
of two tasks:

 '_schedule' => [
                  {
                    'constraints' => {
                                 'Freq' => {
                                       'active' => '1',
                                       'thresholds' => [
                                                         'OnceIn',
                                                         '60'
                                                       ]
                                     },
                                 'DiskIO' => {
                                         'active' => '600',
                                         'thresholds' => [
                                                           '20',
                                                           '20'
                                                         ]
                                       }
                                 },
                    '_task_wait' => bless( {
                                         'direction' => 'down',
                                         'starttime' => 1083119049,
                                         'value' => 0,
                                         'running' => 1



( run in 0.912 second using v1.01-cache-2.11-cpan-39bf76dae61 )