Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cron jobs processing in async mode #1196

Closed
aliaksandrb opened this issue Dec 19, 2023 · 2 comments
Closed

Cron jobs processing in async mode #1196

aliaksandrb opened this issue Dec 19, 2023 · 2 comments

Comments

@aliaksandrb
Copy link

Hi! Thanks for the great library! I'm trying to setup it, but seems like missing some essential configuration detail, maybe you can help.

I have a configuration to setup a cron job that runs every minute. The configuration block for GoodJob looks like this:

  config.active_job.queue_adapter = :good_job
  config.active_job.queue_name_prefix = "#{Rails.env}-api"
  config.active_job.queue_name_delimiter = '-'

  config.good_job.enable_cron = true
  config.good_job.preserve_job_records = true
  config.good_job.retry_on_unhandled_error = false
  config.good_job.on_thread_error = -> (exc) { Rails.logger.error exc }
  config.good_job.execution_mode = :async
  config.good_job.queues = '*'
  config.good_job.max_threads = 5
  config.good_job.poll_interval = 30    # seconds
  config.good_job.shutdown_timeout = 25 # seconds
  config.good_job.enable_cron = true
  config.good_job.cron = {
    alert_emails: {
      cron: '*/1 * * * *',
      class: 'AlertEmailsJob'
    }
  }
  config.good_job.dashboard_default_locale = :en

After server restart, nothing happens, the jobs are not scheduled.
Though, if I start an external process like bundle exec good_job start it works just fine and executes the schedule, so configuration seems to be valid.

My webserver is Puma, but there is not WEB_CONCURENCY enabled, and I'm still testing on dev.

Is it expected that in such setup we should cat GoodJob.restart explicitly somewhere? As I didn't find anything regarding this in README.

@aliaksandrb
Copy link
Author

Oh, I think I've found the reason. Good job starts cron processing only after at least one request is served by Puma.
Until that it does nothing, and it's reproducible every time. Is it expected behavior?

Default Puma config has this, so I was assuming Good Job should be preloaded right from the start.

Min threads: 5
Max threads: 5

@aliaksandrb
Copy link
Author

Looks like this is the reason:

Because of Rails deferred autoloading, jobs enqueued via the rails console may not begin executing on a separate server process until the Rails application is fully initialized by loading a web page once.

It happens not only for separate server process, but the main as well though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

1 participant