scheduled tasks in rails - ruby-on-rails

In my rails app I would like to notify the application users at a future time they select on the interface. I am using rufus-scheduler for annual notifications but I don't know how to use it at a specific time a user picks.
Thanks

Please go through on readme of gem:
require 'rufus-scheduler'
scheduler = Rufus::Scheduler.new
scheduler.at '24h' do
# do something at a given point in time
end
So what you can do, you can make a rake task that will run daily and it will check If there is any tomorrow date of action of user to run anything then It can create a delayed job for that time then It will execute

By reading the previous comments i feel you can do the following
1.Write a rake task to get all the notification to be sent on the next day and insert the same into a new table(its should contain notification_id and user_id,status) where you can track it .
2.Run the above rake task every day using rufus scheduler .
3.Write another rake task which will take the notification_id and user_id from the table and send notification via mail or your notification center and then update the status in the table,this also should be scheduled in Rufus scheduler
Hope the flow will be of your help,this approach will give you ability and flexibility on the notification sent and reports based on status can also be generated over notification

You can run a task at a specific time by using scheduler.at. Example:
scheduler.at '2030/12/12 23:30:00' do
# do something at a given point in time
end
See https://github.com/jmettraux/rufus-scheduler for the full details.

Related

How to create recurring notifications in Rails from an admin interface?

I would like to create a new function in my survey app that lets users set daily, weekly etc. notifications about the stats of their surveys. Also I would like to let users delete these notifications if they don't need them anymore, or their survey ends.
It seems fairly simple to create a Notification model, which stores the necessary information about the notification, but how do I schedule it to send out the e-mail with the stats daily, weekly ( whatever the user sets ) etc.
Sure I can use delayed_job or resque and reschedule everytime but that doesn't seem like an elegant solution.
Any ideas how to make this happen in the most elegant, and efficient way?
I think you will find whenever gem useful
here's an example syntax for it:
every 3.hours do
runner "MyModel.some_process"
rake "my:rake:task"
command "/usr/bin/my_great_command"
end
every 1.day, :at => '4:30 am' do
runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
end

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.

Rails: send mail after some time has passed

I have the following scenario:
1. Someone creates a task in Redmine
2. Certain things are done (or not done) with this task
3. In case 3 days have passed since task creation and it is not in the right state, send an email to interested persons.
My question is how do I schedule an email (or the task that will check if an email is required) in exactly three days with Rails? The only option I could think of so far is to create some rake task and run it every couple of minutes. Are there better options?
You've got a couple of options. You probably want to look at Active Job (Rails > 4.2). This lets you schedule a job to run after a specified period:
MyJob.set(wait_until: Time.now + 3.days).perform_later(record)
If you're on a version of rails prior to 4.2 then DelayedJob or Sidekiq or Resque could be used. Active Job is essentially a layer over the top of something like this anyway so a later migration to that shouldn't be too painful.
Alternatively if you don't need to check after exactly 3 days then you could sweep for tasks that need to have emails generated using cron (whenever is a good wrapper for this). You can sweep as often as you want, although every few minutes is probably excessive, and it means you won't have to set up a queueing back end on your server.
It does mean that you'll have to find the tasks that need emails generated for them whereas with the queuing system you'll know exactly which task you're dealing with already. However, it seems like plenty of those tasks won't need an email anyway so it might be more efficient to actively look for the ones that do.
As an alternative you can use https://github.com/resque/resque and https://github.com/zapnap/resque_mailer
Hope this helps.
You can use Whenever to schedule jobs/tasks/method calls ..almost anything
JUST add the gem ...run bundle install..add your code ....update the schedule.rb
for example:
##in schedule.rb
every 5.minutes do
p "============Running rake task Only"
rake "events:notify_user"
end
every 2.hours do
p "============Running rake task and local script as well as calling a method from your code"
command "/usr/bin/some_great_command"
runner "MyModel.some_method"
rake "some:great:rake:task"
end
You can also watch the Railscasts

Best way to manage gem whenever for sending emails

In my application there are posts which are going to expire in certain time of a day.
After the expiration the customer needs to notify through emails.
I have used Rails cron whenever for in each 1 minute and it makes expire the post at the specified time and sends the email to customers.
Schedule.rb
every 1.minutes do
rake "ad_has_expired_task"
end
Will be this a better way?
Any help is appreciated.
Thanks
For small app, this way is good, but for a bigger app, that may keep cpu busy.
Here is a solution from my site.
gem sidekiq
gem whenever
in scheduler.rb
every 1.day do
rake "scan_expired_task"
end
in rake taks scan_expired_task.rake
if post.will_expire_today
MyMailerWoker.perform_async(related_user_ids, post.expired_at-Time.now)
end
Then you write your mailer program in your MyMailerWoker
The codes above can't be executed directly, you need to customize it by your business.
Hope it helps.
reference:
https://github.com/mperham/sidekiq/wiki/Getting-Started

How can I delete an model entry at a given time (Rails)?

I am trying to create the functionality in my app where a given entry in the database is set to delete at a certain time. I am new to rails an I am unsure how I can achieve this.
For example, once the expired time of an entry has been passed I want it to be deleted automatically. Any hints or ideas how this can be achieved? Thanks again.
You can run a rake task periodically (like every hour or every night). This job will check posts and delete expired ones.
You can schedule rake tasks using whenever gem, for example.
every 3.hours do
rake "jobs:clear_stale"
end
Background job is what will solve your problem. Resque and Sidekiq are two awesome options on background job. You can keep scheduler that runs in specific interval to check if entry has expired and if yes, delete the entry. Here is the railcasts on resque and [this one]. Whenever is also an option but the other two mentioned above are still better.

Resources