Bug 24695: Improve SQL report validation
[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 Modern::Perl;
21
22 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
23 use C4::Context;
24 use Schedule::At;
25
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);
31 }
32
33 =head1 NAME
34
35 C4::Scheduler - Module for running jobs with the unix at command
36
37 =head1 SYNOPSIS
38
39   use C4::Scheduler;
40
41 =head1 DESCRIPTION
42
43 =cut
44
45 =head1 METHODS
46
47 =head2 get_jobs();
48
49 This will return all scheduled jobs
50
51 =cut
52
53 sub get_jobs {
54     my $jobs = get_at_jobs();
55 # add call to get cron jobs here too
56     return ($jobs);
57 }
58
59 =head2 get_at_jobs();
60
61 This will return all At scheduled jobs
62
63 =cut
64
65 sub get_at_jobs {
66         my %jobs = Schedule::At::getJobs();
67         return (\%jobs);
68 }
69
70 =head2 get_at_job($id)
71
72 This will return the At job with the given id
73
74 =cut
75
76 sub get_at_job {
77         my ($id)=@_;
78         my %jobs = Schedule::At::getJobs(JOBID => $id);
79 }
80
81 =head2 add_at_job ($time,$command)
82
83 Given a timestamp and a command this will schedule the job to run at that time.
84
85 Returns true if the job is added to the queue and false otherwise.
86
87 =cut
88
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);
96
97     # FIXME - this method of checking whether the job was added
98     # to the queue is less than perfect:
99     #
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;
118     };
119     if ($@) {
120         return 0;
121     } else {
122         return $job_found;
123     }
124 }
125
126 sub remove_at_job {
127         my ($jobid)=@_;
128         Schedule::At::remove(JOBID => $jobid);
129 }
130
131 1;
132 __END__
133
134 =head1 BUGS
135
136 At some point C<C4::Scheduler> should be refactored:
137
138 =over
139
140 =item At and C<Schedule::At> does not work on Win32.
141
142 =item At is not installed by default on all platforms.
143
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.
147
148 =item There is no support for scheduling a job to run more than once.
149
150 =back
151
152 =head1 AUTHOR
153
154 Chris Cormack <crc@liblime.com>
155
156 =cut