class ProcessFiles
def processOnce()
sleep(3)
endend
require 'process_files'
s = ProcessFiles.new()
while true
s.processOnce
puts "Slept for #{sleep 10} seconds"
end
Alternatively here is the a sample daemon code taken from the demo plus I added a timestamp and a comment on close:
LOG_FILE = 'C:\\test.log'
require 'process_files'
begin
require 'win32/daemon'
include Win32
class DemoDaemon < Daemon
def service_main
my_service = ProcessFiles.new()
while running?
begin
my_service.processOnce
rescue Exception => err
File.open(LOG_FILE, "a"){ |f| f.puts "Service had a failure at #{Time.now} error=#{err} " }
sleep 10
end
sleep 3
File.open("c:\\test.log", "a"){ |f| f.puts "Service is running #{Time.now}" }
end
end
def service_stop
File.open("c:\\test.log", "a"){ |f| f.puts "***Service stopped #{Time.now}" }
exit!
end
end
DemoDaemon.mainloop
rescue Exception => err
File.open(LOG_FILE,'a+'){ |f| f.puts " ***Daemon failure #{Time.now} err=#{err} " }
raise
end
No comments:
Post a Comment