From 9e9a7d98a48e48d1077ef7ab37eb3e4b637d7fa7 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Fri, 22 Nov 2024 09:40:05 -0500 Subject: [PATCH 1/3] Keep existing data if we can't decode source. --- lib/screens/last_trip/poller.ex | 23 ++++++++++++------- lib/screens/last_trip/trip_updates/noop.ex | 5 +++- .../last_trip/vehicle_positions/noop.ex | 5 +++- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/screens/last_trip/poller.ex b/lib/screens/last_trip/poller.ex index 55a6abe96..c5f510a44 100644 --- a/lib/screens/last_trip/poller.ex +++ b/lib/screens/last_trip/poller.ex @@ -2,6 +2,7 @@ defmodule Screens.LastTrip.Poller do @moduledoc """ GenServer that polls predictions to calculate the last trip of the day """ + require Logger alias Screens.LastTrip.Cache alias Screens.LastTrip.Parser alias Screens.LastTrip.TripUpdates @@ -32,14 +33,20 @@ defmodule Screens.LastTrip.Poller do :ok = Cache.reset() %{state | next_reset: next_reset()} else - {:ok, - %{ - trip_updates: trip_updates, - vehicle_positions: vehicle_positions - }} = fetch_trip_updates_and_vehicle_positions() - - update_last_trips(trip_updates) - update_recent_departures(trip_updates, vehicle_positions) + case fetch_trip_updates_and_vehicle_positions() do + {:ok, + %{ + trip_updates: trip_updates, + vehicle_positions: vehicle_positions + }} -> + update_last_trips(trip_updates) + update_recent_departures(trip_updates, vehicle_positions) + + {:error, error} -> + Logger.warning( + "[last_trip_poller] failed while polling for new data: #{inspect(error)}" + ) + end state end diff --git a/lib/screens/last_trip/trip_updates/noop.ex b/lib/screens/last_trip/trip_updates/noop.ex index 5475c8ccf..b164276ec 100644 --- a/lib/screens/last_trip/trip_updates/noop.ex +++ b/lib/screens/last_trip/trip_updates/noop.ex @@ -4,6 +4,9 @@ defmodule Screens.LastTrip.TripUpdates.Noop do @impl true def get do - {:ok, %{status_code: 200, body: %{"entity" => []}}} + case Jason.decode("{\"entity\":[]}") do + {:ok, decoded} -> {:ok, %{status_code: 200, body: decoded}} + error -> error + end end end diff --git a/lib/screens/last_trip/vehicle_positions/noop.ex b/lib/screens/last_trip/vehicle_positions/noop.ex index 2393e2815..04e6920fa 100644 --- a/lib/screens/last_trip/vehicle_positions/noop.ex +++ b/lib/screens/last_trip/vehicle_positions/noop.ex @@ -4,6 +4,9 @@ defmodule Screens.LastTrip.VehiclePositions.Noop do @impl true def get do - {:ok, %HTTPoison.Response{status_code: 200, body: %{"entity" => []}}} + case Jason.decode("{\"entity\":[]}") do + {:ok, decoded} -> {:ok, %HTTPoison.Response{status_code: 200, body: decoded}} + error -> error + end end end From 0f80384ced65808afea9891324cfb97ef1f855cb Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Fri, 22 Nov 2024 14:16:32 -0500 Subject: [PATCH 2/3] Revert "Keep existing data if we can't decode source." This reverts commit 9e9a7d98a48e48d1077ef7ab37eb3e4b637d7fa7. --- lib/screens/last_trip/poller.ex | 23 +++++++------------ lib/screens/last_trip/trip_updates/noop.ex | 5 +--- .../last_trip/vehicle_positions/noop.ex | 5 +--- 3 files changed, 10 insertions(+), 23 deletions(-) diff --git a/lib/screens/last_trip/poller.ex b/lib/screens/last_trip/poller.ex index c5f510a44..55a6abe96 100644 --- a/lib/screens/last_trip/poller.ex +++ b/lib/screens/last_trip/poller.ex @@ -2,7 +2,6 @@ defmodule Screens.LastTrip.Poller do @moduledoc """ GenServer that polls predictions to calculate the last trip of the day """ - require Logger alias Screens.LastTrip.Cache alias Screens.LastTrip.Parser alias Screens.LastTrip.TripUpdates @@ -33,20 +32,14 @@ defmodule Screens.LastTrip.Poller do :ok = Cache.reset() %{state | next_reset: next_reset()} else - case fetch_trip_updates_and_vehicle_positions() do - {:ok, - %{ - trip_updates: trip_updates, - vehicle_positions: vehicle_positions - }} -> - update_last_trips(trip_updates) - update_recent_departures(trip_updates, vehicle_positions) - - {:error, error} -> - Logger.warning( - "[last_trip_poller] failed while polling for new data: #{inspect(error)}" - ) - end + {:ok, + %{ + trip_updates: trip_updates, + vehicle_positions: vehicle_positions + }} = fetch_trip_updates_and_vehicle_positions() + + update_last_trips(trip_updates) + update_recent_departures(trip_updates, vehicle_positions) state end diff --git a/lib/screens/last_trip/trip_updates/noop.ex b/lib/screens/last_trip/trip_updates/noop.ex index b164276ec..5475c8ccf 100644 --- a/lib/screens/last_trip/trip_updates/noop.ex +++ b/lib/screens/last_trip/trip_updates/noop.ex @@ -4,9 +4,6 @@ defmodule Screens.LastTrip.TripUpdates.Noop do @impl true def get do - case Jason.decode("{\"entity\":[]}") do - {:ok, decoded} -> {:ok, %{status_code: 200, body: decoded}} - error -> error - end + {:ok, %{status_code: 200, body: %{"entity" => []}}} end end diff --git a/lib/screens/last_trip/vehicle_positions/noop.ex b/lib/screens/last_trip/vehicle_positions/noop.ex index 04e6920fa..2393e2815 100644 --- a/lib/screens/last_trip/vehicle_positions/noop.ex +++ b/lib/screens/last_trip/vehicle_positions/noop.ex @@ -4,9 +4,6 @@ defmodule Screens.LastTrip.VehiclePositions.Noop do @impl true def get do - case Jason.decode("{\"entity\":[]}") do - {:ok, decoded} -> {:ok, %HTTPoison.Response{status_code: 200, body: decoded}} - error -> error - end + {:ok, %HTTPoison.Response{status_code: 200, body: %{"entity" => []}}} end end From 912d7456c92b22185a8e3a00538c466163456603 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Fri, 22 Nov 2024 14:16:57 -0500 Subject: [PATCH 3/3] Retry fetching LastTrip info before crashing. --- lib/screens/last_trip/trip_updates/gtfs.ex | 3 +++ lib/screens/last_trip/vehicle_positions/gtfs.ex | 3 +++ 2 files changed, 6 insertions(+) diff --git a/lib/screens/last_trip/trip_updates/gtfs.ex b/lib/screens/last_trip/trip_updates/gtfs.ex index e1c42effd..806ead8ce 100644 --- a/lib/screens/last_trip/trip_updates/gtfs.ex +++ b/lib/screens/last_trip/trip_updates/gtfs.ex @@ -5,7 +5,10 @@ defmodule Screens.LastTrip.TripUpdates.GTFS do """ @behaviour Screens.LastTrip.TripUpdates + use Retry.Annotation + @impl true + @retry with: Stream.take(constant_backoff(500), 5) def get do trip_updates_url = Application.fetch_env!(:screens, :trip_updates_url) diff --git a/lib/screens/last_trip/vehicle_positions/gtfs.ex b/lib/screens/last_trip/vehicle_positions/gtfs.ex index a109349ec..e1d6db015 100644 --- a/lib/screens/last_trip/vehicle_positions/gtfs.ex +++ b/lib/screens/last_trip/vehicle_positions/gtfs.ex @@ -5,7 +5,10 @@ defmodule Screens.LastTrip.VehiclePositions.GTFS do """ @behaviour Screens.LastTrip.VehiclePositions + use Retry.Annotation + @impl true + @retry with: Stream.take(constant_backoff(500), 5) def get do vehicle_positions_url = Application.fetch_env!(:screens, :vehicle_positions_url)