Search code examples
ruby-on-railsrubycronwheneverrails-activejob

Calling Active Job Using Whenever Gem


Here is an active job:

# app/jobs/upload_evidence_job.rb

class UploadEvidenceJob < ApplicationJob
  queue_as :default

  def perform(*args)
    UploadRequest.pull_messages
    ...
  end
end

Here is my whenever config file:

set :output, "log/cron_log.log"
env :PATH, ENV['PATH']

every 1.minutes do
  runner 'UploadEvidenceJob.perform_later'
end

I am running the following command to write in cron which is working fine.

$ whenever --update-crontab --set environment=development

The issue is the job is not getting called from the cron, whereas it is working from the rails console.

I think the active jobs are not getting loaded in case of cron, do I need to require it somewhere?


Solution

  • First make sure cron runner is working at all - in cron environment is different, because there's no shell session. For example - if you're using RVM - you need ruby binstubs and may be full path to bundle command. Then try manually running cron command that was generated (see crontab -l for user that you're running whenever under)

    Also spinning up rails every minute just to enqueue a job - is not very optimal, you can use sidekiq-scheduler (if you're using sidekiq) or other ruby scheduler to do so