我使用Rufus-Scheduler守護了一個Ruby調度程序腳本(使用Rufus) DaemonKit並試圖捕獲TERM或INT信號讓應用程序嘗試在退出前保存狀態。Rufus-Scheduler,DaemonKit和陷阱
DaemonKit有它自己的trap_state(private)方法,它會在守護進程腳本之前捕獲信號,所以即使我有這個塊,它也沒有多大作用。
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
啓動方法是一個非常簡單的時間表
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
對不起,但rufus-scheduler 3.x中沒有陷阱(rufus-scheduler 2.x只有一個,但僅限於其特殊的SignalScheduler實現)。 你確定你的'File.delete($ lock)'行已經到達嗎?你確定它會成功,如果達到?在你的陷阱的末端放置一個'放置'退出'...狼擊劍。 – jmettraux 2014-10-09 21:15:32
@jmettraux呃我很笨,我把DaemonKit和Rufus混在一起。它是DaemonKit陷印TERM,我的不好 – blackbird 2014-10-10 01:56:35
請更新你的解釋。提前致謝! – jmettraux 2014-10-10 01:59:39