安装
gem install rufus-scheduler
ruby
#!/usr/bin/env ruby
require 'rubygems'
require 'rufus-scheduler'
scheduler = Rufus::Scheduler.new
scheduler.every '3s' do
puts 'Hello... Rufus'
end
scheduler.join
rails
Gemfile里添加
gem "rufus-scheduler" # 定时器
执行命令
bundle install
新建文件
config/initializers/task.rb
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development'
require File.expand_path(File.dirname(__FILE__) + "/../environment")
require 'rubygems'
require 'rufus/scheduler'
scheduler = Rufus::Scheduler.singleton
scheduler.every("2s") do
puts Time.now
SendIpInfoWorker.perform_async(true)
end
scheduler.join
https://www.jianshu.com/p/14dc7797d5a8
https://github.com/jmettraux/rufus-scheduler