win32-service 0.8.10 → 1.0.1

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,96 +1,96 @@
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
- Dir.mkdir("C:/Tmp") unless File.exist?("C:/Tmp")
16
- 10.times{ |i|
17
- File.open(LOG_FILE , 'a'){ |f| f.puts("#{i}") }
18
- sleep 1
19
- }
20
- end
21
-
22
- # This is the daemon's mainloop. In other words, whatever runs here
23
- # is the code that runs while your service is running. Note that the
24
- # loop is not implicit.
25
- #
26
- # You must setup a loop as I've done here with the 'while running?'
27
- # code, or setup your own loop. Otherwise your service will exit and
28
- # won't be especially useful.
29
- #
30
- # In this particular case, I've setup a loop to append a short message
31
- # and timestamp to a file on your C: drive every 20 seconds. Be sure
32
- # to stop the service when you're done!
33
- #
34
- def service_main(*args)
35
- msg = 'service_main entered at: ' + Time.now.to_s
36
-
37
- File.open(LOG_FILE, 'a'){ |f|
38
- f.puts msg
39
- f.puts "Args: " + args.join(',')
40
- }
41
-
42
- # While we're in here the daemon is running.
43
- while running?
44
- if state == RUNNING
45
- sleep 20
46
- msg = 'Service is running as of: ' + Time.now.to_s
47
- File.open(LOG_FILE, 'a'){ |f| f.puts msg }
48
- else # PAUSED or IDLE
49
- sleep 0.5
50
- end
51
- end
52
-
53
- # We've left the loop, the daemon is about to exit.
54
-
55
- File.open(LOG_FILE, 'a'){ |f| f.puts "STATE: #{state}" }
56
-
57
- msg = 'service_main left at: ' + Time.now.to_s
58
-
59
- File.open(LOG_FILE, 'a'){ |f| f.puts msg }
60
- end
61
-
62
- # This event triggers when the service receives a signal to stop.
63
- #
64
- # NOTE: Older versions of this code used an explicit exit! call
65
- # to force the Ruby interpreter to exit. Don't do that. It is no
66
- # longer required and, in fact, may cause issues.
67
- #
68
- def service_stop
69
- msg = 'Received stop signal at: ' + Time.now.to_s
70
- File.open(LOG_FILE, 'a'){ |f| f.puts msg }
71
- end
72
-
73
- # This event triggers when the service receives a signal to pause.
74
- #
75
- def service_pause
76
- msg = 'Received pause signal at: ' + Time.now.to_s
77
- File.open(LOG_FILE, 'a'){ |f| f.puts msg }
78
- end
79
-
80
- # This event triggers when the service receives a signal to resume
81
- # from a paused state.
82
- #
83
- def service_resume
84
- msg = 'Received resume signal at: ' + Time.now.to_s
85
- File.open(LOG_FILE, 'a'){ |f| f.puts msg }
86
- end
87
- end
88
-
89
- # Create an instance of the Daemon and put it into a loop. I borrowed the
90
- # method name 'mainloop' from Tk, btw.
91
- #
92
- DemoDaemon.mainloop
93
- rescue Exception => err
94
- File.open(LOG_FILE, 'a'){ |fh| fh.puts "Daemon failure: #{err}" }
95
- raise
96
- 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
+ Dir.mkdir("C:/Tmp") unless File.exist?("C:/Tmp")
16
+ 10.times{ |i|
17
+ File.open(LOG_FILE , 'a'){ |f| f.puts("#{i}") }
18
+ sleep 1
19
+ }
20
+ end
21
+
22
+ # This is the daemon's mainloop. In other words, whatever runs here
23
+ # is the code that runs while your service is running. Note that the
24
+ # loop is not implicit.
25
+ #
26
+ # You must setup a loop as I've done here with the 'while running?'
27
+ # code, or setup your own loop. Otherwise your service will exit and
28
+ # won't be especially useful.
29
+ #
30
+ # In this particular case, I've setup a loop to append a short message
31
+ # and timestamp to a file on your C: drive every 20 seconds. Be sure
32
+ # to stop the service when you're done!
33
+ #
34
+ def service_main(*args)
35
+ msg = 'service_main entered at: ' + Time.now.to_s
36
+
37
+ File.open(LOG_FILE, 'a'){ |f|
38
+ f.puts msg
39
+ f.puts "Args: " + args.join(',')
40
+ }
41
+
42
+ # While we're in here the daemon is running.
43
+ while running?
44
+ if state == RUNNING
45
+ sleep 20
46
+ msg = 'Service is running as of: ' + Time.now.to_s
47
+ File.open(LOG_FILE, 'a'){ |f| f.puts msg }
48
+ else # PAUSED or IDLE
49
+ sleep 0.5
50
+ end
51
+ end
52
+
53
+ # We've left the loop, the daemon is about to exit.
54
+
55
+ File.open(LOG_FILE, 'a'){ |f| f.puts "STATE: #{state}" }
56
+
57
+ msg = 'service_main left at: ' + Time.now.to_s
58
+
59
+ File.open(LOG_FILE, 'a'){ |f| f.puts msg }
60
+ end
61
+
62
+ # This event triggers when the service receives a signal to stop.
63
+ #
64
+ # NOTE: Older versions of this code used an explicit exit! call
65
+ # to force the Ruby interpreter to exit. Don't do that. It is no
66
+ # longer required and, in fact, may cause issues.
67
+ #
68
+ def service_stop
69
+ msg = 'Received stop signal at: ' + Time.now.to_s
70
+ File.open(LOG_FILE, 'a'){ |f| f.puts msg }
71
+ end
72
+
73
+ # This event triggers when the service receives a signal to pause.
74
+ #
75
+ def service_pause
76
+ msg = 'Received pause signal at: ' + Time.now.to_s
77
+ File.open(LOG_FILE, 'a'){ |f| f.puts msg }
78
+ end
79
+
80
+ # This event triggers when the service receives a signal to resume
81
+ # from a paused state.
82
+ #
83
+ def service_resume
84
+ msg = 'Received resume signal at: ' + Time.now.to_s
85
+ File.open(LOG_FILE, 'a'){ |f| f.puts msg }
86
+ end
87
+ end
88
+
89
+ # Create an instance of the Daemon and put it into a loop. I borrowed the
90
+ # method name 'mainloop' from Tk, btw.
91
+ #
92
+ DemoDaemon.mainloop
93
+ rescue Exception => err
94
+ File.open(LOG_FILE, 'a'){ |fh| fh.puts "Daemon failure: #{err}" }
95
+ raise
96
+ end
@@ -1,123 +1,123 @@
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:\Tmp\win32_daemon_test.log. Feel free to delete
14
- # it when you're finished.
15
- #
16
- # To run the service, you must install it first.
17
- #
18
- # Usage: ruby demo_daemon_ctl.rb <option>
19
- #
20
- # Note that you *must* pass this program an option
21
- #
22
- # Options:
23
- # install - Installs the service. The service name is "DemoSvc"
24
- # and the display name is "Demo".
25
- # start - Starts the service. Make sure you stop it at some point or
26
- # you will eventually fill up your filesystem!.
27
- # stop - Stops the service.
28
- # pause - Pauses the service.
29
- # resume - Resumes the service.
30
- # uninstall - Uninstalls the service.
31
- # delete - Same as uninstall.
32
- #
33
- # You can also used the Windows Services GUI to start and stop the service.
34
- #
35
- # To get to the Windows Services GUI just follow:
36
- # Start -> Control Panel -> Administrative Tools -> Services
37
- ############################################################################
38
- require 'win32/service'
39
- require 'rbconfig'
40
- include Win32
41
- include RbConfig
42
-
43
- # Make sure you're using the version you think you're using.
44
- puts 'VERSION: ' + Service::VERSION
45
-
46
- SERVICE_NAME = 'DemoSvc'
47
- SERVICE_DISPLAYNAME = 'Demo'
48
-
49
- # Quote the full path to deal with possible spaces in the path name.
50
- ruby = File.join(CONFIG['bindir'], CONFIG['ruby_install_name']).tr('/', '\\')
51
- path = ' "' + File.dirname(File.expand_path($0)).tr('/', '\\')
52
- path += '\demo_daemon.rb"'
53
- cmd = ruby + path
54
-
55
- # You must provide at least one argument.
56
- raise ArgumentError, 'No argument provided' unless ARGV[0]
57
-
58
- case ARGV[0].downcase
59
- when 'install'
60
- Service.new(
61
- :service_name => SERVICE_NAME,
62
- :display_name => SERVICE_DISPLAYNAME,
63
- :description => 'Sample Ruby service',
64
- :binary_path_name => cmd
65
- )
66
- puts 'Service ' + SERVICE_NAME + ' installed'
67
- when 'start'
68
- if Service.status(SERVICE_NAME).current_state != 'running'
69
- Service.start(SERVICE_NAME, nil, 'hello', 'world')
70
- while Service.status(SERVICE_NAME).current_state != 'running'
71
- puts 'One moment...' + Service.status(SERVICE_NAME).current_state
72
- sleep 1
73
- end
74
- puts 'Service ' + SERVICE_NAME + ' started'
75
- else
76
- puts 'Already running'
77
- end
78
- when 'stop'
79
- if Service.status(SERVICE_NAME).current_state != 'stopped'
80
- Service.stop(SERVICE_NAME)
81
- while Service.status(SERVICE_NAME).current_state != 'stopped'
82
- puts 'One moment...' + Service.status(SERVICE_NAME).current_state
83
- sleep 1
84
- end
85
- puts 'Service ' + SERVICE_NAME + ' stopped'
86
- else
87
- puts 'Already stopped'
88
- end
89
- when 'uninstall', 'delete'
90
- if Service.status(SERVICE_NAME).current_state != 'stopped'
91
- Service.stop(SERVICE_NAME)
92
- end
93
- while Service.status(SERVICE_NAME).current_state != 'stopped'
94
- puts 'One moment...' + Service.status(SERVICE_NAME).current_state
95
- sleep 1
96
- end
97
- Service.delete(SERVICE_NAME)
98
- puts 'Service ' + SERVICE_NAME + ' deleted'
99
- when 'pause'
100
- if Service.status(SERVICE_NAME).current_state != 'paused'
101
- Service.pause(SERVICE_NAME)
102
- while Service.status(SERVICE_NAME).current_state != 'paused'
103
- puts 'One moment...' + Service.status(SERVICE_NAME).current_state
104
- sleep 1
105
- end
106
- puts 'Service ' + SERVICE_NAME + ' paused'
107
- else
108
- puts 'Already paused'
109
- end
110
- when 'resume'
111
- if Service.status(SERVICE_NAME).current_state != 'running'
112
- Service.resume(SERVICE_NAME)
113
- while Service.status(SERVICE_NAME).current_state != 'running'
114
- puts 'One moment...' + Service.status(SERVICE_NAME).current_state
115
- sleep 1
116
- end
117
- puts 'Service ' + SERVICE_NAME + ' resumed'
118
- else
119
- puts 'Already running'
120
- end
121
- else
122
- raise ArgumentError, 'unknown option: ' + ARGV[0]
123
- 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:\Tmp\win32_daemon_test.log. Feel free to delete
14
+ # it when you're finished.
15
+ #
16
+ # To run the service, you must install it first.
17
+ #
18
+ # Usage: ruby demo_daemon_ctl.rb <option>
19
+ #
20
+ # Note that you *must* pass this program an option
21
+ #
22
+ # Options:
23
+ # install - Installs the service. The service name is "DemoSvc"
24
+ # and the display name is "Demo".
25
+ # start - Starts the service. Make sure you stop it at some point or
26
+ # you will eventually fill up your filesystem!.
27
+ # stop - Stops the service.
28
+ # pause - Pauses the service.
29
+ # resume - Resumes the service.
30
+ # uninstall - Uninstalls the service.
31
+ # delete - Same as uninstall.
32
+ #
33
+ # You can also used the Windows Services GUI to start and stop the service.
34
+ #
35
+ # To get to the Windows Services GUI just follow:
36
+ # Start -> Control Panel -> Administrative Tools -> Services
37
+ ############################################################################
38
+ require 'win32/service'
39
+ require 'rbconfig'
40
+ include Win32
41
+ include RbConfig
42
+
43
+ # Make sure you're using the version you think you're using.
44
+ puts 'VERSION: ' + Service::VERSION
45
+
46
+ SERVICE_NAME = 'DemoSvc'
47
+ SERVICE_DISPLAYNAME = 'Demo'
48
+
49
+ # Quote the full path to deal with possible spaces in the path name.
50
+ ruby = File.join(CONFIG['bindir'], CONFIG['ruby_install_name']).tr('/', '\\')
51
+ path = ' "' + File.dirname(File.expand_path($0)).tr('/', '\\')
52
+ path += '\demo_daemon.rb"'
53
+ cmd = ruby + path
54
+
55
+ # You must provide at least one argument.
56
+ raise ArgumentError, 'No argument provided' unless ARGV[0]
57
+
58
+ case ARGV[0].downcase
59
+ when 'install'
60
+ Service.new(
61
+ :service_name => SERVICE_NAME,
62
+ :display_name => SERVICE_DISPLAYNAME,
63
+ :description => 'Sample Ruby service',
64
+ :binary_path_name => cmd
65
+ )
66
+ puts 'Service ' + SERVICE_NAME + ' installed'
67
+ when 'start'
68
+ if Service.status(SERVICE_NAME).current_state != 'running'
69
+ Service.start(SERVICE_NAME, nil, 'hello', 'world')
70
+ while Service.status(SERVICE_NAME).current_state != 'running'
71
+ puts 'One moment...' + Service.status(SERVICE_NAME).current_state
72
+ sleep 1
73
+ end
74
+ puts 'Service ' + SERVICE_NAME + ' started'
75
+ else
76
+ puts 'Already running'
77
+ end
78
+ when 'stop'
79
+ if Service.status(SERVICE_NAME).current_state != 'stopped'
80
+ Service.stop(SERVICE_NAME)
81
+ while Service.status(SERVICE_NAME).current_state != 'stopped'
82
+ puts 'One moment...' + Service.status(SERVICE_NAME).current_state
83
+ sleep 1
84
+ end
85
+ puts 'Service ' + SERVICE_NAME + ' stopped'
86
+ else
87
+ puts 'Already stopped'
88
+ end
89
+ when 'uninstall', 'delete'
90
+ if Service.status(SERVICE_NAME).current_state != 'stopped'
91
+ Service.stop(SERVICE_NAME)
92
+ end
93
+ while Service.status(SERVICE_NAME).current_state != 'stopped'
94
+ puts 'One moment...' + Service.status(SERVICE_NAME).current_state
95
+ sleep 1
96
+ end
97
+ Service.delete(SERVICE_NAME)
98
+ puts 'Service ' + SERVICE_NAME + ' deleted'
99
+ when 'pause'
100
+ if Service.status(SERVICE_NAME).current_state != 'paused'
101
+ Service.pause(SERVICE_NAME)
102
+ while Service.status(SERVICE_NAME).current_state != 'paused'
103
+ puts 'One moment...' + Service.status(SERVICE_NAME).current_state
104
+ sleep 1
105
+ end
106
+ puts 'Service ' + SERVICE_NAME + ' paused'
107
+ else
108
+ puts 'Already paused'
109
+ end
110
+ when 'resume'
111
+ if Service.status(SERVICE_NAME).current_state != 'running'
112
+ Service.resume(SERVICE_NAME)
113
+ while Service.status(SERVICE_NAME).current_state != 'running'
114
+ puts 'One moment...' + Service.status(SERVICE_NAME).current_state
115
+ sleep 1
116
+ end
117
+ puts 'Service ' + SERVICE_NAME + ' resumed'
118
+ else
119
+ puts 'Already running'
120
+ end
121
+ else
122
+ raise ArgumentError, 'unknown option: ' + ARGV[0]
123
+ 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
+ }