X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=C4%2FBackgroundJob.pm;h=733f72e11b180be9a4c31c3e44f09953d26e5b61;hb=f3dd750a972ecd95c566d413a7d468ecc01df9fe;hp=9f3acff9f9e9e93e265a2d04f7a325d3abc80be4;hpb=cabb06c56c41ff6cbd8829316abbc3492039a196;p=koha-ffzg.git diff --git a/C4/BackgroundJob.pm b/C4/BackgroundJob.pm index 9f3acff9f9..733f72e11b 100644 --- a/C4/BackgroundJob.pm +++ b/C4/BackgroundJob.pm @@ -5,28 +5,25 @@ package C4::BackgroundJob; # # 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 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 3 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. +# 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., 59 Temple Place, -# Suite 330, Boston, MA 02111-1307 USA +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . -use strict; +use Modern::Perl; use C4::Context; -use C4::Auth qw/get_session/; +use C4::Auth qw( get_session ); use Digest::MD5; -use vars qw($VERSION); -# set the version for version checking -$VERSION = 3.00; =head1 NAME @@ -35,43 +32,31 @@ initiated from the web staff interface =head1 SYNOPSIS -=over 4 - -# 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(); - -=back + # 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. -=cut - =head1 METHODS -=cut - =head2 new -=over 4 - -my $job = C4::BackgroundJob->new($sessionID, $job_name, $job_invoker, $num_work_units); - -=back + 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 @@ -92,6 +77,7 @@ sub new { $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(); @@ -105,29 +91,13 @@ sub _serialize { my $prefix = "job_" . $self->{'jobID'}; my $session = get_session($self->{'sessionID'}); - $session->param("$prefix.name", $self->{'name'}); - $session->param("$prefix.invoker", $self->{'invoker'}); - $session->param("$prefix.size", $self->{'size'}); - $session->param("$prefix.progress", $self->{'size'}); - $session->param("$prefix.status", $self->{'size'}); - if (exists $self->{'results'}) { - my @keys = (); - foreach my $key (keys %{ $self->{'results'} }) { - $session->param("$prefix.results.$key", $self->{'results'}->{$key}); - push @keys, $key; - } - $session->param("$prefix.results_keys", join("\t", @keys)); - } + $session->param($prefix, $self); $session->flush(); } =head2 id -=over 4 - -my $jobID = $job->id(); - -=back + my $jobID = $job->id(); Read-only accessor for job ID. @@ -135,17 +105,13 @@ Read-only accessor for job ID. sub id { my $self = shift; - return $self->{'id'}; + return $self->{'jobID'}; } =head2 name -=over 4 - -my $name = $job->name(); -$job->name($name); - -=back + my $name = $job->name(); + $job->name($name); Read/write accessor for job name. @@ -163,12 +129,8 @@ sub name { =head2 invoker -=over 4 - -my $invoker = $job->invoker(); -$job->invoker($invoker); - -=back + my $invoker = $job->invoker(); +i $job->invoker($invoker); Read/write accessor for job invoker. @@ -186,12 +148,8 @@ sub invoker { =head2 progress -=over 4 - -my $progress = $job->progress(); -$job->progress($progress); - -=back + my $progress = $job->progress(); + $job->progress($progress); Read/write accessor for job progress. @@ -209,11 +167,7 @@ sub progress { =head2 status -=over 4 - -my $status = $job->status(); - -=back + my $status = $job->status(); Read-only accessor for job status. @@ -226,12 +180,8 @@ sub status { =head2 size -=over 4 - -my $size = $job->size(); -$job->size($size); - -=back + my $size = $job->size(); + $job->size($size); Read/write accessor for job size. @@ -249,11 +199,7 @@ sub size { =head2 finish -=over 4 - -$job->finish($results_hashref); - -=back + $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 @@ -264,18 +210,14 @@ the results of the job. sub finish { my $self = shift; my $results_hashref = shift; - my $self->{'status'} = 'completed'; - my $self->{'results'} = $results_hashref; + $self->{'status'} = 'completed'; + $self->{'results'} = $results_hashref; $self->_serialize(); } =head2 results -=over 4 - -my $results_hashref = $job->results(); - -=back + my $results_hashref = $job->results(); Retrieve the results of the current job. Returns undef if the job status is not 'completed'. @@ -284,17 +226,13 @@ if the job status is not 'completed'. sub results { my $self = shift; - return undef unless $self->{'status'} eq 'completed'; + return unless $self->{'status'} eq 'completed'; return $self->{'results'}; } =head2 fetch -=over 4 - -my $job = C4::BackgroundJob->fetch($sessionID, $jobID); - -=back + my $job = C4::BackgroundJob->fetch($sessionID, $jobID); Retrieve a job that has been serialized to the database. Returns C if the job does not exist in the current @@ -309,31 +247,78 @@ sub fetch { my $session = get_session($sessionID); my $prefix = "job_$jobID"; - unless (defined $session->param("$prefix.name")) { - return undef; + unless (defined $session->param($prefix)) { + return; } - my $self = {}; - - $self->{'name'} = $session->param("$prefix.name"); - $self->{'status'} = $session->param("$prefix.status"); - $self->{'invoker'} = $session->param("$prefix.invoker"); - $self->{'size'} = $session->param("$prefix.size"); - $self->{'progress'} = $session->param("$prefix.progress"); - if (defined(my $keys = $session->param("$prefix.results_keys"))) { - my @keys = split /\t/, $keys; - $self->{'results'} = {}; - foreach my $key (@keys) { - $self->{'results'}->{$key} = $session->param("$prefix.results.$key"); - } - } - + 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}; +} + + +=head2 clear + +=over 4 + +=item $job->clear(); + +=back + +Clear the job from the current session. + +=cut + +sub clear { + my $self = shift; + get_session($self->{sessionID})->clear('job_' . $self->{jobID}); +} + + +1; +__END__ + =head1 AUTHOR -Koha Development Team +Koha Development Team Galen Charlton