-->

Resque: calling #perform_later from inside another

2019-07-06 16:48发布

问题:

I have an issue which is likely me doing something weird but I can't for the life of me figure it out:

I have a parent job that is supposed to kick off a bunch of child jobs, but those child jobs never execute.

Here's the parent job:

class TriggerJob < ApplicationJob
  def perform
    start_events
  end

  around_perform do |_, block|
    p 'TriggerJob before_perform'
    block.call
    p 'TriggerJob after_perform'
  end

  private

  def start_events
    events = Events.all

    events.each do |event|
      event.connectors.each do |connector|
        p "Stopping connector #{connector.id} for event #{event.id}"

        StopConnectorJob.perform_later(connector.id)
      end
    end
  end
end

And here's the child job:

class StopConnectorJob < ApplicationJob
  @queue = :default

  class StopConnectorFailed < StandardError; end

  rescue_from(StandardError) do |exception|
    p "ERROR! #{exception.message}"
    raise(StopConnectorFailed, exception.message)
  end

  def perform(connector_id)
    p 'STOPPING CHARGE'
    connector = Connector.find(connector_id)
    do_stuff_with_the_connector(connector)
  end

  around_enqueue do |job, block|
    p "StopConnectorJob before_enqueue: #{Resque.count_all_scheduled_jobs}"
    p job.arguments
    block.call
    p "StopConnectorJob after_enqueue: #{Resque.count_all_scheduled_jobs}"
  end

  around_perform do |job, block|
    p 'StopConnectorJob before_perform'
    p job.arguments
    block.call
    p 'StopConnectorJob after_perform'
  end

  private


  def do_stuff_with_the_connector(connector)
    # ...
  end
end

When I run the base job, this is what gets output:

"TriggerJob before_perform"
"Stopping connector 4 for event 8"
"StopConnectorJob before_enqueue: 0"
[3]
"StopConnectorJob after_enqueue: 0"
"TriggerJob after_perform"

Then nothing. No jobs queued when I look at the jobs web interface. No record of redis persisting it when monitoring with redis-cli monitor.

If I change from #perform_later to #perform_now it works. But I want this to not be blocking.

Any idea why my jobs aren't being run even though it seems like they should have been enqueued but I can't see that they are?