Class: Legion::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/process.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Process

Returns a new instance of Process.



11
12
13
14
15
# File 'lib/legion/process.rb', line 11

def initialize(options)
  @options = options
  options[:logfile] = File.expand_path(logfile) if logfile?
  options[:pidfile] = File.expand_path(pidfile) if pidfile?
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/legion/process.rb', line 9

def options
  @options
end

#quitObject (readonly)

Returns the value of attribute quit.



9
10
11
# File 'lib/legion/process.rb', line 9

def quit
  @quit
end

#serviceObject (readonly)

Returns the value of attribute service.



9
10
11
# File 'lib/legion/process.rb', line 9

def service
  @service
end

Class Method Details

.run!(options) ⇒ Object



5
6
7
# File 'lib/legion/process.rb', line 5

def self.run!(options)
  Legion::Process.new(options).run!
end

Instance Method Details

#check_pidObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/legion/process.rb', line 85

def check_pid
  if pidfile?
    case pid_status(pidfile)
    when :running, :not_owned
      exit(1)
    when :dead
      File.delete(pidfile)
    end
  end
  false
end

#daemonizeObject

DAEMONIZING, PID MANAGEMENT, and OUTPUT REDIRECTION



65
66
67
68
69
70
# File 'lib/legion/process.rb', line 65

def daemonize
  exit if fork
  ::Process.setsid
  exit if fork
  Dir.chdir '/'
end

#daemonize?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/legion/process.rb', line 17

def daemonize?
  options[:daemonize]
end

#info(msg) ⇒ Object



37
38
39
# File 'lib/legion/process.rb', line 37

def info(msg)
  puts "[#{::Process.pid}] [#{Time.now}] #{msg}"
end

#logfileObject



21
22
23
# File 'lib/legion/process.rb', line 21

def logfile
  options[:logfile]
end

#logfile?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/legion/process.rb', line 29

def logfile?
  !logfile.nil?
end

#pid_status(pidfile) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/legion/process.rb', line 97

def pid_status(pidfile)
  return :exited unless File.exist?(pidfile)

  pid = ::File.read(pidfile).to_i
  return :dead if pid.zero?

  ::Process.kill(0, pid)
  :running
rescue Errno::ESRCH
  :dead
rescue Errno::EPERM
  :not_owned
end

#pidfileObject



25
26
27
# File 'lib/legion/process.rb', line 25

def pidfile
  options[:pidfile]
end

#pidfile?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/legion/process.rb', line 33

def pidfile?
  !pidfile.nil?
end

#run!Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/legion/process.rb', line 41

def run!
  start_time = Time.now
  @options[:time_limit] = @options[:time_limit].to_i if @options.key? :time_limit
  @quit = false
  check_pid
  daemonize if daemonize?
  write_pid
  trap_signals

  until quit
    sleep(1)
    @quit = true if @options.key?(:time_limit) && Time.now - start_time > @options[:time_limit]
  end
  Legion::Logging.info('Legion is shutting down!')
  Legion.shutdown
  Legion::Logging.info('Legion has shutdown. Goodbye!')

  exit
end

#trap_signalsObject



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/legion/process.rb', line 111

def trap_signals
  trap('SIGTERM') do
    info 'sigterm'
  end

  trap('SIGHUP') do
    info 'sithup'
  end
  trap('SIGINT') do
    @quit = true
  end
end

#write_pidObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/legion/process.rb', line 72

def write_pid
  if pidfile?
    begin
      File.open(pidfile, ::File::CREAT | ::File::EXCL | ::File::WRONLY) { |f| f.write(::Process.pid.to_s) }
      at_exit { File.delete(pidfile) if File.exist?(pidfile) }
    rescue Errno::EEXIST
      check_pid
      retry
    end
  end
  false
end