【问题标题】:How to delay sidekiq jobs for 10 mins, and count them in the end?如何将sidekiq作业延迟10分钟,最后计算它们?
【发布时间】:2021-06-02 13:19:04
【问题描述】:

目前,每次模型属性更改时,我都会执行特定的 sidekiq 工作。

示例:我正在更改并保存以下每个属性:标题、描述和文本。所以现在将执行 3 个 sidekiq 作业。

title was changeddescription was changedtext was changed

如何设置 sidekiq 在第一次属性更改后等待 10 分钟,然后在 jobs.count > 1 时执行特定工作?

示例:我再次更改并保存以下每个属性:标题、描述和文本。现在在 10 分钟内它应该只执行 1 个带有类似消息的 sidekiq 作业。

3 attributes were updated

【问题讨论】:

    标签: ruby-on-rails ruby jobs sidekiq job-scheduling


    【解决方案1】:

    听起来你需要某种去抖动。 这是一个符合您条件的 sidekiq 去抖动。

    我从未尝试过,但这个 gem gem 'sidekiq-debouncer' 的自述文件完全描述了相同类型的流程。

    https://github.com/paladinsoftware/sidekiq-debouncer 安装这个 gem。

    将参数设置为要考虑去抖动的作业,例如类的主键。

    所以你需要的是这样的东西。

    class MyWorker
      include Sidekiq::Worker
      include Sidekiq::Debouncer
    
      sidekiq_options(
        debounce: {
          time: 10 * 60
        },
        by: -> (job_args) {
            job_args[0] # this is the ID you pass in the argument in `perform` method.
          }
      )
    
      def perform(id, attributes)
        # perform your action on that record.
        # and if you are using the class variable like I've explained below, just reset that.
    
      end
    end
    

    原来如此。

    您可以使用我在 perform 方法中提到的类变量之类的东西作为注释,其中包含有关已更新但未执行 sidekiq 作业的那些属性的信息。

    class Something
      @@unperformed_updated_attributes = {}
    
      def setup
        @@unperformed_updated_attributes[id].merge!(updated_attribute: new_value)
      end
    
      def reset
        @@unperformed_updated_attributes[id] = {}
      end
    end
    

    这是粗略的介绍。但你可以做这样的事情来实现你想要的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      • 2012-10-29
      • 2013-06-10
      • 1970-01-01
      • 2020-07-30
      • 2016-01-04
      • 2013-12-15
      相关资源
      最近更新 更多