bug 1532: ensure that empty hold request expiration is represented as NULL
[koha.git] / C4 / Scheduler.pm
1 package C4::Scheduler;
2
3 # Copyright 2007 Liblime Ltd
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22
23 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
24 use C4::Context;
25 use Schedule::At;
26
27 BEGIN {
28         # set the version for version checking
29         $VERSION = 0.02;
30         require Exporter;
31         @ISA = qw(Exporter);
32         @EXPORT =
33                 qw(get_jobs get_at_jobs get_at_job add_at_job remove_at_job);
34 }
35
36 =head1 NAME
37
38 C4::Scheduler - Module for running jobs with the unix at command
39
40 =head1 SYNOPSIS
41
42   use C4::Scheduler;
43
44 =head1 DESCRIPTION
45
46
47 =head1 METHODS
48
49 =over 2
50
51 =cut
52
53 =item get_jobs();
54
55 This will return all scheduled jobs
56
57 =cut
58
59 sub get_jobs {
60     my $jobs = get_at_jobs();
61 # add call to get cron jobs here too
62     return ($jobs);
63 }
64
65 =item get_at_jobs();
66
67 This will return all At scheduled jobs
68
69 =cut
70
71 sub get_at_jobs {
72         my %jobs = Schedule::At::getJobs();
73         return (\%jobs);
74 }
75
76 =item get_at_job($id)
77
78 This will return the At job with the given id
79
80 =cut
81
82 sub get_at_job {
83         my ($id)=@_;
84         my %jobs = Schedule::At::getJobs(JOBID => $id);
85 }
86
87 =item add_at_job ($time,$command)
88
89 Given a timestamp and a command this will schedule the job to run at that time.
90
91 Returns true if the job is added to the queue and false otherwise.
92
93 =cut
94
95 sub add_at_job {
96         my ($time,$command) = @_;
97     # FIXME - a description of the task to be run 
98     # may be a better tag, since the tag is displayed
99     # in the job list that the administrator sees - e.g.,
100     # "run report foo, send to foo@bar.com"
101         Schedule::At::add(TIME => $time, COMMAND => $command, TAG => $command);
102
103     # FIXME - this method of checking whether the job was added
104     # to the queue is less than perfect:
105     #
106     # 1. Since the command is the tag, it is possible that there is
107     #    already a job in the queue with the same tag.  However, since
108     #    the tag is what displays in the job list, we can't just
109     #    give it a unique ID.
110     # 2. Schedule::At::add() is supposed to return a non-zero
111     #    value if it fails to add a job - however, it does
112     #    not check all error conditions - in particular, it does
113     #    not check the return value of the "at" run; it basically
114     #    complains only if it can't find at.
115     # 3. Similary, Schedule::At::add() does not do something more useful,
116     #    such as returning the job ID.  To be fair, it is possible
117     #    that 'at' does not allow this in any portable way.
118     # 4. Although unlikely, it is possible that a job could be added
119     #    and completed instantly, thus dropping off the queue.
120     my $job_found = 0;
121     eval {
122             my %jobs = Schedule::At::getJobs(TAG => $command);
123         $job_found = scalar(keys %jobs) > 0;
124     };
125     if ($@) {
126         return 0;
127     } else {
128         return $job_found;
129     }
130 }
131
132 sub remove_at_job {
133         my ($jobid)=@_;
134         Schedule::At::remove(JOBID => $jobid);
135 }
136
137 1;
138 __END__
139
140 =back
141
142 =head1 BUGS
143
144 At some point C<C4::Scheduler> should be refactored:
145
146 =over 4
147
148 =item At and C<Schedule::At> does not work on Win32.
149
150 =item At is not installed by default on all platforms.
151
152 =item The At queue used by Koha is owned by the httpd user.  If multiple
153 Koha databases share an Apache instance on a server, everybody can
154 see everybody's jobs.
155
156 =item There is no support for scheduling a job to run more than once.
157
158 =back
159
160 =head1 AUTHOR
161
162 Chris Cormack <crc@liblime.com>
163
164 =cut