|
Line 0
Link Here
|
|
|
1 |
package Koha::BackgroundJob::BatchDeleteAuthority; |
| 2 |
|
| 3 |
use Modern::Perl; |
| 4 |
use JSON qw( encode_json decode_json ); |
| 5 |
|
| 6 |
use Koha::BackgroundJobs; |
| 7 |
use Koha::DateUtils qw( dt_from_string ); |
| 8 |
use C4::AuthoritiesMarc; |
| 9 |
|
| 10 |
use base 'Koha::BackgroundJob'; |
| 11 |
|
| 12 |
sub job_type { |
| 13 |
return 'batch_authority_record_deletion'; |
| 14 |
} |
| 15 |
|
| 16 |
sub process { |
| 17 |
my ( $self, $args ) = @_; |
| 18 |
|
| 19 |
my $job_type = $args->{job_type}; |
| 20 |
|
| 21 |
my $job = Koha::BackgroundJobs->find( $args->{job_id} ); |
| 22 |
|
| 23 |
if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
# FIXME If the job has already been started, but started again (worker has been restart for instance) |
| 28 |
# Then we will start from scratch and so double delete the same records |
| 29 |
|
| 30 |
my $job_progress = 0; |
| 31 |
$job->started_on(dt_from_string) |
| 32 |
->progress($job_progress) |
| 33 |
->status('started') |
| 34 |
->store; |
| 35 |
|
| 36 |
my $mmtid = $args->{mmtid}; |
| 37 |
my @record_ids = @{ $args->{record_ids} }; |
| 38 |
|
| 39 |
my $report = { |
| 40 |
total_records => scalar @record_ids, |
| 41 |
total_success => 0, |
| 42 |
}; |
| 43 |
my @messages; |
| 44 |
my $schema = Koha::Database->new->schema; |
| 45 |
RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) { |
| 46 |
|
| 47 |
last if $job->get_from_storage->status eq 'cancelled'; |
| 48 |
|
| 49 |
next unless $record_id; |
| 50 |
|
| 51 |
$schema->storage->txn_begin; |
| 52 |
|
| 53 |
my $authid = $record_id; |
| 54 |
eval { C4::AuthoritiesMarc::DelAuthority({ authid => $authid }) }; |
| 55 |
if ( $@ ) { |
| 56 |
push @messages, { |
| 57 |
type => 'error', |
| 58 |
code => 'authority_not_deleted', |
| 59 |
authid => $authid, |
| 60 |
error => ($@ ? $@ : 0), |
| 61 |
}; |
| 62 |
$schema->storage->txn_rollback; |
| 63 |
next; |
| 64 |
} else { |
| 65 |
push @messages, { |
| 66 |
type => 'success', |
| 67 |
code => 'authority_deleted', |
| 68 |
authid => $authid, |
| 69 |
}; |
| 70 |
$report->{total_success}++; |
| 71 |
$schema->storage->txn_commit; |
| 72 |
} |
| 73 |
|
| 74 |
$job->progress( ++$job_progress )->store; |
| 75 |
} |
| 76 |
|
| 77 |
my $job_data = decode_json $job->data; |
| 78 |
$job_data->{messages} = \@messages; |
| 79 |
$job_data->{report} = $report; |
| 80 |
|
| 81 |
$job->ended_on(dt_from_string) |
| 82 |
->data(encode_json $job_data); |
| 83 |
$job->status('finished') if $job->status ne 'cancelled'; |
| 84 |
$job->store; |
| 85 |
} |
| 86 |
|
| 87 |
sub enqueue { |
| 88 |
my ( $self, $args) = @_; |
| 89 |
|
| 90 |
# TODO Raise exception instead |
| 91 |
return unless exists $args->{record_ids}; |
| 92 |
|
| 93 |
my @record_ids = @{ $args->{record_ids} }; |
| 94 |
|
| 95 |
$self->SUPER::enqueue({ |
| 96 |
job_size => scalar @record_ids, |
| 97 |
job_args => {record_ids => \@record_ids,} |
| 98 |
}); |
| 99 |
} |
| 100 |
|
| 101 |
1; |