Chronic

 view release on metacpan or  search on metacpan

lib/Schedule/Chronic.pm  view on Meta::CPAN

    # we've run through all tasks at least once. This function
    # is closed under schedule() so it has access to variables
    # local to schedule.

    my $recompute_scheduler_wait = sub { 

        unless (scalar @{$schedule}) { 

            # Oops, there are no tasks. We'll set wait to
            # maximum and hope that tasks show up the next time
            # this function is called.

            $self->debug("no tasks to schedule.");
            $scheduler_wait->set($$self{max_sw});
            $self->debug("scheduler_wait: set to " . $self->time_rd($$self{max_sw}));
            return;

        }

        my $sw = $schedule->[0]->{_task_wait}->get();

        for my $task (@$schedule) { 
            if ($$task{_task_wait}->get() < $sw) {
                $sw = $$task{_task_wait}->get();;
            }
        }

        $sw = $self->{max_sw} if $sw > $self->{max_sw};

        if ($sw > 0) { 
            $scheduler_wait->set($sw);
            $self->debug("scheduler_wait: set to " . $self->time_rd($sw));
        }

    };
 
    $self->debug("entering scheduler loop...");

    while (1) { 

        # Check to see if scheduler_wait is positive.  If so, 
        # go to sleep because all task waits are larger than 
        # scheduler_wait.

        if ($scheduler_wait->get() > 0) { 
            $self->debug("nothing to schedule for " . 
                $self->time_rd($scheduler_wait->get()) . ", sleeping...");
            sleep($scheduler_wait->get());
        }

        # Walk over all tasks, checks constraints and execute tasks when
        # all constraints are met. This is section should end in

        TASK: 
        for my $task (@$schedule) {

            # print Dumper $task; 

            # A task has four components. A set of constraints, a
            # command to run when these constraints are met, the
            # last_ran time and a task wait timer which is the
            # maximum wait time returned by a constraint.

            my $constraints = $$task{constraints};
            my $task_wait   = $$task{_task_wait};
            my $command     = $$task{command};
            my $last_ran    = $$task{last_ran};
            my $uid         = $$task{_uid};
            my $only_once   = $$task{only_once};

            if ($last_ran > 0 and $only_once == 1) { 

                # This task was supposed to run ``only_once'' and it has
                # been run once before, so we will skip it.

                $task_wait->set($$self{only_once_tw});
                next TASK;

            }

            $self->debug("* $command");

            if ($task_wait->get() > 0) { 

                # Constraints have indicated that they will not be met for
                # at least sched_wait seconds.

                $self->debug("  task_wait: " . $self->time_rd($task_wait->get()));
                next TASK;

            };

            my $all_cns_met = 1;

            for my $constraint (keys %$constraints) { 

                # A constraint has two declarative components and a few
                # derived components. The declarative components are the
                # name of the constraint and the thresholds that
                # parameterize the constraint. The derived components
                # include the corresponding constraint object and other
                # transient data structures used by the scheduler.

                my $cobject = $task->{constraints}->{$constraint}->{_object};

                # Now call met() and wait()

                my ($met)  = $cobject->met();
                my ($wait) = $cobject->wait();

                if (not $met) { 

                    # The constraint wasn't met. We'll set all_cns_met to
                    # 0 and compare constraint wait with task_wait to see
                    # if we need to readjust task_wait.

                    $self->debug("  ($constraint) unmet");
                    $all_cns_met = 0;

                    if ($wait != 0 && $wait > $task_wait->get()) { 



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