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