diff options
-rwxr-xr-x | lib/Travelynx.pm | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 3ef7011..2bc151d 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -1485,15 +1485,33 @@ sub startup { ); my $stats = $self->compute_journey_stats(@journeys); - $self->pg->db->insert( - 'journey_stats', + eval { + $self->pg->db->insert( + 'journey_stats', + { + user_id => $uid, + year => $year, + month => $month, + data => JSON->new->encode($stats), + } + ); + }; + if ( my $err = $@ ) { + if ( $err =~ m{duplicate key value violates unique constraint} ) { - user_id => $uid, - year => $year, - month => $month, - data => JSON->new->encode($stats), + # When a user opens the same history page several times in + # short succession, there is a race condition where several + # Mojolicious workers execute this helper, notice that there is + # no up-to-date history, compute it, and insert it using the + # statement above. This will lead to a uniqueness violation + # in each successive insert. However, this is harmless, and + # thus ignored. } - ); + else { + # Otherwise we probably have a problem. + die($@); + } + } return $stats; } |