Rakeタスクを作成して、Wheneverでcronの実行スケジュールを定義したけど・・・これって本当に実行できてるの?と思ったので、簡単なタスクとスケジュールを作って実行しログを確認してみました。
Rakeタスクを編集
publish.rake
namespace :publish do
desc "指定した時刻からの経過時間を表示"
task test: :environment do
time_now = Time.current
time = Time.local(2021,3,19,21,00,00)
puts "#{((time_now - time)/60).to_i}分が経過しました"
end
end
今回はお試しなのでとりあえずシンプルな検証です。とはいえただ文字を出力させるだけじゃ味気ないので、ある時点の時刻 Time.local(2021,3,19,21,00,00) から、現在の時刻 Time.current までの経過時間を出力させる処理を記述します。時刻の差分は秒数で出力されるので、秒数を分数に直して整数化(.to_i)します。
スケジュールの編集
schedule.rb
require File.expand_path(File.dirname(__FILE__) + "/environment")
rails_env = ENV['RAILS_ENV'] || :development
set :environment, rails_env
set :output, "#{Rails.root}/log/cron.log"
every 1.minute do
rake "publish:test"
end
今回は、結果をすぐに確認したいので、1分毎にRakeタスクを走らせます。
Cronのログを確認
無事にRakeタスクが実行できていることが確認できました!