Class: Application::Watcher
- Inherits:
-
Win32::Daemon
- Object
- Win32::Daemon
- Application::Watcher
- Defined in:
- lib/cantemo/portal/agent/cli/commands/watch_folders.rb,
lib/cantemo/portal/agent/cli/commands/watch_folders.rb
Constant Summary collapse
- LOG_FILE_PATH =
'C:\\tmp\\win32_daemon_test.log'
- APP_NAME =
'cantemo-portal-watch-folders'
Class Method Summary collapse
- .daemons_run_proc_with_cleanup(options, &block) ⇒ Object
- .run(args) ⇒ Object
- .run_in_foreground(args, options = { }) ⇒ Object
- .run_with_process_manager(args, options = {}) ⇒ Object
Instance Method Summary collapse
- #service_init ⇒ Object
- #service_main(*args) ⇒ Object
- #service_pause ⇒ Object
- #service_resume ⇒ Object
- #service_stop ⇒ Object
- #write_log(message = nil, &block) ⇒ Object
Class Method Details
.daemons_run_proc_with_cleanup(options, &block) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders.rb', line 175 def self.daemons_run_proc_with_cleanup(, &block) [:dir_mode] = :normal [:dir] = File.split(__FILE__)[0] if block_given? [:mode] = :proc [:proc] = block end controller = Daemons::Controller.new(, ARGV) _command, _controller_part, _app_part = controller.class.split_argv(ARGV) controller_group = Daemons::ApplicationGroup.new(controller.app_name, controller.) controller_group.controller_argv = _controller_part controller_group.app_argv = _app_part controller_group.setup applications = controller_group.applications is_running = applications.find { |a| a.running? } if !applications.empty? puts "Found #{applications.length} existing pid file(s) #{applications.map { |a| a.pid.pid }}" should_zap_all = !is_running || (applications.length == 1 && applications.first.pid.pid == 0) if should_zap_all warn "Found stale pid file(s)" controller_group.zap_all controller_group.[:force] = true # controller_group.applications = [] controller.[:force] = true end end Daemons.run_proc([:app_name], , &block) # controller.catch_exceptions do # controller.run # end end |
.run(args) ⇒ Object
171 172 173 |
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders.rb', line 171 def self.run(args) WATCH_FOLDER_MANAGER_CLASS.run(args) end |
.run_in_foreground(args, options = { }) ⇒ Object
228 229 230 |
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders.rb', line 228 def self.run_in_foreground(args, = { }) self.run(args) end |
.run_with_process_manager(args, options = {}) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders.rb', line 215 def self.run_with_process_manager(args, = {}) # ARGV.unshift 'run' unless %w(start stop restart run zap killall status).include? ARGV.first require 'daemons' proc = Proc.new { self.run(args) } app_name = APP_NAME # options[:app_name] = app_name # daemons_run_proc_with_cleanup(options, &proc) # options[:force] = true Daemons.run_proc(app_name, , &proc) end |
Instance Method Details
#service_init ⇒ Object
114 115 116 |
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders.rb', line 114 def service_init @process_thread = nil end |
#service_main(*args) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders.rb', line 118 def service_main(*args) msg = 'service_main entered at: ' + Time.now.to_s write_log{ |f| f.puts msg f.puts "Args: " + args.join(',') } @watch_folder_manager = nil @process_thread = Thread.new(@watch_folder_manager, args) do |watch_folder_manager, args| watch_folder_manager = WATCH_FOLDER_MANAGER_CLASS.new(args) watch_folder_manager.run end while running? if state == RUNNING sleep 20 msg = 'Service is running as of: ' + Time.now.to_s write_log { |f| f.puts msg } else # PAUSED or IDLE sleep 0.5 end end # We've left the loop, the daemon is about to exit. write_log{ |f| f.puts "STATE: #{state}" } msg = 'service_main left at: ' + Time.now.to_s write_log{ |f| f.puts msg } end |
#service_pause ⇒ Object
157 158 159 |
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders.rb', line 157 def service_pause @watch_folder_manager.pause end |
#service_resume ⇒ Object
161 162 163 |
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders.rb', line 161 def service_resume @watch_folder_manager.resume end |
#service_stop ⇒ Object
153 154 155 |
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders.rb', line 153 def service_stop @watch_folder_manager.stop end |
#write_log(message = nil, &block) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders.rb', line 101 def write_log( = nil, &block) if block_given? begin f = File.open(LOG_FILE_PATH, 'a') yield f ensure f.close if f && f.open? end else File.open(LOG_FILE_PATH, 'a') { |f| f.puts() } end end |