win32-service 0.8.6 → 0.8.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,95 +1,95 @@
1
- LOG_FILE = 'C:\\Tmp\\win32_daemon_test.log'
2
-
3
- begin
4
- require 'rubygems'
5
- require 'win32/daemon'
6
- include Win32
7
-
8
- class DemoDaemon < Daemon
9
- # This method fires off before the +service_main+ mainloop is entered.
10
- # Any pre-setup code you need to run before your service's mainloop
11
- # starts should be put here. Otherwise the service might fail with a
12
- # timeout error when you try to start it.
13
- #
14
- def service_init
15
- 10.times{ |i|
16
- File.open(LOG_FILE , 'a'){ |f| f.puts("#{i}") }
17
- sleep 1
18
- }
19
- end
20
-
21
- # This is the daemon's mainloop. In other words, whatever runs here
22
- # is the code that runs while your service is running. Note that the
23
- # loop is not implicit.
24
- #
25
- # You must setup a loop as I've done here with the 'while running?'
26
- # code, or setup your own loop. Otherwise your service will exit and
27
- # won't be especially useful.
28
- #
29
- # In this particular case, I've setup a loop to append a short message
30
- # and timestamp to a file on your C: drive every 20 seconds. Be sure
31
- # to stop the service when you're done!
32
- #
33
- def service_main(*args)
34
- msg = 'service_main entered at: ' + Time.now.to_s
35
-
36
- File.open(LOG_FILE, 'a'){ |f|
37
- f.puts msg
38
- f.puts "Args: " + args.join(',')
39
- }
40
-
41
- # While we're in here the daemon is running.
42
- while running?
43
- if state == RUNNING
44
- sleep 20
45
- msg = 'Service is running as of: ' + Time.now.to_s
46
- File.open(LOG_FILE, 'a'){ |f| f.puts msg }
47
- else # PAUSED or IDLE
48
- sleep 0.5
49
- end
50
- end
51
-
52
- # We've left the loop, the daemon is about to exit.
53
-
54
- File.open(LOG_FILE, 'a'){ |f| f.puts "STATE: #{state}" }
55
-
56
- msg = 'service_main left at: ' + Time.now.to_s
57
-
58
- File.open(LOG_FILE, 'a'){ |f| f.puts msg }
59
- end
60
-
61
- # This event triggers when the service receives a signal to stop.
62
- #
63
- # NOTE: Older versions of this code used an explicit exit! call
64
- # to force the Ruby interpreter to exit. Don't do that. It is no
65
- # longer required and, in fact, may cause issues.
66
- #
67
- def service_stop
68
- msg = 'Received stop signal at: ' + Time.now.to_s
69
- File.open(LOG_FILE, 'a'){ |f| f.puts msg }
70
- end
71
-
72
- # This event triggers when the service receives a signal to pause.
73
- #
74
- def service_pause
75
- msg = 'Received pause signal at: ' + Time.now.to_s
76
- File.open(LOG_FILE, 'a'){ |f| f.puts msg }
77
- end
78
-
79
- # This event triggers when the service receives a signal to resume
80
- # from a paused state.
81
- #
82
- def service_resume
83
- msg = 'Received resume signal at: ' + Time.now.to_s
84
- File.open(LOG_FILE, 'a'){ |f| f.puts msg }
85
- end
86
- end
87
-
88
- # Create an instance of the Daemon and put it into a loop. I borrowed the
89
- # method name 'mainloop' from Tk, btw.
90
- #
91
- DemoDaemon.mainloop
92
- rescue Exception => err
93
- File.open(LOG_FILE, 'a'){ |fh| fh.puts "Daemon failure: #{err}" }
94
- raise
95
- end
1
+ LOG_FILE = 'C:\\Tmp\\win32_daemon_test.log'
2
+
3
+ begin
4
+ require 'rubygems'
5
+ require 'win32/daemon'
6
+ include Win32
7
+
8
+ class DemoDaemon < Daemon
9
+ # This method fires off before the +service_main+ mainloop is entered.
10
+ # Any pre-setup code you need to run before your service's mainloop
11
+ # starts should be put here. Otherwise the service might fail with a
12
+ # timeout error when you try to start it.
13
+ #
14
+ def service_init
15
+ 10.times{ |i|
16
+ File.open(LOG_FILE , 'a'){ |f| f.puts("#{i}") }
17
+ sleep 1
18
+ }
19
+ end
20
+
21
+ # This is the daemon's mainloop. In other words, whatever runs here
22
+ # is the code that runs while your service is running. Note that the
23
+ # loop is not implicit.
24
+ #
25
+ # You must setup a loop as I've done here with the 'while running?'
26
+ # code, or setup your own loop. Otherwise your service will exit and
27
+ # won't be especially useful.
28
+ #
29
+ # In this particular case, I've setup a loop to append a short message
30
+ # and timestamp to a file on your C: drive every 20 seconds. Be sure
31
+ # to stop the service when you're done!
32
+ #
33
+ def service_main(*args)
34
+ msg = 'service_main entered at: ' + Time.now.to_s
35
+
36
+ File.open(LOG_FILE, 'a'){ |f|
37
+ f.puts msg
38
+ f.puts "Args: " + args.join(',')
39
+ }
40
+
41
+ # While we're in here the daemon is running.
42
+ while running?
43
+ if state == RUNNING
44
+ sleep 20
45
+ msg = 'Service is running as of: ' + Time.now.to_s
46
+ File.open(LOG_FILE, 'a'){ |f| f.puts msg }
47
+ else # PAUSED or IDLE
48
+ sleep 0.5
49
+ end
50
+ end
51
+
52
+ # We've left the loop, the daemon is about to exit.
53
+
54
+ File.open(LOG_FILE, 'a'){ |f| f.puts "STATE: #{state}" }
55
+
56
+ msg = 'service_main left at: ' + Time.now.to_s
57
+
58
+ File.open(LOG_FILE, 'a'){ |f| f.puts msg }
59
+ end
60
+
61
+ # This event triggers when the service receives a signal to stop.
62
+ #
63
+ # NOTE: Older versions of this code used an explicit exit! call
64
+ # to force the Ruby interpreter to exit. Don't do that. It is no
65
+ # longer required and, in fact, may cause issues.
66
+ #
67
+ def service_stop
68
+ msg = 'Received stop signal at: ' + Time.now.to_s
69
+ File.open(LOG_FILE, 'a'){ |f| f.puts msg }
70
+ end
71
+
72
+ # This event triggers when the service receives a signal to pause.
73
+ #
74
+ def service_pause
75
+ msg = 'Received pause signal at: ' + Time.now.to_s
76
+ File.open(LOG_FILE, 'a'){ |f| f.puts msg }
77
+ end
78
+
79
+ # This event triggers when the service receives a signal to resume
80
+ # from a paused state.
81
+ #
82
+ def service_resume
83
+ msg = 'Received resume signal at: ' + Time.now.to_s
84
+ File.open(LOG_FILE, 'a'){ |f| f.puts msg }
85
+ end
86
+ end
87
+
88
+ # Create an instance of the Daemon and put it into a loop. I borrowed the
89
+ # method name 'mainloop' from Tk, btw.
90
+ #
91
+ DemoDaemon.mainloop
92
+ rescue Exception => err
93
+ File.open(LOG_FILE, 'a'){ |fh| fh.puts "Daemon failure: #{err}" }
94
+ raise
95
+ end
@@ -1,122 +1,122 @@
1
- ############################################################################
2
- # demo_daemon_ctl.rb
3
- #
4
- # This is a command line script for installing and/or running a small
5
- # Ruby program as a service. The service will simply write a small bit
6
- # of text to a file every 20 seconds. It will also write some text to the
7
- # file during the initialization (service_init) step.
8
- #
9
- # It should take about 10 seconds to start, which is intentional - it's a test
10
- # of the service_init hook, so don't be surprised if you see "one moment,
11
- # start pending" about 10 times on the command line.
12
- #
13
- # The file in question is C:\test.log. Feel free to delete it when finished.
14
- #
15
- # To run the service, you must install it first.
16
- #
17
- # Usage: ruby demo_daemon_ctl.rb <option>
18
- #
19
- # Note that you *must* pass this program an option
20
- #
21
- # Options:
22
- # install - Installs the service. The service name is "DemoSvc"
23
- # and the display name is "Demo".
24
- # start - Starts the service. Make sure you stop it at some point or
25
- # you will eventually fill up your filesystem!.
26
- # stop - Stops the service.
27
- # pause - Pauses the service.
28
- # resume - Resumes the service.
29
- # uninstall - Uninstalls the service.
30
- # delete - Same as uninstall.
31
- #
32
- # You can also used the Windows Services GUI to start and stop the service.
33
- #
34
- # To get to the Windows Services GUI just follow:
35
- # Start -> Control Panel -> Administrative Tools -> Services
36
- ############################################################################
37
- require 'win32/service'
38
- require 'rbconfig'
39
- include Win32
40
- include RbConfig
41
-
42
- # Make sure you're using the version you think you're using.
43
- puts 'VERSION: ' + Service::VERSION
44
-
45
- SERVICE_NAME = 'DemoSvc'
46
- SERVICE_DISPLAYNAME = 'Demo'
47
-
48
- # Quote the full path to deal with possible spaces in the path name.
49
- ruby = File.join(CONFIG['bindir'], CONFIG['ruby_install_name']).tr('/', '\\')
50
- path = ' "' + File.dirname(File.expand_path($0)).tr('/', '\\')
51
- path += '\demo_daemon.rb"'
52
- cmd = ruby + path
53
-
54
- # You must provide at least one argument.
55
- raise ArgumentError, 'No argument provided' unless ARGV[0]
56
-
57
- case ARGV[0].downcase
58
- when 'install'
59
- Service.new(
60
- :service_name => SERVICE_NAME,
61
- :display_name => SERVICE_DISPLAYNAME,
62
- :description => 'Sample Ruby service',
63
- :binary_path_name => cmd
64
- )
65
- puts 'Service ' + SERVICE_NAME + ' installed'
66
- when 'start'
67
- if Service.status(SERVICE_NAME).current_state != 'running'
68
- Service.start(SERVICE_NAME, nil, 'hello', 'world')
69
- while Service.status(SERVICE_NAME).current_state != 'running'
70
- puts 'One moment...' + Service.status(SERVICE_NAME).current_state
71
- sleep 1
72
- end
73
- puts 'Service ' + SERVICE_NAME + ' started'
74
- else
75
- puts 'Already running'
76
- end
77
- when 'stop'
78
- if Service.status(SERVICE_NAME).current_state != 'stopped'
79
- Service.stop(SERVICE_NAME)
80
- while Service.status(SERVICE_NAME).current_state != 'stopped'
81
- puts 'One moment...' + Service.status(SERVICE_NAME).current_state
82
- sleep 1
83
- end
84
- puts 'Service ' + SERVICE_NAME + ' stopped'
85
- else
86
- puts 'Already stopped'
87
- end
88
- when 'uninstall', 'delete'
89
- if Service.status(SERVICE_NAME).current_state != 'stopped'
90
- Service.stop(SERVICE_NAME)
91
- end
92
- while Service.status(SERVICE_NAME).current_state != 'stopped'
93
- puts 'One moment...' + Service.status(SERVICE_NAME).current_state
94
- sleep 1
95
- end
96
- Service.delete(SERVICE_NAME)
97
- puts 'Service ' + SERVICE_NAME + ' deleted'
98
- when 'pause'
99
- if Service.status(SERVICE_NAME).current_state != 'paused'
100
- Service.pause(SERVICE_NAME)
101
- while Service.status(SERVICE_NAME).current_state != 'paused'
102
- puts 'One moment...' + Service.status(SERVICE_NAME).current_state
103
- sleep 1
104
- end
105
- puts 'Service ' + SERVICE_NAME + ' paused'
106
- else
107
- puts 'Already paused'
108
- end
109
- when 'resume'
110
- if Service.status(SERVICE_NAME).current_state != 'running'
111
- Service.resume(SERVICE_NAME)
112
- while Service.status(SERVICE_NAME).current_state != 'running'
113
- puts 'One moment...' + Service.status(SERVICE_NAME).current_state
114
- sleep 1
115
- end
116
- puts 'Service ' + SERVICE_NAME + ' resumed'
117
- else
118
- puts 'Already running'
119
- end
120
- else
121
- raise ArgumentError, 'unknown option: ' + ARGV[0]
122
- end
1
+ ############################################################################
2
+ # demo_daemon_ctl.rb
3
+ #
4
+ # This is a command line script for installing and/or running a small
5
+ # Ruby program as a service. The service will simply write a small bit
6
+ # of text to a file every 20 seconds. It will also write some text to the
7
+ # file during the initialization (service_init) step.
8
+ #
9
+ # It should take about 10 seconds to start, which is intentional - it's a test
10
+ # of the service_init hook, so don't be surprised if you see "one moment,
11
+ # start pending" about 10 times on the command line.
12
+ #
13
+ # The file in question is C:\test.log. Feel free to delete it when finished.
14
+ #
15
+ # To run the service, you must install it first.
16
+ #
17
+ # Usage: ruby demo_daemon_ctl.rb <option>
18
+ #
19
+ # Note that you *must* pass this program an option
20
+ #
21
+ # Options:
22
+ # install - Installs the service. The service name is "DemoSvc"
23
+ # and the display name is "Demo".
24
+ # start - Starts the service. Make sure you stop it at some point or
25
+ # you will eventually fill up your filesystem!.
26
+ # stop - Stops the service.
27
+ # pause - Pauses the service.
28
+ # resume - Resumes the service.
29
+ # uninstall - Uninstalls the service.
30
+ # delete - Same as uninstall.
31
+ #
32
+ # You can also used the Windows Services GUI to start and stop the service.
33
+ #
34
+ # To get to the Windows Services GUI just follow:
35
+ # Start -> Control Panel -> Administrative Tools -> Services
36
+ ############################################################################
37
+ require 'win32/service'
38
+ require 'rbconfig'
39
+ include Win32
40
+ include RbConfig
41
+
42
+ # Make sure you're using the version you think you're using.
43
+ puts 'VERSION: ' + Service::VERSION
44
+
45
+ SERVICE_NAME = 'DemoSvc'
46
+ SERVICE_DISPLAYNAME = 'Demo'
47
+
48
+ # Quote the full path to deal with possible spaces in the path name.
49
+ ruby = File.join(CONFIG['bindir'], CONFIG['ruby_install_name']).tr('/', '\\')
50
+ path = ' "' + File.dirname(File.expand_path($0)).tr('/', '\\')
51
+ path += '\demo_daemon.rb"'
52
+ cmd = ruby + path
53
+
54
+ # You must provide at least one argument.
55
+ raise ArgumentError, 'No argument provided' unless ARGV[0]
56
+
57
+ case ARGV[0].downcase
58
+ when 'install'
59
+ Service.new(
60
+ :service_name => SERVICE_NAME,
61
+ :display_name => SERVICE_DISPLAYNAME,
62
+ :description => 'Sample Ruby service',
63
+ :binary_path_name => cmd
64
+ )
65
+ puts 'Service ' + SERVICE_NAME + ' installed'
66
+ when 'start'
67
+ if Service.status(SERVICE_NAME).current_state != 'running'
68
+ Service.start(SERVICE_NAME, nil, 'hello', 'world')
69
+ while Service.status(SERVICE_NAME).current_state != 'running'
70
+ puts 'One moment...' + Service.status(SERVICE_NAME).current_state
71
+ sleep 1
72
+ end
73
+ puts 'Service ' + SERVICE_NAME + ' started'
74
+ else
75
+ puts 'Already running'
76
+ end
77
+ when 'stop'
78
+ if Service.status(SERVICE_NAME).current_state != 'stopped'
79
+ Service.stop(SERVICE_NAME)
80
+ while Service.status(SERVICE_NAME).current_state != 'stopped'
81
+ puts 'One moment...' + Service.status(SERVICE_NAME).current_state
82
+ sleep 1
83
+ end
84
+ puts 'Service ' + SERVICE_NAME + ' stopped'
85
+ else
86
+ puts 'Already stopped'
87
+ end
88
+ when 'uninstall', 'delete'
89
+ if Service.status(SERVICE_NAME).current_state != 'stopped'
90
+ Service.stop(SERVICE_NAME)
91
+ end
92
+ while Service.status(SERVICE_NAME).current_state != 'stopped'
93
+ puts 'One moment...' + Service.status(SERVICE_NAME).current_state
94
+ sleep 1
95
+ end
96
+ Service.delete(SERVICE_NAME)
97
+ puts 'Service ' + SERVICE_NAME + ' deleted'
98
+ when 'pause'
99
+ if Service.status(SERVICE_NAME).current_state != 'paused'
100
+ Service.pause(SERVICE_NAME)
101
+ while Service.status(SERVICE_NAME).current_state != 'paused'
102
+ puts 'One moment...' + Service.status(SERVICE_NAME).current_state
103
+ sleep 1
104
+ end
105
+ puts 'Service ' + SERVICE_NAME + ' paused'
106
+ else
107
+ puts 'Already paused'
108
+ end
109
+ when 'resume'
110
+ if Service.status(SERVICE_NAME).current_state != 'running'
111
+ Service.resume(SERVICE_NAME)
112
+ while Service.status(SERVICE_NAME).current_state != 'running'
113
+ puts 'One moment...' + Service.status(SERVICE_NAME).current_state
114
+ sleep 1
115
+ end
116
+ puts 'Service ' + SERVICE_NAME + ' resumed'
117
+ else
118
+ puts 'Already running'
119
+ end
120
+ else
121
+ raise ArgumentError, 'unknown option: ' + ARGV[0]
122
+ end
@@ -1,30 +1,30 @@
1
- #######################################################################
2
- # demo_services.rb
3
- #
4
- # Test script for general futzing that shows off the basic
5
- # capabilities of this library. Modify as you see fit.
6
- #
7
- # You can run this sample program via the "example:services" task.
8
- #######################################################################
9
- require 'win32/service'
10
- include Win32
11
-
12
- puts "VERSION: " + Service::VERSION
13
-
14
- p Service.exists?('Schedule')
15
- p Service.exists?('bogusxxx')
16
-
17
- status = Service.status('Schedule')
18
- p status
19
-
20
- info = Service.config_info('Schedule')
21
-
22
- print "\n\nShowing config info for Schedule service\n\n"
23
- p info
24
-
25
- print "\n\nAbout to show all services\n\n"
26
- sleep 10
27
-
28
- Service.services{ |struct|
29
- p struct
30
- }
1
+ #######################################################################
2
+ # demo_services.rb
3
+ #
4
+ # Test script for general futzing that shows off the basic
5
+ # capabilities of this library. Modify as you see fit.
6
+ #
7
+ # You can run this sample program via the "example:services" task.
8
+ #######################################################################
9
+ require 'win32/service'
10
+ include Win32
11
+
12
+ puts "VERSION: " + Service::VERSION
13
+
14
+ p Service.exists?('Schedule')
15
+ p Service.exists?('bogusxxx')
16
+
17
+ status = Service.status('Schedule')
18
+ p status
19
+
20
+ info = Service.config_info('Schedule')
21
+
22
+ print "\n\nShowing config info for Schedule service\n\n"
23
+ p info
24
+
25
+ print "\n\nAbout to show all services\n\n"
26
+ sleep 10
27
+
28
+ Service.services{ |struct|
29
+ p struct
30
+ }