Skip to content

Commit

Permalink
Fix bug when retried jobs didn't correctly reset their failed flag wh…
Browse files Browse the repository at this point in the history
…en ran again (fixes #61)
  • Loading branch information
pokonski committed Mar 12, 2019
1 parent 006d7f5 commit 0303f52
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/gush/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def perform

def start!
@started_at = current_timestamp
@failed_at = nil
end

def enqueue!
Expand Down
49 changes: 47 additions & 2 deletions spec/features/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,53 @@
end
end

context 'when one of the jobs fails initally' do
it 'succeeds when the job retries' do
FAIL_THEN_SUCCEED_SPY = double()
allow(FAIL_THEN_SUCCEED_SPY).to receive(:foo).and_return('failure', 'success')

class FailsThenSucceeds < Gush::Job
def perform
if FAIL_THEN_SUCCEED_SPY.foo == 'failure'
raise NameError
end
end
end

class SecondChanceWorkflow < Gush::Workflow
def configure
run Prepare
run FailsThenSucceeds, after: Prepare
run NormalizeJob, after: FailsThenSucceeds
end
end

flow = SecondChanceWorkflow.create
flow.start!

expect(Gush::Worker).to have_jobs(flow.id, jobs_with_id(['Prepare']))
perform_one

expect(Gush::Worker).to have_jobs(flow.id, jobs_with_id(['FailsThenSucceeds']))
expect do
perform_one
end.to raise_error(NameError)

expect(flow.reload).to be_failed
expect(Gush::Worker).to have_jobs(flow.id, jobs_with_id(['FailsThenSucceeds']))

# Retry the same job again, but this time succeeds
perform_one

expect(Gush::Worker).to have_jobs(flow.id, jobs_with_id(['NormalizeJob']))
perform_one

flow = flow.reload
expect(flow).to be_finished
expect(flow).to_not be_failed
end
end

it "runs the whole workflow in proper order" do
flow = TestWorkflow.create
flow.start!
Expand Down Expand Up @@ -77,8 +124,6 @@ def configure

perform_one
expect(flow.reload.find_job("PrependJob").output_payload).to eq("A prefix: SOME TEXT")


end

it "passes payloads from workflow that runs multiple same class jobs with nameized payloads" do
Expand Down
12 changes: 10 additions & 2 deletions spec/gush/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,18 @@
describe "#start!" do
it "resets flags and marks as running" do
job = described_class.new(name: "a-job")

job.enqueue!
job.fail!

now = Time.now.to_i
expect(job.started_at).to eq(nil)
expect(job.failed_at).to eq(now)

job.start!

expect(job.started_at).to eq(Time.now.to_i)
expect(job.enqueued?).to eq(false)
expect(job.running?).to eq(true)
expect(job.failed_at).to eq(nil)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PersistSecondJob < Gush::Job; end
class NormalizeJob < Gush::Job; end
class BobJob < Gush::Job; end

GUSHFILE = Pathname.new(__FILE__).parent.join("Gushfile")
GUSHFILE = Pathname.new(__FILE__).parent.join("Gushfile")

class TestWorkflow < Gush::Workflow
def configure
Expand Down

0 comments on commit 0303f52

Please sign in to comment.