1 package C4::BackgroundJob;
3 # Copyright (C) 2007 LibLime
4 # Galen Charlton <galen.charlton@liblime.com>
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #use warnings; FIXME - Bug 2505
24 use C4::Auth qw/get_session/;
27 use vars qw($VERSION);
30 # set the version for version checking
36 C4::BackgroundJob - manage long-running jobs
37 initiated from the web staff interface
43 # start tracking a job
44 my $job = C4::BackgroundJob->new($sessionID, $job_name, $job_invoker, $num_work_units);
45 my $jobID = $job->id();
46 $job->progress($work_units_processed);
47 $job->finish($job_result_hashref);
49 # get status and results of a job
50 my $job = C4::BackgroundJob->fetch($sessionID, $jobID);
51 my $max_work_units = $job->size();
52 my $work_units_processed = $job->progress();
53 my $job_status = $job->status();
54 my $job_name = $job->name();
55 my $job_invoker = $job->invoker();
56 my $results_hashref = $job->results();
60 This module manages tracking the progress and results
61 of (potentially) long-running jobs initiated from
62 the staff user interface. Such jobs can include
63 batch MARC and patron record imports.
75 my $job = C4::BackgroundJob->new($sessionID, $job_name, $job_invoker, $num_work_units);
79 Create a new job object and set its status to 'running'. C<$num_work_units>
80 should be a number representing the size of the job; the units of the
81 job size are up to the caller and could be number of records,
88 my ($sessionID, $job_name, $job_invoker, $num_work_units) = @_;
91 $self->{'sessionID'} = $sessionID;
92 $self->{'name'} = $job_name;
93 $self->{'invoker'} = $job_invoker;
94 $self->{'size'} = $num_work_units;
95 $self->{'progress'} = 0;
96 $self->{'status'} = "running";
97 $self->{'jobID'} = Digest::MD5::md5_hex(Digest::MD5::md5_hex(time().{}.rand().{}.$$));
105 # store object in CGI session
109 my $prefix = "job_" . $self->{'jobID'};
110 my $session = get_session($self->{'sessionID'});
111 $session->param($prefix, $self);
119 my $jobID = $job->id();
123 Read-only accessor for job ID.
129 return $self->{'jobID'};
136 my $name = $job->name();
141 Read/write accessor for job name.
148 $self->{'name'} = shift;
151 return $self->{'name'};
159 my $invoker = $job->invoker();
160 $job->invoker($invoker);
164 Read/write accessor for job invoker.
171 $self->{'invoker'} = shift;
174 return $self->{'invoker'};
182 my $progress = $job->progress();
183 $job->progress($progress);
187 Read/write accessor for job progress.
194 $self->{'progress'} = shift;
197 return $self->{'progress'};
205 my $status = $job->status();
209 Read-only accessor for job status.
215 return $self->{'status'};
222 my $size = $job->size();
227 Read/write accessor for job size.
234 $self->{'size'} = shift;
237 return $self->{'size'};
245 $job->finish($results_hashref);
249 Mark the job as finished, setting its status to 'completed'.
250 C<$results_hashref> should be a reference to a hash containing
251 the results of the job.
257 my $results_hashref = shift;
258 $self->{'status'} = 'completed';
259 $self->{'results'} = $results_hashref;
267 my $results_hashref = $job->results();
271 Retrieve the results of the current job. Returns undef
272 if the job status is not 'completed'.
278 return undef unless $self->{'status'} eq 'completed';
279 return $self->{'results'};
286 my $job = C4::BackgroundJob->fetch($sessionID, $jobID);
290 Retrieve a job that has been serialized to the database.
291 Returns C<undef> if the job does not exist in the current
298 my $sessionID = shift;
301 my $session = get_session($sessionID);
302 my $prefix = "job_$jobID";
303 unless (defined $session->param($prefix)) {
306 my $self = $session->param($prefix);
316 Koha Development Team <info@koha.org>
318 Galen Charlton <galen.charlton@liblime.com>