Search code examples
ruby-on-railssidekiq

How running second worker, during the first worker?


I have a table with two columns foo and bar, and I have a sidekiq worker who records objects in foo, as well as second worker who takes objects from foo converts them and put to bar.

Second worker starts after successful completion of first worker, but I want to run it after few second as start first daemon.That they work at the same time.

I trying use a sidekiq-superworker gem, and do this

Superworker.define(:MySuperworker) do
  FirstWorker do
    SecondWorker
  end
end

but second worker was start after full complete first worker, how to do this?


Solution

  • Jobs are asynchronous so you can't control exactly when they run but you can control when you create them:

    class FirstWorker
      def perform
        # create foo
        SecondWorker.perform_async(...)
        # do more work
      end
    end