Bug 34587: Accept date inputs upon confirming 'run now' button
[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->{begin_date},
80         $args->{end_date},
81         $args->{report_type},
82         {
83             report_info_callback => sub { $self->report_info(@_); },
84             step_callback        => sub { $self->step; },
85             set_size_callback    => sub { $self->set_job_size(@_); },
86             add_message_callback => sub { $self->add_message(@_); },
87         }
88     );
89
90     # Prepare job report
91     my $report = {
92         report_type      => $ud_provider->{report_type},
93         total_records    => $ud_provider->{total_records},
94         us_report_info   => $self->{us_report_info},
95         ud_provider_id   => $ud_provider->erm_usage_data_provider_id,
96         ud_provider_name => $ud_provider->name,
97     };
98
99     my $data = $self->decoded_data;
100     $data->{report}   = $report;
101     $data->{messages} = \@{ $self->{messages} };
102
103     $self->finish($data);
104 }
105
106 =head3 report_info
107
108 Setter for report_info
109
110 =cut
111
112 sub report_info {
113     my ( $self, $info ) = @_;
114
115     $self->{us_report_info}{$info}++;
116 }
117
118 =head3 set_job_size
119
120 Setter for job_size
121
122 =cut
123
124 sub set_job_size {
125     my ( $self, $size ) = @_;
126
127     $self->size($size)->store();
128 }
129
130 =head3 add_message
131
132     $job->add_message(
133         {
134             type => 'success', # success, warning or error
135             code => 'object_added', # object_added or object_already_exists
136             title => $row->{Title},
137         }
138     );
139
140 Add a new job message
141
142 =cut
143
144 sub add_message {
145     my ( $self, $message ) = @_;
146
147     push @{ $self->{messages} }, $message;
148
149 }
150
151 =head3 enqueue
152
153 Enqueue the new job
154
155 =cut
156
157 sub enqueue {
158     my ( $self, $args ) = @_;
159
160     $self->SUPER::enqueue(
161         {
162             job_size  => 1,
163             job_args  => {%$args},
164             job_queue => 'long_tasks',
165         }
166     );
167 }
168
169 1;