Class: Balmora

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

Defined Under Namespace

Classes: Arguments, Cli, Command, Config, Context, Contexts, Error, Extension, Logger, Restart, Shell, State, Stop, Variables

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, extension, state) ⇒ Balmora

Returns a new instance of Balmora.



30
31
32
33
34
# File 'lib/balmora.rb', line 30

def initialize(logger, extension, state)
  @logger = logger
  @extension = extension
  @state = state
end

Class Method Details

.factory(state) ⇒ Object

def self.create(options = {}, arguments = {})

state = Balmora::State.create(options, arguments)
return state.balmora

end



26
27
28
# File 'lib/balmora.rb', line 26

def self.factory(state)
  return self.new(state.logger, state.extension, state)
end

.run(task, arguments = {}, options = {}) ⇒ Object



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

def self.run(task, arguments = {}, options = {})
  state = Balmora::State.create(options, arguments)
  state.balmora.run(task, state)
end

Instance Method Details

#run(task, state) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/balmora.rb', line 36

def run(task, state)
  restarts = state.config.get([:max_restarts], default: 3)

  restarts.
    times() { |index|
    begin
      dir = state.config.get(:chdir, default: Dir.pwd)
      dir = state.variables.inject(dir)
      Dir.chdir(dir) {
        commands = state.config.get([:tasks, task.to_sym(), :commands])
        run_commands(state, commands)
      }
      return 0
    rescue Restart
      @logger.debug("Restarting task (#{restarts - index} attempts left)")
      state = Balmora::State.create(@state.options, @state.arguments)
    end
  }


  raise Error.new("Maximal restart attempts count (#{restarts}) reached")
rescue Stop => stop
  @logger.debug("Stop with status #{stop.status} catched")
  return stop.status
end

#run_command(state, command) ⇒ Object



69
70
71
72
73
# File 'lib/balmora.rb', line 69

def run_command(state, command)
  @extension.
    create_command(state, command).
    execute()
end

#run_commands(state, commands) ⇒ Object



62
63
64
65
66
67
# File 'lib/balmora.rb', line 62

def run_commands(state, commands)
  commands.
    each() { |command|
      run_command(state, command)
    }
end