Skip to content

fix(notifications): improve scheduling logic to avoid sending early

Ben Whitmore requested to merge scheduled-notifications-1238 into main

See #1238 (closed)

I haven't investigated enough to get to the bottom of why notifications aren't being sent (or if they're being sent late, or early). But the logic looked unsafe to me, and at risk of sending notifications a day early. Here's my reasoning:

  1. A task's due date is currently always set at 1 ms before midnight, i.e. end of day. So if a task is due on Tuesday, then a notification 0 days before/after the due date should go out first thing Wednesday (all in the team's timezone).
  2. Our scheduler was set to fire every midnight (team timezone) and then look for notifications due within one day before or after: the notification due date was determined as the task due date plus or minus however many days. So let's say the scheduler fires bang on midnight, start of Tuesday. Even with no delays before the notification code is run, the notification that should be sent Wednesday is already calculated as being just under a day ahead, and so it is sent a day early.

I've changed this so it insists the notification must fall within a single day period, and it explicitly pushes the notification ahead 1 millisecond to the Wednesday. I've also refactored for ease of reading. I don't know if this fixes anything and I haven't properly tested it. I'm pushing this to review now because I expect I may be unwell for a few days and unable to contribute further.

Merge request reports