Skip to content

Set successfully_enqueued in Job.enqueue #603

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

Merged
merged 1 commit into from
Jul 21, 2025
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
1 change: 1 addition & 0 deletions app/models/solid_queue/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def enqueue(active_job, scheduled_at: Time.current)

create_from_active_job(active_job).tap do |enqueued_job|
active_job.provider_job_id = enqueued_job.id if enqueued_job.persisted?
active_job.successfully_enqueued = enqueued_job.persisted?
end
end

Expand Down
1 change: 0 additions & 1 deletion app/models/solid_queue/recurring_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ def enqueue_and_record(run_at:)
active_job.run_callbacks(:enqueue) do
Job.enqueue(active_job)
end
active_job.successfully_enqueued = true
end
end
end
Expand Down
34 changes: 30 additions & 4 deletions test/models/solid_queue/recurring_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ def perform
end
end

class JobWithConcurrencyControlsAndDiscard < ApplicationJob
limits_concurrency key: -> { true }, on_conflict: :discard

def perform
JobBuffer.add "job_with_concurrency_controls"
end
end

test "job without arguments" do
task = recurring_task_with(class_name: "JobWithoutArguments")
enqueue_and_assert_performed_with_result task, "job_without_arguments"
Expand Down Expand Up @@ -88,6 +96,20 @@ def perform
end
end

test "error when enqueuing job because of concurrency controls and discard" do
JobWithConcurrencyControlsAndDiscard.perform_later

task = recurring_task_with(class_name: "JobWithConcurrencyControlsAndDiscard")
assert_no_difference -> { SolidQueue::Job.count } do
task.enqueue(at: Time.now)
end

# Run the enqueued job that was preventing a new one from being enqueued
run_all_jobs_inline

enqueue_and_assert_performed_with_result task, "job_with_concurrency_controls"
end

test "error when enqueuing job using another adapter that raises ActiveJob::EnqueueError" do
ActiveJob::QueueAdapters::AsyncAdapter.any_instance.stubs(:enqueue).raises(ActiveJob::EnqueueError)
previous_size = JobBuffer.size
Expand Down Expand Up @@ -174,10 +196,7 @@ def enqueue_and_assert_performed_with_result(task, result)
end

assert_difference -> { JobBuffer.size }, +1 do
SolidQueue::Worker.new(queues: "*").tap do |worker|
worker.mode = :inline
worker.start
end
run_all_jobs_inline
end

assert_equal result, JobBuffer.last_value
Expand All @@ -192,4 +211,11 @@ def recurring_task_with(class_name: nil, **options)

SolidQueue::RecurringTask.from_configuration("task-id", **options)
end

def run_all_jobs_inline
SolidQueue::Worker.new(queues: "*").tap do |worker|
worker.mode = :inline
worker.start
end
end
end