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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 #use warnings; FIXME - Bug 2505
24 use C4::Auth qw/get_session/;
33 C4::BackgroundJob - manage long-running jobs
34 initiated from the web staff interface
38 # start tracking a job
39 my $job = C4::BackgroundJob->new($sessionID, $job_name, $job_invoker, $num_work_units);
40 my $jobID = $job->id();
41 $job->progress($work_units_processed);
42 $job->finish($job_result_hashref);
44 # get status and results of a job
45 my $job = C4::BackgroundJob->fetch($sessionID, $jobID);
46 my $max_work_units = $job->size();
47 my $work_units_processed = $job->progress();
48 my $job_status = $job->status();
49 my $job_name = $job->name();
50 my $job_invoker = $job->invoker();
51 my $results_hashref = $job->results();
53 This module manages tracking the progress and results
54 of (potentially) long-running jobs initiated from
55 the staff user interface. Such jobs can include
56 batch MARC and patron record imports.
62 my $job = C4::BackgroundJob->new($sessionID, $job_name, $job_invoker, $num_work_units);
64 Create a new job object and set its status to 'running'. C<$num_work_units>
65 should be a number representing the size of the job; the units of the
66 job size are up to the caller and could be number of records,
73 my ($sessionID, $job_name, $job_invoker, $num_work_units) = @_;
76 $self->{'sessionID'} = $sessionID;
77 $self->{'name'} = $job_name;
78 $self->{'invoker'} = $job_invoker;
79 $self->{'size'} = $num_work_units;
80 $self->{'progress'} = 0;
81 $self->{'status'} = "running";
82 $self->{'jobID'} = Digest::MD5::md5_hex(Digest::MD5::md5_hex(time().{}.rand().{}.$$));
83 $self->{'extra_values'} = {};
91 # store object in CGI session
95 my $prefix = "job_" . $self->{'jobID'};
96 my $session = get_session($self->{'sessionID'});
97 $session->param($prefix, $self);
103 my $jobID = $job->id();
105 Read-only accessor for job ID.
111 return $self->{'jobID'};
116 my $name = $job->name();
119 Read/write accessor for job name.
126 $self->{'name'} = shift;
129 return $self->{'name'};
135 my $invoker = $job->invoker();
136 i $job->invoker($invoker);
138 Read/write accessor for job invoker.
145 $self->{'invoker'} = shift;
148 return $self->{'invoker'};
154 my $progress = $job->progress();
155 $job->progress($progress);
157 Read/write accessor for job progress.
164 $self->{'progress'} = shift;
167 return $self->{'progress'};
173 my $status = $job->status();
175 Read-only accessor for job status.
181 return $self->{'status'};
186 my $size = $job->size();
189 Read/write accessor for job size.
196 $self->{'size'} = shift;
199 return $self->{'size'};
205 $job->finish($results_hashref);
207 Mark the job as finished, setting its status to 'completed'.
208 C<$results_hashref> should be a reference to a hash containing
209 the results of the job.
215 my $results_hashref = shift;
216 $self->{'status'} = 'completed';
217 $self->{'results'} = $results_hashref;
223 my $results_hashref = $job->results();
225 Retrieve the results of the current job. Returns undef
226 if the job status is not 'completed'.
232 return unless $self->{'status'} eq 'completed';
233 return $self->{'results'};
238 my $job = C4::BackgroundJob->fetch($sessionID, $jobID);
240 Retrieve a job that has been serialized to the database.
241 Returns C<undef> if the job does not exist in the current
248 my $sessionID = shift;
251 my $session = get_session($sessionID);
252 my $prefix = "job_$jobID";
253 unless (defined $session->param($prefix)) {
256 my $self = $session->param($prefix);
265 =item $job->set($hashref);
269 Set some variables into the hashref.
270 These variables can be retrieved using the get method.
275 my ($self, $hashref) = @_;
276 while ( my ($k, $v) = each %$hashref ) {
277 $self->{extra_values}->{$k} = $v;
287 =item $value = $job->get($key);
291 Get a variable which has been previously stored with the set method.
296 my ($self, $key) = @_;
297 return $self->{extra_values}->{$key};
309 Clear the job from the current session.
315 get_session($self->{sessionID})->clear('job_' . $self->{jobID});
324 Koha Development Team <http://koha-community.org/>
326 Galen Charlton <galen.charlton@liblime.com>