Class: MailReceiver::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/mail-receiver/daemon.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.app_nameObject

Returns the value of attribute app_name.



5
6
7
# File 'lib/mail-receiver/daemon.rb', line 5

def app_name
  @app_name
end

.daemonObject

Returns the value of attribute daemon.



5
6
7
# File 'lib/mail-receiver/daemon.rb', line 5

def daemon
  @daemon
end

.log_fileObject

Returns the value of attribute log_file.



5
6
7
# File 'lib/mail-receiver/daemon.rb', line 5

def log_file
  @log_file
end

.modeObject

Returns the value of attribute mode.



5
6
7
# File 'lib/mail-receiver/daemon.rb', line 5

def mode
  @mode
end

.optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/mail-receiver/daemon.rb', line 5

def options
  @options
end

.pid_fileObject

Returns the value of attribute pid_file.



5
6
7
# File 'lib/mail-receiver/daemon.rb', line 5

def pid_file
  @pid_file
end

.runblockObject

Returns the value of attribute runblock.



5
6
7
# File 'lib/mail-receiver/daemon.rb', line 5

def runblock
  @runblock
end

Class Method Details

.fork_child_process!Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mail-receiver/daemon.rb', line 95

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

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

    if self.daemon == true
      redirect_stdout
    end

    # puts "in child: #{Sails.service.object_id}"
    self.runblock.call
  end
  # http://ruby-doc.org/core-1.9.3/Process.html#detach-method
  Process.detach(pid)
  pid
end

.fork_master_process!Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mail-receiver/daemon.rb', line 69

def fork_master_process!
  fork do
    # WARN: DO NOT CALL Sails IN THIS BLOCK!
    $PROGRAM_NAME = self.app_name + " [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 = {}, &block) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/mail-receiver/daemon.rb', line 7

def init(opts = {}, &block)
  self.app_name = 'gitlab-mail-receiver'
  self.pid_file = File.join(opts[:root], "tmp/pids/gitlab-mail-receiver.pid")
  self.log_file = File.join(opts[:root], 'log/gitlab-mail-receiver.log')
  self.daemon = opts[:daemon]
  self.options = opts
  self.runblock = block
end

.read_pidObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mail-receiver/daemon.rb', line 16

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



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mail-receiver/daemon.rb', line 57

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mail-receiver/daemon.rb', line 30

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

  if self.daemon
    puts "pid_file: #{self.pid_file}"
    puts "log_file: #{self.log_file}"
  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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mail-receiver/daemon.rb', line 119

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