Usage Whenever a gem with Rails Active Job assigns a batch email job

I am trying to figure out how to use everything correctly, or if I even use it to work correctly. I created an assignment:

  class ScheduleSendNotificationsJob < ActiveJob::Base
  queue_as :notification_emails

  def perform(*args)
      user_ids = User.
                joins(:receipts).
                where(receipts: {is_read: false}).
                select('DISTINCT users.id').
                map(&:id)

      user_ids.each do |user_id|
          SendNotificationsJob.create(id: user_id)
          Rails.logger.info "Scheduled a job to send notifications to user #{user_id}"
        end  
    end
   end

      

I would like to do this work every day at the appointed time. Poll polls to see if there are any outstanding notifications, batches them, and then send them to users so that the user can get one bunch of notifications email instead of multiple single email notification emails. I tried to do this with Delayed Job, but it doesn't seem to be designed for scheduling something on a permanent basis. So now I'm trying to do this with every gem, but I can't figure out how to set it up correctly.

This is what I have in my config / schedule.rb file:

every 1.minute do
   runner ScheduleSendNotifications.create
end

      

When I run every time -i in the console, I get this:

Lorenzs-MacBook-Pro:Heartbeat-pods lorenzsell$ whenever -i
config/schedule.rb:13:in `block in initialize': uninitialized constant Whenever::JobList::ScheduleSendNotifications (NameError)

      

What am I doing wrong here? Should I be using something else? I am just learning ruby ​​and rails so any help is greatly appreciated. Thank.

+3


source to share


1 answer


Whenever gem takes a string as an argument to the runner function. Whenever the Rails framework doesn't actually load, it doesn't know about your ScheduleSendNotifications class.

The code below should set up the crontab correctly to do your job.



every 1.minute do
  runner "ScheduleSendNotifications.create"
end

      

In the project directory, run whenever -w

to set up the crontab file. Run crontab -l

to view the written crontab file. The system will execute your Rails runner every minute. From there, you may need to debug your code ScheduleSendNotifications.create

if something doesn't work.

+5


source







All Articles