Chronic
view release on metacpan or search on metacpan
lib/Schedule/Chronic/Tab.pm view on Meta::CPAN
my %task;
my @pairs = split /;/, $entry;
my $good = 0; # track is this is a good pair
for (@pairs) {
# Extract key = value; pairs
my ($key, $value) = split /=/, $_, 2;
# Normalize key and value
$key = $normalize->($key);
$value = $normalize->($value);
next unless $key and $value;
$good = 1;
if ($key eq 'command') {
# Remove quotes from the command
$value =~ s/^"//;
$value =~ s/"$//;
$task{$key} = $value;
} elsif ($key eq 'constraint') {
# A constraint contains a constrain name followed by an
# optional list of parameters for the constraint.
my ($constraint, @thresholds) = split /,/, $value;
my @n_thresholds;
$constraint = $normalize->($constraint);
for (@thresholds) {
push @n_thresholds, $normalize->($_);
}
$task{constraints}->{$constraint} = {};
$task{constraints}->{$constraint}{thresholds} = [@n_thresholds] if
scalar @n_thresholds;
}
else {
# All other keys are read in verbatim.
$task{$key} = $value;
}
}
# If there's no command, this task is useless to us.
$good = 0 unless exists $task{command};
if ($good) {
# Initialize the task.
#
# Add a last_ran of 0 (execute soon as possible)
# if a last_ran is not available. Create a
# task_wait timer and initialize other task
# parameters.
$task{last_ran} = 0 unless exists $task{last_ran};
$task{only_once} = 0 unless exists $task{only_once};
$task{_task_wait} = new Schedule::Chronic::Timer ('down');
$task{_task_wait}->set(0);
$task{_uid} = $uid;
$task{_chrontab} = $tab;
$task{_last_rv} = 0;
push @{$self->{_schedule}}, {%task};
$tasks++;
} else {
# This entry is b0rken. Show it to the user.
# We should probably barf here and ask the user
# to correct the error. FIX.
$self->debug("Syntax error in line $linecursor of $tab - ignoring.");
}
}
$self->debug("$tasks task(s) loaded.");
close TAB;
}
sub write_chrontab {
my ($self, $tab) = @_;
open TAB, ">$tab" or die "$tab: $!\n";
# Walk over the _schedule and write all tasks to the config
# file. This essentially serializes the _schedule in a format
# as close as the original file as possible.
for (@{$self->{_schedule}}) {
my $task = $_;
unless ($$task{_chrontab} eq $tab) {
# This task belongs to another chrontab,
# skip over it.
next;
}
if ($$task{only_once} == 1 and $$task{last_ran} > 0) {
# This was an ``only_once'' task that has been
# executed once. Don't write back to the
# chrontab.
( run in 1.537 second using v1.01-cache-2.11-cpan-39bf76dae61 )