Files
hatchet/examples/ruby/on_failure/worker.rb
Gabe Ruttner 7875d78057 Feat: Official Ruby SDK (#3004)
* feat: initial ruby sdk

* fix: run listener

* fix: scope

* feat: rest feature clients

* fix: bugs

* fix: concurrent register

* fix: tests and ergonomics

* docs: all of them

* chore: lint

* feat: add RBS

* feat: add GitHub Actions workflow for Ruby SDK with linting, testing, and publishing steps

* chore: lint

* refactor: simplify load path setup for Hatchet REST client and remove symlink creation

* fix: cert path

* fix: test

* fix: blocking

* fix: ensure Hatchet client is only initialized once across examples

* fix: tests

* remove: unused example

* fix: bubble up errors

* test: skip flaky for now

* remove: lifespans

* fix: durable context bugs

* fix: bulk replay

* fix: tests

* cleanup: generate tooling

* fix: integration test

* chore: lint

* release: 0.1.0

* chore: remove python comments

* refactor: remove OpenTelemetry configuration and related unused options

* fix: default no healthcheck

* chore: lockfile

* feat: register as ruby

* chore: lint

* chore: update py/ts apis to include ruby

* chore: docs pass

* chore: lint

* chore: generate

* chore: cleanup

* chore: generate examples

* tests: add e2e tests

* tests: cache examples dependencies

* fix: namespace

* fix: namespace

* fix: namespaces

* chore:lint

* fix: improve cancellation workflow polling logic and add error handling

* revert: py/ts versions
2026-02-15 14:32:15 -08:00

74 lines
1.8 KiB
Ruby

# frozen_string_literal: true
require "hatchet-sdk"
HATCHET = Hatchet::Client.new(debug: false) unless defined?(HATCHET)
ERROR_TEXT = "step1 failed"
# > OnFailure Step
# This workflow will fail because the step will throw an error
# we define an onFailure step to handle this case
ON_FAILURE_WF = HATCHET.workflow(name: "OnFailureWorkflow")
ON_FAILURE_WF.task(:step1, execution_timeout: 1) do |input, ctx|
# This step will always raise an exception
raise ERROR_TEXT
end
# After the workflow fails, this special step will run
ON_FAILURE_WF.on_failure_task do |input, ctx|
# We can do things like perform cleanup logic
# or notify a user here
# Fetch the errors from upstream step runs from the context
puts ctx.task_run_errors.inspect
{ "status" => "success" }
end
# > OnFailure With Details
# We can access the failure details in the onFailure step
# via the context method
ON_FAILURE_WF_WITH_DETAILS = HATCHET.workflow(name: "OnFailureWorkflowWithDetails")
DETAILS_STEP1 = ON_FAILURE_WF_WITH_DETAILS.task(:details_step1, execution_timeout: 1) do |input, ctx|
raise ERROR_TEXT
end
# After the workflow fails, this special step will run
ON_FAILURE_WF_WITH_DETAILS.on_failure_task do |input, ctx|
error = ctx.get_task_run_error(DETAILS_STEP1)
unless error
next { "status" => "unexpected success" }
end
# We can access the failure details here
raise "Expected Hatchet::TaskRunError" unless error.is_a?(Hatchet::TaskRunError)
if error.message.include?("step1 failed")
next {
"status" => "success",
"failed_run_external_id" => error.task_run_external_id
}
end
raise "unexpected failure"
end
def main
worker = HATCHET.worker(
"on-failure-worker",
slots: 4,
workflows: [ON_FAILURE_WF, ON_FAILURE_WF_WITH_DETAILS]
)
worker.start
end
main if __FILE__ == $PROGRAM_NAME