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_type = $args->{record_type};
39 my @record_ids = @{ $args->{record_ids} };
42 total_records => scalar @record_ids,
46 my $dbh = C4::Context->dbh;
47 $dbh->{RaiseError} = 1;
48 RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) {
50 last if $job->get_from_storage->status eq 'cancelled';
52 next unless $biblionumber;
56 my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
57 C4::MarcModificationTemplates::ModifyRecordWithTemplate( $mmtid, $record );
58 my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
59 C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode );
61 if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
64 code => 'biblio_not_modified',
65 biblionumber => $biblionumber,
66 error => ($@ ? $@ : $error),
71 code => 'biblio_modified',
72 biblionumber => $biblionumber,
74 $report->{total_success}++;
76 $job->progress( ++$job_progress )->store;
79 my $job_data = decode_json $job->data;
80 $job_data->{messages} = \@messages;
81 $job_data->{report} = $report;
83 $job->ended_on(dt_from_string)
84 ->data(encode_json $job_data);
85 $job->status('finished') if $job->status ne 'cancelled';
90 my ( $self, $args) = @_;
92 # TODO Raise exception instead
93 return unless exists $args->{mmtid};
94 return unless exists $args->{record_type}; #FIXME RMME
95 return unless exists $args->{record_ids};
97 my $mmtid = $args->{mmtid};
98 my $record_type = $args->{record_type};
99 my @record_ids = @{ $args->{record_ids} };
101 $self->SUPER::enqueue({
102 job_size => scalar @record_ids,
103 job_args => {mmtid => $mmtid, record_type => $record_type, record_ids => \@record_ids,}