ruby-on-railssidekiq

Rails 6 how to check if sidekiq job is running


In my Rails 6 API only app I've got FetchAllProductsWorker background job which takes around 1h30m.

module Imports
  class FetchAllProductsWorker
    include Sidekiq::Worker
    sidekiq_options queue: 'imports_fetch_all'

    def perform
      # do some things
    end
  end
end

During this time the frontend app sends requests to the endpoint on BE which checks if the job is still running. I need to send true/false of that process. According to the docs there is a scan method - https://github.com/mperham/sidekiq/wiki/API#scan but none of these works for me even when worker is up and running:

# endpoint method to check sidekiq status
def status
  ss = Sidekiq::ScheduledSet.new

  render json: ss.scan('FetchAllProductsWorker') { |job| job.present? }
end

The console shows me:

> ss.scan("\"class\":\"FetchAllProductsWorker\"") {|job| job }
=> nil
> ss.scan("FetchAllProductsWorker") { |job| job }
=> nil

How to check if particular sidekiq process is not finished?


Solution

  • Maybe this will be useful for someone. Sidekiq provides programmatic access to the current active worker using Sidekiq::Workers https://github.com/mperham/sidekiq/wiki/API#workers

    So based on that we could do something like:

    active_workers = Sidekiq::Workers.new.map do |_process_id, _thread_id, work|
      work
    end
    
    active_workers.select do |worker|
      worker['queue'] == 'imports_fetch_all'
    end.present?