rufus-scheduler 定时任务示例

邹高峻
2023-12-01

这个东东有点类似于 flask 的 apscheduler,Linux 下的 crontab ,要用的话需要先安装 rufus-scheduler 依赖。 

gem install rufus-scheduler

 然后写好定时任务(如果要直接运行脚本的话,记得最后要加上 scheduler.join 才会生效)

require 'rubygems'
require 'rufus/scheduler'

scheduler = Rufus::Scheduler.new

scheduler.in '20m' do
  # after 20 minutes
  puts "hello world 1"
end

scheduler.at 'Thu Mar 26 07:31:43 +0900 2009' do
  # at the appointed time
  puts 'hello world 2'
end

scheduler.cron '0 22 * * 1-5' do
  # every day of the week at 22:00 (10pm)
  puts 'hello world 3'
end

scheduler.every '5m' do
  # every 5 minutes
  puts 'hello world 4'
end

scheduler.join

 

 类似资料: