Bug 22417: Switch to STOMP
[koha.git] / Koha / BackgroundJob / BatchUpdateAuthority.pm
1 package Koha::BackgroundJob::BatchUpdateAuthority;
2
3 use Modern::Perl;
4 use Koha::BackgroundJobs;
5 use Koha::DateUtils qw( dt_from_string );
6 use JSON qw( encode_json decode_json );
7
8 use base 'Koha::BackgroundJob';
9
10 our $channel;
11 sub process {
12     my ( $self, $args, $channel ) = @_;
13
14     my $job_type = $args->{job_type};
15
16     return unless exists $args->{job_id};
17
18     my $job = Koha::BackgroundJobs->find( $args->{job_id} );
19
20     my $job_progress = 0;
21     $job->started_on(dt_from_string)
22         ->progress($job_progress)
23         ->status('started')
24         ->store;
25
26     my $mmtid = $args->{mmtid};
27     my $record_type = $args->{record_type};
28     my @record_ids = @{ $args->{record_ids} };
29
30     my $report = {
31         total_records => 0,
32         total_success => 0,
33     };
34     my @messages;
35     my $dbh = C4::Context->dbh;
36     $dbh->{RaiseError} = 1;
37     RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
38         $report->{total_records}++;
39         next unless $record_id;
40         # Authorities
41         my $authid = $record_id;
42         my $error = eval {
43             my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
44             my $record = $authority->record;
45             ModifyRecordWithTemplate( $mmtid, $record );
46             ModAuthority( $authid, $record, $authority->authtypecode );
47         };
48         if ( $error and $error != $authid or $@ ) {
49             push @messages, {
50                 type => 'error',
51                 code => 'authority_not_modified',
52                 authid => $authid,
53                 error => ($@ ? $@ : 0),
54             };
55         } else {
56             push @messages, {
57                 type => 'success',
58                 code => 'authority_modified',
59                 authid => $authid,
60             };
61             $report->{total_success}++;
62         }
63         $job->progress( ++$job_progress )->store;
64     }
65
66     my $job_data = decode_json $job->data;
67     $job_data->{messages} = \@messages;
68     $job_data->{report} = $report;
69
70     $job->ended_on(dt_from_string)
71         ->status('finished')
72         ->data(encode_json $job_data)
73         ->store;
74
75     $channel->ack(); # FIXME Is that ok even on failure?
76 }
77
78 sub enqueue {
79     my ( $self, $args) = @_;
80
81     # TODO Raise exception instead
82     return unless exists $args->{mmtid};
83     return unless exists $args->{record_type}; #FIXME RMME
84     return unless exists $args->{record_ids};
85
86     my $mmtid = $args->{mmtid};
87     my $record_type = $args->{record_type};
88     my @record_ids = @{ $args->{record_ids} };
89
90     $self->SUPER::enqueue({
91         job_type => 'batch_record_modification',
92         job_size => scalar @record_ids,
93         job_args => {mmtid => $mmtid, record_type => $record_type, record_ids => \@record_ids,}
94     });
95 }
96
97 1;