5

Is it possible to snipe or cancel specific Sidekiq workers/running jobs - effectively invoking an exception or something into the worker thread to terminate it.

I have some fairly simple background ruby (MRI 1.9.3) jobs under Sidekiq (latest) that run fine and are dependent on external systems. The external systems can take varying amounts of time during which the worker must remain available.

I think I can use Sidekiq's API to get to the appropriate worker - but I don't see any 'terminate/cancel/quite/exit' methods in the docs - is this possible? Is this something other people have done?

Ps. I know I could use an async loop within the workers job to trap relevant signals and shut itself down ..but that will complicate things a bit due to the nature of the external systems.

Vic
  • 53
  • 3

1 Answers1

0

Async loop is the best way to do it as sidekiq has no way to terminate running job.

def perform
  main_thread = Thread.new do
    ActiveRecord::Base.connection_pool.with_connection do
      begin
        # ...
      ensure
        $redis.set some_thread_key, 1
      end
    end
  end

  watcher_thread = Thread.new do
    ActiveRecord::Base.connection_pool.with_connection do
      until $redis.del(some_thread_key) == 1 do
        sleep 1
      end
      main_thread.kill
      until !!main_thread.status == false do
        sleep 0.1
      end
    end
  end

  [main_thread, watcher_thread].each(&:join)
end
aratak
  • 364
  • 2
  • 8