Class: Daemontools::Service
- Inherits:
-
Object
- Object
- Daemontools::Service
- Defined in:
- lib/daemontools/service.rb
Constant Summary collapse
- CACHED_SERVICES =
{}
Class Method Summary collapse
Instance Method Summary collapse
-
#add(command, options) ⇒ Object
Actions.
-
#add_empty ⇒ Object
Tmp Actions.
- #add_empty_tmp ⇒ Object
- #check_service_exists(raise_error = true) ⇒ Object
- #delete(rm_cmd) ⇒ Object
- #down? ⇒ Boolean
-
#initialize(name) ⇒ Service
constructor
A new instance of Service.
- #move_tmp ⇒ Object
- #restart ⇒ Object
-
#run_status ⇒ Object
Run States.
- #run_status_down! ⇒ Object
- #run_status_down? ⇒ Boolean
- #run_status_up! ⇒ Object
- #run_status_up? ⇒ Boolean
- #start ⇒ Object
-
#status ⇒ Object
Statuses.
- #stop ⇒ Object
- #tmp_exists? ⇒ Boolean
- #up? ⇒ Boolean
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, ) (command: command, **) 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([:wait_timeout]) unless [:not_wait] true end |
#add_empty ⇒ Object
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_tmp ⇒ Object
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
74 75 76 |
# File 'lib/daemontools/service.rb', line 74 def down? status[0] == 'down' end |
#move_tmp ⇒ Object
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 |
#restart ⇒ Object
55 56 57 |
# File 'lib/daemontools/service.rb', line 55 def restart run_svc('t') end |
#run_status ⇒ Object
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
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
90 91 92 |
# File 'lib/daemontools/service.rb', line 90 def run_status_up? run_status == 'up' end |
#start ⇒ Object
51 52 53 |
# File 'lib/daemontools/service.rb', line 51 def start run_svc('u') end |
#status ⇒ Object
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 |
#stop ⇒ Object
47 48 49 |
# File 'lib/daemontools/service.rb', line 47 def stop run_svc('d') end |
#tmp_exists? ⇒ 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
70 71 72 |
# File 'lib/daemontools/service.rb', line 70 def up? status[0] == 'up' end |