View | Details | Raw Unified | Return to bug 22417
Collapse All | Expand All

(-)a/Koha/BackgroundJob.pm (-4 / +17 lines)
Lines 7-13 use Net::Stomp; Link Here
7
7
8
use C4::Context;
8
use C4::Context;
9
use Koha::DateUtils qw( dt_from_string );
9
use Koha::DateUtils qw( dt_from_string );
10
use Koha::BackgroundJobs;
10
use Koha::Exceptions;
11
use Koha::BackgroundJob::BatchUpdateBiblio;
12
use Koha::BackgroundJob::BatchUpdateAuthority;
11
13
12
use base qw( Koha::Object );
14
use base qw( Koha::Object );
13
15
Lines 21-27 sub connect { Link Here
21
sub enqueue {
23
sub enqueue {
22
    my ( $self, $params ) = @_;
24
    my ( $self, $params ) = @_;
23
25
24
    my $job_type = $params->{job_type};
26
    my $job_type = $self->job_type;
25
    my $job_size = $params->{job_size};
27
    my $job_size = $params->{job_size};
26
    my $job_args = $params->{job_args};
28
    my $job_args = $params->{job_args};
27
29
Lines 47-60 sub enqueue { Link Here
47
49
48
            my $conn = $self->connect;
50
            my $conn = $self->connect;
49
            $conn->send_with_receipt( { destination => $job_type, body => $json_args } )
51
            $conn->send_with_receipt( { destination => $job_type, body => $json_args } )
50
              or Koha::Exception->throw('Job has not been enqueued');
52
              or Koha::Exceptions::Exception->throw('Job has not been enqueued');
51
        }
53
        }
52
    );
54
    );
53
55
54
    return $job_id;
56
    return $job_id;
55
}
57
}
56
58
57
sub process { croak "This method must be subclassed" }
59
sub process {
60
    my ( $self, $args ) = @_;
61
62
    my $job_type = $self->type;
63
    return $job_type eq 'batch_biblio_record_modification'
64
      ? Koha::BackgroundJob::BatchUpdateBiblio->process($args)
65
      : $job_type eq 'batch_authority_record_modification'
66
      ? Koha::BackgroundJob::BatchUpdateAuthority->process($args)
67
      : Koha::Exceptions::Exception->throw('->process called without valid job_type');
68
}
69
70
sub job_type { croak "This method must be subclassed" }
58
71
59
sub messages {
72
sub messages {
60
    my ( $self ) = @_;
73
    my ( $self ) = @_;
(-)a/Koha/BackgroundJob/BatchUpdateAuthority.pm (-14 / +18 lines)
Lines 1-22 Link Here
1
package Koha::BackgroundJob::BatchUpdateAuthority;
1
package Koha::BackgroundJob::BatchUpdateAuthority;
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use JSON qw( encode_json decode_json );
5
6
use C4::MarcModificationTemplates;
7
use C4::AuthoritiesMarc;
4
use Koha::BackgroundJobs;
8
use Koha::BackgroundJobs;
5
use Koha::DateUtils qw( dt_from_string );
9
use Koha::DateUtils qw( dt_from_string );
6
use JSON qw( encode_json decode_json );
10
use Koha::MetadataRecord::Authority;
7
11
8
use base 'Koha::BackgroundJob';
12
use base 'Koha::BackgroundJob';
9
13
10
our $channel;
14
sub job_type {
11
sub process {
15
    return 'batch_authority_record_modification';
12
    my ( $self, $args, $channel ) = @_;
16
}
13
14
    my $job_type = $args->{job_type};
15
17
16
    return unless exists $args->{job_id};
18
sub process {
19
    my ( $self, $args ) = @_;
17
20
18
    my $job = Koha::BackgroundJobs->find( $args->{job_id} );
21
    my $job = Koha::BackgroundJobs->find( $args->{job_id} );
19
22
23
    if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
24
        return;
25
    }
26
20
    my $job_progress = 0;
27
    my $job_progress = 0;
21
    $job->started_on(dt_from_string)
28
    $job->started_on(dt_from_string)
22
        ->progress($job_progress)
29
        ->progress($job_progress)
Lines 28-41 sub process { Link Here
28
    my @record_ids = @{ $args->{record_ids} };
35
    my @record_ids = @{ $args->{record_ids} };
29
36
30
    my $report = {
37
    my $report = {
31
        total_records => 0,
38
        total_records => scalar @record_ids,
32
        total_success => 0,
39
        total_success => 0,
33
    };
40
    };
34
    my @messages;
41
    my @messages;
35
    my $dbh = C4::Context->dbh;
42
    my $dbh = C4::Context->dbh;
36
    $dbh->{RaiseError} = 1;
43
    $dbh->{RaiseError} = 1;
37
    RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
44
    RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
38
        $report->{total_records}++;
39
        next unless $record_id;
45
        next unless $record_id;
40
        # Authorities
46
        # Authorities
41
        my $authid = $record_id;
47
        my $authid = $record_id;
Lines 68-78 sub process { Link Here
68
    $job_data->{report} = $report;
74
    $job_data->{report} = $report;
69
75
70
    $job->ended_on(dt_from_string)
76
    $job->ended_on(dt_from_string)
71
        ->status('finished')
77
        ->data(encode_json $job_data);
72
        ->data(encode_json $job_data)
78
    $job->status('finished') if $job->status ne 'cancelled';
73
        ->store;
79
    $job->store;
74
80
75
    $channel->ack(); # FIXME Is that ok even on failure?
76
}
81
}
77
82
78
sub enqueue {
83
sub enqueue {
Lines 88-94 sub enqueue { Link Here
88
    my @record_ids = @{ $args->{record_ids} };
93
    my @record_ids = @{ $args->{record_ids} };
89
94
90
    $self->SUPER::enqueue({
95
    $self->SUPER::enqueue({
91
        job_type => 'batch_record_modification',
92
        job_size => scalar @record_ids,
96
        job_size => scalar @record_ids,
93
        job_args => {mmtid => $mmtid, record_type => $record_type, record_ids => \@record_ids,}
97
        job_args => {mmtid => $mmtid, record_type => $record_type, record_ids => \@record_ids,}
94
    });
98
    });
(-)a/Koha/BackgroundJob/BatchUpdateBiblio.pm (-2 / +11 lines)
Lines 1-12 Link Here
1
package Koha::BackgroundJob::BatchUpdateBiblio;
1
package Koha::BackgroundJob::BatchUpdateBiblio;
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use JSON qw( encode_json decode_json );
5
4
use Koha::BackgroundJobs;
6
use Koha::BackgroundJobs;
5
use Koha::DateUtils qw( dt_from_string );
7
use Koha::DateUtils qw( dt_from_string );
6
use JSON qw( encode_json decode_json );
8
use C4::Biblio;
9
use C4::MarcModificationTemplates;
7
10
8
use base 'Koha::BackgroundJob';
11
use base 'Koha::BackgroundJob';
9
12
13
sub job_type {
14
    return 'batch_biblio_record_modification';
15
}
16
10
sub process {
17
sub process {
11
    my ( $self, $args ) = @_;
18
    my ( $self, $args ) = @_;
12
19
Lines 18-23 sub process { Link Here
18
        return;
25
        return;
19
    }
26
    }
20
27
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
30
21
    my $job_progress = 0;
31
    my $job_progress = 0;
22
    $job->started_on(dt_from_string)
32
    $job->started_on(dt_from_string)
23
        ->progress($job_progress)
33
        ->progress($job_progress)
Lines 89-95 sub enqueue { Link Here
89
    my @record_ids = @{ $args->{record_ids} };
99
    my @record_ids = @{ $args->{record_ids} };
90
100
91
    $self->SUPER::enqueue({
101
    $self->SUPER::enqueue({
92
        job_type => 'batch_biblio_record_modification', # FIXME Must be a global const
93
        job_size => scalar @record_ids,
102
        job_size => scalar @record_ids,
94
        job_args => {mmtid => $mmtid, record_type => $record_type, record_ids => \@record_ids,}
103
        job_args => {mmtid => $mmtid, record_type => $record_type, record_ids => \@record_ids,}
95
    });
104
    });
(-)a/Koha/BackgroundJobs.pm (+1 lines)
Lines 2-7 package Koha::BackgroundJobs; Link Here
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use base qw(Koha::Objects);
4
use base qw(Koha::Objects);
5
use Koha::BackgroundJob;
5
6
6
sub _type {
7
sub _type {
7
    return 'BackgroundJob';
8
    return 'BackgroundJob';
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt (-3 / +37 lines)
Lines 60-66 Link Here
60
                            </div>
60
                            </div>
61
                        [% END %]
61
                        [% END %]
62
                    [% END %]
62
                    [% END %]
63
                [% CASE %][% job.type | html %]
63
                [% CASE 'batch_authority_record_modification' %]
64
                    [% SET report = job.report %]
65
                    [% IF report %]
66
                        [% IF report.total_records == report.total_success %]
67
                            <div class="dialog message">
68
                                All records have successfully been modified! <a href="/cgi-bin/koha/tools/batch_record_modification.pl" title="New batch record modification">New batch record modification</a>
69
                            </div>
70
                        [% ELSE %]
71
                            <div class="dialog message">
72
                                [% report.total_success | html %] / [% report.total_records | html %] records have successfully been modified. Some errors occurred.
73
                                [% IF job.status == 'cancelled' %]The job has been cancelled before it finished.[% END %]
74
                                <a href="/cgi-bin/koha/tools/batch_record_modification.pl" title="New batch record modification">New batch record modification</a>
75
                            </div>
76
                        [% END %]
77
                    [% END %]
78
                [% CASE %]Job type "[% job.type | html %]" not handled in the template
64
                [% END %]
79
                [% END %]
65
            </li>
80
            </li>
66
            <li><label for="job_data">Detailed messages: </label>
81
            <li><label for="job_data">Detailed messages: </label>
Lines 77-83 Link Here
77
                            [% END %]
92
                            [% END %]
78
                            [% SWITCH m.code %]
93
                            [% SWITCH m.code %]
79
                            [% CASE 'biblio_not_modified' %]
94
                            [% CASE 'biblio_not_modified' %]
80
                                Bibliographic record <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% m.biblionumber | uri %]">[% m.biblionumber | html %]</a> has not been modified. An error occurred on modifying it.
95
                                Bibliographic record <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% m.biblionumber | uri %]">[% m.biblionumber | html %]</a> has not been modified. An error occurred on modifying it.[% IF m.error %] ([% m.error %])[% END %].
81
                            [% CASE 'biblio_modified' %]
96
                            [% CASE 'biblio_modified' %]
82
                                Bibliographic record <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% m.biblionumber | uri %]">[% m.biblionumber | html %]</a> has successfully been modified.
97
                                Bibliographic record <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% m.biblionumber | uri %]">[% m.biblionumber | html %]</a> has successfully been modified.
83
                                <h3>Next steps</h3>
98
                                <h3>Next steps</h3>
Lines 98-104 Link Here
98
                            [% END %]
113
                            [% END %]
99
                        </div>
114
                        </div>
100
                    [% END %]
115
                    [% END %]
101
                [% CASE %][% job.type | html %]
116
                [% CASE 'batch_authority_record_modification' %]
117
                    [% FOR m IN job.messages %]
118
                        <div class="dialog message">
119
                            [% IF m.type == 'success' %]
120
                                <i class="fa fa-check success"></i>
121
                            [% ELSIF m.type == 'warning' %]
122
                                <i class="fa fa-warning warn"></i>
123
                            [% ELSIF m.type == 'error' %]
124
                                <i class="fa fa-exclamation error"></i>
125
                            [% END %]
126
                            [% SWITCH m.code %]
127
                            [% CASE 'authority_not_modified' %]
128
                                Authority record <a href="/cgi-bin/koha/authorities/detail.pl?authid=[% m.authid | uri %]">[% m.authid | html %]</a> has not been modified. An error occurred on modifying it[% IF m.error %] ([% m.error %])[% END %].
129
                            [% CASE 'authority_modified' %]
130
                                Authority record <a href="/cgi-bin/koha/authorities/detail.pl?authid=[% m.authid | uri %]">[% m.authid | html %]</a> has successfully been modified..
131
                            [% END %]
132
                        </div>
133
                    [% END %]
134
                [% CASE %]Job type "[% job.type | html %]" not handled in the template
102
                [% END %]
135
                [% END %]
103
            </li>
136
            </li>
104
        </ol>
137
        </ol>
Lines 143-148 Link Here
143
                    <td>
176
                    <td>
144
                        [% SWITCH job.type %]
177
                        [% SWITCH job.type %]
145
                        [% CASE 'batch_biblio_record_modification' %]Batch bibliographic record modification
178
                        [% CASE 'batch_biblio_record_modification' %]Batch bibliographic record modification
179
                        [% CASE 'batch_authority_record_modification' %]Batch authority record modification
146
                        [% CASE %][% job.type | html %]
180
                        [% CASE %][% job.type | html %]
147
                        [% END %]
181
                        [% END %]
148
                    </td>
182
                    </td>
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_record_modification.tt (-7 / +3 lines)
Lines 42-51 Link Here
42
                                    Bibliographic record [% message.biblionumber | html %] does not exist in the database.
42
                                    Bibliographic record [% message.biblionumber | html %] does not exist in the database.
43
                                [% ELSIF message.code == 'authority_not_exists' %]
43
                                [% ELSIF message.code == 'authority_not_exists' %]
44
                                    Authority record [% message.authid | html %] does not exist in the database.
44
                                    Authority record [% message.authid | html %] does not exist in the database.
45
                                [% ELSIF message.code == 'authority_not_modified' %]
46
                                    Authority record <a href="/cgi-bin/koha/authorities/detail.pl?authid=[% message.authid | uri %]">[% message.authid | html %]</a> has not been modified. An error occurred on modifying it.
47
                                [% ELSIF message.code == 'authority_modified' %]
48
                                    Bibliographic record <a href="/cgi-bin/koha/authorities/detail.pl?authid=[% message.authid | uri %]">[% message.authid | html %]</a> has successfully been modified.
49
                                [% ELSIF message.code == 'cannot_enqueue_job' %]
45
                                [% ELSIF message.code == 'cannot_enqueue_job' %]
50
                                    Cannot enqueue this job.
46
                                    Cannot enqueue this job.
51
                                [% END %]
47
                                [% END %]
Lines 249-257 Link Here
249
                        [% END %]
245
                        [% END %]
250
                     [% ELSIF view == 'enqueued' %]
246
                     [% ELSIF view == 'enqueued' %]
251
                        <div class="dialog message">
247
                        <div class="dialog message">
252
                            The job has been enqueued! It will be processed as soon as possible (FIXME - well, it could depend on a config?)
248
                            <p>The job has been enqueued! It will be processed as soon as possible (FIXME - well, it could depend on a config?)</p>
253
                            <a href="/cgi-bin/koha/admin/background_jobs.pl?op=view&id=[% job_id | uri %]" title="View detail of the enqueued job">View detail of the enqueued job</a>
249
                            <p><a href="/cgi-bin/koha/admin/background_jobs.pl?op=view&id=[% job_id | uri %]" title="View detail of the enqueued job">View detail of the enqueued job</a>
254
                            | <a href="/cgi-bin/koha/tools/batch_record_modification.pl" title="New batch record modification">New batch record modification</a>
250
                            | <a href="/cgi-bin/koha/tools/batch_record_modification.pl" title="New batch record modification">New batch record modification</a></p>
255
                        </div>
251
                        </div>
256
                    [% ELSE %]
252
                    [% ELSE %]
257
                        <div class="dialog alert">
253
                        <div class="dialog alert">
(-)a/misc/background_jobs_worker.pl (-5 / +9 lines)
Lines 18-31 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
use JSON qw( encode_json decode_json );
19
use JSON qw( encode_json decode_json );
20
20
21
use Koha::BackgroundJob::BatchUpdateBiblio;
21
use Koha::BackgroundJobs;
22
use Koha::BackgroundJob;
23
22
24
my $conn = Koha::BackgroundJob->connect;
23
my $conn = Koha::BackgroundJob->connect;
25
24
26
my $job_type = 'batch_biblio_record_modification';
25
my @job_types = qw( batch_biblio_record_modification batch_authority_record_modification );
27
26
28
$conn->subscribe({ destination => $job_type, ack => 'client' });
27
for my $job_type ( @job_types ) {
28
    $conn->subscribe({ destination => $job_type, ack => 'client' });
29
}
29
while (1) {
30
while (1) {
30
    my $frame = $conn->receive_frame;
31
    my $frame = $conn->receive_frame;
31
    if ( !defined $frame ) {
32
    if ( !defined $frame ) {
Lines 36-42 while (1) { Link Here
36
    my $body = $frame->body;
37
    my $body = $frame->body;
37
    my $args = decode_json($body);
38
    my $args = decode_json($body);
38
39
39
    my $success = Koha::BackgroundJob::BatchUpdateBiblio->process( $args );
40
    # FIXME This means we need to have create the DB entry before
41
    # It could work in a first step, but then we will want to handle job that will be created from the message received
42
    my $job = Koha::BackgroundJobs->find($args->{job_id});
43
    my $success = $job->process( $args );
40
44
41
    $conn->ack( { frame => $frame } ); # FIXME depending on $success?
45
    $conn->ack( { frame => $frame } ); # FIXME depending on $success?
42
}
46
}
(-)a/tools/batch_record_modification.pl (-8 / +12 lines)
Lines 33-38 use C4::MarcModificationTemplates qw( GetModificationTemplateActions GetModifica Link Here
33
33
34
use Koha::Biblios;
34
use Koha::Biblios;
35
use Koha::BackgroundJob::BatchUpdateBiblio;
35
use Koha::BackgroundJob::BatchUpdateBiblio;
36
use Koha::BackgroundJob::BatchUpdateAuthority;
36
use Koha::MetadataRecord::Authority;
37
use Koha::MetadataRecord::Authority;
37
use Koha::Virtualshelves;
38
use Koha::Virtualshelves;
38
39
Lines 151-163 if ( $op eq 'form' ) { Link Here
151
    my @record_ids = $input->multi_param('record_id');
152
    my @record_ids = $input->multi_param('record_id');
152
153
153
    try {
154
    try {
154
        my $job_id = Koha::BackgroundJob::BatchUpdateBiblio->new->enqueue(
155
        my $params = {
155
            {
156
            mmtid       => $mmtid,
156
                mmtid       => $mmtid,
157
            record_type => $recordtype,
157
                record_type => $recordtype,
158
            record_ids  => \@record_ids,
158
                record_ids  => \@record_ids,
159
        };
159
            }
160
160
        );
161
        my $job_id =
162
          $recordtype eq 'biblio'
163
          ? Koha::BackgroundJob::BatchUpdateBiblio->new->enqueue($params)
164
          : Koha::BackgroundJob::BatchUpdateAuthority->new->enqueue($params);
165
161
        $template->param(
166
        $template->param(
162
            view => 'enqueued',
167
            view => 'enqueued',
163
            job_id => $job_id,
168
            job_id => $job_id,
164
- 

Return to bug 22417