Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only enforce scheduler_key unique index on first execution #6

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/gouda/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ def enqueue_all(active_jobs)
# We can't tell Postgres to ignore conflicts on _both_ the scheduler key and the enqueue concurrency key but not on
# the ID - it is either "all indexes" or "just one", but never "this index and that index". MERGE https://www.postgresql.org/docs/current/sql-merge.html
# is in theory capable of solving this but let's not complicate things all to hastily, the hour is getting late
scheduler_key = active_job.try(:executions) == 0 ? active_job.scheduler_key : nil # only enforce scheduler key on first workload
{
active_job_id: active_job.job_id, # Multiple jobs can have the same ID due to retries, job-iteration etc.
scheduled_at: active_job.scheduled_at || t_now,
scheduler_key: active_job.scheduler_key, # So that the scheduler_key gets retained between retries
scheduler_key: scheduler_key,
priority: active_job.priority,
execution_concurrency_key: extract_execution_concurrency_key(active_job),
enqueue_concurrency_key: extract_enqueue_concurrency_key(active_job),
Expand Down
25 changes: 23 additions & 2 deletions test/gouda/scheduler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class FailingJob < ActiveJob::Base
class MegaError < StandardError
end

gouda_control_concurrency_with(enqueue_limit: 1, key: -> { self.class.to_s })

retry_on StandardError, wait: :polynomially_longer, attempts: 5
retry_on Gouda::InterruptError, wait: 0, attempts: 5
retry_on MegaError, attempts: 3, wait: 0
Expand Down Expand Up @@ -55,6 +53,29 @@ def perform
assert Gouda::Workload.count > 3
end

test "retries do not have a scheduler_key" do
tab = {
second_minutely: {
cron: "*/1 * * * * *", # every second
class: "GoudaSchedulerTest::FailingJob"
}
}

assert_nothing_raised do
Gouda::Scheduler.build_scheduler_entries_list!(tab)
Gouda::Scheduler.upsert_workloads_from_entries_list!
end

assert_equal 1, Gouda::Workload.enqueued.count
assert_equal "second_minutely_*/1 * * * * *_GoudaSchedulerTest::FailingJob", Gouda::Workload.enqueued.first.scheduler_key
sleep(2)
Gouda::Workload.checkout_and_perform_one(executing_on: "Unit test")

assert_equal 1, Gouda::Workload.retried.reload.count
assert_nil Gouda::Workload.retried.first.scheduler_key
assert_equal "enqueued", Gouda::Workload.retried.first.state
end

test "re-inserts the next subsequent job after executing the queued one" do
tab = {
second_minutely: {
Expand Down