Class: Daemontools::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/daemontools/service.rb

Constant Summary collapse

CACHED_SERVICES =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Service

Returns a new instance of Service.



10
11
12
13
14
# File 'lib/daemontools/service.rb', line 10

def initialize(name)
  @name = name
  @path = "#{Daemontools.svc_root}/#{name}"
  @log_path = "#{@path}/log"
end

Class Method Details

.[](name) ⇒ Object



6
7
8
# File 'lib/daemontools/service.rb', line 6

def self.[](name)
  CACHED_SERVICES[name] ||= new(name)
end

Instance Method Details

#add(command, options) ⇒ Object

Actions



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/daemontools/service.rb', line 18

def add(command, options)
  apply_options(command: command, **options)

  Dir.exist?(@path) ? stop : Dir.mkdir(@path)
  Dir.mkdir(@log_path) unless Dir.exist?(@log_path)

  File.open("#{@path}/down", 'w') { |f| f.write('') }
  File.open("#{@log_path}/down", 'w') { |f| f.write('') }
  File.open("#{@log_path}/run", 'w', 0o755) { |f| f.write(run_template('log.erb')) }
  File.open("#{@path}/run", 'w', 0o755) { |f| f.write(run_template('run.erb')) }

  wait_start(options[:wait_timeout]) unless options[:not_wait]

  true
end

#add_emptyObject

Tmp Actions



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/daemontools/service.rb', line 114

def add_empty
  Dir.mkdir(@path) unless Dir.exist?(@path)
  File.open("#{@path}/down", 'w') { |f| f.write('') }

  wait_start(10)

  File.delete("#{@path}/down")
  stop

  true
end

#add_empty_tmpObject



126
127
128
129
130
131
# File 'lib/daemontools/service.rb', line 126

def add_empty_tmp
  path = "#{Daemontools.tmp_root}/daemontools_service_#{@name}"
  Dir.mkdir(path) unless Dir.exist?(path)

  true
end

#check_service_exists(raise_error = true) ⇒ Object



78
79
80
81
# File 'lib/daemontools/service.rb', line 78

def check_service_exists(raise_error = true)
  exists = Dir.exist?(@path)
  raise_error && !exists ? raise("Service #{@name} not exists") : exists
end

#delete(rm_cmd) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/daemontools/service.rb', line 34

def delete(rm_cmd)
  return false unless check_service_exists(false)

  stop
  sleep 0.3
  cmd = rm_cmd.nil? ? "sudo rm -rf #{@path} 2>&1" : "#{rm_cmd} #{@path}"
  r = `#{cmd}`
  raise r if $?.exitstatus != 0

  CACHED_SERVICES.delete(@name)
  true
end

#down?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/daemontools/service.rb', line 74

def down?
  status[0] == 'down'
end

#move_tmpObject



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/daemontools/service.rb', line 133

def move_tmp
  tmp_path = "#{Daemontools.tmp_root}/daemontools_service_#{@name}"
  svc_path = @path

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

  wait_start(10)

  true
end

#restartObject



55
56
57
# File 'lib/daemontools/service.rb', line 55

def restart
  run_svc('t')
end

#run_statusObject

Run States



85
86
87
88
# File 'lib/daemontools/service.rb', line 85

def run_status
  check_service_exists
  File.exist?("#{@path}/down") ? 'down' : 'up'
end

#run_status_down!Object



104
105
106
107
108
109
110
# File 'lib/daemontools/service.rb', line 104

def run_status_down!
  check_service_exists
  File.open("#{@path}/down", 'w') { |f| f.write('') }
  File.open("#{@log_path}/down", 'w') { |f| f.write('') } if Dir.exist?(@log_path)

  true
end

#run_status_down?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/daemontools/service.rb', line 94

def run_status_down?
  run_status == 'down'
end

#run_status_up!Object



98
99
100
101
102
# File 'lib/daemontools/service.rb', line 98

def run_status_up!
  File.delete("#{@path}/down")
  File.delete("#{@log_path}/down") if Dir.exist?(@log_path)
  true
end

#run_status_up?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/daemontools/service.rb', line 90

def run_status_up?
  run_status == 'up'
end

#startObject



51
52
53
# File 'lib/daemontools/service.rb', line 51

def start
  run_svc('u')
end

#statusObject

Statuses



61
62
63
64
65
66
67
68
# File 'lib/daemontools/service.rb', line 61

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

  [::Regexp.last_match(1), ::Regexp.last_match(2).to_i]
end

#stopObject



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

def stop
  run_svc('d')
end

#tmp_exists?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/daemontools/service.rb', line 146

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

#up?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/daemontools/service.rb', line 70

def up?
  status[0] == 'up'
end