3
0

More than 3 years have passed since last update.

Rakeタスクが、本当にWheneverを使ってcronで実行できているか試してみた。

Posted at

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のログを確認

スクリーンショット 2021-03-19 23.36.15.png

無事にRakeタスクが実行できていることが確認できました!

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0