Class: Explorer::ProcessManager
- Inherits:
-
Object
- Object
- Explorer::ProcessManager
- Defined in:
- lib/explorer/process_manager.rb
Instance Method Summary collapse
- #add(label, command, working_dir: ENV['PWD']) ⇒ Object
- #exists?(label) ⇒ Boolean
-
#initialize(options = {}) ⇒ ProcessManager
constructor
A new instance of ProcessManager.
- #labels ⇒ Object
- #load(file) ⇒ Object
- #processes ⇒ Object
- #remove(label) ⇒ Object
- #restart(label) ⇒ Object
- #running?(label) ⇒ Boolean
- #save(file) ⇒ Object
- #start(label) ⇒ Object
- #stop(label) ⇒ Object
- #terminate ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ProcessManager
Returns a new instance of ProcessManager.
6 7 8 9 |
# File 'lib/explorer/process_manager.rb', line 6 def initialize ={} @log_watcher = .fetch(:log_watcher) { Explorer.log_watcher } @processes = {} end |
Instance Method Details
#add(label, command, working_dir: ENV['PWD']) ⇒ Object
17 18 19 20 |
# File 'lib/explorer/process_manager.rb', line 17 def add(label, command, working_dir: ENV['PWD']) env = load_env(working_dir) @processes[label] = Process.new(label, command, working_dir: working_dir, log_watcher: @log_watcher, env: env) end |
#exists?(label) ⇒ Boolean
43 44 45 |
# File 'lib/explorer/process_manager.rb', line 43 def exists?(label) !!@processes[label] end |
#labels ⇒ Object
56 57 58 |
# File 'lib/explorer/process_manager.rb', line 56 def labels @processes.keys end |
#load(file) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/explorer/process_manager.rb', line 60 def load file return unless File.exist? file yaml = YAML.load_file file yaml.each do |cfg| add(cfg[:label], cfg[:command], working_dir: cfg[:working_dir]) end end |
#processes ⇒ Object
52 53 54 |
# File 'lib/explorer/process_manager.rb', line 52 def processes @processes.values end |
#remove(label) ⇒ Object
22 23 24 25 26 |
# File 'lib/explorer/process_manager.rb', line 22 def remove(label) fail "Label is unknown" unless @processes[label] @processes[label].terminate @processes.delete label end |
#restart(label) ⇒ Object
38 39 40 41 |
# File 'lib/explorer/process_manager.rb', line 38 def restart(label) stop(label) start(label) end |
#running?(label) ⇒ Boolean
47 48 49 50 |
# File 'lib/explorer/process_manager.rb', line 47 def running?(label) fail "Label is unknown" unless @processes[label] @processes[label].started? end |
#save(file) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/explorer/process_manager.rb', line 69 def save file Dir.mkdir File.dirname(file) unless Dir.exist?(File.dirname(file)) File.write file, YAML.dump(processes.map do |p| { label: p.label, command: p.command, working_dir: p.working_dir } end) end |
#start(label) ⇒ Object
28 29 30 31 |
# File 'lib/explorer/process_manager.rb', line 28 def start(label) fail "Label is unknown" unless @processes[label] @processes[label].start end |
#stop(label) ⇒ Object
33 34 35 36 |
# File 'lib/explorer/process_manager.rb', line 33 def stop(label) fail "Label is unknown" unless @processes[label] @processes[label].stop end |
#terminate ⇒ Object
11 12 13 14 15 |
# File 'lib/explorer/process_manager.rb', line 11 def terminate labels.each do |label| remove(label) end end |