Skip to content

Commit ce65712

Browse files
committed
Better sqlite timestamp default and pruning query
The `CURRENT_TIMESTAMP` type lacks a trailing `z`, which causes it to be compared incorrectly against UTC datetimes. Any jobs inserted without a `scheduled_at`, where the default is used, could be returned in queries that compare against a `DateTime`. This prevents the issue in the future two ways: 1. Switch the default `inserted_at/scheduled_at` timestamp to a format that can be queried properly. 2. Change the pruning check to use `completed_at` rather than `scheduled_at` for existing databases. This is a more accurate query that was avoided before because it didn't match the `Basic` engine. Closes #1391
1 parent 88f6557 commit ce65712

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

lib/oban/engines/lite.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ defmodule Oban.Engines.Lite do
146146
select_query =
147147
queryable
148148
|> select([j], map(j, [:id, :queue, :state]))
149-
|> where([j], j.state == "completed" and j.scheduled_at < ^time)
149+
|> where([j], j.state == "completed" and j.completed_at < ^time)
150150
|> or_where([j], j.state == "cancelled" and j.cancelled_at < ^time)
151151
|> or_where([j], j.state == "discarded" and j.discarded_at < ^time)
152152
|> limit(^limit)

lib/oban/migrations/sqlite.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ defmodule Oban.Migrations.SQLite do
2020
add :max_attempts, :integer, null: false, default: 20
2121
add :priority, :integer, null: false, default: 0
2222

23-
add :inserted_at, :utc_datetime_usec, null: false, default: fragment("CURRENT_TIMESTAMP")
24-
add :scheduled_at, :utc_datetime_usec, null: false, default: fragment("CURRENT_TIMESTAMP")
23+
add :inserted_at, :utc_datetime_usec,
24+
null: false,
25+
default: fragment("(strftime('%Y-%m-%dT%H:%M:%SZ', 'now'))")
26+
27+
add :scheduled_at, :utc_datetime_usec,
28+
null: false,
29+
default: fragment("(strftime('%Y-%m-%dT%H:%M:%SZ', 'now'))")
2530

2631
add :attempted_at, :utc_datetime_usec
2732
add :attempted_by, :json, null: false, default: []

0 commit comments

Comments
 (0)