forked from Koha-Community/Koha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackgroundJob.pm
312 lines (226 loc) · 6.11 KB
/
BackgroundJob.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package C4::BackgroundJob;
# Copyright (C) 2007 LibLime
# Galen Charlton <[email protected]>
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with Koha; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use strict;
#use warnings; FIXME - Bug 2505
use C4::Context;
use C4::Auth qw/get_session/;
use Digest::MD5;
use vars qw($VERSION);
BEGIN {
# set the version for version checking
$VERSION = 3.07.00.049;
}
=head1 NAME
C4::BackgroundJob - manage long-running jobs
initiated from the web staff interface
=head1 SYNOPSIS
# start tracking a job
my $job = C4::BackgroundJob->new($sessionID, $job_name, $job_invoker, $num_work_units);
my $jobID = $job->id();
$job->progress($work_units_processed);
$job->finish($job_result_hashref);
# get status and results of a job
my $job = C4::BackgroundJob->fetch($sessionID, $jobID);
my $max_work_units = $job->size();
my $work_units_processed = $job->progress();
my $job_status = $job->status();
my $job_name = $job->name();
my $job_invoker = $job->invoker();
my $results_hashref = $job->results();
This module manages tracking the progress and results
of (potentially) long-running jobs initiated from
the staff user interface. Such jobs can include
batch MARC and patron record imports.
=head1 METHODS
=head2 new
my $job = C4::BackgroundJob->new($sessionID, $job_name, $job_invoker, $num_work_units);
Create a new job object and set its status to 'running'. C<$num_work_units>
should be a number representing the size of the job; the units of the
job size are up to the caller and could be number of records,
number of bytes, etc.
=cut
sub new {
my $class = shift;
my ($sessionID, $job_name, $job_invoker, $num_work_units) = @_;
my $self = {};
$self->{'sessionID'} = $sessionID;
$self->{'name'} = $job_name;
$self->{'invoker'} = $job_invoker;
$self->{'size'} = $num_work_units;
$self->{'progress'} = 0;
$self->{'status'} = "running";
$self->{'jobID'} = Digest::MD5::md5_hex(Digest::MD5::md5_hex(time().{}.rand().{}.$$));
$self->{'extra_values'} = {};
bless $self, $class;
$self->_serialize();
return $self;
}
# store object in CGI session
sub _serialize {
my $self = shift;
my $prefix = "job_" . $self->{'jobID'};
my $session = get_session($self->{'sessionID'});
$session->param($prefix, $self);
$session->flush();
}
=head2 id
my $jobID = $job->id();
Read-only accessor for job ID.
=cut
sub id {
my $self = shift;
return $self->{'jobID'};
}
=head2 name
my $name = $job->name();
$job->name($name);
Read/write accessor for job name.
=cut
sub name {
my $self = shift;
if (@_) {
$self->{'name'} = shift;
$self->_serialize();
} else {
return $self->{'name'};
}
}
=head2 invoker
my $invoker = $job->invoker();
i $job->invoker($invoker);
Read/write accessor for job invoker.
=cut
sub invoker {
my $self = shift;
if (@_) {
$self->{'invoker'} = shift;
$self->_serialize();
} else {
return $self->{'invoker'};
}
}
=head2 progress
my $progress = $job->progress();
$job->progress($progress);
Read/write accessor for job progress.
=cut
sub progress {
my $self = shift;
if (@_) {
$self->{'progress'} = shift;
$self->_serialize();
} else {
return $self->{'progress'};
}
}
=head2 status
my $status = $job->status();
Read-only accessor for job status.
=cut
sub status {
my $self = shift;
return $self->{'status'};
}
=head2 size
my $size = $job->size();
$job->size($size);
Read/write accessor for job size.
=cut
sub size {
my $self = shift;
if (@_) {
$self->{'size'} = shift;
$self->_serialize();
} else {
return $self->{'size'};
}
}
=head2 finish
$job->finish($results_hashref);
Mark the job as finished, setting its status to 'completed'.
C<$results_hashref> should be a reference to a hash containing
the results of the job.
=cut
sub finish {
my $self = shift;
my $results_hashref = shift;
$self->{'status'} = 'completed';
$self->{'results'} = $results_hashref;
$self->_serialize();
}
=head2 results
my $results_hashref = $job->results();
Retrieve the results of the current job. Returns undef
if the job status is not 'completed'.
=cut
sub results {
my $self = shift;
return unless $self->{'status'} eq 'completed';
return $self->{'results'};
}
=head2 fetch
my $job = C4::BackgroundJob->fetch($sessionID, $jobID);
Retrieve a job that has been serialized to the database.
Returns C<undef> if the job does not exist in the current
session.
=cut
sub fetch {
my $class = shift;
my $sessionID = shift;
my $jobID = shift;
my $session = get_session($sessionID);
my $prefix = "job_$jobID";
unless (defined $session->param($prefix)) {
return;
}
my $self = $session->param($prefix);
bless $self, $class;
return $self;
}
=head2 set
=over 4
=item $job->set($hashref);
=back
Set some variables into the hashref.
These variables can be retrieved using the get method.
=cut
sub set {
my ($self, $hashref) = @_;
while ( my ($k, $v) = each %$hashref ) {
$self->{extra_values}->{$k} = $v;
}
$self->_serialize();
return;
}
=head2 get
=over 4
=item $value = $job->get($key);
=back
Get a variable which has been previously stored with the set method.
=cut
sub get {
my ($self, $key) = @_;
return $self->{extra_values}->{$key};
}
1;
__END__
=head1 AUTHOR
Koha Development Team <http://koha-community.org/>
Galen Charlton <[email protected]>
=cut