Class: Explorer::ProcessManager

Inherits:
Object
  • Object
show all
Defined in:
lib/explorer/process_manager.rb

Instance Method Summary collapse

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 options={}
  @log_watcher = options.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

Returns:

  • (Boolean)


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

def exists?(label)
  !!@processes[label]
end

#labelsObject



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

#processesObject



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

Returns:

  • (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

#terminateObject



11
12
13
14
15
# File 'lib/explorer/process_manager.rb', line 11

def terminate
  labels.each do |label|
    remove(label)
  end
end