Bug 14266: Trim the email address in the pl script
[koha.git] / C4 / BackgroundJob.pm
blob8bd5feda201caaef281f75d866d346dd37f02e7e
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>.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Context;
24 use C4::Auth qw/get_session/;
25 use Digest::MD5;
27 use vars qw($VERSION);
29 BEGIN {
30 # set the version for version checking
31 $VERSION = 3.07.00.049;
34 =head1 NAME
36 C4::BackgroundJob - manage long-running jobs
37 initiated from the web staff interface
39 =head1 SYNOPSIS
41 # start tracking a job
42 my $job = C4::BackgroundJob->new($sessionID, $job_name, $job_invoker, $num_work_units);
43 my $jobID = $job->id();
44 $job->progress($work_units_processed);
45 $job->finish($job_result_hashref);
47 # get status and results of a job
48 my $job = C4::BackgroundJob->fetch($sessionID, $jobID);
49 my $max_work_units = $job->size();
50 my $work_units_processed = $job->progress();
51 my $job_status = $job->status();
52 my $job_name = $job->name();
53 my $job_invoker = $job->invoker();
54 my $results_hashref = $job->results();
56 This module manages tracking the progress and results
57 of (potentially) long-running jobs initiated from
58 the staff user interface. Such jobs can include
59 batch MARC and patron record imports.
61 =head1 METHODS
63 =head2 new
65 my $job = C4::BackgroundJob->new($sessionID, $job_name, $job_invoker, $num_work_units);
67 Create a new job object and set its status to 'running'. C<$num_work_units>
68 should be a number representing the size of the job; the units of the
69 job size are up to the caller and could be number of records,
70 number of bytes, etc.
72 =cut
74 sub new {
75 my $class = shift;
76 my ($sessionID, $job_name, $job_invoker, $num_work_units) = @_;
78 my $self = {};
79 $self->{'sessionID'} = $sessionID;
80 $self->{'name'} = $job_name;
81 $self->{'invoker'} = $job_invoker;
82 $self->{'size'} = $num_work_units;
83 $self->{'progress'} = 0;
84 $self->{'status'} = "running";
85 $self->{'jobID'} = Digest::MD5::md5_hex(Digest::MD5::md5_hex(time().{}.rand().{}.$$));
86 $self->{'extra_values'} = {};
88 bless $self, $class;
89 $self->_serialize();
91 return $self;
94 # store object in CGI session
95 sub _serialize {
96 my $self = shift;
98 my $prefix = "job_" . $self->{'jobID'};
99 my $session = get_session($self->{'sessionID'});
100 $session->param($prefix, $self);
101 $session->flush();
104 =head2 id
106 my $jobID = $job->id();
108 Read-only accessor for job ID.
110 =cut
112 sub id {
113 my $self = shift;
114 return $self->{'jobID'};
117 =head2 name
119 my $name = $job->name();
120 $job->name($name);
122 Read/write accessor for job name.
124 =cut
126 sub name {
127 my $self = shift;
128 if (@_) {
129 $self->{'name'} = shift;
130 $self->_serialize();
131 } else {
132 return $self->{'name'};
136 =head2 invoker
138 my $invoker = $job->invoker();
139 i $job->invoker($invoker);
141 Read/write accessor for job invoker.
143 =cut
145 sub invoker {
146 my $self = shift;
147 if (@_) {
148 $self->{'invoker'} = shift;
149 $self->_serialize();
150 } else {
151 return $self->{'invoker'};
155 =head2 progress
157 my $progress = $job->progress();
158 $job->progress($progress);
160 Read/write accessor for job progress.
162 =cut
164 sub progress {
165 my $self = shift;
166 if (@_) {
167 $self->{'progress'} = shift;
168 $self->_serialize();
169 } else {
170 return $self->{'progress'};
174 =head2 status
176 my $status = $job->status();
178 Read-only accessor for job status.
180 =cut
182 sub status {
183 my $self = shift;
184 return $self->{'status'};
187 =head2 size
189 my $size = $job->size();
190 $job->size($size);
192 Read/write accessor for job size.
194 =cut
196 sub size {
197 my $self = shift;
198 if (@_) {
199 $self->{'size'} = shift;
200 $self->_serialize();
201 } else {
202 return $self->{'size'};
206 =head2 finish
208 $job->finish($results_hashref);
210 Mark the job as finished, setting its status to 'completed'.
211 C<$results_hashref> should be a reference to a hash containing
212 the results of the job.
214 =cut
216 sub finish {
217 my $self = shift;
218 my $results_hashref = shift;
219 $self->{'status'} = 'completed';
220 $self->{'results'} = $results_hashref;
221 $self->_serialize();
224 =head2 results
226 my $results_hashref = $job->results();
228 Retrieve the results of the current job. Returns undef
229 if the job status is not 'completed'.
231 =cut
233 sub results {
234 my $self = shift;
235 return unless $self->{'status'} eq 'completed';
236 return $self->{'results'};
239 =head2 fetch
241 my $job = C4::BackgroundJob->fetch($sessionID, $jobID);
243 Retrieve a job that has been serialized to the database.
244 Returns C<undef> if the job does not exist in the current
245 session.
247 =cut
249 sub fetch {
250 my $class = shift;
251 my $sessionID = shift;
252 my $jobID = shift;
254 my $session = get_session($sessionID);
255 my $prefix = "job_$jobID";
256 unless (defined $session->param($prefix)) {
257 return;
259 my $self = $session->param($prefix);
260 bless $self, $class;
261 return $self;
264 =head2 set
266 =over 4
268 =item $job->set($hashref);
270 =back
272 Set some variables into the hashref.
273 These variables can be retrieved using the get method.
275 =cut
277 sub set {
278 my ($self, $hashref) = @_;
279 while ( my ($k, $v) = each %$hashref ) {
280 $self->{extra_values}->{$k} = $v;
282 $self->_serialize();
283 return;
286 =head2 get
288 =over 4
290 =item $value = $job->get($key);
292 =back
294 Get a variable which has been previously stored with the set method.
296 =cut
298 sub get {
299 my ($self, $key) = @_;
300 return $self->{extra_values}->{$key};
304 __END__
306 =head1 AUTHOR
308 Koha Development Team <http://koha-community.org/>
310 Galen Charlton <galen.charlton@liblime.com>
312 =cut