Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Open311 updates from being sent out of order #5298

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions perllib/Open311/PostServiceRequestUpdates.pm
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,30 @@ sub process_update {
return;
}

# Comments are ordered randomly.
# Some cobrands/APIs do not handle ordering by age their end (e.g.
# Northumberland + Alloy) so we skip comment for now if an older unsent
# one exists for the problem. Otherwise an older update may overwrite a
# newer one in Alloy etc.
my $formatter = FixMyStreet::DB->schema->storage->datetime_parser;
my $unsent_comment_for_problem
= $problem->comments->search(
{
state => 'confirmed',
send_state => 'unprocessed',
confirmed => { '<' =>
$formatter->format_datetime( $comment->confirmed ) },
id => { '!=' => $comment->id },
},
{ rows => 1 },
)->single;

if ($unsent_comment_for_problem) {
$self->log( $comment,
'Skipping for now because of older unprocessed update' );
return;
}

# Some cobrands (e.g. Buckinghamshire) don't want to receive updates
# from anyone except the original problem reporter.
if (my $skip = $cobrand->call_hook(should_skip_sending_update => $comment)) {
Expand Down
58 changes: 58 additions & 0 deletions t/script/send-daemon.t
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,62 @@ subtest 'Normal update sending works' => sub {
is $c->external_id, 456, 'Correct external ID';
};

subtest 'Multiple updates on same problem should send in order of confirmation' => sub {
my $mock = Test::MockModule->new('Open311');
$mock->mock('post_service_request_update', sub { 456 });

my ($p3) = $mech->create_problems_for_body(
1,
$body->id,
'Title',
{ category => 'Graffiti',
whensent => '\NOW()',
send_state => 'sent',
external_id => 999,
send_method_used => 'Open311',
}
);

my $c1_p3
= $mech->create_comment_for_problem( $p3, $p3->user, $p3->user->name,
'An update 1', 'f', 'confirmed', 'confirmed',
{ confirmed => '2024-12-11 15:30:00' } );
my $c2_p3
= $mech->create_comment_for_problem( $p3, $p3->user, $p3->user->name,
'An update 2', 'f', 'confirmed', 'confirmed',
{ confirmed => '2024-12-11 15:31:00' } );
my $c3_p3
= $mech->create_comment_for_problem( $p3, $p3->user, $p3->user->name,
'An update 3', 'f', 'confirmed', 'confirmed',
{ confirmed => '2024-12-11 15:32:00' } );

my $countdown = 20; # Ran test 100 times and no failure, so seems a solid number
my %pending = map { $_->id => $_ } ( $c1_p3, $c2_p3, $c3_p3 );
my @sent;
while ( $countdown && _check_updates( \%pending, \@sent ) ) {
FixMyStreet::Script::SendDaemon::look_for_update($opts);
$countdown--;
}

is_deeply \@sent, [ $c1_p3->id, $c2_p3->id, $c3_p3->id ],
'comments sent in order';
};

sub _check_updates {
my ( $pending, $sent ) = @_;

my $unsent = 0;
for ( values %$pending ) {
$_->discard_changes;
if ( $_->send_state eq 'unprocessed' ) {
$unsent++;
} elsif ( $_->send_state eq 'sent' ) {
delete $pending->{ $_->id };
push @$sent, $_->id;
}
}

return $unsent;
}

done_testing;
Loading