如何使用heroku中的一个worker和rails上的ruby正确运行多个任务

时间:2018-06-01 00:04:09

标签: ruby-on-rails ruby heroku scheduled-tasks rake

我试图同时在Heroku中运行许多(超过20个)任务,但我很确定我没有正确地执行它。我目前正在使用Standard-1X dynos 512 MB RAM。我非常确定无需升级heroku就可以实现我的目标。我的解决方案有点工作,但它占用了我很多的记忆,并没有同时运行所有任务。有人可以帮我提出更好的解决方案吗?

这是我的Rakefile:

Rails.application.load_tasks

task :update_exchanges_1 => :environment do
 until 2 < 1 do
  Rake::Task['exchange_markets_update:gdax'].execute
  Rake::Task['exchange_markets_update:gemini'].execute
  Rake::Task['exchange_markets_update:bitstamp'].execute
  Rake::Task['exchange_markets_update:bitflyer'].execute
  Rake::Task['exchange_markets_update:okex'].execute
  Rake::Task['exchange_markets_update:cex'].execute
  sleep(60.seconds)
 end
end

task :update_exchanges_2 => :environment do
  until 2 < 1 do
   Rake::Task['exchange_markets_update:bitfinex'].execute
   Rake::Task['exchange_markets_update:bittrex'].execute
   Rake::Task['exchange_markets_update:hitbtc'].execute
   Rake::Task['exchange_markets_update:poloniex'].execute
   Rake::Task['exchange_markets_update:kraken'].execute
   Rake::Task['exchange_markets_update:binance'].execute
   Rake::Task['exchange_markets_update:bithumb'].execute
   sleep(60.seconds)
 end
end

task :update_exchanges_3 => :environment do
  until 2 < 1 do
    Rake::Task['exchange_markets_update:gateio'].execute
    Rake::Task['exchange_markets_update:idex'].execute
    Rake::Task['exchange_markets_update:bitbank'].execute
    Rake::Task['exchange_markets_update:zb'].execute
    Rake::Task['exchange_markets_update:bibox'].execute
    Rake::Task['exchange_markets_update:ethfinex'].execute
    sleep(60.seconds)
  end
 end

基本上,每个任务都被无限调用,用于调用用于更新数据库的多个外部API。每个exchange_markets_update任务大约需要10-15秒。

这是我的Procfile(我使用foreman gem,所以我可以在一个进程中运行进程):

web: bundle exec rails server -p $PORT
worker: bundle exec foreman start -f Procfile.workers

这是我的Profile.workers:

dj_worker: bundle exec rake update_exchanges_1
dj_worker: bundle exec rake update_exchanges_2
dj_worker: bundle exec rake update_exchanges_3

同样重要的是要注意我的工人在一段时间后崩溃了。任何帮助将不胜感激!

0 个答案:

没有答案