我用Rufus-Scheduler DaemonKit守护了一个 Ruby 调度程序脚本(使用 Rufus),我试图捕获 TERM 或 INT 信号,让应用程序在退出前尝试保存状态。
DaemonKit 有自己的 trap_state(私有)方法,它在守护程序脚本之前捕获信号,所以即使我有这个块,它也没有做太多。
DaemonKit::Application.running! do |config|
surprise = Surprise.new(interval, frequency, false)
surprise.start
config.trap( 'SIGINT' ) do #tried INT and TERM as well
puts 'Exiting'
surprise.stop
File.delete($lock)
end
end
作为副作用(可能是我的实现中的错误?)在 sigterm 之后 .rufus 锁定文件仍然存在
现在 ctrl-c 上的行为是这样的
[daemon-kit]: DaemonKit (0.3.1) booted, now running surprise
log writing failed. can't be called from trap context
[daemon-kit]: Running signal traps for INT
log writing failed. can't be called from trap context
[daemon-kit]: Running shutdown hooks
log writing failed. can't be called from trap context
[daemon-kit]: Shutting down surprise
start 方法是一个非常简单的时间表
def start
@scheduler = Rufus::Scheduler.new(:lockfile => $lock)
@scheduler.every '1d', :first_at => @first, :overlap => false do |job|
... # some work
end
@scheduler.join
end
def stop
# save state
@scheduler.shutdown
end