Bug 34587: SUSHI COUNTER Harvester
[koha.git] / Koha / BackgroundJob / ErmSushiHarvester.pm
1 package Koha::BackgroundJob::ErmSushiHarvester;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Try::Tiny;
21
22 use Koha::DateUtils qw( dt_from_string );
23 use Koha::ERM::UsageDataProviders;
24
25 use base 'Koha::BackgroundJob';
26
27 =head1 NAME
28
29 Koha::BackgroundJob::ErmSushiHarvester - Background job derived class to process the ERM Usage Statistics SUSHI Harvester
30 =head1 API
31
32 =head2 Class methods
33
34 =head3 job_type
35
36 Define the job type of this job: erm_sushi_harvester
37
38 =cut
39
40 sub job_type {
41     return 'erm_sushi_harvester';
42 }
43
44 =head3 process
45
46 Koha::BackgroundJob->find($id)->process(
47     {
48         ud_provider_id => $self->erm_usage_data_provider_id
49     }
50 );
51
52 Process the harvesting.
53
54 =cut
55
56 sub process {
57     my ( $self, $args ) = @_;
58
59     if ( $self->status eq 'cancelled' ) {
60         return;
61     }
62
63     $self->{us_report_info} = {
64         skipped_mus => 0,
65         skipped_yus => 0,
66         added_mus   => 0,
67         added_yus   => 0
68     };
69
70 # FIXME If the job has already been started, but started again (worker has been restart for instance)
71 # Then we will start from scratch and so double process the same records
72
73     $self->start;
74
75     my $ud_provider =
76       Koha::ERM::UsageDataProviders->find( $args->{ud_provider_id} );
77
78     $ud_provider->harvest(
79         $args->{report_type},
80         {
81             report_info_callback => sub { $self->report_info(@_); },
82             step_callback        => sub { $self->step; },
83             set_size_callback    => sub { $self->set_job_size(@_); },
84             add_message_callback => sub { $self->add_message(@_); },
85         }
86     );
87
88     # Prepare job report
89     my $report = {
90         report_type      => $ud_provider->{report_type},
91         total_records    => $ud_provider->{total_records},
92         us_report_info   => $self->{us_report_info},
93         ud_provider_id   => $ud_provider->erm_usage_data_provider_id,
94         ud_provider_name => $ud_provider->name,
95     };
96
97     my $data = $self->decoded_data;
98     $data->{report}   = $report;
99     $data->{messages} = \@{ $self->{messages} };
100
101     $self->finish($data);
102 }
103
104 =head3 report_info
105
106 Setter for report_info
107
108 =cut
109
110 sub report_info {
111     my ( $self, $info ) = @_;
112
113     $self->{us_report_info}{$info}++;
114 }
115
116 =head3 set_job_size
117
118 Setter for job_size
119
120 =cut
121
122 sub set_job_size {
123     my ( $self, $size ) = @_;
124
125     $self->size($size)->store();
126 }
127
128 =head3 add_message
129
130     $job->add_message(
131         {
132             type => 'success', # success, warning or error
133             code => 'object_added', # object_added or object_already_exists
134             title => $row->{Title},
135         }
136     );
137
138 Add a new job message
139
140 =cut
141
142 sub add_message {
143     my ( $self, $message ) = @_;
144
145     push @{ $self->{messages} }, $message;
146
147 }
148
149 =head3 enqueue
150
151 Enqueue the new job
152
153 =cut
154
155 sub enqueue {
156     my ( $self, $args ) = @_;
157
158     $self->SUPER::enqueue(
159         {
160             job_size  => 1,
161             job_args  => {%$args},
162             job_queue => 'long_tasks',
163         }
164     );
165 }
166
167 1;