| Last Update: | Thu Jun 12 2008 |
The Daemon class is a wrapper class that allows you to run your code as a Windows service.
require 'win32/daemon'
include Win32
class Daemon
def service_main
while running?
sleep 3
File.open("c:\\test.log", "a"){ |f| f.puts "service is running" }
end
end
def service_stop
exit!
end
end
Daemon.mainloop
Daemon.mainloop
This is the method that actually puts your code into a loop and allows it to run as a service. The code that is actually run while in the mainloop is what you defined in the Daemon#service_main method.
Daemon#running?
Returns whether or not the daemon is running. This is just a shortcut for checking if the state is RUNNING, PAUSED or IDLE. This is typically used within your service_main method. See the demo_daemon.rb file in the 'examples' directory for an example of how it's used in practice.
Daemon#service_init
Any code defined defined within this method occurs before service_main is reached. Any initialization code that takes more than two seconds to execute should be placed here. Otherwise, your service may timeout when you try to start it.
Daemon#service_main(*args)
You are expected to define your own service_main() method. The code defined in this method is the code that will run while running as a service. Any +args+ passed to Service.start are passed to this method.
Daemon#state
Returns the current state of the Daemon. For a list of valid states, see the Constants section below.
These methods are called if defined within your Daemon class, and the appropriate signal is received by your service.
Daemon#service_stop
Called if the service receives a SERVICE_CONTROL_STOP signal. This is what the Service.stop() method sends.
Daemon#service_pause
Called if the service receives a SERVICE_CONTROL_PAUSE signal. This is what the Service.pause() method sends.
Daemon#service_resume
Called if the service receives a SERVICE_CONTROL_CONTINUE signal. This is what the Service.resume() method sends.
Daemon#service_interrogate
Called if the service receives a SERVICE_CONTROL_INTERROGATE signal. This notifies a service that it should report its current status information to the service control manager.
Daemon#service_shutdown
Called if the service receives a SERVICE_CONTROL_SHUTDOWN signal.
Daemon#service_netbindadd
Called if the service receives a SERVICE_CONTROL_NETBINDADD signal. This notifies a network service that there is a new component for binding.
Daemon#service_netbinddisable
Called if the service receives a SERVICE_CONTROL_NETBINDDISABLE signal. This notifies a network service that one of its bindings has been disabled.
Daemon#service_netbindenable
Called if the service receives a SERVICE_CONTROL_NETBINDENABLE signal. This Notifies a network service that a disabled binding has been enabled.
Daemon#service_netbindremove
Called if the service receives a SERVICE_CONTROL_NETBINDREMOVE signal. This notifies a network service that that a component for binding has been removed.
Daemon#service_paramchange
Called if the service receives a SERVICE_CONTROL_PARAMCHANGE signal. This notifies a service that its startup parameters have changed.
Daemon::CONTINUE_PENDING
The service continue is pending.
Daemon::PAUSE_PENDING
The service pause is pending.
Daemon::PAUSED
The service is paused (but not STOPPED).
Daemon::RUNNING
The service is running.
Daemon::START_PENDING
The service is starting (but is not yet in a RUNNING state).
Daemon::STOP_PENDING
The service is stopping (but is not yet in a STOPPED state).
Daemon::STOPPED
The service is not running.
Daemon::IDLE
The service is running, in an idle state. This is a custom state that we added that gets around a thread blocking issue.
You must create a service before you can actually run it. Look in the examples directory for the files 'demo_daemon.rb' and 'demodaemon_ctl.rb'. They're small and straightforward examples of how to control, install and setup your own Daemon.
None known. Please report any bugs you find on the Bug tracker at http://rubyforge.org/projects/win32utils.
Suggestions welcome. Please log them on the Feature Request tracker at http://rubyforge.org/projects/win32utils
Many thanks go to Patrick Hurley for providing the fix for the thread blocking issue. Thanks also go to Kevin Burge for his patch that solved service responsiveness issues.
(C) 2003-2008 Daniel J. Berger, All Rights Reserved
Ruby's
This package is provided "as is" and without any express or implied warranties, including, without limitation, the implied warranties of merchantability and fitness for a particular purpose.