Bug 23091: Move _Restore to Koha::Item store trigger
[koha.git] / C4 / Scheduler.pm
bloba1c1ae557f96dbfda19c16548e6f31da398439ea
1 package C4::Scheduler;
3 # Copyright 2007 Liblime Ltd
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
23 use C4::Context;
24 use Schedule::At;
26 BEGIN {
27 require Exporter;
28 @ISA = qw(Exporter);
29 @EXPORT =
30 qw(get_jobs get_at_jobs get_at_job add_at_job remove_at_job);
33 =head1 NAME
35 C4::Scheduler - Module for running jobs with the unix at command
37 =head1 SYNOPSIS
39 use C4::Scheduler;
41 =head1 DESCRIPTION
43 =cut
45 =head1 METHODS
47 =head2 get_jobs();
49 This will return all scheduled jobs
51 =cut
53 sub get_jobs {
54 my $jobs = get_at_jobs();
55 # add call to get cron jobs here too
56 return ($jobs);
59 =head2 get_at_jobs();
61 This will return all At scheduled jobs
63 =cut
65 sub get_at_jobs {
66 my %jobs = Schedule::At::getJobs();
67 return (\%jobs);
70 =head2 get_at_job($id)
72 This will return the At job with the given id
74 =cut
76 sub get_at_job {
77 my ($id)=@_;
78 my %jobs = Schedule::At::getJobs(JOBID => $id);
81 =head2 add_at_job ($time,$command)
83 Given a timestamp and a command this will schedule the job to run at that time.
85 Returns true if the job is added to the queue and false otherwise.
87 =cut
89 sub add_at_job {
90 my ($time,$command) = @_;
91 # FIXME - a description of the task to be run
92 # may be a better tag, since the tag is displayed
93 # in the job list that the administrator sees - e.g.,
94 # "run report foo, send to foo@bar.com"
95 Schedule::At::add(TIME => $time, COMMAND => $command, TAG => $command);
97 # FIXME - this method of checking whether the job was added
98 # to the queue is less than perfect:
100 # 1. Since the command is the tag, it is possible that there is
101 # already a job in the queue with the same tag. However, since
102 # the tag is what displays in the job list, we can't just
103 # give it a unique ID.
104 # 2. Schedule::At::add() is supposed to return a non-zero
105 # value if it fails to add a job - however, it does
106 # not check all error conditions - in particular, it does
107 # not check the return value of the "at" run; it basically
108 # complains only if it can't find at.
109 # 3. Similary, Schedule::At::add() does not do something more useful,
110 # such as returning the job ID. To be fair, it is possible
111 # that 'at' does not allow this in any portable way.
112 # 4. Although unlikely, it is possible that a job could be added
113 # and completed instantly, thus dropping off the queue.
114 my $job_found = 0;
115 eval {
116 my %jobs = Schedule::At::getJobs(TAG => $command);
117 $job_found = scalar(keys %jobs) > 0;
119 if ($@) {
120 return 0;
121 } else {
122 return $job_found;
126 sub remove_at_job {
127 my ($jobid)=@_;
128 Schedule::At::remove(JOBID => $jobid);
132 __END__
134 =head1 BUGS
136 At some point C<C4::Scheduler> should be refactored:
138 =over
140 =item At and C<Schedule::At> does not work on Win32.
142 =item At is not installed by default on all platforms.
144 =item The At queue used by Koha is owned by the httpd user. If multiple
145 Koha databases share an Apache instance on a server, everybody can
146 see everybody's jobs.
148 =item There is no support for scheduling a job to run more than once.
150 =back
152 =head1 AUTHOR
154 Chris Cormack <crc@liblime.com>
156 =cut