Skip to content

Commit

Permalink
Add GoodJob module
Browse files Browse the repository at this point in the history
  • Loading branch information
antulik committed Jul 23, 2024
1 parent a91704d commit 9ebd937
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/active_interaction/extras.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions lib/active_interaction/extras/good_job.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9ebd937

Please sign in to comment.