Skip to content

Commit 0451fce

Browse files
committed
Allow by period uniqueness to be based off scheduled time
This one follows up [1] in the main repository, in which we fix what could be considered a bug in that by period uniqueness was always based off the current time, even though a job may have been given a custom value for `scheduled_at`, which really should take precedence. [1] riverqueue/river#734
1 parent 0d2d588 commit 0451fce

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- `by_period` uniqueness is now based off a job's `scheduled_at` instead of the current time if it has a value. [PR #39](https://github.com/riverqueue/riverqueue-ruby/pull/39).
13+
1014
## [0.8.0] - 2024-12-19
1115

1216
⚠️ Version 0.8.0 contains breaking changes to transition to River's new unique jobs implementation and to enable broader, more flexible application of unique jobs. Detailed notes on the implementation are contained in [the original River PR](https://github.com/riverqueue/river/pull/590), and the notes below include short summaries of the ways this impacts this client specifically.

lib/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def insert_many(args)
220220
end
221221

222222
if unique_opts.by_period
223-
lower_period_bound = truncate_time(@time_now_utc.call, unique_opts.by_period).utc
223+
lower_period_bound = truncate_time(insert_params.scheduled_at || @time_now_utc.call, unique_opts.by_period).utc
224224

225225
unique_key += "&period=#{lower_period_bound.strftime("%FT%TZ")}"
226226
end

spec/client_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,24 @@ def check_bigint_bounds(int)
291291
expect(insert_res.unique_skipped_as_duplicated).to be false
292292
end
293293

294+
it "inserts a new unique job with period determined from `scheduled_at`" do
295+
job_args = ComplexArgs.new(customer_id: 1, order_id: 2, trace_id: 3, email: "john@example.com")
296+
insert_opts = River::InsertOpts.new(
297+
scheduled_at: now + 3600,
298+
unique_opts: River::UniqueOpts.new(
299+
by_period: 15 * 60
300+
)
301+
)
302+
303+
insert_res = client.insert(job_args, insert_opts: insert_opts)
304+
expect(insert_res.job).to_not be_nil
305+
expect(insert_res.unique_skipped_as_duplicated).to be false
306+
307+
unique_key_str = "&kind=#{insert_res.job.kind}" \
308+
"&period=#{client.send(:truncate_time, now + 3600, 15 * 60).utc.strftime("%FT%TZ")}"
309+
expect(insert_res.job.unique_key).to eq(Digest::SHA256.digest(unique_key_str))
310+
end
311+
294312
it "skips unique check if unique opts empty" do
295313
job_args = SimpleArgsWithInsertOpts.new(job_num: 1)
296314
job_args.insert_opts = River::InsertOpts.new(

0 commit comments

Comments
 (0)