I am running a ruby on rails application, and as part of a certain process, i need to send a mail to a user after 10 minutes from a certain operation if a specific condition occurs.
I saw that a gem called rufus can be used to implement this ( https://github.com/jmettraux/rufus-scheduler/blob/two/README.rdoc ), and it looks great to just do something like :
scheduler.in '3s' do puts 'Hello... Rufus' end
from what i understand, it runs in a background in-process in-memory thread and fires the proc at a desired time. But, from what I understood, it is not persistent which creates a big problem for us, since we continuously deploy, and a deploy will kill the existing process thus killing the thread and the desired action to occur.
Can someone suggest a working approach for this, maybe another framework that supports persistence or maybe use an existing gem with some tweaks?
thanks a lot, Alex
Use sidekiq , it's basically a job queue and persists jobs into a Redis DB, also has a nice web interface, retries etc. etc.
For running scheduled jobs take a look at https://github.com/mperham/sidekiq/wiki/Scheduled-Jobs
YourAfterJobWorker.perform_in 10.minutes, user.id
The implementation of your worker would look like
# app/workers/your_after_job_worker.rb
class YourAfterJobWorker
include Sidekiq::Worker
def perform(user_id)
user = User.find(user_id)
TheAfterJobMailer.some_email(user).deliver if user.matches_your_condition?
end
end
If you don't want another operational dependency (Sidekiq requires Redis), there's always DelayedJob which works with a new table in your SQL environment (they support other backends now too), albeit less efficiently than Sidekiq/Redis.
The syntax is nearly identical to Sidekiq and other ActiveJob implementations.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.