Sidekiq - Scheduling emails using a Datetime object - ruby-on-rails

I'm creating a Todo list app in Rails 4 to learn more about running background jobs and cron. Currently I use Sidekiq to send task confirmation mails asynchronously EmailWorker.perform_in(5.minutes, #todo.id).
Now, I have a Schedule field in the task creation form and it's a DateTime field. I would like to know how to send the task reminder mail at that exact date and time. Do I have to calculate the days and hours from Time.now and covert them to minutes using a model method and pass in the value or is there an easier way to get this done?

I tried a couple of different ideas. Finally, I got it to work by converting the difference between Time.now and the scheduled time in minutes.

Related

Using sidekiq in rails, how can I send messages to a bunch of users, each of which has a unique time they want to receive it?

Hey I have a rails application and a bunch of users and I want to send them a message once per day at a preferred local time of the user's choice. (A message is a text, email, or chatbot notification.)
I believe I could add something to my User model that would allow for something to be performed every 24 hours at the preferred time but I'm not sure specifically how to implement that AND I also don't know how to remove these jobs from the queue if, for example, the user changes their preferred time or they want to disable messages all together.
Any thoughts on how I could do this?
Where is your application hosted?
You can have a cron task or a scheduler (Heroku) that runs every hour (or every 10 minutes). It would query in a users preferences table searching for users who want to receive the email at this moment (or in a range near this moment, such as the next 10 minutes).
time = Time.now
#users = User.include(:settings).where(“settings.receive_email_at between ? and ?”, time, time + 10.minutes)
You may need some changes to handle timezones and to avoid sending duplicate emails, but that’s just an idea.
It's simple. Build two models - One to store user and its time mapping (UserTimeMapping) and the second one to create a unique entry for each day when a message is sent to user (UserMessage).
Write a cron task which runs every 10 minutes and pulls all the records from UserTimeMapping which are to be executed for next 10 minutes and schedules a worker task for the exact time (say MessageTriggerWorker). The worker has to check in UserMessage table whether a record exists for the given user_id for today and if yes it has to return without performing any task. If that is not the case then it should send the message and create a record in the UserMessage table.

Ruby on Rails - Trigger an event automatically on specific DateTime

I want to trigger an event, specifically send out an email and update attributes on some models, on a specific DateTime (which is a column in one of my models).
I have searched around but have not really found any solutions to this.
Is there any way of achieving this?
You can use a queueing solution along with a scheduler.
Queueing solution: Resque with Redis backend
https://github.com/resque/resque
Scheduling solution : https://github.com/resque/resque-scheduler
Resque.enqueue_at(5.days.from_now, SomeJob)
You can schedule a job to run at a particular date. Combine this with the ability to setup a schedule.
Say you trigger the first job on Jan 1st and want it to run every 30 days.
You can use a queuing solution to send out email asynchronously : something like Resque with a Redis backend.

rails + sidekiq specific date and time job

How can we add job to sidekiq using rails to a specific date and time using db time, I want to send emails when an appointment time comes, so what will be the best approach to do that?
You might refer to the native Active Job API introduce in rails 4.2 (which come in front of Sidekiq).
You build job with the rails API, you specify in conf file that you use sidekiq, and rails will put the job in sidekiq.
Advantages : You can switch to another enqueuing gem, easier to delay job at a specific date, you can perform after_action, which are normally in the professional version of Sidekiq.
See the tutorial to learn more about it : http://edgeguides.rubyonrails.org/active_job_basics.html
A quick example :
# Enqueue a job to be performed tomorrow at noon.
MyJob.set(wait_until: Date.tomorrow.noon).perform_later(record)
Hope it help
Attach an appointment date (or even better a specific appointment notification datetime) to the appointment model.
Then have a background job queued every minute that checks for all the unsent notifications for close events (for instance events where the appointment notification is lower than 1 minute in the future and not sent yet) and for each appointment queue an email to be sent.
You may want to use sidekiq to queue the emails, and a cron system such as clockwork to enqueue a task on sidekiq to check every minute for notifications to be sent.

A way to create an auction-style timed trigger in Ruby on Rails

What gems/technology would be best suited for creating an auction style system where a certain action is run when a given duration of time has expired? (ie. sending an email message to users telling them the auction is over).
If you want real time notification I think resque_sceduler suites you. So you can schedule the task to send the email for the exact time the auction ends.
If there is no problem to wait some minutes or hours you can use cron jobs or something like that. There are many options actually.

Rails send email on schedule

I have an app that presents a calendar allowing you to create events that display on the calendar. One of the fields you set is the day the event starts. I would like to be able to send email reminders out when the day of the event arrives.
I'm new to rails so I'm not exactly sure what I need or where to start. I read the rails guide for ActionMailer and generated a mailer, I was assuming I would need one of those. I also read the rails guide for observers and thought maybe that might be a route to what I want to accomplish.
Let's say every day, 7 days a week at 9am central, a job (cron or rake task) should run that searches the 'start_at' column of every Event in the database (sqlite locally, Postegre with heroku in production) and if the start_date is equal to 'today?' then send the action mailer template to remind the user.
So what's the best way to (and with what tool) to build a job to examine that database and kick off emails every day? In the future I guess this would grow to reminders ahead of time.
Some pseudo-code:
def send_email
if self.start_at == Date.today #assuming self.start_at.today? works too.
// send email
end
Just not sure how to get that working.
You could have a Controller/Action that does it, then call the url from a cron job on a specified schedule using a command like cURL.

Resources