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

Conflicting hooks between Resque and ActiveJob (e.g. before_enqueue) #1787

Open
hector opened this issue Jan 19, 2022 · 3 comments
Open

Conflicting hooks between Resque and ActiveJob (e.g. before_enqueue) #1787

hector opened this issue Jan 19, 2022 · 3 comments

Comments

@hector
Copy link

hector commented Jan 19, 2022

I believe Resque hooks are clashing with ActiveJob ones.

Let 's see an example with the before_enqueue hook.
In a Resque job you can define the before_enqueue method as a hook to be executed before enqueueing; if false is returned the enqueuing does not happen (docs):

class MyJob < ApplicationJob
  def before_enqueue
    # do something
  end
end

In ActiveJob, you can use the before_enqueue method to add a hook to be run before enqueueing (api docs):

class VideoProcessJob < ActiveJob::Base
  queue_as :default

  before_enqueue do |job|
    $statsd.increment "enqueue-video-job.try"
  end

  def perform(video_id)
    Video.find(video_id).process
  end
end

Because of this clash:

  1. Resque tries to run any before_enqueue method if it exists
  2. It does always exist because it is defined by ActiveJob
  3. Resque runs before_enqueue which makes no sense at all

The conflicting hooks are: after_enqueue, after_perform, around_enqueue, around_perform, before_enqueue, before_perform

@hector
Copy link
Author

hector commented Jan 19, 2022

I post the temporal workaround I am using:

# Workaround for this problem: https://github.com/resque/resque/issues/1787
# Modify conflicting hooks (Resque vs ActiveJob) so that Resque hooks end with and underscore and
# so we can still use them as `before_enqueue_whatever`
module Resque
  module Plugin
    def before_hooks(job)
      get_hook_names(job, 'before_perform_')
    end

    def around_hooks(job)
      get_hook_names(job, 'around_perform_')
    end

    def after_hooks(job)
      get_hook_names(job, 'after_perform_')
    end

    def after_enqueue_hooks(job)
      get_hook_names(job, 'after_enqueue_')
    end

    def before_enqueue_hooks(job)
      get_hook_names(job, 'before_enqueue_')
    end
  end
end

@iloveitaly
Copy link
Contributor

PRs to fix this are welcome!

@nvolker
Copy link

nvolker commented Sep 19, 2022

@iloveitaly, @hector I've put forward #1833 for discussion.

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

No branches or pull requests

3 participants