Class: Application::Watcher

Inherits:
Win32::Daemon
  • Object
show all
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

Instance Method Summary collapse

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(options, &block)
  options[:dir_mode] = :normal
  options[:dir]      = File.split(__FILE__)[0]

  if block_given?
    options[:mode] = :proc
    options[:proc] = block
  end

  controller                            = Daemons::Controller.new(options, ARGV)
  _command, _controller_part, _app_part = controller.class.split_argv(ARGV)

  controller_group                 = Daemons::ApplicationGroup.new(controller.app_name, controller.options)
  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.options[:force] = true
      # controller_group.applications = []

      controller.options[:force] = true
    end
  end

  Daemons.run_proc(options[:app_name], options, &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, options = { })
  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, options = {})
  # 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, options, &proc)
end

Instance Method Details

#service_initObject



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_pauseObject



157
158
159
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders.rb', line 157

def service_pause
  @watch_folder_manager.pause
end

#service_resumeObject



161
162
163
# File 'lib/cantemo/portal/agent/cli/commands/watch_folders.rb', line 161

def service_resume
  @watch_folder_manager.resume
end

#service_stopObject



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(message = 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(message) }
  end
end