diff --git a/lib/gush.rb b/lib/gush.rb index 15da935..5419758 100644 --- a/lib/gush.rb +++ b/lib/gush.rb @@ -15,6 +15,7 @@ require "gush/configuration" require "gush/errors" require "gush/job" +require "gush/nil_job" require "gush/worker" require "gush/workflow" diff --git a/lib/gush/job.rb b/lib/gush/job.rb index 9d21ccc..5b3ab0d 100644 --- a/lib/gush/job.rb +++ b/lib/gush/job.rb @@ -36,6 +36,8 @@ def to_json(options = {}) def self.from_hash(hash) hash[:klass].constantize.new(hash) + rescue NameError + Gush::NilJob.new(hash) end def output(data) diff --git a/lib/gush/nil_job.rb b/lib/gush/nil_job.rb new file mode 100644 index 0000000..f1df349 --- /dev/null +++ b/lib/gush/nil_job.rb @@ -0,0 +1,7 @@ +module Gush + class NilJob < Job + def name + @name ||= "Removed - #{klass}|#{id}" + end + end +end diff --git a/spec/gush/job_spec.rb b/spec/gush/job_spec.rb index 8748ec9..091655f 100644 --- a/spec/gush/job_spec.rb +++ b/spec/gush/job_spec.rb @@ -91,33 +91,66 @@ end describe ".from_hash" do - it "properly restores state of the job from hash" do - job = described_class.from_hash( - { - klass: 'Gush::Job', - id: '702bced5-bb72-4bba-8f6f-15a3afa358bd', - incoming: ['a', 'b'], - outgoing: ['c'], - failed_at: 123, - finished_at: 122, - started_at: 55, - enqueued_at: 444 - } - ) + context 'when the job is existed' do + it "properly restores state of the job from hash" do + job = described_class.from_hash( + { + klass: 'Gush::Job', + id: '702bced5-bb72-4bba-8f6f-15a3afa358bd', + incoming: ['a', 'b'], + outgoing: ['c'], + failed_at: 123, + finished_at: 122, + started_at: 55, + enqueued_at: 444 + } + ) - expect(job.id).to eq('702bced5-bb72-4bba-8f6f-15a3afa358bd') - expect(job.name).to eq('Gush::Job|702bced5-bb72-4bba-8f6f-15a3afa358bd') - expect(job.class).to eq(Gush::Job) - expect(job.klass).to eq("Gush::Job") - expect(job.finished?).to eq(true) - expect(job.failed?).to eq(true) - expect(job.enqueued?).to eq(true) - expect(job.incoming).to eq(['a', 'b']) - expect(job.outgoing).to eq(['c']) - expect(job.failed_at).to eq(123) - expect(job.finished_at).to eq(122) - expect(job.started_at).to eq(55) - expect(job.enqueued_at).to eq(444) + expect(job.id).to eq('702bced5-bb72-4bba-8f6f-15a3afa358bd') + expect(job.name).to eq('Gush::Job|702bced5-bb72-4bba-8f6f-15a3afa358bd') + expect(job.class).to eq(Gush::Job) + expect(job.klass).to eq("Gush::Job") + expect(job.finished?).to eq(true) + expect(job.failed?).to eq(true) + expect(job.enqueued?).to eq(true) + expect(job.incoming).to eq(['a', 'b']) + expect(job.outgoing).to eq(['c']) + expect(job.failed_at).to eq(123) + expect(job.finished_at).to eq(122) + expect(job.started_at).to eq(55) + expect(job.enqueued_at).to eq(444) + end + end + + context 'when the job is removed' do + it "properly restores state of the job from hash" do + job = described_class.from_hash( + { + klass: 'Gush::RemovedJob', + id: '702bced5-bb72-4bba-8f6f-15a3afa358bd', + incoming: ['a', 'b'], + outgoing: ['c'], + failed_at: 123, + finished_at: 122, + started_at: 55, + enqueued_at: 444 + } + ) + + expect(job.id).to eq('702bced5-bb72-4bba-8f6f-15a3afa358bd') + expect(job.name).to eq('Removed - Gush::RemovedJob|702bced5-bb72-4bba-8f6f-15a3afa358bd') + expect(job.class).to eq(Gush::NilJob) + expect(job.klass).to eq("Gush::RemovedJob") + expect(job.finished?).to eq(true) + expect(job.failed?).to eq(true) + expect(job.enqueued?).to eq(true) + expect(job.incoming).to eq(['a', 'b']) + expect(job.outgoing).to eq(['c']) + expect(job.failed_at).to eq(123) + expect(job.finished_at).to eq(122) + expect(job.started_at).to eq(55) + expect(job.enqueued_at).to eq(444) + end end end end diff --git a/spec/gush/nil_job_spec.rb b/spec/gush/nil_job_spec.rb new file mode 100644 index 0000000..68925de --- /dev/null +++ b/spec/gush/nil_job_spec.rb @@ -0,0 +1,14 @@ +require 'spec_helper' + +describe Gush::NilJob do + describe '#name' do + it 'prepends "Removed - "' do + job = described_class.from_hash( + klass: 'Gush::RemovedJob', + id: '702bced5-bb72-4bba-8f6f-15a3afa358bd' + ) + + expect(job.name).to eq('Removed - Gush::RemovedJob|702bced5-bb72-4bba-8f6f-15a3afa358bd') + end + end +end