summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoland Huß <roland@jolokia.org>2013-12-13 14:00:00 +0000
committerRoland Huß <roland@jolokia.org>2013-12-13 14:00:00 +0000
commitc18285e9feead4b349c6b4ea32821a8443e88b76 (patch)
treee7a261c235f577d104d44a682c8fd5c28ddec577
parentfc15e06ccd06d1ecd86e82321ae6dde23384a972 (diff)
Added new option 'timeshift' (#69177) and updated meta data and readme.
-rw-r--r--CHANGES1
-rw-r--r--MANIFEST2
-rw-r--r--META.yml6
-rw-r--r--README17
-rw-r--r--lib/Schedule/Cron.pm43
5 files changed, 59 insertions, 10 deletions
diff --git a/CHANGES b/CHANGES
index c828b5f..4f6ef38 100644
--- a/CHANGES
+++ b/CHANGES
@@ -4,6 +4,7 @@
- Fixed debug output slipped id (#83462)
- Fixed crontab with empty lines (#75176)
- Fixed skipping of jobs triggered at the same time when rescheduling (#70975)
+- Added option "timeshift" in order to shift the execution point a bit (#69177)
1.01
diff --git a/MANIFEST b/MANIFEST
index 6f1e3fc..8abaf10 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -24,4 +24,6 @@ t/sighandler.t
t/startup.t
t/test.crontab
t/delete_entry.t
+t/same_time_with_reschedule.t
+t/timeshift.t
META.json
diff --git a/META.yml b/META.yml
index 2adb717..ad142ed 100644
--- a/META.yml
+++ b/META.yml
@@ -17,10 +17,12 @@ name: Schedule-Cron
provides:
Schedule::Cron:
file: lib/Schedule/Cron.pm
- version: 1.01
+ version: 1.02_1
requires:
Data::Dumper: 0
Time::ParseDate: 2011.0505
resources:
+ bugtracker: https://github.com/rhuss/schedule-cron/issues
license: http://dev.perl.org/licenses/
-version: 1.01
+ repository: git://github.com/rhuss/schedule-cron.git
+version: 1.02_1
diff --git a/README b/README
index d413cdb..b861f44 100644
--- a/README
+++ b/README
@@ -91,9 +91,6 @@ starting jobs and detaching itself to the background:
* For detaching it uses either setsid (POSIX) or the ioctl call
TIOCNOTTY
-The roadmap include plans for porting the fork mechanism over to a
-thread based scheme, which should make dynamic update much easier.
-
If the system calls mentioned above are not available (which should
hapen nowadays only under rare circumstances), you can still use the
'nofork' option to run all jobs within a single process/thread. Please
@@ -142,10 +139,22 @@ execution date (relative to the reference time). The dates can be
specified in a format understood by 'parsedate' from Time::ParseDate
(like 'now + 5 days'). Please include the output of this command.
+REPOSITORY
+----------
+
+Schedule::Cron's source is located at
+https://github.com/rhuss/schedule-cron Please feel free to send me
+pull requests if they apply to the license below. Also, don't forget
+documentation and tests.
+
+Please note also, that the active development for this module has been
+stopped since it is considered to be feature complete. Bugs and minor
+(external) additions will be added from time to time, though.
+
LICENSE
-------
-Copyright 1999-2011 Roland Huss.
+Copyright 1999-2013 Roland Huss.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
diff --git a/lib/Schedule/Cron.pm b/lib/Schedule/Cron.pm
index b9d0eac..cce0447 100644
--- a/lib/Schedule/Cron.pm
+++ b/lib/Schedule/Cron.pm
@@ -363,9 +363,11 @@ sub new
die "Dispatcher not a ref to a subroutine" unless ref($dispatcher) eq "CODE";
my $cfg = ref($_[0]) eq "HASH" ? $_[0] : { @_ };
$cfg->{processprefix} = "Schedule::Cron" unless $cfg->{processprefix};
+ my $timeshift = $cfg->{timeshift} || 0;
my $self = {
cfg => $cfg,
dispatcher => $dispatcher,
+ timeshift => $timeshift,
queue => [ ],
map => { }
};
@@ -877,7 +879,7 @@ sub run
die "No more jobs to run\n";
}
my ($indexes,$time) = $self->_get_next_jobs();
- my $now = time;
+ my $now = $self->_now();
my $sleep = 0;
if ($time < $now)
{
@@ -918,7 +920,7 @@ sub run
} else {
sleep($sleep);
}
- $sleep = $time - time;
+ $sleep = $time - $self->_now();
}
for my $index (@$indexes) {
@@ -1167,6 +1169,33 @@ sub get_next_execution_time
}
}
+=item $cron->set_timeshift($ts)
+
+Modify global time shift for all timetable. The timeshift is subbed from localtime
+to calculate next execution time for all scheduled jobs.
+
+ts parameter must be in seconds. Default value is 0. Negative values are allowed to
+shift time in the past.
+
+Returns actual timeshift in seconds.
+
+Example:
+
+ $cron->set_timeshift(120);
+
+ Will delay all jobs 2 minutes in the future.
+
+=cut
+
+sub set_timeshift
+{
+ my $self = shift;
+ my $value = shift || 0;
+
+ $self->{timeshift} = $value;
+ return $self->{timeshift};
+}
+
# ==================================================
# PRIVATE METHODS:
# ==================================================
@@ -1307,7 +1336,7 @@ sub _update_queue
my $new_time = $self->get_next_execution_time($entry->{time});
# Check, whether next execution time is *smaller* than the current time.
# This can happen during DST backflip:
- my $now = time;
+ my $now = $self->_now();
if ($new_time <= $now) {
dbg "Adjusting time calculation because of DST back flip (new_time - now = ",$new_time - $now,")" if $DEBUG;
# We are adding hours as long as our target time is in the future
@@ -1322,6 +1351,12 @@ sub _update_queue
}
+# Out "now" which can be shifted if as argument
+sub _now {
+ my $self = shift;
+ return time + $self->{timeshift};
+}
+
# The heart of the module.
# calculate the next concrete date
# for execution from a crontab entry
@@ -1331,7 +1366,7 @@ sub _calc_time
my $now = shift;
my $expanded = shift;
- my $offset = ($expanded->[5] ? 1 : 60);
+ my $offset = ($expanded->[5] ? 1 : 60) + $self->{timeshift};
my ($now_sec,$now_min,$now_hour,$now_mday,$now_mon,$now_wday,$now_year) =
(localtime($now+$offset))[0,1,2,3,4,6,5];
$now_mon++;