From f58c979a7df8f6cb6b61ba680d4d09e5b0fb41ba Mon Sep 17 00:00:00 2001 From: AndreiTocu Date: Fri, 2 Oct 2020 14:13:12 +0300 Subject: [PATCH 1/1] Send POST request to remote address with job result --- lib/Gruntmaster/Daemon.pm | 3 +++ lib/Gruntmaster/SendResults.pm | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 lib/Gruntmaster/SendResults.pm diff --git a/lib/Gruntmaster/Daemon.pm b/lib/Gruntmaster/Daemon.pm index 1c820db..ef24364 100644 --- a/lib/Gruntmaster/Daemon.pm +++ b/lib/Gruntmaster/Daemon.pm @@ -8,6 +8,7 @@ our $VERSION = '5999.000_005'; use Gruntmaster::Daemon::Constants qw/ERR/; use Gruntmaster::Daemon::Format qw/prepare_files stopvms/; +use Gruntmaster::SendResults qw/send_results_request/; use File::Slurp qw/read_file/; use File::Temp qw/tempdir/; @@ -110,6 +111,7 @@ sub process{ $meta->{errors} = $errors; get_logger->info('Job result: ' . $meta->{result_text}); + send_results_request($meta->{job_id}, $meta->{result}, $meta->{result_text}); } sub process_job { @@ -138,6 +140,7 @@ sub process_job { format => $pb->{verformat}, content => $pb->{versource}, } if $pb->{verformat}; + $meta->{job_id} = $job->{id}; process $meta; diff --git a/lib/Gruntmaster/SendResults.pm b/lib/Gruntmaster/SendResults.pm new file mode 100644 index 0000000..e01a9a6 --- /dev/null +++ b/lib/Gruntmaster/SendResults.pm @@ -0,0 +1,36 @@ +package Gruntmaster::SendResults; + +use 5.014000; +use strict; +use warnings; +use parent qw/Exporter/; + +use HTTP::Request; +use JSON qw/encode_json decode_json/; +use LWP::UserAgent; + +our $VERSION = '5999.000_005'; +our @EXPORT_OK = qw/send_results_request/; + +sub send_results_request { + my ($job_id, $result, $result_text) = @_; + + my $url = $ENV{REMOTE_ADDRESS}; + my $header = [ + 'Content-Type' => 'application/json' + ]; + my $data = { + gm_id => $job_id, + result => $result, + result_text => $result_text + }; + + my $encoded_data = encode_json($data); + my $request = HTTP::Request->new(POST => $url, $header, $encoded_data); + my $ua = LWP::UserAgent->new(); + my $response = $ua->request($request); + + return $response; +} + +1; -- 2.39.2