Module: Daemontools

Defined in:
lib/daemontools.rb,
lib/daemontools/version.rb,
lib/daemontools/service_builder.rb,
lib/daemontools/service_remover.rb

Defined Under Namespace

Classes: Builder, Remover

Constant Summary collapse

VERSION =
"0.2.8"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.log_rootObject

Returns the value of attribute log_root.



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

def log_root
  @log_root
end

.svc_rootObject

Returns the value of attribute svc_root.



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

def svc_root
  @svc_root
end

.tmp_rootObject

Returns the value of attribute tmp_root.



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

def tmp_root
  @tmp_root
end

Class Method Details

.add(name, command, options = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/daemontools.rb', line 88

def self.add(name, command, options = {})
  @name = name
  @command = command
  @log_dir = options[:log_dir] || "#{@log_root}/#{@name}"
  @pre_command = options[:pre_command]
  @sleep = options[:sleep] || 3
  @path = "#{@svc_root}/#{name}"
  @change_user_command = options[:change_user_command]
  @ulimit = options[:ulimit]
  @write_time = options[:write_time]

  if Dir.exists?(@path)
    stop(name)
  else
    Dir.mkdir(@path)
  end
  File.open("#{@path}/down", 'w') {|f| f.write('')}
  Dir.mkdir("#{@path}/log") unless Dir.exists?("#{@path}/log")
  File.open("#{@path}/log/run", 'w', 0755) {|f| f.write(run_template('log.erb'))}
  File.open("#{@path}/run", 'w', 0755) {|f| f.write(run_template('run.erb'))}

  unless options[:not_wait]
    wait_timeout = options[:wait_timeout] || 10
    now = Time.now.to_f
    while `sudo svstat #{@path} 2>&1`.match(/unable to open/i)
      raise "Timeout wait for svc add service" if Time.now.to_f - now > wait_timeout
      sleep 0.1
    end
  end

  true
end

.add_empty(name) ⇒ Object



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

def self.add_empty(name)
  path = "#{@svc_root}/#{name}"
  Dir.mkdir(path) unless Dir.exists?(path)
  File.open("#{path}/down", 'w') {|f| f.write('')}
  now = Time.now.to_f
  while `sudo svstat #{path} 2>&1`.match(/unable to open/i)
    raise "Timeout wait for svc add service" if Time.now.to_f - now > 10
    sleep 0.1
  end
  File.delete("#{path}/down")
  stop(name)
  true
end

.add_empty_tmp(name) ⇒ Object



65
66
67
68
69
# File 'lib/daemontools.rb', line 65

def self.add_empty_tmp(name)
  path = "#{@tmp_root}/daemontools_service_#{name}"
  Dir.mkdir(path) unless Dir.exists?(path)
  true
end

.delete(name, rm_cmd = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/daemontools.rb', line 121

def self.delete(name, rm_cmd = nil)
  return false unless exists?(name)
  stop(name)
  sleep 0.3
  cmd = rm_cmd.nil? ? "sudo rm -rf #{@path} 2>&1" : "#{rm_cmd} #{@path}"
  r = `#{cmd}`
  raise r if $?.exitstatus != 0
  true
end

.down?(name) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/daemontools.rb', line 35

def self.down?(name)
  status(name)[0] == "down"
end

.exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/daemontools.rb', line 15

def self.exists?(name)
  check_service_exists(name, false)
end

.make_run_status_down(name) ⇒ Object



149
150
151
152
153
# File 'lib/daemontools.rb', line 149

def self.make_run_status_down(name)
  check_service_exists(name)
  File.open("#{@path}/down", 'w') {|f| f.write('')}
  true
end

.make_run_status_up(name) ⇒ Object



144
145
146
147
# File 'lib/daemontools.rb', line 144

def self.make_run_status_up(name)
  File.delete("#{@path}/down")
  true
end

.move_tmp(name) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/daemontools.rb', line 71

def self.move_tmp(name)
  tmp_path = "#{@tmp_root}/daemontools_service_#{name}"
  svc_path = "#{@svc_root}/#{name}"

  r = `mv #{tmp_path} #{svc_path}`
  raise r if $?.exitstatus != 0
  raise r if ! r.empty?

  now = Time.now.to_f
  while `sudo svstat #{svc_path} 2>&1`.match(/unable to open/i)
    raise "Timeout wait for svc add service" if Time.now.to_f - now > 10
    sleep 0.1
  end

  true
end

.restart(name) ⇒ Object



47
48
49
# File 'lib/daemontools.rb', line 47

def self.restart(name)
  run_svc(name, 't')
end

.run_status(name) ⇒ Object



131
132
133
134
# File 'lib/daemontools.rb', line 131

def self.run_status(name)
  check_service_exists(name)
  File.exists?("#{@path}/down") ? "down" : "up"
end

.run_status_down?(name) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/daemontools.rb', line 140

def self.run_status_down?(name)
  run_status(name) == "down"
end

.run_status_up?(name) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/daemontools.rb', line 136

def self.run_status_up?(name)
  run_status(name) == "up"
end

.start(name) ⇒ Object



43
44
45
# File 'lib/daemontools.rb', line 43

def self.start(name)
  run_svc(name, 'u')
end

.status(name) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/daemontools.rb', line 23

def self.status(name)
  check_service_exists(name)
  r = `sudo svstat #{@path} 2>&1`
  raise r if $?.exitstatus != 0
  raise "Unknown status" unless r.match(/.*?:\s*(\S+).*\s(\d+) seconds.*/)
  [$1, $2.to_i]
end

.stop(name) ⇒ Object



39
40
41
# File 'lib/daemontools.rb', line 39

def self.stop(name)
  run_svc(name, 'd')
end

.tmp_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/daemontools.rb', line 19

def self.tmp_exists?(name)
  Dir.exists?("#{@tmp_root}/daemontools_service_#{name}")
end

.up?(name) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/daemontools.rb', line 31

def self.up?(name)
  status(name)[0] == "up"
end