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