Class: Fate

Inherits:
Object
  • Object
show all
Includes:
Harp
Defined in:
lib/fate.rb,
lib/fate/repl.rb,
lib/fate/logger.rb,
lib/fate/output.rb,
lib/fate/service.rb,
lib/fate/process_manager.rb

Defined Under Namespace

Modules: Output Classes: MultiLogger, ProcessManager, Service

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec, options = {}) ⇒ Fate

Returns a new instance of Fate.



18
19
20
21
22
23
24
25
26
# File 'lib/fate.rb', line 18

def initialize(spec, options={})
  @service = Service.new(spec, options)
  @completions = @service.completions

  @spec = spec
  @logger = @service.logger["Fate Control"]

  @manager = ProcessManager.new(@service)
end

Instance Attribute Details

#completionsObject (readonly)

Returns the value of attribute completions.



16
17
18
# File 'lib/fate.rb', line 16

def completions
  @completions
end

#loggerObject (readonly)

Returns the value of attribute logger.



16
17
18
# File 'lib/fate.rb', line 16

def logger
  @logger
end

#managerObject (readonly)

Returns the value of attribute manager.



16
17
18
# File 'lib/fate.rb', line 16

def manager
  @manager
end

#serviceObject (readonly)

Returns the value of attribute service.



16
17
18
# File 'lib/fate.rb', line 16

def service
  @service
end

Class Method Details

.start(specification, &block) ⇒ Object



12
13
14
# File 'lib/fate.rb', line 12

def self.start(specification, &block)
  self.new(specification).start(&block)
end

Instance Method Details

#log(*args, &block) ⇒ Object



28
29
30
# File 'lib/fate.rb', line 28

def log(*args, &block)
  @logger.log(*args, &block)
end

#restartObject



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

def restart
  stop
  # FIXME: this is here to prevent redis-server from crying
  sleep 0.5
  start
end

#restart_command(name) ⇒ Object



101
102
103
104
# File 'lib/fate.rb', line 101

def restart_command(name)
  stop_command(name)
  start_command(name)
end

#run(&block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/fate.rb', line 32

def run(&block)
  if start
    if block
      yield(self)
      stop
    end
  else
    logger.error "Failed to start"
  end
end

#runningObject



106
107
108
# File 'lib/fate.rb', line 106

def running
  manager.running
end

#start(command_specs = []) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fate.rb', line 43

def start(command_specs=[])
  if command_specs.size > 0
    command_specs.each do |command_spec|
      self.start_command(command_spec)
    end
  else
    if manager.start_group(@service.commands)
      logger.green "All commands are running."
      true
    else
      false
    end
  end
end

#start_command(command_spec) ⇒ Object



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

def start_command(command_spec)
  names = @service.resolve_commands(command_spec)
  if names.empty?
    puts "No commands found for: #{command_spec}"
  else
    commands = {}
    names.each do |name|
      command = @service.commands[name]
      commands[name] = command
    end
    if manager.start_group(commands)
      logger.green "All commands in '#{command_spec}' running."
    else
      logger.red "Failed to start '#{command_spec}'."
    end
  end
end

#stopObject



58
59
60
61
62
63
# File 'lib/fate.rb', line 58

def stop
  ordered = @service.stop_order(manager.running)
  ordered.each do |name|
    manager.stop_command(name)
  end
end

#stop_command(command_spec) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/fate.rb', line 90

def stop_command(command_spec)
  names = @service.resolve_commands(command_spec)
  if names.empty?
    puts "No commands found for: #{command_spec}"
  else
    names.each do |name|
      manager.stop_command(name)
    end
  end
end

#system(command) ⇒ Object

ad hoc shell out, with rescuing because of some apparent bugs in MRI 1.8.7’s ability to cope with unusual exit codes.



112
113
114
115
116
117
118
# File 'lib/fate.rb', line 112

def system(command)
  begin
    Kernel.system command
  rescue => error
    puts "Exception raised when executing '#{command}': #{error.inspect}"
  end
end