Skip to content

Commit

Permalink
Error if self enqueue fails
Browse files Browse the repository at this point in the history
  • Loading branch information
hlascelles committed Mar 11, 2019
1 parent 81bfd01 commit 10f2fa3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased

- Add re-enqueue checks [#76](https://github.com/hlascelles/que-scheduler/pull/76)

## 3.2.3 (2019-03-06)

- Namespace support modules [#54](https://github.com/hlascelles/que-scheduler/pull/54)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/activesupport_4.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
que-scheduler (3.2.2)
que-scheduler (3.2.3)
activesupport (>= 4.0)
backports (~> 3.10)
fugit (~> 1.1, >= 1.1.8)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/activesupport_5.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
que-scheduler (3.2.2)
que-scheduler (3.2.3)
activesupport (>= 4.0)
backports (~> 3.10)
fugit (~> 1.1, >= 1.1.8)
Expand Down
6 changes: 5 additions & 1 deletion lib/que/scheduler/scheduler_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,15 @@ def check_enqueued_job(enqueued_job, job_class, args, logs)

def enqueue_self_again(scheduler_job_args, last_full_execution, job_dictionary, enqueued_jobs)
next_run_at = scheduler_job_args.as_time.beginning_of_minute + SCHEDULER_FREQUENCY
SchedulerJob.enqueue(
enqueued_job = SchedulerJob.enqueue(
last_run_time: last_full_execution.iso8601,
job_dictionary: job_dictionary,
run_at: next_run_at
)
unless check_enqueued_job(enqueued_job, SchedulerJob, {}, [])
raise 'SchedulerJob could not self-schedule. Has `.enqueue` been monkey patched?'
end

Audit.append(attrs[:job_id], scheduler_job_args.as_time, enqueued_jobs)
end
end
Expand Down
23 changes: 21 additions & 2 deletions spec/que/scheduler/scheduler_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@
)
end

# Some middlewares can cause an enqueue not to enqueue a job.
# When the main job fails to self enqueue, we must error.
it "errors when the enqueue call does not enqueue the #{described_class} job" do
described_class.enqueue
expect(described_class).to receive(:enqueue).and_return(false)
expect_any_instance_of(::Que::Job).to receive(:handle_error).once.and_call_original
::Que::Job.work

# The job will have been re-enqueued, not by itself during normal operation, but by the
# standard que job error retry semantics.
hash = expect_one_itself_job
expect(hash['error_count']).to eq(1)
end

def run_test(initial_job_args, to_be_scheduled)
described_class.enqueue(initial_job_args)
::Que::Job.work
Expand Down Expand Up @@ -172,12 +186,17 @@ def expect_one_result(args, queue, priority)
end
end

def expect_itself_enqueued
def expect_one_itself_job
itself_jobs = DbSupport.jobs_by_class(QS::SchedulerJob)
expect(itself_jobs.count).to eq(1)
hash = itself_jobs.first.to_h
itself_jobs.first.to_h
end

def expect_itself_enqueued
hash = expect_one_itself_job
expect(hash['queue']).to eq('')
expect(hash['priority']).to eq(0)
expect(hash['error_count']).to eq(0)
expect(hash['job_class']).to eq('Que::Scheduler::SchedulerJob')
expect(hash['run_at']).to eq(
run_time.beginning_of_minute + described_class::SCHEDULER_FREQUENCY
Expand Down

0 comments on commit 10f2fa3

Please sign in to comment.