Rails. Delayed_job works two hours apart

1

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).

    
asked by Jorge Vela Plaza 20.07.2017 в 19:22
source

1 answer

1

As you mention, the problem is due to the difference between your time zone and the one you have configured in your application, both Rails and ActiveRecord .

To solve this you must specify your time zone (in both cases), adding / modifying these lines in the config / application.rb file:

config.time_zone = "Mexico City"
config.active_record.default_timezone = :local

In the example I used Mexico City as the time zone (which is my local time), so replace that zone with the one corresponding to your location; to find it you can execute the following command:

$ rake time:zones:all

Important : You must restart the Rails server after modifying application.rb for the changes to take effect.

    
answered by 20.07.2017 / 19:40
source