Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parent_job attr to change inheritance af jobs #1478

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kevynlebouille
Copy link

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

@ankane
Copy link
Owner

ankane commented Feb 21, 2022

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

@kevynlebouille
Copy link
Author

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

@sasharevzin
Copy link

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

@marcusmalmberg
Copy link

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)

# Conflicts:
#	lib/searchkick.rb
#	lib/searchkick/reindex_v2_job.rb
@kevynlebouille
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants