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

(-)a/Koha/BackgroundJob.pm (+89 lines)
Line 0 Link Here
1
package Koha::BackgroundJob;
2
3
use Modern::Perl;
4
use JSON qw( encode_json decode_json );
5
use Carp qw( croak );
6
use C4::Context;
7
use Koha::DateUtils qw( dt_from_string );
8
use Koha::BackgroundJobs;
9
10
use base qw( Koha::Object );
11
12
sub connect {
13
    my ( $self );
14
    my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect(
15
        host => 'localhost', # TODO Move this to KOHA_CONF
16
        port => 5672,
17
        user => 'guest',
18
        pass => 'guest',
19
        vhost => '/',
20
    );
21
22
    return $conn;
23
}
24
25
sub enqueue {
26
    my ( $self, $params ) = @_;
27
28
    my $job_type = $params->{job_type};
29
    my $job_size = $params->{job_size};
30
    my $job_args = $params->{job_args};
31
32
    my $json_args = encode_json $job_args;
33
    $self->set({
34
        status => 'new',
35
        type => $job_type,
36
        size => $job_size,
37
        data => $json_args,
38
        enqueued_on => dt_from_string,
39
        borrowernumber => C4::Context->userenv->{id}, # FIXME Handle non GUI calls
40
    })->store;
41
42
    my $job_id = $self->id;
43
    $job_args->{job_id} = $job_id;
44
    $json_args = encode_json $job_args,
45
46
    my $conn = $self->connect;
47
    my $channel = $conn->open_channel();
48
49
    $channel->declare_queue(
50
        queue => $job_type,
51
        durable => 1,
52
    );
53
54
    $channel->publish(
55
        exchange => '',
56
        routing_key => $job_type, # TODO Must be different?
57
        body => $json_args,
58
    );
59
    $conn->close;
60
    return $job_id;
61
}
62
63
sub process { croak "This method must be subclassed" }
64
65
sub messages {
66
    my ( $self ) = @_;
67
68
    my @messages;
69
    my $data_dump = decode_json $self->data;
70
    if ( exists $data_dump->{messages} ) {
71
        @messages = @{ $data_dump->{messages} };
72
    }
73
74
    return @messages;
75
}
76
77
sub report {
78
    my ( $self ) = @_;
79
80
    my $data_dump = decode_json $self->data;
81
    return $data_dump->{report};
82
}
83
84
85
sub _type {
86
    return 'BackgroundJob';
87
}
88
89
1;
(-)a/Koha/BackgroundJobs.pm (-1 / +14 lines)
Line 0 Link Here
0
- 
1
package Koha::BackgroundJobs;
2
3
use Modern::Perl;
4
use base qw(Koha::Objects);
5
6
sub _type {
7
    return 'BackgroundJob';
8
}
9
10
sub object_class {
11
    return 'Koha::BackgroundJob';
12
}
13
14
1;

Return to bug 22417