Search code examples
ruby-on-railspumarufus-scheduler

rufus-scheduler executes rake task only once with puma


I seem to have a question very similar to Rufus-Scheduler Running Only Once, but I don't quite get the refactoring suggested and we use puma. A google search yielded some very old hits.

I am attempting to execute a Rake task with rufus-scheduler. It executes once and then no more.

My scheduler file looks like

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

My log starts like this:

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

Not an expert on puma, this is the config:

# 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

Based on the verbage, I tried scheduler = Rufus::Scheduler.singleton(:max_work_threads => 5) but that did nothing. Also, tried adding scheduler.join, but that made the startup hang and print only

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

puma 3.12.2 rufus-scheduler-3.6.0 rails-5.2.4 ruby-2.5.5-p157 macOS 10.14.6


Solution

  • I am the author of most of what you consider "verbiage". I will add to this mountain of verbiage by answering to your question.

    In fact, I double checked by creating a simpler version of your project. It's at

    https://github.com/jmettraux/sof63386937

    What you are encountering is not a rufus-scheduler issue. It's simply you misunderstanding Rake.

    The way to make Rake cooperate:

    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
    

    Reading this could help:

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

    Enjoy.