Rails 5.1, recurring events that user can create - ruby-on-rails

I'm looking for a solution so my users would be able to create recurring events. For example lets say that a user wants to add a post every month on a specific date. Every month on that date a new post would be automatically created with some settings the user will give (title, content etc...)
My app will be pushed on heroku after, I know that heroku handle cron jobs by himslef, is there any option or gem that exist to do that ?
I checked the whenever gem but it's not working with heroku.
EDIT : I bring more infos about what I'm looking for exactly. A user create a post, below this new form I'd like to add a recurring option with a recurrence to select. Every month, week, day. Once the post is created, if the post is recurrent, then the same post is created again according to the selection the user made. The post will be created again and again until the user update the post and stop the recurrence or delete the post.

You could give resque-delayed a try
https://github.com/elucid/resque-delayed/blob/master/README.md

In the current project I use rufus-scheduler
It can be configured to run a task like:
require 'rufus-scheduler'
scheduler = Rufus::Scheduler.new
scheduler.cron '5 0 * * *' do
# do something every day, five minutes after midnight
# (see "man 5 crontab" in your terminal)
end
You can use https://crontab.guru/ to check the cron scheduler expression.
OR
You can take a look on heroku scheduled jobs

Related

Execute a function when a time is equal to a certain value in Rails 6

I am building an online e-commerce store, and I am trying to use rails with action cable to update a product from being out of stock to in-stock at a certain date time e.g 12:00:00 2020-02-19.
The idea is as soon as the time is reached, I want to push a Websocket that the product is now available.
I have tried a few solutions such as:
Thread.new do
while true do
if **SOMETIME** == Time.now
ActionCable.server.broadcast "product_channel",content: "product-in-stock"
end
end
end
The main issue with this approach is that it creates another thread and makes rails unresponsive. Furthermore, if this value is set for say 1 week from now I do not want every user who queries the endpoint to create a brand-new thread running like this.
You have two option use sidekiq jobs or use whenever job gem
https://github.com/mperham/sidekiq/wiki/Scheduled-Jobs
Whenever allow you to set specific day and time, check the documentation for more info
https://github.com/javan/whenever

Rails: Simplest way of sending mail when X is true

So I've been looking for the simplest way to send an e-mail when X column of Payments table in the database is == 'condition'. Basically what I want is to add a payment and set a date like 6 months. When 6 months have passed I want to send the mail. I've seen many solutions like using Whenever cron jobs and others but I want to know the absolute simplest way (perhaps using Rails only without relying on outside source) to keep my application light and clean. I was thinking I could use the auto generated created_at to evaluate when x time has passed.
Since you have a column in your db for the time to send email, make it a datetime datatype and you can set the email date as soon as the event payment event is created. Then, you can have a rake task where,
range = Time.now.beginning_of_day..Time.now.end_of_day
Payment.where(your_datetime_custom_column: range).each do |payment|
payment.user.send_email
end
and you can run this task everyday from the scheduler.
The "easiest" way is to use Active Job in conjunction with a state machine:
EmailJob.set(wait: 6.months).perform_later(user.id) if user.X_changed?
The problem with this is that the queue will accumulate jobs since jobs don't get handled right away. This may lead to other performance issues since there are now more jobs to scan and they're taking up more memory.
Cron jobs are well suited for this kind of thing. Depending on your hosting platform, there may be various other ways to handle this; for example, Heroku has Heroku Scheduler.
There are likely other ways to schedule repeating tasks without cron, such as this SO answer.
edit: I did use a gem once called 'fist_of_fury', but it's not currently maintained and I'm not sure how it would perform in a production environment. Below are some snippets for how I used it in a rails project:
in Gemfile
gem 'fist_of_fury'
in config/initializers/fist_of_fury.rb
# Ensure the jobs run only in a web server.
if defined?(Rails::Server)
FistOfFury.attack! do
ObserveAllJob.recurs { minutely(1) }
end
end
in app/jobs/observe_all_job.rb
class ObserveAllJob
include SuckerPunch::Job
include FistOfFury::Recurrent
def perform
::Task.all.each(&:observe)
end
end

Multi timezone task scheduling in Rails (using whenever)

We're currently using 'whenever' to schedule jobs in a Rails project. The system is expanding to support users in multiple timezones (timezone is stored in User model) and we would like to send users an email at a specific time of THEIR day.
Any ideas on the best pattern to achieve this? I'm foreseeing 24 (or more - there are half timezones) 'whenever' tasks per email job each with a different timezone filter on the user table.
Any ideas for a cleaner solution? A better scheduler than whenever perhaps? Something that will create the 24/48 cronjobs and call a callback passing a timezone or a UTC offset? Something like that.
I am not familiar with 'whenever' but I had a similar challenge and used clockwork
My problem was that I had to run a process at midnight for each of my customers. And like you, my customers can be anywhere in the world so it had to be ran at midnight their time.
The process of running the job at midnight looked something like this:
TZInfo::Timezone.all_country_zone_identifiers.each do |zone|
every(1.day, {time_zone: zone}, at: '00:00', tz: "#{zone}") do |job_attr|
time_zone = job_attr[:time_zone]
YourJob.perform_later(time_zone)
end
end
The class of 'YourJob' will just find all my customers with that time_zone and perform a job.
Hope that helps.

How to create a report scheduler?

A user can choose (and continually edit) which days they want to receive certain reports:
3 report options
7 possible days (Monday - Sunday)
If a user can receive any of the 3 reports on any day of the week, or at the end of the month, what is the best way to store the data in the database?
User 1's report schedule = report 1 monday and tuesday, report 2 monday, report 3 at the end of every month
My first thought was to use a Schedule model and have boolean values but it soon got out of control:
Schedule model
attr_accessible :report_one_monday, :report_one_tuesday, :report_one_wednesday, :report_one_thursday, :report_one_friday, :report_one_saturday, :report_one_sunday
:report_two_monday, :report_two_tuesday, :report_two_wednesday, :report_two_thursday, :report_two_friday, :report_two_saturday, :report_two_sunday
:report_three_monday, :report_three_tuesday, :report_three_wednesday, :report_three_thursday, :report_three_friday, :report_three_saturday, :report_three_sunday
:report_one_end_of_month etc
I want to use heroku scheduler to run a rake task daily to see which reports it must send. The end of the month task would look something like:
task :send_reports => :environment do
if Date.today == Date.today.end_of_month
Business.where(report_one_end_of_month: true).each do |business|
MyMailer.send_month_end_report(business).deliver
end
end
end
Have a look at resque and resque-scheduler which is super set of refus scheduler.
We are using this tool to generate reports in background and email it to user from long time which will definitely solve your problem.
Resque is the redis based tool to run background jobs and resque-scheduler is of course scheduler, which can accept cron schedule to schedule the resque job in future.

scheduled email release

what will be the best method of releasing a scheduled email say 2 days before an event in RoR. the event date is going to be stored into the database, and i just want a reminder sent out 2 days prior to event.
you can try this
in your schedule.rb file
every 1.day do
trigger mailer
end
and in mailer method
def mail_setup
if Date.today == event_date - 2.days
mail setup
else
do nothing
end
end
You might have figured this alreay out by yourself. Well...anyway: In cases as the described I always use an external crontab job. In a certain interval (in your case e.g. each day) you dial into your application via
rails runner script.rb
or
rails runner -e class-method
The script or the method then scans the db for pending trigger dates and does whatever it should do, e.g. sending emails.
ps: Of course you have to take care of the correct environment for your rails calls.

Resources