作业正在运行但是sidekiq队列是空的

时间:2016-03-15 21:10:21

标签: ruby-on-rails-3 sidekiq

我试图找出如何使用sidekiq访问后台作业的状态。

要检查作业的状态,请检查队列的大小。

class PagesWorker
    include Sidekiq::Worker
    include Sidekiq::Status::Worker

    sidekiq_options queue: "high"

    def perform(ifile)
        system "rake db:..."

        # What do the following do? In what scenario would I need to set these?
        total 100
        at 5, "Almost done"
        store vino: 'veritas'
        vino = retrieve :vino
    end
end

class PagesController
    def submit
        job_id = SomeWorker.perform_async(file)
        flash[:notice] = "#{job_id} is being processed."
    end

    def finished
        queue = Sidekiq::Status.new('high')
        if queue.size == 0
           flash[:success] = "Job is finished."
        elsif queue.size > 0
           flash[:notice] = "Job is being processed."
        end
    end
end

从一开始就打印“作业已完成”(当作业仍在运行时),因为队列始终为零。具体来说,在myrailsapp / sidekiq页面中,我看到以下内容:

  • 每次提交作业时,已处理作业的数量都会增加一,这是正确的。
  • “高”队列在那里,但其大小始终为0,这是不正确的。
  • 队列下没有列出任何作业,这也是不正确的。

然而,正在处理作业并成功完成。这份工作没有出现在sidekiq页面的原因是,工作在不到一分钟内完成了吗?或者也许是因为工人经营耙子流程?

我还在终端中检查了以下内容,同时进程正在运行:

Sidekiq::Status::complete? job_id #returns false
Sidekiq::Status::status(job_id) #returns nothing
Sidekiq::Status::get_all job_id #returns {"update_time"=>"1458063970", "total"=>"100", "at"=>"5", "message"=>"Almost done"}, and after a while returns {}

总而言之,我的队列为空,我的作业列表为空,但我的作业正在运行。这怎么可能?我该怎么做才能跟踪我的工作状态?

修改

我使用作业状态来检查它是否已完成。

控制器:

def finished
    job_id = params[:job_id]
    if Sidekiq::Status::complete? job_id == true
       flash[:notice] = "Job is finished."
    elsif Sidekiq::Status::complete? job_id == false
       flash[:notice] = "Job is being processed."
    end
end

形式:

=form_tag({action: :submit},multipart: true) do # submit is my controller method as defined above
    = file_field_tag 'file'
    = submit_tag 'submit'

=flash[:notice] # defined above as "#{job_id} is being processed."
-job_id = flash[:notice].split(" ")[0]
=form_tag({action: :finished}) do
    = hidden_field_tag 'job_id', job_id
    = submit_tag 'check'

路线:

 resources :pages do
     collection do
        post 'submit' 
        post 'finished'
     end
 end

但是Sidekiq :: Status ::完成了吗? job_id永远不会变为真。我正在拖尾日志文件以查看进程何时完成,然后单击检查按钮但仍然得到它仍在处理中。为什么呢?

1 个答案:

答案 0 :(得分:0)

据我了解,您使用的是sidekiq-status gem,您可以使用以下方式

https://github.com/utgarda/sidekiq-status#retrieving-status

如果您只是想知道作业是否排队,那么您可以停止运行sidekiq进程并使用sidekiq客户端发送作业,然后您可以在那里列出redis-cli中的队列

$ redis-cli
> select 0 # (or whichever namespace Sidekiq is using)
> keys * # (just to get an idea of what you're working with)
> smembers queues
> lrange queues:app_queue 0 -1
> lrem queues:app_queue -1 "payload"

并查看作业是否排队,再次启动sidekiq进程后,您会看到正在处理的作业,您也可以输入perform方法,并且也会打印在sidekiq的日志中。