简体   繁体   English

Rails Sidekiq队列大小不起作用

[英]Rails Sidekiq Queue size not working

I have a Rails app with 2 jobs (ImportCsvJob and ProcessCsvJob). 我有一个有2个工作的Rails应用程序(ImportCsvJob和ProcessCsvJob)。 So that I can visibly hint in the app that there are still jobs in the queue I have this helper method (inside application_helper.rb): 这样我就可以在应用程序中明显暗示队列中还有作业,我有此帮助程序方法(在application_helper.rb内部):

module ApplicationHelper
   def queued_job_count
        Sidekiq::Queue.new.size
    end
end

Then I use it on my index controller which is then passed to the view for processing and giving the visual hint in the app. 然后,在索引控制器上使用它,然后将其传递到视图以进行处理并在应用程序中提供可视提示。

  def index
    @still_have_jobs = !queued_job_count.zero?
  end

However, this works when I still had 1 background Job (ImportCsvJob), but when I added the (ProcessCsvJob) it does not work anymore. 但是,当我还有1个后台作业(ImportCsvJob)时,此方法起作用,但是当我添加(ProcessCsvJob)时,它不再起作用。

import_csv_job.rb import_csv_job.rb

require 'open-uri'

class ImportCsvJob < ActiveJob::Base
  queue_as :default

  def perform(csv_record)
    csv_record[:object_changes] = ApplicationController.helpers.generate_hash(csv_record[:object_changes])
    ObjectRecord.create(csv_record)
  end
end

process_csv_job.rb process_csv_job.rb

class ProcessCsvJob < ActiveJob::Base
  queue_as :default

  def perform(csv_path)
    csv_file = open(csv_path,'rb:UTF-8')

    options = {
        row_sep: :auto, col_sep: ",", 
        user_provided_headers: [:object_id, :object_type, :timestamp, :object_changes], 
        remove_empty_values: true, 
        headers_in_file: true
    }

    SmarterCSV.process(csv_file, options) do |array|
        ImportCsvJob.perform_later(array.first)
    end
  end

end

and lastly, in the model where this is called: 最后,在模型中将其称为:

ProcessCsvJob.perform_later(gdrive.uploaded_file_link)

When I try to debug in Rails console using Sidekiq::Queue.new.size, it still gives out 0. 当我尝试使用Sidekiq :: Queue.new.size在Rails控制台中调试时,它仍然给出0。

Running: 正在运行:

redis-server
bundle exec sidekiq

A job that is executing is not enqueued anymore. 正在执行的作业不再排队。 The Sidekiq process has already popped it off the queue and is executing it. Sidekiq进程已将其从队列中弹出并正在执行。 The queue is empty but the job is not finished yet. 队列为空,但作业尚未​​完成。

So, basically I added a monitoring for sidekiq using the web interface to see what was happening: 因此,基本上,我使用网络界面添加了对sidekiq的监视,以查看发生了什么:

在此处输入图片说明

And as I inspected, there were no enqueued tasks nor scheduled since most of the job is set to perform almost immediately (on parallel). 正如我检查的那样,没有排队的任务,也没有计划的时间,因为大多数工作都设置为几乎立即(并行)执行。

Thus here's my solution to know if the count of busy jobs: 因此,这是我的解决方案,以了解是否有繁忙的工作:

module ApplicationHelper
    def queued_job_count
        Sidekiq::ProcessSet.new.first['busy']
    end
end

and then on the index: 然后在索引上:

  def index
    @still_have_jobs = !queued_job_count.zero?
  end

it works! 有用! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM