15
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ジョブスケジューリングを行う rufus-scheduler gem を試す #ruby

Posted at

:gem: 概要

ジョブスケジューリングを行う rufus-scheduler gem を試しました

インストール

$ gem install rufus-scheduler

サンプル

サンプルコード: in

in.rb
require 'rufus-scheduler'

scheduler = Rufus::Scheduler.new
puts Time.now
scheduler.in '10s' do
  puts Time.now
  puts "10秒後に通知"
end

sleep(15)
puts Time.now
  • 出力確認
% ruby in.rb
2015-04-24 22:20:58 +0900
2015-04-24 22:21:08 +0900
10秒後に通知
2015-04-24 22:21:13 +0900

サンプルコード: at

at.rb
require 'rufus-scheduler'

scheduler = Rufus::Scheduler.new
puts Time.now
scheduler.at '2015/04/24 22:30:00' do
  puts Time.now
  puts "2015/04/24 22:30:00 に通知"
end

sleep(15)
puts Time.now
  • 出力確認
% ruby at.rb
2015-04-24 22:29:49 +0900
2015-04-24 22:30:00 +0900
2015/04/24 22:30:00 に通知
2015-04-24 22:30:04 +0900

サンプルコード: every

every.rb
require 'rufus-scheduler'

scheduler = Rufus::Scheduler.new
puts Time.now
scheduler.every '2s' do
  puts Time.now
  puts "2秒おきに通知"
end

sleep(10)
puts Time.now
  • 出力確認
% ruby every.rb
2015-04-24 22:27:04 +0900
2015-04-24 22:27:07 +0900
2秒おきに通知
2015-04-24 22:27:09 +0900
2秒おきに通知
2015-04-24 22:27:11 +0900
2秒おきに通知
2015-04-24 22:27:13 +0900
2秒おきに通知
2015-04-24 22:27:14 +0900

その他

cron 形式もサポート。
詳細は、 GitHub の README 参照。

rufus-scheduler GitHub

:books: 外部資料

15
13
1

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
15
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?