Class: Sails::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/sails/daemon.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.app_nameObject

Returns the value of attribute app_name


4
5
6
# File 'lib/sails/daemon.rb', line 4

def app_name
  @app_name
end

.daemonObject

Returns the value of attribute daemon


4
5
6
# File 'lib/sails/daemon.rb', line 4

def daemon
  @daemon
end

.modeObject

Returns the value of attribute mode


4
5
6
# File 'lib/sails/daemon.rb', line 4

def mode
  @mode
end

.optionsObject

Returns the value of attribute options


4
5
6
# File 'lib/sails/daemon.rb', line 4

def options
  @options
end

.pid_fileObject

Returns the value of attribute pid_file


4
5
6
# File 'lib/sails/daemon.rb', line 4

def pid_file
  @pid_file
end

Class Method Details

.fork_child_process!Object


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sails/daemon.rb', line 90

def fork_child_process!
  pid = fork do
    $PROGRAM_NAME = self.app_name + " [sails child]"
    Signal.trap("QUIT") do
      exit
    end

    Signal.trap("USR2") do
      # TODO: reload Sails in current process
      exit
    end

    if self.daemon == true
      redirect_stdout
    else
      log_to_stdout
    end
    # puts "in child: #{Sails.service.object_id}"
    Sails.start!(self.mode)
  end
  # http://ruby-doc.org/core-1.9.3/Process.html#detach-method
  Process.detach(pid)
  pid
end

.fork_master_process!Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sails/daemon.rb', line 64

def fork_master_process!
  fork do
    # WARN: DO NOT CALL Sails IN THIS BLOCK!
    $PROGRAM_NAME = self.app_name + " [sails master]"
    @child_pid = fork_child_process!

    Signal.trap("QUIT") do
      Process.kill("QUIT", @child_pid)
      exit
    end

    Signal.trap("USR2") do
      Process.kill("USR2", @child_pid)
    end

    loop do
      sleep 1
      begin
        Process.getpgid(@child_pid)
      rescue Errno::ESRCH
        @child_pid = fork_child_process!
      end
    end
  end
end

.init(opts = {}) ⇒ Object


6
7
8
9
10
11
12
13
# File 'lib/sails/daemon.rb', line 6

def init(opts = {})
  self.app_name = Sails.config.app_name
  self.mode = opts[:mode]
  self.app_name = "#{Sails::Daemon.app_name}-thread" if self.mode == "thread"
  self.pid_file = Sails.root.join("tmp/#{Sails::Daemon.app_name}.pid")
  self.daemon = opts[:daemon]
  self.options = opts
end

.read_pidObject


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sails/daemon.rb', line 15

def read_pid
  if !File.exist?(pid_file)
    return nil
  end

  pid = File.open(pid_file).read.to_i
  begin
    Process.getpgid(pid)
  rescue
    pid = nil
  end
  pid
end

.restart_process(options = {}) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sails/daemon.rb', line 52

def restart_process(options = {})
  old_pid = read_pid
  if old_pid == nil
    puts colorize("#{app_name} process not found on pid #{old_pid}", :red)
    return
  end

  print "Restarting #{app_name}..."
  Process.kill("USR2", old_pid)
  puts colorize(" [OK]", :green)
end

.start_processObject


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sails/daemon.rb', line 29

def start_process
  old_pid = read_pid
  if !old_pid.nil?
    puts colorize("Current have #{app_name} process in running on pid #{old_pid}", :red)
    return
  end

  # start master process
  @master_pid = fork_master_process!
  File.open(pid_file, "w+") do |f|
    f.puts @master_pid
  end

  puts "Started #{app_name} on pid: #{@master_pid}"
  # puts "in init: #{Sails.service.object_id}"

  if not self.daemon
    Process.waitpid(@master_pid)
  else
    exit
  end
end

.stop_processObject


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sails/daemon.rb', line 115

def stop_process
  pid = read_pid
  if pid.nil?
    puts colorize("#{app_name} process not found, pid #{pid}", :red)
    return
  end

  print "Stoping #{app_name}..."
  begin
    Process.kill("QUIT", pid)
  ensure
    File.delete(pid_file)
  end
  puts colorize(" [OK]", :green)
end