diff options
-rwxr-xr-x | bin/fixmystreet.com/bromley-echo | 33 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Bromley.pm | 29 |
2 files changed, 60 insertions, 2 deletions
diff --git a/bin/fixmystreet.com/bromley-echo b/bin/fixmystreet.com/bromley-echo new file mode 100755 index 000000000..01fdec110 --- /dev/null +++ b/bin/fixmystreet.com/bromley-echo @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +# bromley-echo +# Call the Echo API in parallel + +use v5.14; +use warnings; + +BEGIN { + use File::Basename qw(dirname); + use File::Spec; + my $d = dirname(File::Spec->rel2abs($0)); + require "$d/../../setenv.pl"; +} + +use Getopt::Long::Descriptive; +use JSON::MaybeXS; +use Storable; +use FixMyStreet::Cobrand::Bromley; + +my ($opts, $usage) = describe_options( + '%c %o', + ['out=s', 'where to output CSV data'], + ['calls=s', 'JSON of what API calls to make'], + ['help|h', "print usage message and exit" ], +); +$usage->die if $opts->help; + +my $cobrand = FixMyStreet::Cobrand::Bromley->new; + +my $calls = decode_json($opts->calls); +$calls = $cobrand->_parallel_api_calls(@$calls); +Storable::store($calls, $opts->out); diff --git a/perllib/FixMyStreet/Cobrand/Bromley.pm b/perllib/FixMyStreet/Cobrand/Bromley.pm index 80a9e0be1..18a730d3e 100644 --- a/perllib/FixMyStreet/Cobrand/Bromley.pm +++ b/perllib/FixMyStreet/Cobrand/Bromley.pm @@ -6,9 +6,12 @@ use warnings; use utf8; use DateTime::Format::W3CDTF; use DateTime::Format::Flexible; +use File::Temp; use Integrations::Echo; +use JSON::MaybeXS; use Parallel::ForkManager; use Sort::Key::Natural qw(natkeysort_inplace); +use Storable; use Try::Tiny; use FixMyStreet::DateRange; use FixMyStreet::WorkingDays; @@ -447,7 +450,7 @@ sub look_up_property { $self->{c}->detach('/page_error_403_access_denied', []) if $count > $cfg->{max_per_day}; } - my $calls = $self->_parallel_api_calls( + my $calls = $self->call_api( GetPointAddress => [ $id ], GetServiceUnitsForObject => [ $id ], GetEventsForObject => [ 'PointAddress', $id ], @@ -565,7 +568,7 @@ sub bin_services_for_address { } push @to_fetch, GetTasks => \@task_refs if @task_refs; - my $calls = $self->_parallel_api_calls(@to_fetch); + my $calls = $self->call_api(@to_fetch); my @out; my %task_ref_to_row; @@ -946,6 +949,28 @@ sub admin_templates_external_status_code_hook { return "$res_code,$task_type,$task_state"; } +sub call_api { + my $self = shift; + + my $tmp = File::Temp->new; + my @cmd = ( + FixMyStreet->path_to('bin/fixmystreet.com/bromley-echo'), + '--out', $tmp, + '--calls', encode_json(\@_), + ); + + # We cannot fork directly under mod_fcgid, so + # call an external script that calls back in. + my $data; + if (FixMyStreet->test_mode) { + $data = $self->_parallel_api_calls(@_); + } else { + system(@cmd); + $data = Storable::fd_retrieve($tmp); + } + return $data; +} + sub _parallel_api_calls { my $self = shift; my $echo = $self->feature('echo'); |