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
Related
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.
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
Im on Rails 4, I'm creating a listing/rental site where people can list things and then other people can rent them. I'm using Stripe to handle all my payments, and I have a form set up that gets the users credit card and makes them a customer when they request to book a rental. After that, the owner of the rental can view the request and confirm or deny it. If they confirm it, the user renting gets their card charged and their money goes into holding.
When a user requests a booking, they choose a pick-up and drop-off date. I would like to have an action that calls a payout from stripe to the listings owner 24 hours after the pick up date. I am not sure how to go about this, so any suggestions are great! Of course if anyone knows of any tutorials implementing such a thing that would be awesome :).
Thanks.
Couple of things you can do
delayed_job: requires a database and a running process to run scheduled jobs; You can use it on heroku as shown here
resque-scheduler: requires redis and resque and a running process to run scheduled jobs. You can use it on heroku as shown here. Use resque-web and resque-cleaner to check and handle failed jobs.
whenever: requires access to cron jobs and your own script to be setup to run every hour or every few minutes to then pickup listings that need to be processed and then process them away. You'll need to work out a good error reporting system. Doesn't run on heroku
heroku scheduler: that is all managed through heroku but essentially gives you the same capabilities as whenever.
Resque would probably be my choice, but you should know better about your domain.
Install the whenever gem and documentation is available here: https://github.com/javan/whenever
Then in config/scheduler.rb file mention your function name and defined in the model or as per requirement. It will be behave like CRON job but major difference is that it can run application internal function as well.
There are many ways of doing this.
All of which involve some sort of data store that is checked every X minutes. Within this data store you can usually set a run on time.
Checkout:
https://github.com/collectiveidea/delayed_job
or
https://github.com/mperham/sidekiq
In my rails application, users can subscribe to some events. I'd like to send a mail to the user 24 hours before the begining of the the event.
I'm not sure how to implement that. A rake task ? Is there some gems or some best practices to follow ?
Thanks!
EDIT:
I guess that my cron must be run every minute to get each event that starts in 24 hours. But if the cron stops, or it takes more than a minute, I can't skip some emails. In the other way, how can I be sure I'm not sending the same email twice ?
Check out:
http://railscasts.com/episodes/206-action-mailer-in-rails-3
for sending mail and
http://railscasts.com/episodes/164-cron-in-ruby-revised
for using whenever to set up a cron to call your mailer
EDIT: Just saw your comment. In that case I would look at using something like resque-scheduler or the equivalent with sidekiq:
https://github.com/bvandenbos/resque-scheduler
then you can do things like this:
Resque.enqueue_at(24.hours.from_now, SendFollowUpEmail, :user_id => current_user.id)
sidekiq example:
FollowUpEmailer.perform_in(24.hours.from_now, current_user.id)
https://github.com/mperham/sidekiq/pull/257
This will help you to solve automatic mailing system in scheduled time!
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.