【问题标题】:Setting priority with ActiveJob when using Delayed::Job使用 Delayed::Job 时使用 ActiveJob 设置优先级
【发布时间】:2018-10-29 02:30:19
【问题描述】:

在使用 ActiveJob 排队作业时如何设置延迟作业优先级?

class GuestsCleanupJob < ApplicationJob
  queue_as :high_priority

  def perform(*guests)
    # Do something later
  end
end

【问题讨论】:

    标签: ruby-on-rails delayed-job rails-activejob


    【解决方案1】:

    定义一个定义优先级的实例方法是可行的,但是不允许我重载该值。 给定这个类

    class GuestsCleanupJob < ApplicationJob
      queue_as :high_priority
    
      def priority
        1
      end
    
      def perform(*guests)
        # Do something later
      end
    end
    

    如果我跑了

    GuestsCleanupJob.set(priority: 55).perform_later(user, lead) # priority set to 1 not 55
    

    它将优先级为 1 的作业排队,并忽略我通过的 55。

    这并没有为我的用例提供足够的控制,所以我做了。

    class GuestsCleanupJob < ApplicationJob
      queue_as :high_priority
    
      def default_priority
        1
      end
    
      def priority
        @priority || default_priority
      end
    
      def perform(*guests)
        # Do something later
      end
    end
    
    

    使用上面的代码,默认情况下优先级会设置为1,但我可以使用我的

    GuestsCleanupJob.set(priority: 55).perform_later(user, lead) # priority set to 55
    

    【讨论】:

    【解决方案2】:

    使用 Delayed::Worker.queue_attributes 的解决方案看起来不错,并且已记录在案,但对我不起作用... 无论 queue_attributes 中设置的队列优先级如何,所有作业的优先级=0。 这对我有用:

    class GuestsCleanupJob < ApplicationJob
      queue_as :high_priority
    
      def priority
        1
      end
    
      def perform(*guests)
        # Do something later
      end
    end
    

    【讨论】:

      【解决方案3】:

      我花了一些时间,但我在 Delayed::Job 文档中找到了这个方法:

      Delayed::Worker.queue_attributes = {
        default:       { priority: 11 },
        high_priority: { priority: 1 },
        low_priority:  { priority: 75 }
      }
      

      我已将此添加到我的初始化程序中,如果其他人遇到此问题,我只想分享!

      【讨论】:

        猜你喜欢
        • 2018-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-23
        • 2016-02-13
        • 1970-01-01
        • 2011-01-10
        • 2010-12-01
        相关资源
        最近更新 更多