searchkick icon indicating copy to clipboard operation
searchkick copied to clipboard

Add parent_job attr to change inheritance af jobs

Open kevynlebouille opened this issue 3 years ago • 5 comments

Hi,

In order to add some callbacks to Searchkick jobs or use new features from ActiveJob, it could be nice to have a config option to change the default inheritance.

This is also an opportunity to thank you for this awesome gem! Kevyn

kevynlebouille avatar Mar 29 '21 19:03 kevynlebouille

Hey @kevynlebouille, thanks for the PR (and sorry for the long delay)! Can you explain more about the use case?

ankane avatar Feb 21 '22 01:02 ankane

Hi @ankane , with a multi-tenant context, I need to set a Current attribute while indexing.

Long story short, below my code sample from my initializer:

module Searchkick
  module TenantJobDecorator
    def self.prepended(base)
      base.attr_accessor :current_project

      base.around_perform do |job, block|
        Current.set(project: job.current_project || Current.project, &block)
      end
    end

    def serialize
      super.merge('project_id' => Current.project&.id)
    end

    def deserialize(job_data)
      super
      self.current_project = Project.find_by(id: job_data['project_id'])
    end
  end
end

Searchkick::BulkReindexJob.prepend Searchkick::TenantJobDecorator
Searchkick::ProcessBatchJob.prepend Searchkick::TenantJobDecorator
Searchkick::ProcessQueueJob.prepend Searchkick::TenantJobDecorator
Searchkick::ReindexV2Job.prepend Searchkick::TenantJobDecorator

Cheers, Kevyn

kevynlebouille avatar Feb 21 '22 08:02 kevynlebouille

I also find it very useful when using sidekiq with sidekiq_options that are not defined in ActiveJob

sasharevzin avatar Sep 12 '22 13:09 sasharevzin

This would be very useful for us as well 👍

Our use case is that we have statistics tracking and retry logic defined in our ApplicationJob. Currently we have to open all the job classes and monkey patch them with the same logic (and keep track of changes regarding new jobs in this project)

marcusmalmberg avatar Jul 03 '23 14:07 marcusmalmberg

Hi @ankane , it's the come back of my PR (freshly rebased). If you don't want to merged that PR, could you explain quickly why ? Thx

kevynlebouille avatar Mar 10 '24 19:03 kevynlebouille