Rails cron job run

Rails offers a standardized interface for adding jobs to a queue and invoking a perform method. Solution 2 is functional for me as I utilize [specific tools]. Additionally, Solution 3 suggests an alternative approach where, instead of adding a cron job, the job itself can be programmed to repeat 1 day after its initial execution.

Question:

I am the owner of a multitenant application built using Rails. Within my application, I have a list of branches, each with a unique branch_name and branch_email. My goal is to set up a cronjob that will send a “good morning” email to every branch belonging to each tenant. For this purpose, I am utilizing

Postgres

as my SQL scheme for managing tenants.



Solution:


Utilize the Sidekiq gem for background worker functionality and employ the
Sidekiq Cron
gem for scheduling purposes.

Establish a Worker that will
send email
to every branch belonging to each tenant.

create a file

app/workers/good_morning_email_worker.rb

class GoodMorningEmailWorker
 include Sidekiq::Worker
 def perform
   # send your emails
 end
end

Generate a
cron job
using the

rails console

to execute the worker on a daily basis.

Sidekiq::Cron::Job.create(name: 'Email Worker - everyday @5am', cron: '0 5 * * *', class: 'GoodMorningEmailWorker')

Frequently Asked Questions