From d2eb3f3494a179aa898048fbf0884c83d887ac77 Mon Sep 17 00:00:00 2001 From: Ace Subido Date: Tue, 14 May 2019 22:17:16 +0800 Subject: [PATCH 1/6] Add Job#skipped_at attribute --- lib/gush/job.rb | 11 +++++++++-- spec/gush/job_spec.rb | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/gush/job.rb b/lib/gush/job.rb index d718e72..f7705ad 100644 --- a/lib/gush/job.rb +++ b/lib/gush/job.rb @@ -1,8 +1,9 @@ module Gush class Job attr_accessor :workflow_id, :incoming, :outgoing, :params, - :finished_at, :failed_at, :started_at, :enqueued_at, :payloads, - :klass, :queue, :wait + :finished_at, :failed_at, :started_at, :enqueued_at, :payloads, + :klass, :queue, :wait, :skipped_at + attr_reader :id, :klass, :output_payload, :params def initialize(opts = {}) @@ -20,6 +21,7 @@ def as_json finished_at: finished_at, enqueued_at: enqueued_at, started_at: started_at, + skipped_at: skipped_at, failed_at: failed_at, params: params, workflow_id: workflow_id, @@ -74,6 +76,10 @@ def finished? !finished_at.nil? end + def skipped? + !skipped_at.nil? + end + def failed? !failed_at.nil? end @@ -128,6 +134,7 @@ def assign_variables(opts) @workflow_id = opts[:workflow_id] @queue = opts[:queue] @wait = opts[:wait] + @skipped_at = opts[:skipped_at] end end end diff --git a/spec/gush/job_spec.rb b/spec/gush/job_spec.rb index 8748ec9..4c425ef 100644 --- a/spec/gush/job_spec.rb +++ b/spec/gush/job_spec.rb @@ -78,6 +78,7 @@ outgoing: [], failed_at: nil, started_at: nil, + skipped_at: nil, finished_at: 123, enqueued_at: 120, params: {}, From 3d185682bb50a186c08718cf0c9dc98d3987ea8f Mon Sep 17 00:00:00 2001 From: Ace Subido Date: Tue, 14 May 2019 22:18:38 +0800 Subject: [PATCH 2/6] Add Job#skip! method that marks a job as skipped and jumps out of the nested ruby construct --- lib/gush/job.rb | 5 +++++ spec/gush/job_spec.rb | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/gush/job.rb b/lib/gush/job.rb index f7705ad..6422b51 100644 --- a/lib/gush/job.rb +++ b/lib/gush/job.rb @@ -68,6 +68,11 @@ def fail! @finished_at = @failed_at = current_timestamp end + def skip! + @finished_at = @skipped_at = current_timestamp + throw(:skipped_job) + end + def enqueued? !enqueued_at.nil? end diff --git a/spec/gush/job_spec.rb b/spec/gush/job_spec.rb index 4c425ef..e7769cb 100644 --- a/spec/gush/job_spec.rb +++ b/spec/gush/job_spec.rb @@ -9,6 +9,7 @@ expect(job.output_payload).to eq("something") end end + describe "#fail!" do it "sets finished and failed to true and records time" do job = described_class.new(name: "a-job") @@ -21,6 +22,18 @@ end end + describe "#skip!" do + it "sets finished and skipped to true and records time" do + job = described_class.new(name: "a-job") + expect { job.skip! }.to throw_symbol(:skipped_job) + expect(job.skipped_at).to eq(Time.now.to_i) + expect(job.skipped?).to eq(true) + expect(job.finished?).to eq(true) + expect(job.running?).to eq(false) + expect(job.enqueued?).to eq(false) + end + end + describe "#finish!" do it "sets finished to false and failed to false and records time" do job = described_class.new(name: "a-job") From d35404675e833dc5e028a821f5e1593661fbb5e9 Mon Sep 17 00:00:00 2001 From: Ace Subido Date: Tue, 14 May 2019 22:26:42 +0800 Subject: [PATCH 3/6] Catch :skipped_job from Worker#perform --- lib/gush/worker.rb | 4 +++- spec/gush/worker_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/gush/worker.rb b/lib/gush/worker.rb index 6578cd1..ea1a709 100644 --- a/lib/gush/worker.rb +++ b/lib/gush/worker.rb @@ -18,7 +18,9 @@ def perform(workflow_id, job_id) mark_as_started begin - job.perform + catch(:skipped_job) do + job.perform + end rescue StandardError => error mark_as_failed raise error diff --git a/spec/gush/worker_spec.rb b/spec/gush/worker_spec.rb index 21c9c10..1470d8a 100644 --- a/spec/gush/worker_spec.rb +++ b/spec/gush/worker_spec.rb @@ -33,6 +33,33 @@ def configure end end + context "when job is skipped" do + it "should skip the rest of the code" do + class SkippedJob < Gush::Job + def perform + self.skip! + output = "Hello" + end + end + + class NormalWorkflow < Gush::Workflow + def configure + run SkippedJob + end + end + + workflow = NormalWorkflow.create + + subject.perform(workflow.id, "SkippedJob") + job = client.find_job(workflow.id, "SkippedJob") + + expect(job).to be_skipped + expect(job).to be_finished + expect(job.output_payload).not_to eq "Hello" + expect(job.output_payload).to be_nil + end + end + context "when job completes successfully" do it "should mark it as succedeed" do expect(subject).to receive(:mark_as_finished) From 46b937d4e5bed88f6d45175c79464faa95c98c85 Mon Sep 17 00:00:00 2001 From: Ace Subido Date: Tue, 14 May 2019 22:41:56 +0800 Subject: [PATCH 4/6] Update README --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 73f9a74..2d9f5a4 100644 --- a/README.md +++ b/README.md @@ -383,6 +383,25 @@ class NotifyWorkflow < Gush::Workflow end ``` +### Skipping a Job + +Sometimes you'd like to skip a job, which doesn't mean that job/workflow has failed. You can call `self.skip!` inside the job to mark it as a `skipped`: + +```ruby +class ProcessUserJob < Gush::Job + def perform + user = User.find params[:id] + + self.skip! if user.processed? + + # Code beneath here will not be called anymore + # and the job will be marked as 'skipped' + # + # The workflow will continue on normally to the other jobs + end +end +``` + ## Command line interface (CLI) ### Checking status From 635f16ab5a9452b8feff620ac2dddc456e3ce134 Mon Sep 17 00:00:00 2001 From: Nate Montgomery Date: Tue, 5 Mar 2024 18:55:19 -0800 Subject: [PATCH 5/6] Add back changelog, add integration spec --- CHANGELOG.md | 5 ++++ spec/features/integration_spec.rb | 45 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28e6d0f..7d1871d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## Unreleased + +### Added +- Add `Gush::Job#skip!` that marks a job as `#skipped?` and skips the remaining code. [See pull request](https://github.com/chaps-io/gush/pull/66). + ## 2.1.0 ### Added diff --git a/spec/features/integration_spec.rb b/spec/features/integration_spec.rb index e7544d9..8881423 100644 --- a/spec/features/integration_spec.rb +++ b/spec/features/integration_spec.rb @@ -243,4 +243,49 @@ def configure expect(flow).to be_finished expect(flow).to_not be_failed end + + context 'when one of the jobs is skipped' do + it 'still runs the rest of the jobs in the workflow' do + SKIPPED_SPY = double() + + class FirstJob < Gush::Job + def perform + SKIPPED_SPY.foo + end + end + + class SkippedJob < Gush::Job + def perform + self.skip! + SKIPPED_SPY.bar + end + end + + class FinalJob < Gush::Job + def perform + SKIPPED_SPY.baz + end + end + + class PartiallySkippedWorkflow < Gush::Workflow + def configure + run FirstJob + run SkippedJob, after: FirstJob + run FinalJob, after: SkippedJob + end + end + + flow = PartiallySkippedWorkflow.create + flow.start! + + expect(SKIPPED_SPY).to receive(:foo) + perform_one + + expect(SKIPPED_SPY).not_to receive(:bar) + perform_one + + expect(SKIPPED_SPY).to receive(:baz) + perform_one + end + end end From fdb6a8964b6cd33e63b36d232d8e0fa1fbf69a7c Mon Sep 17 00:00:00 2001 From: Nate Montgomery Date: Wed, 6 Mar 2024 09:23:28 -0800 Subject: [PATCH 6/6] Update URL for PR --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d1871d..6ead4b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## Unreleased ### Added -- Add `Gush::Job#skip!` that marks a job as `#skipped?` and skips the remaining code. [See pull request](https://github.com/chaps-io/gush/pull/66). +- Add `Gush::Job#skip!` that marks a job as `#skipped?` and skips the remaining code. [See pull request](https://github.com/chaps-io/gush/pull/109). ## 2.1.0