diff options
-rwxr-xr-x | bin/send-reports | 12 | ||||
-rw-r--r-- | perllib/FixMyStreet/Queue/Item/Report.pm | 36 | ||||
-rw-r--r-- | perllib/FixMyStreet/Script/Reports.pm | 54 | ||||
-rw-r--r-- | t/cobrand/zurich.t | 3 | ||||
-rw-r--r-- | t/cobrand/zurich_attachments.txt | 2 |
5 files changed, 54 insertions, 53 deletions
diff --git a/bin/send-reports b/bin/send-reports index 81be691e7..9651c271c 100755 --- a/bin/send-reports +++ b/bin/send-reports @@ -17,6 +17,7 @@ BEGIN { require "$d/../setenv.pl"; } +use Getopt::Long::Descriptive; use CronFns; use FixMyStreet; @@ -25,4 +26,13 @@ use FixMyStreet::Script::Reports; my $site = CronFns::site(FixMyStreet->config('BASE_URL')); CronFns::language($site); -FixMyStreet::Script::Reports::send(); +my ($opts, $usage) = describe_options( + '%c %o', + ['verbose|v', 'more verbose output'], + ['nomail', 'do not send any email, print instead'], + ['debug', 'always try and send reports (no back-off skipping)'], + ['help|h', "print usage message and exit" ], +); +$usage->die if $opts->help; + +FixMyStreet::Script::Reports::send($opts->verbose, $opts->nomail, $opts->debug); diff --git a/perllib/FixMyStreet/Queue/Item/Report.pm b/perllib/FixMyStreet/Queue/Item/Report.pm index 1ef640fc4..afa672c7f 100644 --- a/perllib/FixMyStreet/Queue/Item/Report.pm +++ b/perllib/FixMyStreet/Queue/Item/Report.pm @@ -41,19 +41,18 @@ has h => ( is => 'rwp' ); has reporters => ( is => 'rwp' ); # Run parameters +has verbose => ( is => 'ro'); has nomail => ( is => 'ro' ); -has debug_mode => ( is => 'ro' ); +has debug => ( is => 'ro' ); sub process { my $self = shift; FixMyStreet::DB->schema->cobrand($self->cobrand); - if ($self->debug_mode) { - $self->manager->debug_unsent_count++; - print "\n"; + if ($self->verbose) { my $row = $self->report; - $self->debug_print("state=" . $row->state . ", bodies_str=" . $row->bodies_str . ($row->cobrand? ", cobrand=" . $row->cobrand : ""), $row->id); + $self->log("state=" . $row->state . ", bodies_str=" . $row->bodies_str . ($row->cobrand? ", cobrand=" . $row->cobrand : "")); } # Cobranded and non-cobranded messages can share a database. In this case, the conf file @@ -61,7 +60,7 @@ sub process { # more than once if there are multiple vhosts running off the same database. The email_host # call checks if this is the host that sends mail for this cobrand. if (! $self->cobrand->email_host()) { - $self->debug_print("skipping because this host does not send reports for cobrand " . $self->cobrand->moniker, $self->report->id); + $self->log("skipping because this host does not send reports for cobrand " . $self->cobrand->moniker); return; } @@ -79,11 +78,11 @@ sub _check_abuse { my $self = shift; if ( $self->report->is_from_abuser) { $self->report->update( { state => 'hidden' } ); - $self->debug_print("hiding because its sender is flagged as an abuser", $self->report->id); + $self->log("hiding because its sender is flagged as an abuser"); return; } elsif ( $self->report->title =~ /app store test/i ) { $self->report->update( { state => 'hidden' } ); - $self->debug_print("hiding because it is an app store test message", $self->report->id); + $self->log("hiding because it is an app store test message"); return; } return 1; @@ -179,16 +178,16 @@ sub _create_reporters { my $sender = "FixMyStreet::SendReport::" . $sender_info->{method}; if ( ! exists $self->senders->{ $sender } ) { - warn sprintf "No such sender [ $sender ] for body %s ( %d )", $body->name, $body->id; + $self->log(sprintf "No such sender [ $sender ] for body %s ( %d )", $body->name, $body->id); next; } $reporters{ $sender } ||= $sender->new(); - if ( $reporters{ $sender }->should_skip( $row, $self->debug_mode ) ) { + if ( $reporters{ $sender }->should_skip($row, $self->debug) ) { $skip = 1; - $self->debug_print("skipped by sender " . $sender_info->{method} . " (might be due to previous failed attempts?)", $row->id); + $self->log("skipped by sender " . $sender_info->{method} . " (might be due to previous failed attempts?)"); } else { - $self->debug_print("OK, adding recipient body " . $body->id . ":" . $body->name . ", " . $sender_info->{method}, $row->id); + $self->log("OK, adding recipient body " . $body->id . ":" . $body->name . ", " . $sender_info->{method}); push @dear, $body->name; $reporters{ $sender }->add_body( $body, $sender_info->{config} ); } @@ -228,7 +227,7 @@ sub _send { my $result = -1; for my $sender ( keys %{$self->reporters} ) { - $self->debug_print("sending using " . $sender, $self->report->id); + $self->log("sending using " . $sender); $sender = $self->reporters->{$sender}; my $res = $sender->send( $self->report, $self->h ); $result *= $res; @@ -261,7 +260,7 @@ sub _post_send { $self->h->{sent_confirm_id_ref} = $self->report->$send_confirmation_email; $self->_send_report_sent_email; } - $self->debug_print("send successful: OK", $self->report->id); + $self->log("send successful: OK"); } else { my @errors; for my $sender ( keys %{$self->reporters} ) { @@ -270,7 +269,7 @@ sub _post_send { } } $self->report->update_send_failed( join( '|', @errors ) ); - $self->debug_print("send FAILED: " . join( '|', @errors ), $self->report->id); + $self->log("send FAILED: " . join( '|', @errors )); } } @@ -297,9 +296,10 @@ sub _send_report_sent_email { ); } -sub debug_print { - my $self = shift; - $self->manager->debug_print(@_); +sub log { + my ($self, $msg) = @_; + return unless $self->verbose; + STDERR->print("[fmsd] [" . $self->report->id . "] $msg\n"); } 1; diff --git a/perllib/FixMyStreet/Script/Reports.pm b/perllib/FixMyStreet/Script/Reports.pm index 2b558fdc0..6e3d9072d 100644 --- a/perllib/FixMyStreet/Script/Reports.pm +++ b/perllib/FixMyStreet/Script/Reports.pm @@ -7,69 +7,63 @@ use FixMyStreet::DB; use FixMyStreet::Queue::Item::Report; has verbose => ( is => 'ro' ); -has debug_mode => ( is => 'ro' ); -has debug_unsent_count => ( is => 'rw', default => 0 ); has unconfirmed_data => ( is => 'ro', default => sub { {} } ); has test_data => ( is => 'ro', default => sub { {} } ); # Static method, used by send-reports cron script and tests. # Creates a manager object from provided data and processes it. -sub send(;$) { - my ($site_override) = @_; - my $rs = FixMyStreet::DB->resultset('Problem'); - - # Set up site, language etc. - my ($verbose, $nomail, $debug_mode) = CronFns::options(); +sub send { + my ($verbose, $nomail, $debug) = @_; my $manager = __PACKAGE__->new( verbose => $verbose, - debug_mode => $debug_mode, ); - my $base_url = FixMyStreet->config('BASE_URL'); - my $site = $site_override || CronFns::site($base_url); - + my $site = CronFns::site(FixMyStreet->config('BASE_URL')); my $states = [ FixMyStreet::DB::Result::Problem::open_states() ]; $states = [ 'submitted', 'confirmed', 'in progress', 'feedback pending', 'external', 'wish' ] if $site eq 'zurich'; - my $unsent = $rs->search( { + + my $unsent = FixMyStreet::DB->resultset('Problem')->search( { state => $states, whensent => undef, bodies_str => { '!=', undef }, } ); - $manager->debug_print("starting to loop through unsent problem reports..."); + $manager->log("starting to loop through unsent problem reports..."); + my $unsent_count = 0; while (my $row = $unsent->next) { + $unsent_count++; my $item = FixMyStreet::Queue::Item::Report->new( report => $row, manager => $manager, + verbose => $verbose, nomail => $nomail, - debug_mode => $debug_mode, + debug => $debug, ); $item->process; } - $manager->end_debug_line; + $manager->end_line($unsent_count); $manager->end_summary_unconfirmed; return $manager->test_data; } -sub end_debug_line { - my $self = shift; - return unless $self->debug_mode; +sub end_line { + my ($self, $unsent_count) = @_; + return unless $self->verbose; - print "\n"; - if ($self->debug_unsent_count) { - $self->debug_print("processed all unsent reports (total: " . $self->debug_unsent_count . ")"); + if ($unsent_count) { + $self->log("processed all unsent reports (total: $unsent_count)"); } else { - $self->debug_print("no unsent reports were found (must have whensent=null and suitable bodies_str & state) -- nothing to send"); + $self->log("no unsent reports were found (must have whensent=null and suitable bodies_str & state) -- nothing to send"); } } sub end_summary_unconfirmed { my $self = shift; - return unless $self->verbose || $self->debug_mode; + return unless $self->verbose; my %unconfirmed_data = %{$self->unconfirmed_data}; print "Council email addresses that need checking:\n" if keys %unconfirmed_data; @@ -102,14 +96,10 @@ sub end_summary_failures { } } -sub debug_print { - my $self = shift; - return unless $self->debug_mode; - - my $msg = shift; - my $id = shift || ''; - $id = "report $id: " if $id; - print "[] $id$msg\n"; +sub log { + my ($self, $msg) = @_; + return unless $self->verbose; + STDERR->print("[fmsd] $msg\n"); } 1; diff --git a/t/cobrand/zurich.t b/t/cobrand/zurich.t index 0a144de23..8c5acddca 100644 --- a/t/cobrand/zurich.t +++ b/t/cobrand/zurich.t @@ -31,7 +31,7 @@ my $sample_file = path(__FILE__)->parent->parent->child("app/controller/sample.j ok $sample_file->exists, "sample file $sample_file exists"; sub send_reports_for_zurich { - FixMyStreet::Script::Reports::send('zurich'); + FixMyStreet::Script::Reports::send(); } sub reset_report_state { my ($report, $created) = @_; @@ -50,6 +50,7 @@ sub reset_report_state { my $UPLOAD_DIR = File::Temp->newdir(); FixMyStreet::override_config { STAGING_FLAGS => { send_reports => 1 }, + BASE_URL => 'https://www.zurich', ALLOWED_COBRANDS => 'zurich', MAPIT_URL => 'http://mapit.zurich/', MAPIT_TYPES => [ 'O08' ], diff --git a/t/cobrand/zurich_attachments.txt b/t/cobrand/zurich_attachments.txt index 26b7eb8ca..e9b717618 100644 --- a/t/cobrand/zurich_attachments.txt +++ b/t/cobrand/zurich_attachments.txt @@ -12,7 +12,7 @@ Content-Transfer-Encoding: quoted-printable Gr=C3=BCezi External Body,
-=C3=96ffentliche URL: http://www.example.org/report/REPORT_ID
+=C3=96ffentliche URL: https://www.zurich/report/REPORT_ID
Bei Fragen zu "Z=C3=BCri wie neu" wenden Sie sich bitte an =
gis-zentrum@zuerich.ch.=
|