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

(-)a/Koha/BackgroundJob.pm (+69 lines)
Lines 25-30 use Try::Tiny qw( catch try ); Link Here
25
use C4::Context;
25
use C4::Context;
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::Exceptions::BackgroundJob;
28
29
29
use base qw( Koha::Object );
30
use base qw( Koha::Object );
30
31
Lines 161-166 sub process { Link Here
161
    return $derived_class->process( $args );
162
    return $derived_class->process( $args );
162
}
163
}
163
164
165
=head3 start
166
167
    $self->start;
168
169
Marks the job as started.
170
171
=cut
172
173
sub start {
174
    my ($self) = @_;
175
176
    Koha::Exceptions::BackgroundJob::InconsistentStatus->throw(
177
        current_status  => $self->status,
178
        expected_status => 'new'
179
    ) unless $self->status eq 'new';
180
181
    return $self->set(
182
        {
183
            started_on => \'NOW()',
184
            progress   => 0,
185
            status     => 'started',
186
        }
187
    )->store;
188
}
189
190
=head3 step
191
192
    $self->step;
193
194
Makes the job record a step has taken place.
195
196
=cut
197
198
sub step {
199
    my ($self) = @_;
200
201
    Koha::Exceptions::BackgroundJob::InconsistentStatus->throw(
202
        current_status  => $self->status,
203
        expected_status => 'started'
204
    ) unless $self->status eq 'started';
205
206
    # reached the end of the tasks already
207
    Koha::Exceptions::BackgroundJob::StepOutOfBounds->throw()
208
        unless $self->progress < $self->size;
209
210
    return $self->progress( $self->progress + 1 )->store;
211
}
212
213
=head3 finish
214
215
    $self->finish;
216
217
Makes the job record as finished.
218
219
=cut
220
221
sub finish {
222
    my ( $self, $data ) = @_;
223
224
    return $self->set(
225
        {
226
            ended_on => \'NOW()',
227
            data     => encode_json($data),
228
            status   => 'finished',
229
        }
230
    )->store;
231
}
232
164
=head3 job_type
233
=head3 job_type
165
234
166
Return the job type of the job. Must be a string.
235
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 / +59 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 85-87 subtest 'enqueue() tests' => sub { Link Here
85
88
86
    $schema->storage->txn_rollback;
89
    $schema->storage->txn_rollback;
87
};
90
};
88
- 
91
92
subtest 'start(), step() and finish() tests' => sub {
93
94
    plan tests => 16;
95
96
    $schema->storage->txn_begin;
97
98
    # FIXME: This all feels we need to do it better...
99
    my $job_id = Koha::BackgroundJob::BatchUpdateItem->new->enqueue( { record_ids => [ 1, 2 ] } );
100
    my $job    = Koha::BackgroundJobs->find($job_id)->_derived_class;
101
102
    is( $job->started_on, undef, 'started_on not set yet' );
103
    is( $job->size, 2, 'Two steps' );
104
105
    $job->start;
106
107
    isnt( $job->started_on, undef, 'started_on set' );
108
    is( $job->status, 'started' );
109
    is( $job->progress, 0, 'No progress yet' );
110
111
    $job->step;
112
    is( $job->progress, 1, 'First step' );
113
    $job->step;
114
    is( $job->progress, 2, 'Second step' );
115
    throws_ok
116
        { $job->step; }
117
        'Koha::Exceptions::BackgroundJob::StepOutOfBounds',
118
        'Tried to make a forbidden extra step';
119
120
    is( $job->progress, 2, 'progress remains unchanged' );
121
122
    my $data = { some => 'data' };
123
124
    $job->finish( $data );
125
126
    is( $job->status, 'finished' );
127
    isnt( $job->ended_on, undef, 'ended_on set' );
128
    is_deeply( decode_json( $job->data ), $data );
129
130
    throws_ok
131
        { $job->start; }
132
        'Koha::Exceptions::BackgroundJob::InconsistentStatus',
133
        'Exception thrown trying to start a finished job';
134
135
    is( $@->expected_status, 'new' );
136
137
    throws_ok
138
        { $job->step; }
139
        'Koha::Exceptions::BackgroundJob::InconsistentStatus',
140
        'Exception thrown trying to start a finished job';
141
142
    is( $@->expected_status, 'started' );
143
144
    $schema->storage->txn_rollback;
145
};

Return to bug 30360