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