Class: SVUtil::ProcessManager

Inherits:
Object
  • Object
show all
Defined in:
lib/svutil/process_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass_or_instance, config) ⇒ ProcessManager

Returns a new instance of ProcessManager.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/svutil/process_manager.rb', line 3

def initialize(klass_or_instance, config)
  @server_instance = if klass_or_instance.instance_of?(Class)
    klass_or_instance.new
  else
    klass_or_instance
  end
  @config = config

  # TODO: Add ability for users to specify these signals
  Signal.trap("INT") { shutdown('Interupted', 1) }
  Signal.trap("TERM") { shutdown('Terminated', 2) }
  unless RUBY_PLATFORM =~ /(win|w)32$/
    Signal.trap("PIPE") { shutdown('Broken Pipe', 4) }
  end
  if running?
    STDERR.puts "There is already a '#{$0}' process running"
    exit 1
  end
end

Instance Method Details

#startObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/svutil/process_manager.rb', line 23

def start
  begin
    daemonize if @config.daemon
    write_pid_file
    @server_instance.run
  rescue SystemExit => e
    shutdown("System Exited")
  rescue Exception => e
    Log.error(e.message)
    Log.error(e.backtrace.join("\n")) if @config.trace
    shutdown("Process Completed with Error", 1)
  end
  shutdown("Process Completed")
end