@@ -, +, @@ --- Koha/BackgroundJob.pm | 21 ++++++++-- Koha/BackgroundJob/BatchUpdateAuthority.pm | 32 ++++++++------- Koha/BackgroundJob/BatchUpdateBiblio.pm | 13 +++++- Koha/BackgroundJobs.pm | 1 + .../prog/en/modules/admin/background_jobs.tt | 40 +++++++++++++++++-- .../tools/batch_record_modification.tt | 10 ++--- misc/background_jobs_worker.pl | 14 ++++--- tools/batch_record_modification.pl | 19 +++++---- 8 files changed, 108 insertions(+), 42 deletions(-) --- a/Koha/BackgroundJob.pm +++ a/Koha/BackgroundJob.pm @@ -7,7 +7,9 @@ use Net::Stomp; use C4::Context; use Koha::DateUtils qw( dt_from_string ); -use Koha::BackgroundJobs; +use Koha::Exceptions; +use Koha::BackgroundJob::BatchUpdateBiblio; +use Koha::BackgroundJob::BatchUpdateAuthority; use base qw( Koha::Object ); @@ -21,7 +23,7 @@ sub connect { sub enqueue { my ( $self, $params ) = @_; - my $job_type = $params->{job_type}; + my $job_type = $self->job_type; my $job_size = $params->{job_size}; my $job_args = $params->{job_args}; @@ -47,14 +49,25 @@ sub enqueue { my $conn = $self->connect; $conn->send_with_receipt( { destination => $job_type, body => $json_args } ) - or Koha::Exception->throw('Job has not been enqueued'); + or Koha::Exceptions::Exception->throw('Job has not been enqueued'); } ); return $job_id; } -sub process { croak "This method must be subclassed" } +sub process { + my ( $self, $args ) = @_; + + my $job_type = $self->type; + return $job_type eq 'batch_biblio_record_modification' + ? Koha::BackgroundJob::BatchUpdateBiblio->process($args) + : $job_type eq 'batch_authority_record_modification' + ? Koha::BackgroundJob::BatchUpdateAuthority->process($args) + : Koha::Exceptions::Exception->throw('->process called without valid job_type'); +} + +sub job_type { croak "This method must be subclassed" } sub messages { my ( $self ) = @_; --- a/Koha/BackgroundJob/BatchUpdateAuthority.pm +++ a/Koha/BackgroundJob/BatchUpdateAuthority.pm @@ -1,22 +1,29 @@ package Koha::BackgroundJob::BatchUpdateAuthority; use Modern::Perl; +use JSON qw( encode_json decode_json ); + +use C4::MarcModificationTemplates; +use C4::AuthoritiesMarc; use Koha::BackgroundJobs; use Koha::DateUtils qw( dt_from_string ); -use JSON qw( encode_json decode_json ); +use Koha::MetadataRecord::Authority; use base 'Koha::BackgroundJob'; -our $channel; -sub process { - my ( $self, $args, $channel ) = @_; - - my $job_type = $args->{job_type}; +sub job_type { + return 'batch_authority_record_modification'; +} - return unless exists $args->{job_id}; +sub process { + my ( $self, $args ) = @_; my $job = Koha::BackgroundJobs->find( $args->{job_id} ); + if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) { + return; + } + my $job_progress = 0; $job->started_on(dt_from_string) ->progress($job_progress) @@ -28,14 +35,13 @@ sub process { my @record_ids = @{ $args->{record_ids} }; my $report = { - total_records => 0, + total_records => scalar @record_ids, total_success => 0, }; my @messages; my $dbh = C4::Context->dbh; $dbh->{RaiseError} = 1; RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) { - $report->{total_records}++; next unless $record_id; # Authorities my $authid = $record_id; @@ -68,11 +74,10 @@ sub process { $job_data->{report} = $report; $job->ended_on(dt_from_string) - ->status('finished') - ->data(encode_json $job_data) - ->store; + ->data(encode_json $job_data); + $job->status('finished') if $job->status ne 'cancelled'; + $job->store; - $channel->ack(); # FIXME Is that ok even on failure? } sub enqueue { @@ -88,7 +93,6 @@ sub enqueue { my @record_ids = @{ $args->{record_ids} }; $self->SUPER::enqueue({ - job_type => 'batch_record_modification', job_size => scalar @record_ids, job_args => {mmtid => $mmtid, record_type => $record_type, record_ids => \@record_ids,} }); --- a/Koha/BackgroundJob/BatchUpdateBiblio.pm +++ a/Koha/BackgroundJob/BatchUpdateBiblio.pm @@ -1,12 +1,19 @@ package Koha::BackgroundJob::BatchUpdateBiblio; use Modern::Perl; +use JSON qw( encode_json decode_json ); + use Koha::BackgroundJobs; use Koha::DateUtils qw( dt_from_string ); -use JSON qw( encode_json decode_json ); +use C4::Biblio; +use C4::MarcModificationTemplates; use base 'Koha::BackgroundJob'; +sub job_type { + return 'batch_biblio_record_modification'; +} + sub process { my ( $self, $args ) = @_; @@ -18,6 +25,9 @@ sub process { return; } + # FIXME If the job has already been started, but started again (worker has been restart for instance) + # Then we will start from scratch and so double process the same records + my $job_progress = 0; $job->started_on(dt_from_string) ->progress($job_progress) @@ -89,7 +99,6 @@ sub enqueue { my @record_ids = @{ $args->{record_ids} }; $self->SUPER::enqueue({ - job_type => 'batch_biblio_record_modification', # FIXME Must be a global const job_size => scalar @record_ids, job_args => {mmtid => $mmtid, record_type => $record_type, record_ids => \@record_ids,} }); --- a/Koha/BackgroundJobs.pm +++ a/Koha/BackgroundJobs.pm @@ -2,6 +2,7 @@ package Koha::BackgroundJobs; use Modern::Perl; use base qw(Koha::Objects); +use Koha::BackgroundJob; sub _type { return 'BackgroundJob'; --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt @@ -60,7 +60,22 @@ [% END %] [% END %] - [% CASE %][% job.type | html %] + [% CASE 'batch_authority_record_modification' %] + [% SET report = job.report %] + [% IF report %] + [% IF report.total_records == report.total_success %] +
+ [% ELSE %] + + [% END %] + [% END %] + [% CASE %]Job type "[% job.type | html %]" not handled in the template [% END %]