Commit 90dd96e0 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

Handle race condition when several workers are updating the same history entry

parent 7c7b5e9f
Loading
Loading
Loading
Loading
+25 −7
Original line number Diff line number Diff line
@@ -1485,6 +1485,7 @@ sub startup {
			);
			my $stats = $self->compute_journey_stats(@journeys);

			eval {
				$self->pg->db->insert(
					'journey_stats',
					{
@@ -1494,6 +1495,23 @@ sub startup {
						data    => JSON->new->encode($stats),
					}
				);
			};
			if ( my $err = $@ ) {
				if ( $err =~ m{duplicate key value violates unique constraint} )
				{
                 # 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;
		}