From ea23d0a0f6bbbc003b9e1a981861eff5270af178 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 25 Feb 2019 21:42:43 -0300 Subject: [PATCH] Bug 22417: Add Koha::BackgroundJob[s] Signed-off-by: Tomas Cohen Arazi Signed-off-by: David Cook Signed-off-by: Marcel de Rooy Signed-off-by: Kyle M Hall --- Koha/BackgroundJob.pm | 89 ++++++++++++++++++++++++++++++++++++++++++ Koha/BackgroundJobs.pm | 14 +++++++ 2 files changed, 103 insertions(+) create mode 100644 Koha/BackgroundJob.pm create mode 100644 Koha/BackgroundJobs.pm diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm new file mode 100644 index 0000000000..6eab98c6b5 --- /dev/null +++ b/Koha/BackgroundJob.pm @@ -0,0 +1,89 @@ +package Koha::BackgroundJob; + +use Modern::Perl; +use JSON qw( encode_json decode_json ); +use Carp qw( croak ); +use C4::Context; +use Koha::DateUtils qw( dt_from_string ); +use Koha::BackgroundJobs; + +use base qw( Koha::Object ); + +sub connect { + my ( $self ); + my $conn = Net::RabbitFoot->new()->load_xml_spec()->connect( + host => 'localhost', # TODO Move this to KOHA_CONF + port => 5672, + user => 'guest', + pass => 'guest', + vhost => '/', + ); + + return $conn; +} + +sub enqueue { + my ( $self, $params ) = @_; + + my $job_type = $params->{job_type}; + my $job_size = $params->{job_size}; + my $job_args = $params->{job_args}; + + my $json_args = encode_json $job_args; + $self->set({ + status => 'new', + type => $job_type, + size => $job_size, + data => $json_args, + enqueued_on => dt_from_string, + borrowernumber => C4::Context->userenv->{id}, # FIXME Handle non GUI calls + })->store; + + my $job_id = $self->id; + $job_args->{job_id} = $job_id; + $json_args = encode_json $job_args, + + my $conn = $self->connect; + my $channel = $conn->open_channel(); + + $channel->declare_queue( + queue => $job_type, + durable => 1, + ); + + $channel->publish( + exchange => '', + routing_key => $job_type, # TODO Must be different? + body => $json_args, + ); + $conn->close; + return $job_id; +} + +sub process { croak "This method must be subclassed" } + +sub messages { + my ( $self ) = @_; + + my @messages; + my $data_dump = decode_json $self->data; + if ( exists $data_dump->{messages} ) { + @messages = @{ $data_dump->{messages} }; + } + + return @messages; +} + +sub report { + my ( $self ) = @_; + + my $data_dump = decode_json $self->data; + return $data_dump->{report}; +} + + +sub _type { + return 'BackgroundJob'; +} + +1; diff --git a/Koha/BackgroundJobs.pm b/Koha/BackgroundJobs.pm new file mode 100644 index 0000000000..6e44990c9e --- /dev/null +++ b/Koha/BackgroundJobs.pm @@ -0,0 +1,14 @@ +package Koha::BackgroundJobs; + +use Modern::Perl; +use base qw(Koha::Objects); + +sub _type { + return 'BackgroundJob'; +} + +sub object_class { + return 'Koha::BackgroundJob'; +} + +1; -- 2.24.1 (Apple Git-126)