Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby_version: [2.7.8, 3.0.7]
ruby_version: [3.3.10]
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
Expand Down
6 changes: 6 additions & 0 deletions lib/active_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
module ActiveOperation
class Error < RuntimeError; end
class AlreadyCompletedError < Error; end

# Internal control-flow signals raised by #halt / #succeed from inside an
# operation's #execute. Inheriting from Exception (not StandardError) so
# user code's bare `rescue` does not silently swallow them.
class Halted < Exception; end
class Succeeded < Exception; end
end

require_relative "active_operation/version"
Expand Down
19 changes: 15 additions & 4 deletions lib/active_operation/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,15 @@ def perform
run_callbacks :execute do
catch(:abort) do
next if completed?
@output = execute
self.state = :succeeded
@in_execute = true
begin
@output = execute
self.state = :succeeded
rescue ActiveOperation::Halted, ActiveOperation::Succeeded
# state and @output already set by #halt / #succeed
ensure
@in_execute = false
end
end
end

Expand Down Expand Up @@ -197,14 +204,18 @@ def halt(*args)

self.state = :halted
@output = args.length > 1 ? args : args.first
throw :abort
# Inside #execute, raise so we propagate cleanly through any enclosing
# ActiveRecord transaction (which will roll back). Outside #execute
# (i.e. from before/after callbacks), keep using `throw :abort` so
# ActiveSupport's callback chain halts and after-callbacks still run.
@in_execute ? raise(ActiveOperation::Halted) : throw(:abort)
end

def succeed(*args)
raise ActiveOperation::AlreadyCompletedError if completed?

self.state = :succeeded
@output = args.length > 1 ? args : args.first
throw :abort
@in_execute ? raise(ActiveOperation::Succeeded) : throw(:abort)
end
end
Loading