1 package Koha::BackgroundJob::BatchUpdateBiblio;
4 use JSON qw( encode_json decode_json );
6 use Koha::BackgroundJobs;
7 use Koha::DateUtils qw( dt_from_string );
9 use C4::MarcModificationTemplates;
11 use base 'Koha::BackgroundJob';
14 return 'batch_biblio_record_modification';
18 my ( $self, $args ) = @_;
20 my $job_type = $args->{job_type};
22 my $job = Koha::BackgroundJobs->find( $args->{job_id} );
24 if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
28 # FIXME If the job has already been started, but started again (worker has been restart for instance)
29 # Then we will start from scratch and so double process the same records
32 $job->started_on(dt_from_string)
33 ->progress($job_progress)
37 my $mmtid = $args->{mmtid};
38 my @record_ids = @{ $args->{record_ids} };
41 total_records => scalar @record_ids,
45 my $dbh = C4::Context->dbh;
46 $dbh->{RaiseError} = 1;
47 RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) {
49 last if $job->get_from_storage->status eq 'cancelled';
51 next unless $biblionumber;
55 my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
56 C4::MarcModificationTemplates::ModifyRecordWithTemplate( $mmtid, $record );
57 my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
58 C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode );
60 if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
63 code => 'biblio_not_modified',
64 biblionumber => $biblionumber,
65 error => ($@ ? $@ : $error),
70 code => 'biblio_modified',
71 biblionumber => $biblionumber,
73 $report->{total_success}++;
75 $job->progress( ++$job_progress )->store;
78 my $job_data = decode_json $job->data;
79 $job_data->{messages} = \@messages;
80 $job_data->{report} = $report;
82 $job->ended_on(dt_from_string)
83 ->data(encode_json $job_data);
84 $job->status('finished') if $job->status ne 'cancelled';
89 my ( $self, $args) = @_;
91 # TODO Raise exception instead
92 return unless exists $args->{mmtid};
93 return unless exists $args->{record_ids};
95 my $mmtid = $args->{mmtid};
96 my @record_ids = @{ $args->{record_ids} };
98 $self->SUPER::enqueue({
99 job_size => scalar @record_ids,
100 job_args => {mmtid => $mmtid, record_ids => \@record_ids,}