Answer the question
In order to leave comments, you need to log in
What is the correct way to apply delayed_job to create recurring events?
Good afternoon, ladies and gentlemen. I'm sorry for the possibly stupid question, but the understanding does not come.
There is a task: you need to create events in the calendar, depending on the model attribute (: repeat) the event can be daily, weekly, monthly, annual. In each specific case, a set of forward events is created (for each day, month, week, year). Since the creation of 365 events takes a long time, the process is put into the background (thread). Here is my create method:
def create
@event = current_user.events.create(event_params)
Thread.new do
case @event.every
when 1
for i in 1..365
@event = current_user.events.create(event_params)
@event.update_attributes( start_at: @event.start_at + i.day )
@event.update_attributes( end_at: @event.end_at + i.day )
end
when 2
for i in 1..53
@event = current_user.events.create(event_params)
@event.update_attributes( start_at: @event.start_at + i.week )
@event.update_attributes( end_at: @event.end_at + i.week )
end
when 3
for i in 1..11
@event = current_user.events.create(event_params)
@event.update_attributes( start_at: @event.start_at + i.month )
@event.update_attributes( end_at: @event.end_at + i.month )
end
when 4
for i in 1..10
@event = current_user.events.create(event_params)
@event.update_attributes( start_at: @event.start_at + i.year )
@event.update_attributes( end_at: @event.end_at + i.year )
end
end
end
end
Answer the question
In order to leave comments, you need to log in
If I were you, I would not plan all events in advance, but plan the next event. And after its execution, he planned the following, etc. Now everything is hardcoded in your code, for example, a daily event will stop executing after a year, and an annual event after 10 years.
In total, you need to
1) Keep a record somewhere that task X has been scheduled since date Y with a frequency of Z
2) Keep a record of the event execution history from which you can find out when task X was last executed
3) Background event scheduling task, which will be executed for example 1 time per hour and will read the data from paragraphs 1-2 and create the next approaching event.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question