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

(-)a/Koha/BackgroundJob.pm (+70 lines)
Lines 26-31 use C4::Context; Link Here
26
use Koha::DateUtils qw( dt_from_string );
26
use Koha::DateUtils qw( dt_from_string );
27
use Koha::Exceptions;
27
use Koha::Exceptions;
28
use Koha::Plugins;
28
use Koha::Plugins;
29
use Koha::Exceptions::BackgroundJob;
29
30
30
use base qw( Koha::Object );
31
use base qw( Koha::Object );
31
32
Lines 164-169 sub process { Link Here
164
    return $derived_class->process( $args );
165
    return $derived_class->process( $args );
165
}
166
}
166
167
168
=head3 start
169
170
    $self->start;
171
172
Marks the job as started.
173
174
=cut
175
176
sub start {
177
    my ($self) = @_;
178
179
    Koha::Exceptions::BackgroundJob::InconsistentStatus->throw(
180
        current_status  => $self->status,
181
        expected_status => 'new'
182
    ) unless $self->status eq 'new';
183
184
    return $self->set(
185
        {
186
            started_on => \'NOW()',
187
            progress   => 0,
188
            status     => 'started',
189
        }
190
    )->store;
191
}
192
193
=head3 step
194
195
    $self->step;
196
197
Makes the job record a step has taken place.
198
199
=cut
200
201
sub step {
202
    my ($self) = @_;
203
204
    Koha::Exceptions::BackgroundJob::InconsistentStatus->throw(
205
        current_status  => $self->status,
206
        expected_status => 'started'
207
    ) unless $self->status eq 'started';
208
209
    # reached the end of the tasks already
210
    Koha::Exceptions::BackgroundJob::StepOutOfBounds->throw()
211
        unless $self->progress < $self->size;
212
213
    return $self->progress( $self->progress + 1 )->store;
214
}
215
216
=head3 finish
217
218
    $self->finish;
219
220
Makes the job record as finished. If the job status is I<cancelled>, it is kept.
221
222
=cut
223
224
sub finish {
225
    my ( $self, $data ) = @_;
226
227
    $self->status('finished') unless $self->status eq 'cancelled';
228
229
    return $self->set(
230
        {
231
            ended_on => \'NOW()',
232
            data     => encode_json($data),
233
        }
234
    )->store;
235
}
236
167
=head3 job_type
237
=head3 job_type
168
238
169
Return the job type of the job. Must be a string.
239
Return the job type of the job. Must be a string.
(-)a/Koha/Exceptions/BackgroundJob.pm (+60 lines)
Line 0 Link Here
1
package Koha::Exceptions::BackgroundJob;
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 Koha::Exception;
21
22
use Exception::Class (
23
24
    'Koha::Exceptions::BackgroundJob' => {
25
        isa => 'Koha::Exception',
26
    },
27
    'Koha::Exceptions::BackgroundJob::InconsistentStatus' => {
28
        isa         => 'Koha::Exceptions::BackgroundJob',
29
        description => 'Status change requested but an invalid status found',
30
        fields      => ['current_status', 'expected_status']
31
    },
32
    'Koha::Exceptions::BackgroundJob::StepOutOfBounds' => {
33
        isa         => 'Koha::Exceptions::BackgroundJob',
34
        description => 'Cannot move progress forward'
35
    },
36
);
37
38
=head1 NAME
39
40
Koha::Exceptions::BackgroundJob - Base class for BackgroundJob exceptions
41
42
=head1 Exceptions
43
44
=head2 Koha::Exceptions::BackgroundJob
45
46
Generic BackgroundJob exception
47
48
=head2 Koha::Exceptions::BackgroundJob::InconsistentStatus
49
50
Exception to be used when an action on an BackgroundJob requires the job to
51
be in an specific I<status>, but it is not the case.
52
53
=head2 Koha::Exceptions::BackgroundJob::StepOutOfBounds
54
55
Exception to be used when the it is tried to advance one step in progress, but
56
the job size limit as been reached already.
57
58
=cut
59
60
1;
(-)a/t/db_dependent/Koha/BackgroundJob.t (-2 / +67 lines)
Lines 17-28 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 2;
20
use Test::More tests => 3;
21
use Test::Exception;
21
22
22
use Koha::Database;
23
use Koha::Database;
23
use Koha::BackgroundJobs;
24
use Koha::BackgroundJobs;
24
use Koha::BackgroundJob::BatchUpdateItem;
25
use Koha::BackgroundJob::BatchUpdateItem;
25
26
27
use JSON qw( decode_json );
28
26
use t::lib::Mocks;
29
use t::lib::Mocks;
27
use t::lib::TestBuilder;
30
use t::lib::TestBuilder;
28
31
Lines 86-88 subtest 'enqueue() tests' => sub { Link Here
86
89
87
    $schema->storage->txn_rollback;
90
    $schema->storage->txn_rollback;
88
};
91
};
89
- 
92
93
subtest 'start(), step() and finish() tests' => sub {
94
95
    plan tests => 19;
96
97
    $schema->storage->txn_begin;
98
99
    # FIXME: This all feels we need to do it better...
100
    my $job_id = Koha::BackgroundJob::BatchUpdateItem->new->enqueue( { record_ids => [ 1, 2 ] } );
101
    my $job    = Koha::BackgroundJobs->find($job_id)->_derived_class;
102
103
    is( $job->started_on, undef, 'started_on not set yet' );
104
    is( $job->size, 2, 'Two steps' );
105
106
    $job->start;
107
108
    isnt( $job->started_on, undef, 'started_on set' );
109
    is( $job->status, 'started' );
110
    is( $job->progress, 0, 'No progress yet' );
111
112
    $job->step;
113
    is( $job->progress, 1, 'First step' );
114
    $job->step;
115
    is( $job->progress, 2, 'Second step' );
116
    throws_ok
117
        { $job->step; }
118
        'Koha::Exceptions::BackgroundJob::StepOutOfBounds',
119
        'Tried to make a forbidden extra step';
120
121
    is( $job->progress, 2, 'progress remains unchanged' );
122
123
    my $data = { some => 'data' };
124
125
    $job->status('cancelled')->store;
126
    $job->finish( $data );
127
128
    is( $job->status, 'cancelled', "'finish' leaves 'cancelled' untouched" );
129
    isnt( $job->ended_on, undef, 'ended_on set' );
130
    is_deeply( decode_json( $job->data ), $data );
131
132
    $job->status('started')->store;
133
    $job->finish( $data );
134
135
    is( $job->status, 'finished' );
136
    isnt( $job->ended_on, undef, 'ended_on set' );
137
    is_deeply( decode_json( $job->data ), $data );
138
139
    throws_ok
140
        { $job->start; }
141
        'Koha::Exceptions::BackgroundJob::InconsistentStatus',
142
        'Exception thrown trying to start a finished job';
143
144
    is( $@->expected_status, 'new' );
145
146
    throws_ok
147
        { $job->step; }
148
        'Koha::Exceptions::BackgroundJob::InconsistentStatus',
149
        'Exception thrown trying to start a finished job';
150
151
    is( $@->expected_status, 'started' );
152
153
    $schema->storage->txn_rollback;
154
};

Return to bug 30360