I am implementing automation tasks to send emails at a specific time with this gem in the following way:
def create
@competition = Competition.new(competition_params)
if @competition.save
@competition.delay(run_at: @competition.startdate).start_competition
@competition.delay(run_at: @competition.deadline).finish_competition
.....
end
Both start_competition
and finish_competition
are two methods that send an email in notice that the competition starts and ends, respectively.
Well, everything is done correctly, but the submission is made two hours later than what is saved in startdate
as in deadline
.
I know that this happens through my time zone, which is +02:00
, so for example, if stardate
I put it, when creating a competition, at 09:50, the email will be sent at 11:50.
In the same way and reviewing this, I realized that when I create any object in the app, both created_at
and updated_up
are created with datetime +00:00
. That is to say, if I create a competition at today's 19:00
, created_at
will be equal to 2017-07-20 17:00:00
.
How can I fix this?
Example
I have on the one hand the following delayed_job
corresponding to the first of them (the one that executes start_competition
)
#<Delayed::Backend::ActiveRecord::Job
id: 35,
priority: 0,
attempts: 0,
handler: "--- !ruby/object:Delayed::PerformableMethod\nobject...",
last_error: nil,
run_at: "2017-07-20 08:30:00",
locked_at: nil,
failed_at: nil,
locked_by: nil,
queue: nil,
created_at: "2017-07-20 06:24:38",
updated_at: "2017-07-20 06:24:38">
As you can see, I programmed it to run at 08:30
, and the mail reached me at 10:30
. Also, create it at 08:24
and save created_at
as 06:24
.
On the other hand, the one corresponding to the second of them (the one that executes finish_competition
)
#<Delayed::Backend::ActiveRecord::Job
id: 36,
priority: 0,
attempts: 0,
handler: "--- !ruby/object:Delayed::PerformableMethod\nobject...",
last_error: nil,
run_at: "2017-07-20 08:31:00",
locked_at: nil,
failed_at: nil,
locked_by: nil,
queue: nil,
created_at: "2017-07-20 06:24:38",
updated_at: "2017-07-20 06:24:38">
As you can see, I programmed it to run at 08:31
, and the mail reached me at 10:31
. Also, create it at 08:24
and save created_at
as 06:24
(since it is created at the same time as the first one).