Rake任务在本地开发环境中运行,但不在生产服务器上运行

时间:2019-07-24 10:17:51

标签: ruby-on-rails-4 cron scheduled-tasks capistrano whenever

在运行的应用程序中,每小时都有一个额外的调度程序,用于更新配置文件表中的GPS状态。我使用任务来运行调度程序,在本地开发中运行rake任务时,数据库被更新,但是在部署rake任务后,数据库不被更新。我的代码有问题吗?

该应用程序规范会在Capistrano中使用Rails 4,gem。

profile.rb

def self.update_profiles
  @profile_short = Heartrate.select('Date_Format(TIMEDIFF(now(), MAX(heartrates.start_time)), "%H") selisih_date, profiles.id, MAX(heartrates.start_time) start_time').joins(:profile).where('heartrates.start_time BETWEEN ? AND ?', DateTime.now.end_of_day-1, DateTime.now.end_of_day).where('profiles.gps_status = 1').group(:profile_id).order('profiles.id ASC')
    if @profile_short.present?
    @profile_short.each do |profile|
      if profile.selisih_date.to_i < 1
        cek_safety =  SafetyAlert.select('Date_Format(TIMEDIFF(now(), created_at), "%H") selisih_date_safety').where(profile_id: profile.id).where('created_at BETWEEN ? AND ?', profile.start_time, DateTime.now.end_of_day).last
        if cek_safety.present?
        else
          if profile.selisih_date.to_i >= 1
            Profile.where(id: profile.profile_id).update_all(gps_status: 0)
          end
        end
      else
        cek_safety =  SafetyAlert.select('Date_Format(TIMEDIFF(now(), created_at), "%H") selisih_date_safety').where(profile_id: profile.id).where('created_at BETWEEN ? AND ?', profile.start_time, DateTime.now.end_of_day).last
        if cek_safety.present?
          if cek_safety.selisih_date_safety.to_i >= 1
            Profile.where(id: profile.id).update_all(gps_status: 0)
          end
        else
          Profile.where(id: profile.id).update_all(gps_status: 0)
        end
      end
    end
  end
end

schedule.rb

every :hour do
  rake "profile_update:inactive"
end

profile_update.rake

namespace :profile_update do
  desc "Update gps status profiles"
  task inactive: :environment do
    Profile.update_profiles
  end
end

0 个答案:

没有答案