Sidekiq 工作条件 [英] Sidekiq work conditions

查看:31
本文介绍了Sidekiq 工作条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有 25 个工作人员,这些工作人员由不同用户的操作随机使用.

On my application I have 25 workers that are randomly used by different users' actions.

每个用户只允许同时进行一项(活动/忙碌)工作.

Only one simultaneous (active / busy) work is allowed by each user.

不能在控制器上被阻止,因为这个想法不是阻止动作创建.需要创建操作,但要保持一致,直到处理完同一用户的所有先前请求,然后才会为同一用户(重新)分配工作人员.

It can't be blocked on the controller because the idea is not to block the action creation. Actions need to be created but hold in line till all previos requests by the same user are processed and only after that a worker will be (re)-assigned for the same user.

如果在此期间另一个用户请求创建一个工作,如果剩余的 24 个工作人员中至少有一个可用,它应该立即开始.

If another user, in the meantime, requests a job creation, it should start instantly if at least one of the 24 remaining workers are available.

有什么方法可以查找队列行并使用其参数来构建处理条件吗?

Is there any way to look for the queue line and use its parameters to build the processing condition?

谢谢

推荐答案

您可以使用 Sidekiq Unique Jobs

You can use Sidekiq Unique Jobs

使用这种方法,只有 1 个具有相同参数的作业会同时存在.

with this approach only 1 job with the same params will be simultaneous present.

创建模型

class UserJobs
  belongs_to :user
end

class User
  has_many :user_jobs
end

class Worker
  sidekiq_options unique: true

  def perform params
    user = User.find(params[:id])
    user.user_jobs.order('id asc').each do |job|
      job.worker_class.constantize.new.perform(job.params)
      job.destroy
    end
  end
end

当您需要为用户运行任何作业时:

than when you need run any job for user do:

user.user_jobs.create worker_class: Klass, params: params
Worker.perform_async(user_id: user.id)

这篇关于Sidekiq 工作条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆