Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
13
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

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

: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: 外部資料

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
13
Help us understand the problem. What are the problem?