Class: Opener::Daemons::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/opener/daemons/controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Controller

Returns a new instance of Controller.



16
17
18
19
20
# File 'lib/opener/daemons/controller.rb', line 16

def initialize(options={})
  @exec_path = options.fetch(:exec_path)
  @name = determine_name(options[:name])
  read_commandline
end

Instance Attribute Details

#exec_pathObject (readonly)

Returns the value of attribute exec_path.



14
15
16
# File 'lib/opener/daemons/controller.rb', line 14

def exec_path
  @exec_path
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/opener/daemons/controller.rb', line 14

def name
  @name
end

Instance Method Details

#create_pid(pid) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/opener/daemons/controller.rb', line 62

def create_pid(pid)
  begin
    open(pid_path, 'w') do |f|
      f.puts pid
    end
  rescue => e
    STDERR.puts "Error: Unable to open #{pid_path} for writing:\n\t" +
      "(#{e.class}) #{e.message}"
    exit!
  end
end

#determine_name(name) ⇒ Object



22
23
24
25
# File 'lib/opener/daemons/controller.rb', line 22

def determine_name(name)
  return identify(name) unless name.nil?
  get_name_from_exec_path
end

#get_name_from_exec_pathObject



27
28
29
# File 'lib/opener/daemons/controller.rb', line 27

def get_name_from_exec_path
  File.basename(exec_path, ".rb")
end

#get_pidObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/opener/daemons/controller.rb', line 74

def get_pid
  pid = false
  begin
    open(pid_path, 'r') do |f|
      pid = f.readline
      pid = pid.to_s.gsub(/[^0-9]/,'')
    end
  rescue => e
    STDOUT.puts "Info: Unable to open #{pid_path} for reading while checking for existing PID:\n\t" +
      "(#{e.class}) #{e.message}"
  end


  if pid
    return pid.to_i
  else
    return nil
  end
end

#identify(string) ⇒ Object



161
162
163
# File 'lib/opener/daemons/controller.rb', line 161

def identify(string)
  string.gsub(/\W/,"-").gsub("--","-")
end

#optionsObject



44
45
46
47
48
# File 'lib/opener/daemons/controller.rb', line 44

def options
  return @options if @options
  args = ARGV.dup
  @options = Opener::Daemons::OptParser.pre_parse!(args)
end

#pid_pathObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/opener/daemons/controller.rb', line 51

def pid_path
  return @pid_path unless @pid_path.nil?
  @pid_path = if options[:pid]
                File.expand_path(@options[:pid])
              elsif options[:pidpath]
                File.expand_path(File.join(@options[:pidpath], "#{name}.pid"))
              else
                "/var/run/#{File.basename($0, ".rb")}.pid"
              end
end

#process_exists?Boolean

Returns:

  • (Boolean)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/opener/daemons/controller.rb', line 104

def process_exists?
  begin
    pid = get_pid
    return false unless pid
    Process.kill(0, pid)
    true
  rescue Errno::ESRCH, TypeError # "PID is NOT running or is zombied
    false
  rescue Errno::EPERM
    STDERR.puts "No permission to query #{pid}!";
    false
  rescue => e
    STDERR.puts "Error: Unable to determine status for pid: #{pid}.\n\t" +
      "(#{e.class}) #{e.message}"
    false
  end
end

#read_commandlineObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/opener/daemons/controller.rb', line 31

def read_commandline
  if ARGV[0] == 'start'
    start
  elsif ARGV[0] == 'stop'
    stop
  elsif ARGV[0] == 'restart'
    stop
    start
  else
    start_foreground
  end
end

#remove_pidfileObject



94
95
96
97
98
99
100
101
102
# File 'lib/opener/daemons/controller.rb', line 94

def remove_pidfile
  begin
    File.unlink(pid_path)
  rescue => e
    STDERR.puts "ERROR: Unable to unlink #{path}:\n\t" +
      "(#{e.class}) #{e.message}"
    exit
  end
end

#startObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/opener/daemons/controller.rb', line 139

def start
  if process_exists?
    STDERR.puts "Error: The process #{name} already running. Restarting the process"
    stop
  end

  STDOUT.puts "Starting DAEMON"
  pid = Spoon.spawnp exec_path, *ARGV
  STDOUT.puts "Started DAEMON"
  create_pid(pid)
  begin
    Process.setsid
  rescue Errno::EPERM => e
    STDERR.puts "Process.setsid not permitted on this platform, not critical. Continuing normal operations.\n\t (#{e.class}) #{e.message}"
  end
  File::umask(0)
end

#start_foregroundObject



157
158
159
# File 'lib/opener/daemons/controller.rb', line 157

def start_foreground
  exec [exec_path, ARGV].flatten.join(" ")
end

#stopObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/opener/daemons/controller.rb', line 122

def stop
  begin
    pid = get_pid
    STDOUT.puts "Stopping pid: #{pid}"
    while true do
      Process.kill("TERM", pid)
      Process.wait(pid)
      sleep(0.1)
    end
  rescue Errno::ESRCH # no more process to kill
    remove_pidfile
    STDOUT.puts 'Stopped the process'
  rescue => e
    STDERR.puts "Unable to terminate process: (#{e.class}) #{e.message}"
  end
end