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

(-)a/Koha/BackgroundJob/Test.pm (+44 lines)
Line 0 Link Here
1
package Koha::BackgroundJob::Test;
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 base 'Koha::BackgroundJob';
21
22
sub job_type {
23
    return 'test';
24
}
25
26
sub process {
27
    my ( $self ) = @_;
28
29
    warn "Starting " . $self->id;
30
    $self->start;
31
    sleep 1;
32
    $self->finish;
33
}
34
35
sub enqueue {
36
    my ( $self ) = @_;
37
38
    $self->SUPER::enqueue({
39
        job_size => 1,
40
        job_args => {},
41
    });
42
}
43
44
1;
(-)a/misc/background_jobs_worker.pl (-26 / +13 lines)
Lines 78-84 if ( $conn ) { Link Here
78
    # FIXME cf note in Koha::BackgroundJob about $namespace
78
    # FIXME cf note in Koha::BackgroundJob about $namespace
79
    my $namespace = C4::Context->config('memcached_namespace');
79
    my $namespace = C4::Context->config('memcached_namespace');
80
    for my $queue (@queues) {
80
    for my $queue (@queues) {
81
        $conn->subscribe({ destination => sprintf("/queue/%s-%s", $namespace, $queue), ack => 'client' });
81
        $conn->subscribe({
82
            destination => sprintf("/queue/%s-%s", $namespace, $queue),
83
            ack => 'client',
84
            'activemq.prefetchSize' => 1,
85
        });
82
    }
86
    }
83
}
87
}
84
while (1) {
88
while (1) {
Lines 90-127 while (1) { Link Here
90
        }
94
        }
91
95
92
        my $body = $frame->body;
96
        my $body = $frame->body;
97
        warn "GOT $body";
93
        my $args = decode_json($body); # TODO Should this be from_json? Check utf8 flag.
98
        my $args = decode_json($body); # TODO Should this be from_json? Check utf8 flag.
94
99
100
        $conn->ack( { frame => $frame } ); # FIXME depending on success?
101
95
        # FIXME This means we need to have create the DB entry before
102
        # FIXME This means we need to have create the DB entry before
96
        # It could work in a first step, but then we will want to handle job that will be created from the message received
103
        # It could work in a first step, but then we will want to handle job that will be created from the message received
97
        my $job = Koha::BackgroundJobs->find($args->{job_id});
104
        my $job = Koha::BackgroundJobs->find($args->{job_id});
98
105
99
        process_job( $job, $args );
106
        warn "Starting " . $args->{job_id};
100
        $conn->ack( { frame => $frame } ); # FIXME depending on success?
107
        $job->start;
108
        sleep 1;
109
        warn "Finishing" . $args->{job_id};
110
        $job->finish;
101
111
102
    } else {
103
        my $jobs = Koha::BackgroundJobs->search({ status => 'new', queue => \@queues });
104
        while ( my $job = $jobs->next ) {
105
            my $args = $job->json->decode($job->data);
106
            process_job( $job, { job_id => $job->id, %$args } );
107
        }
108
        sleep 10;
109
    }
112
    }
110
}
113
}
111
$conn->disconnect;
114
$conn->disconnect;
112
113
sub process_job {
114
    my ( $job, $args ) = @_;
115
116
    my $pid;
117
    if ( $pid = fork ) {
118
        wait;
119
        return;
120
    }
121
122
    die "fork failed!" unless defined $pid;
123
124
    $job->process( $args );
125
126
    exit;
127
}
(-)a/test_bg.pl (-1 / +30 lines)
Line 0 Link Here
0
- 
1
use Modern::Perl;
2
use Koha::BackgroundJob::Test;
3
use Koha::BackgroundJobs;
4
my @job_ids;
5
for my $i ( 1 .. 100 ) {
6
    my $job = Koha::BackgroundJob::Test->new;
7
    $job->enqueue;
8
    push @job_ids, $job->id;
9
}
10
11
while ( 1 ) {
12
    my $jobs = Koha::BackgroundJobs->search({id => \@job_ids}, { order_by => 'id' });
13
    my $last_status;
14
    while ( my $j = $jobs->next ) {
15
        my $s = $j->status;
16
        my $ss = '?';
17
        if ( $s eq 'new' ) {
18
            $ss = "\e[1;31m;";
19
            print sprintf("\e[1;31m;%s\e[0m", $j->id);
20
        } elsif ( $s eq 'started' ) {
21
            print sprintf("\e[1;32m;%s\e[0m", $j->id);
22
        } elsif ( $s eq 'finished' ) {
23
            print sprintf("\e[1;33m;%s\e[0m", $j->id);
24
        }
25
        $last_status = $s;
26
    }
27
    print "\n";
28
    sleep 1;
29
    last if $last_status eq 'finished';
30
}

Return to bug 32481