rufus-scheduler 使用 puma 只执行一次 rake 任务

rufus-scheduler executes rake task only once with puma

我似乎有一个与 非常相似的问题,但我不太明白建议的重构,我们使用 puma。 google 搜索产生了一些非常古老的匹配项。

我正在尝试使用 rufus-scheduler 执行 Rake 任务。它执行一次,然后不再执行。

我的调度程序文件看起来像

require 'rufus-scheduler'
require 'rake'

Rails.app_class.load_tasks
scheduler = Rufus::Scheduler.singleton

scheduler.every '2m' do
# scheduler.cron('0 08 * * 6') do
  begin
    Rake::Task['load_organizations'].invoke
  rescue Exception => e
    Rails.logger.error 'ERROR in load_organizations'
    Rails.logger.error e.inspect
    Rails.logger.error e.backtrace
  end
end

我的日志是这样开始的:

bash -c "RBENV_VERSION=2.5.5 /usr/local/bin/rbenv exec ruby /Users/.../bin/rails server -b 0.0.0.0 -p 3000 -e development"
=> Booting Puma
=> Rails 5.2.4 application starting in development 
=> Run `rails server -h` for more startup options
* Starting metrics server on tcp://0.0.0.0:3030
Puma starting in single mode...
* Version 3.12.2 (ruby 2.5.5-p157), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

Load organizations from github ### the task is executed once

不是puma专家,这是配置:

# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
threads threads_count, threads_count

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port        ENV.fetch("PORT") { 3000 }

# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }

# Specifies the `pidfile` that Puma will use.
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }

# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked webserver processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
# workers ENV.fetch("WEB_CONCURRENCY") { 2 }

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory.
#
# preload_app!

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart
plugin :metrics

根据措辞,我尝试了 scheduler = Rufus::Scheduler.singleton(:max_work_threads => 5) 但没有任何效果。此外,尝试添加 scheduler.join,但这会使启动挂起并仅打印

bash -c "RBENV_VERSION=2.5.5 /usr/local/bin/rbenv exec ruby /Users/.../bin/rails server -b 0.0.0.0 -p 3000 -e development"
=> Booting Puma
=> Rails 5.2.4 application starting in development 
=> Run `rails server -h` for more startup options

美洲狮 3.12.2 rufus-scheduler-3.6.0 rails-5.2.4 ruby-2.5.5-p157 macOS 10.14.6

我是大多数你认为是“废话”的作者。我会通过回答你的问题来增加这个冗长的废话。

事实上,我通过创建一个更简单的项目版本来仔细检查。在

https://github.com/jmettraux/sof63386937

您遇到的不是 rufus-scheduler 问题。只是你误解了Rake。

Rake合作方式:

scheduler.every '2m' do
# scheduler.cron('0 08 * * 6') do
  begin
    Rake::Task['load_organizations'].invoke
    Rake::Task['load_organizations'].reenable # <------------ ADD THIS
  rescue Exception => e
    Rails.logger.error 'ERROR in load_organizations'
    Rails.logger.error e.inspect
    Rails.logger.error e.backtrace
  end
end

阅读本文可能有所帮助:

https://til.hashrocket.com/posts/4897ed42b7-invoking-rake-tasks-multiple-times

尽情享受吧。