From 9ebd93759425c571af782cf6c001ce081cace21c Mon Sep 17 00:00:00 2001 From: Anton Katunin Date: Tue, 23 Jul 2024 20:21:33 +1000 Subject: [PATCH] Add GoodJob module --- lib/active_interaction/extras.rb | 1 + lib/active_interaction/extras/good_job.rb | 61 +++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 lib/active_interaction/extras/good_job.rb diff --git a/lib/active_interaction/extras.rb b/lib/active_interaction/extras.rb index 7c9239e..5a50441 100644 --- a/lib/active_interaction/extras.rb +++ b/lib/active_interaction/extras.rb @@ -34,6 +34,7 @@ module Jobs autoload(:Rspec, "active_interaction/extras/rspec") autoload(:ActiveJob, "active_interaction/extras/active_job") + autoload(:GoodJob, "active_interaction/extras/good_job") autoload(:Sidekiq, "active_interaction/extras/sidekiq") concern :FormFor do diff --git a/lib/active_interaction/extras/good_job.rb b/lib/active_interaction/extras/good_job.rb new file mode 100644 index 0000000..f43d417 --- /dev/null +++ b/lib/active_interaction/extras/good_job.rb @@ -0,0 +1,61 @@ +require 'active_job' + +module ActiveInteraction::Extras::GoodJob + extend ActiveSupport::Concern + include ActiveInteraction::Extras::ActiveJob + + module BatchJobPerform + def perform(batch, options) + job_klass = batch.properties[:job_klass] + job_klass_params = batch.properties[:job_klass_params] + + ActiveInteraction::Extras::Current::CurrentContext + .set(batch_event: options[:event]) do + job_klass.constantize.perform_now(job_klass_params) + end + end + end + + module Perform + extend ActiveSupport::Concern + include ActiveInteraction::Extras::ActiveJob::Perform + + def _good_job_default_concurrency_key + Digest::MD5.base64digest([self.class.name, arguments].to_param) + end + end + + class_methods do + def define_job_class(klass) + super + + unless const_defined?(:BatchJob, false) + const_set(:BatchJob, Class.new(job_class) do + include BatchJobPerform + end) + end + end + + def batch_job(job_klass_params) + batch = GoodJob::Batch.new + batch.description = self.name + batch.on_finish = self::BatchJob.name + batch.properties = { + job_klass: self.job_class.name, + job_klass_params: + } + batch + end + end + + class MyBatchCallbackJob < ApplicationJob + def perform(batch, options) + job_klass = batch.properties[:job_klass] + job_klass_params = batch.properties[:job_klass_params] + + ActiveInteraction::Extras::Current::CurrentContext.set(batch_event: options[:event]) do + job_klass.constantize.perform_now(job_klass_params) + end + end + end +end