From 51c90b21aa50735bab5106be9deeeef2d0f7a7d2 Mon Sep 17 00:00:00 2001 From: shubh Date: Fri, 16 Jan 2026 14:28:33 +0400 Subject: [PATCH 1/5] loggers --- lib/facio/concerns/execution.rb | 5 ++++- lib/facio/service.rb | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/facio/concerns/execution.rb b/lib/facio/concerns/execution.rb index 98dcfb5..d96e714 100644 --- a/lib/facio/concerns/execution.rb +++ b/lib/facio/concerns/execution.rb @@ -7,12 +7,13 @@ def facio_perform_with_arguments(*args) @context = self.class.context_class.new(arguments.first) @result = self.class.result_class&.new @performed = false - + Rails.logger.error "******************* facio_perform_with_arguments #{@context.valid?}********************" if @context.valid? result = nil if transactional && defined?(ActiveRecord::Base) ActiveRecord::Base.transaction(requires_new: true) do # result = block.call + Rails.logger.error "*******************Facio transactional********************" 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. @@ -20,11 +21,13 @@ def facio_perform_with_arguments(*args) end else + Rails.logger.error "*******************Facio not transactional********************" result = facio_perform_without_arguments end @performed = true # Purely as a convenience, but also to enforce a standard + Rails.logger.error "*******************Facio @result #{@result}********************" context.result ||= result if @result.nil? end end diff --git a/lib/facio/service.rb b/lib/facio/service.rb index 389675a..52a058f 100644 --- a/lib/facio/service.rb +++ b/lib/facio/service.rb @@ -15,12 +15,14 @@ class Service < ActiveJob::Base class << self def perform(...) + Rails.logger.error "*******************Facio perform********************" job = job_or_instantiate(...) job.perform_now job end def perform_later(...) + Rails.logger.error "*******************Facio perform_later********************" job = job_or_instantiate(...) enqueue_result = job.enqueue @@ -51,6 +53,7 @@ def result # Returns whether the service has been performed (only when using perform) def performed? + Rails.logger.error "*******************Facio performed? #{performed?}********************" @performed end From fe390ed9ae7d932cf8a99bc3c444470efff9caea Mon Sep 17 00:00:00 2001 From: shubh Date: Fri, 16 Jan 2026 15:11:57 +0400 Subject: [PATCH 2/5] loggers --- lib/facio/service.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/facio/service.rb b/lib/facio/service.rb index 52a058f..77e17e3 100644 --- a/lib/facio/service.rb +++ b/lib/facio/service.rb @@ -53,7 +53,7 @@ def result # Returns whether the service has been performed (only when using perform) def performed? - Rails.logger.error "*******************Facio performed? #{performed?}********************" + Rails.logger.error "*******************Facio performed? #{@performed}********************" @performed end From 616f292d0eccc9225308a35c6834a385e6ca179c Mon Sep 17 00:00:00 2001 From: shubh Date: Fri, 16 Jan 2026 15:30:19 +0400 Subject: [PATCH 3/5] loggers --- lib/facio/concerns/execution.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/facio/concerns/execution.rb b/lib/facio/concerns/execution.rb index d96e714..ef56958 100644 --- a/lib/facio/concerns/execution.rb +++ b/lib/facio/concerns/execution.rb @@ -8,6 +8,7 @@ def facio_perform_with_arguments(*args) @result = self.class.result_class&.new @performed = false Rails.logger.error "******************* facio_perform_with_arguments #{@context.valid?}********************" + Rails.logger.error "******************* facio_perform_with_arguments context #{@context}********************" if @context.valid? result = nil if transactional && defined?(ActiveRecord::Base) From 7decd108bfbadae4433ce649a9e4a0f197d3b767 Mon Sep 17 00:00:00 2001 From: shubh Date: Fri, 16 Jan 2026 15:43:55 +0400 Subject: [PATCH 4/5] loggers --- lib/facio/concerns/execution.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/facio/concerns/execution.rb b/lib/facio/concerns/execution.rb index ef56958..4db0860 100644 --- a/lib/facio/concerns/execution.rb +++ b/lib/facio/concerns/execution.rb @@ -8,7 +8,7 @@ def facio_perform_with_arguments(*args) @result = self.class.result_class&.new @performed = false Rails.logger.error "******************* facio_perform_with_arguments #{@context.valid?}********************" - Rails.logger.error "******************* facio_perform_with_arguments context #{@context}********************" + Rails.logger.error "******************* facio_perform_with_arguments context #{@context.inspect}********************" if @context.valid? result = nil if transactional && defined?(ActiveRecord::Base) From 256fdf09581ebab6c5b267394d00c659bd47f72a Mon Sep 17 00:00:00 2001 From: Tom de Grunt Date: Fri, 16 Jan 2026 16:23:52 +0000 Subject: [PATCH 5/5] Cleanup and use callbacks again --- README.md | 5 +++++ lib/facio/concerns/callbacks.rb | 4 ++-- lib/facio/concerns/execution.rb | 28 +--------------------------- lib/facio/service.rb | 17 +---------------- 4 files changed, 9 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 89f5f50..4041c2c 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/lib/facio/concerns/callbacks.rb b/lib/facio/concerns/callbacks.rb index ad22084..bab0a80 100644 --- a/lib/facio/concerns/callbacks.rb +++ b/lib/facio/concerns/callbacks.rb @@ -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 @@ -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 diff --git a/lib/facio/concerns/execution.rb b/lib/facio/concerns/execution.rb index 4db0860..fa4de54 100644 --- a/lib/facio/concerns/execution.rb +++ b/lib/facio/concerns/execution.rb @@ -4,33 +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 - Rails.logger.error "******************* facio_perform_with_arguments #{@context.valid?}********************" - Rails.logger.error "******************* facio_perform_with_arguments context #{@context.inspect}********************" - if @context.valid? - result = nil - if transactional && defined?(ActiveRecord::Base) - ActiveRecord::Base.transaction(requires_new: true) do - # result = block.call - Rails.logger.error "*******************Facio transactional********************" - 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 - Rails.logger.error "*******************Facio not transactional********************" - result = facio_perform_without_arguments - end - - @performed = true - # Purely as a convenience, but also to enforce a standard - Rails.logger.error "*******************Facio @result #{@result}********************" - context.result ||= result if @result.nil? - end + facio_perform_without_arguments end included do diff --git a/lib/facio/service.rb b/lib/facio/service.rb index 77e17e3..284b9b2 100644 --- a/lib/facio/service.rb +++ b/lib/facio/service.rb @@ -11,30 +11,16 @@ class Service < ActiveJob::Base include ServiceResult include Transactional include Translations + include Callbacks include Execution class << self def perform(...) - Rails.logger.error "*******************Facio perform********************" job = job_or_instantiate(...) job.perform_now job end - def perform_later(...) - Rails.logger.error "*******************Facio 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 @@ -53,7 +39,6 @@ def result # Returns whether the service has been performed (only when using perform) def performed? - Rails.logger.error "*******************Facio performed? #{@performed}********************" @performed end