【问题标题】:How do I separate workers into pools of jobs with delayed job + heroku?如何将工作人员分为工作延迟 + heroku 的工作池?
【发布时间】:2012-04-10 03:57:18
【问题描述】:

我的环境是 rails 3.1,heroku 竹栈,delayed_job_active_record,(https://github.com/collectiveidea/delayed_job) 并尝试使用hirefire。 (https://github.com/meskyanichi/hirefire) - 我可以看到delayed_job 队列文档,但是如何在heroku 上应用它?

我有一组最高优先级的任务,这些任务每小时产生一次,我需要 3 名工人来完成,大约需要 26 分钟才能完成。在此期间,需要继续执行不太重要的后台任务,可能需要 1 名工作人员专门处理这些任务。

所以我将把优先任务块设置在一个命名队列中,例如'hourtask',然后为其他所有内容命名一个队列 'everythingelse' :)

问题是,如何将 heroku 工作人员专用于特定队列?根据文档,它与环境变量有关吗?它说:

# Set the --queue or --queues option to work from a particular queue.
$ RAILS_ENV=production script/delayed_job --queue=tracking start
$ RAILS_ENV=production script/delayed_job --queues=mailers,tasks start

但我对 heroku 设置不够熟悉,无法弄清楚如何将其应用于我的 heroku 生产环境?

【问题讨论】:

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


    【解决方案1】:

    在延迟作业 3 的自述文件中:

    DJ 3 引入了 Resque 风格的命名队列,同时仍保留 DJ 风格的优先级。目标是提供一个系统,用于对由不同的工作人员池工作的任务进行分组,这些工作人员可以单独扩展和控制。

    可以通过设置队列选项将作业分配给队列:

    object.delay(:queue => 'tracking').method
    
    Delayed::Job.enqueue job, :queue => 'tracking'
    
    handle_asynchronously :tweet_later, :queue => 'tweets'
    

    script/delayed_job 可用于管理将开始工作的后台进程。

    为此,请将 gem "daemons" 添加到您的 Gemfile 并确保您已运行 rails generate delayed_job

    然后您可以执行以下操作:

    $ RAILS_ENV=production script/delayed_job start
    $ RAILS_ENV=production script/delayed_job stop
    
    # Runs two workers in separate processes.
    $ RAILS_ENV=production script/delayed_job -n 2 start
    $ RAILS_ENV=production script/delayed_job stop
    
    # Set the --queue or --queues option to work from a particular queue.
    $ RAILS_ENV=production script/delayed_job --queue=tracking start
    $ RAILS_ENV=production script/delayed_job --queues=mailers,tasks start
    

    通过设置 QUEUE 或 QUEUES 环境变量来处理队列。

    QUEUE=tracking rake jobs:work
    QUEUES=mailers,tasks rake jobs:work
    

    在 Heroku 上,在您的 procfile 中,创建两个条目:

    worker1: QUEUE=tracking rake jobs:work
    worker2: QUEUES=mailers,tasks rake jobs:work
    

    并单独缩放它们:

    heroku ps:scale worker1=2 worker2=1 
    

    【讨论】:

    • 我的问题是,如果我在 heroku 上扩展到 4 个工作人员,我如何设置其中 3 个执行命名队列 a,其中 1 个专门处理命名队列 b。
    • “Procfile 和进程管理命令(heroku run 和 heroku scale)仅在 Cedar 堆栈上可用。” - 我在 Bamboo,不知道如何配置,但我不认为不能使用 procfile? devcenter.heroku.com/articles/procfile
    • 你需要在 Cedar 上才能完成这类事情(Rails 3.1 也建议这样做)。
    • 你的 procfile 中的 heroku 是什么意思?那是我项目中的文件吗?它的路径是什么?
    【解决方案2】:

    最初的问题也是关于 HireFire 的。目前 HireFire 不支持命名队列(see HireFire website),这使得自动缩放变得困难。

    【讨论】:

    猜你喜欢
    • 2014-04-03
    • 2018-02-09
    • 2012-04-16
    • 2011-11-14
    • 2011-08-29
    • 2014-09-21
    • 2017-11-17
    • 2011-08-05
    • 1970-01-01
    相关资源
    最近更新 更多