diff options
author | Dave Whiteland <dave@mysociety.org> | 2012-05-29 15:57:41 +0100 |
---|---|---|
committer | Dave Whiteland <dave@mysociety.org> | 2012-05-29 15:57:41 +0100 |
commit | 67da8efc720d2d0bd22bd9fe8655b7e983b35bb4 (patch) | |
tree | 38b8570647124df06c637d4b923f6010211ef328 /bin/send-comments | |
parent | 40b3a51d33caefa8f5fb97ce9be18ef936c7e260 (diff) | |
parent | 131ff6e9bf3626d6a8fff6ae54669d250148a63a (diff) |
Merge remote branch 'origin/master' into fmb-read-only
Conflicts:
bin/send-reports
perllib/FixMyStreet/Cobrand/Default.pm
perllib/FixMyStreet/Cobrand/FixMyStreet.pm
templates/web/fixmystreet/alert/index.html
templates/web/fixmystreet/around/display_location.html
web/cobrands/fixmystreet/_layout.scss
web/js/map-OpenLayers.js
Diffstat (limited to 'bin/send-comments')
-rwxr-xr-x | bin/send-comments | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/bin/send-comments b/bin/send-comments new file mode 100755 index 000000000..0fb3a75b9 --- /dev/null +++ b/bin/send-comments @@ -0,0 +1,99 @@ +#!/usr/bin/env perl + +# send-reports: +# Send new problem reports to councils +# +# Copyright (c) 2011 UK Citizens Online Democracy. All rights reserved. +# Email: matthew@mysociety.org. WWW: http://www.mysociety.org + +use strict; +use warnings; +require 5.8.0; + +use Digest::MD5; +use Encode; +use Error qw(:try); +use CronFns; + +use FixMyStreet::App; + +use Utils; +use mySociety::Config; +use mySociety::EmailUtil; + +use Open311; + +# maximum number of webservice attempts to send before not trying any more (XXX may be better in config?) +use constant SEND_FAIL_RETRIES_CUTOFF => 3; + +# send_method config values found in by-area config data, for selecting to appropriate method +use constant SEND_METHOD_EMAIL => 'email'; +use constant SEND_METHOD_OPEN311 => 'Open311'; + +# Set up site, language etc. +my ($verbose, $nomail) = CronFns::options(); +my $base_url = mySociety::Config::get('BASE_URL'); +my $site = CronFns::site($base_url); + +my $councils = FixMyStreet::App->model('DB::Open311Conf')->search( { + send_method => SEND_METHOD_OPEN311, + send_comments => 1, +} ); + +while ( my $council = $councils->next ) { + my $comments = FixMyStreet::App->model('DB::Comment')->search( { + 'me.whensent' => undef, + 'me.external_id' => undef, + 'me.state' => 'confirmed', + 'me.confirmed' => { '!=' => undef }, + 'problem.whensent' => { '!=' => undef }, + 'problem.external_id' => { '!=' => undef }, + 'problem.council' => { -like => '%' . $council->area_id .'%' }, + }, + { + join => 'problem', + } + ); + + my $o = Open311->new( + endpoint => $council->endpoint, + jurisdiction => $council->jurisdiction, + api_key => $council->api_key, + ); + + while ( my $comment = $comments->next ) { + my $cobrand = FixMyStreet::Cobrand->get_class_for_moniker($comment->cobrand)->new(); + + if ( $comment->send_fail_count ) { + next if bromley_retry_timeout( $comment ); + } + + my $id = $o->post_service_request_update( $comment ); + + if ( $id ) { + $comment->update( { + external_id => $id, + whensent => \'ms_current_timestamp()', + } ); + } else { + $comment->update( { + send_fail_count => $comment->send_fail_count + 1, + send_fail_timestamp => \'ms_current_timestamp()', + send_fail_reason => 'Failed to post over Open311', + } ); + } + } +} + +sub bromley_retry_timeout { + my $row = shift; + + my $tz = DateTime::TimeZone->new( name => 'local' ); + my $now = DateTime->now( time_zone => $tz ); + my $diff = $now - $row->send_fail_timestamp; + if ( $diff->in_units( 'minutes' ) < 30 ) { + return 1; + } + + return 0; +} |