Skip to content

fix: Actually start the ServiceDateRollover worker#3248

Merged
joshlarson merged 3 commits into
mainfrom
jdl/fix/actually-run-the-service-date-rollover
Jun 11, 2026
Merged

fix: Actually start the ServiceDateRollover worker#3248
joshlarson merged 3 commits into
mainfrom
jdl/fix/actually-run-the-service-date-rollover

Conversation

@joshlarson

Copy link
Copy Markdown
Contributor

Scope

No ticket - just realized that there actually was a way to test this.... and then realized that it wasn't working!

Implementation

Two things:

  1. Add Dotcom.ServiceDateRollover to the supervision tree.
  2. Make sure that we don't try to schedule the "next rollover" for a time in the past.

Per point number 2, one of the times I was testing this out, I got 👇 error. Might as well avoid that!

2026-06-10 22:03:00.009 [error] GenServer Dotcom.ServiceDateRollover terminating
** (ArgumentError) errors were found at the given arguments:

  * 1st argument: out of range

    (erts 16.3) :erlang.send_after(-10, #PID<0.678.0>, :dispatch_change)
    (dotcom 0.0.1) lib/dotcom/service_date_rollover.ex:42: Dotcom.ServiceDateRollover.handle_continue/2
    (stdlib 7.3) gen_server.erl:2424: :gen_server.try_handle_continue/3
    (stdlib 7.3) gen_server.erl:2291: :gen_server.loop/4
    (stdlib 7.3) proc_lib.erl:333: :proc_lib.init_p_do_apply/3
Last message: {:continue, :schedule_next_run}
State: %{}

Screenshots

Screen.Recording.2026-06-10.at.9.59.51.PM.mov

I was able to make ServiceDateTime pretend that the service-rollover-time was 9:59PM, which meant that today's bus schedules didn't show up until after that time (since before the service-rollover-time, it pulls yesterday's schedules instead, which are all in the past). So before service-rollover-time, bus schedules will be absent, and the last trip will be labeled Last; after service-rollover-time, today's schedules will populate. ☝️ video shows the schedules spontaneously populating (at my customized service-rollover-time of 9:59).

How to test

It actually is possible to mangle ServiceDateTime's service_date/0 and end_of_service_day/0 functions to set your service day to whenever you want (I chose one minute in the future). Most of that module assumes that the service rollover time will be right on the hour, so there's some additional mangling you might need to do if you want to be able to test it more than once per hour.

I rewrote the functions like so 👇

@service_rollover_time ~T[22:15:00] # ...or whatever time you choose

# Rewrote this entirely
def service_date(date_time \\ @date_time_module.now()) do
  calendar_date = DateTime.to_date(date_time)

  before_rollover_time? =
    date_time
    |> DateTime.to_time()
    |> Time.before?(@service_rollover_time)

  if before_rollover_time? do
    calendar_date |> Date.shift(day: -1)
  else
    calendar_date
  end
end

# ...

def end_of_service_day(date_time \\ @date_time_module.now())

def end_of_service_day(%Date{} = date) do
  date
  |> Timex.to_datetime(@timezone)
  |> coerce_ambiguous_date_time()
  |> Timex.shift(
    days: 1,
    hours: @service_rollover_time.hour,
    # Add this if you want a service-rollover-time that isn't right on the hour
    minutes: @service_rollover_time.minute,
    microseconds: -1
  )
  |> coerce_ambiguous_date_time()

  # Commented this because it only works if the service-rollover-time is right on the hour
  # |> Map.put(:hour, @service_rollover_time.hour - 1)
end

def end_of_service_day(%DateTime{} = date_time) do
  date_time
  |> service_date()
  |> end_of_service_day()
end

Notes

I have now achieved semantic satiation with the phrase "service rollover time". I hope you're happy.

@joshlarson joshlarson requested a review from a team as a code owner June 11, 2026 02:33
@joshlarson joshlarson requested a review from jlucytan June 11, 2026 02:33
@joshlarson joshlarson enabled auto-merge (squash) June 11, 2026 10:04
@joshlarson joshlarson merged commit 064df30 into main Jun 11, 2026
17 checks passed
@joshlarson joshlarson deleted the jdl/fix/actually-run-the-service-date-rollover branch June 11, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants