File: | C4/Scheduler.pm |
Coverage: | 95.6% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
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 | 4 4 4 | 51806 9 170 | use strict; | |||
21 | #use warnings; FIXME - Bug 2505 | |||||
22 | ||||||
23 | 4 4 4 | 17 5 435 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | |||
24 | 4 4 4 | 400 37 93 | use C4::Context; | |||
25 | 4 4 4 | 81284 14716 253 | use Schedule::At; | |||
26 | ||||||
27 | BEGIN { | |||||
28 | # set the version for version checking | |||||
29 | 4 | 7 | $VERSION = 0.02; | |||
30 | 4 | 18 | require Exporter; | |||
31 | 4 | 38 | @ISA = qw(Exporter); | |||
32 | 4 | 722 | @EXPORT = | |||
33 | qw(get_jobs get_at_jobs get_at_job add_at_job remove_at_job); | |||||
34 | } | |||||
35 | ||||||
36 - 46 | =head1 NAME C4::Scheduler - Module for running jobs with the unix at command =head1 SYNOPSIS use C4::Scheduler; =head1 DESCRIPTION =cut | |||||
47 | ||||||
48 - 54 | =head1 METHODS =head2 get_jobs(); This will return all scheduled jobs =cut | |||||
55 | ||||||
56 | sub get_jobs { | |||||
57 | 2 | 722264 | my $jobs = get_at_jobs(); | |||
58 | # add call to get cron jobs here too | |||||
59 | 2 | 458 | return ($jobs); | |||
60 | } | |||||
61 | ||||||
62 - 66 | =head2 get_at_jobs(); This will return all At scheduled jobs =cut | |||||
67 | ||||||
68 | sub get_at_jobs { | |||||
69 | 4 | 78 | my %jobs = Schedule::At::getJobs(); | |||
70 | 4 | 188440 | return (\%jobs); | |||
71 | } | |||||
72 | ||||||
73 - 77 | =head2 get_at_job($id) This will return the At job with the given id =cut | |||||
78 | ||||||
79 | sub get_at_job { | |||||
80 | 2 | 125 | my ($id)=@_; | |||
81 | 2 | 176 | my %jobs = Schedule::At::getJobs(JOBID => $id); | |||
82 | } | |||||
83 | ||||||
84 - 90 | =head2 add_at_job ($time,$command) Given a timestamp and a command this will schedule the job to run at that time. Returns true if the job is added to the queue and false otherwise. =cut | |||||
91 | ||||||
92 | sub add_at_job { | |||||
93 | 2 | 34773 | my ($time,$command) = @_; | |||
94 | # FIXME - a description of the task to be run | |||||
95 | # may be a better tag, since the tag is displayed | |||||
96 | # in the job list that the administrator sees - e.g., | |||||
97 | # "run report foo, send to foo@bar.com" | |||||
98 | 2 | 381 | Schedule::At::add(TIME => $time, COMMAND => $command, TAG => $command); | |||
99 | ||||||
100 | # FIXME - this method of checking whether the job was added | |||||
101 | # to the queue is less than perfect: | |||||
102 | # | |||||
103 | # 1. Since the command is the tag, it is possible that there is | |||||
104 | # already a job in the queue with the same tag. However, since | |||||
105 | # the tag is what displays in the job list, we can't just | |||||
106 | # give it a unique ID. | |||||
107 | # 2. Schedule::At::add() is supposed to return a non-zero | |||||
108 | # value if it fails to add a job - however, it does | |||||
109 | # not check all error conditions - in particular, it does | |||||
110 | # not check the return value of the "at" run; it basically | |||||
111 | # complains only if it can't find at. | |||||
112 | # 3. Similary, Schedule::At::add() does not do something more useful, | |||||
113 | # such as returning the job ID. To be fair, it is possible | |||||
114 | # that 'at' does not allow this in any portable way. | |||||
115 | # 4. Although unlikely, it is possible that a job could be added | |||||
116 | # and completed instantly, thus dropping off the queue. | |||||
117 | 2 | 43901 | my $job_found = 0; | |||
118 | 2 | 276 | eval { | |||
119 | 2 | 437 | my %jobs = Schedule::At::getJobs(TAG => $command); | |||
120 | 2 | 22823 | $job_found = scalar(keys %jobs) > 0; | |||
121 | }; | |||||
122 | 2 | 274 | if ($@) { | |||
123 | 0 | 0 | return 0; | |||
124 | } else { | |||||
125 | 2 | 635 | return $job_found; | |||
126 | } | |||||
127 | } | |||||
128 | ||||||
129 | sub remove_at_job { | |||||
130 | 2 | 128 | my ($jobid)=@_; | |||
131 | 2 | 236 | Schedule::At::remove(JOBID => $jobid); | |||
132 | } | |||||
133 | ||||||
134 | 1; |