Skip to content
Merged
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ end

```

```
_enqueue_callbacks.map.with_index {|c,i| [i, c.kind, c.name, c.filter]}
_perform_callbacks.map.with_index {|c,i| [i, c.kind, c.name, c.filter]}
```

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
4 changes: 2 additions & 2 deletions lib/facio/concerns/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module Callbacks
# This is the most close to expected behaviour this can get.
raise ActiveRecord::Rollback if context.failed?
end

else
result = block.call
end
Expand All @@ -27,10 +26,11 @@ module Callbacks
end
end

before_enqueue do |job|
around_enqueue do |job, block|
@performed = false
@context = self.class.context_class.new(arguments.first)
@result = self.class.result_class&.new
block.call
end
end
end
24 changes: 1 addition & 23 deletions lib/facio/concerns/execution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,7 @@ module Execution
# Facio doesn't support arguments in the perform method, that's done with the context object.
# This is why we override perform here.
def facio_perform_with_arguments(*args)
@context = self.class.context_class.new(arguments.first)
@result = self.class.result_class&.new
@performed = false

if @context.valid?
result = nil
if transactional && defined?(ActiveRecord::Base)
ActiveRecord::Base.transaction(requires_new: true) do
# result = block.call
result = facio_perform_without_arguments
# This will only rollback the changes of the service, SILENTLY, however the context will be failed? already.
# This is the most close to expected behaviour this can get.
raise ActiveRecord::Rollback if context.failed?
end

else
result = facio_perform_without_arguments
end

@performed = true
# Purely as a convenience, but also to enforce a standard
context.result ||= result if @result.nil?
end
facio_perform_without_arguments
end

included do
Expand Down
14 changes: 1 addition & 13 deletions lib/facio/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Service < ActiveJob::Base
include ServiceResult
include Transactional
include Translations
include Callbacks
include Execution

class << self
Expand All @@ -20,19 +21,6 @@ def perform(...)
job
end

def perform_later(...)
job = job_or_instantiate(...)
enqueue_result = job.enqueue

job.instance_variable_set(:@performed, false)
job.instance_variable_set(:@context, context_class.new(job.arguments.first))
job.instance_variable_set(:@result, result_class&.new)

yield job if block_given?

enqueue_result
end

alias_method :perform_now, :perform
end

Expand Down