Class: Loops::Engine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEngine

Returns a new instance of Engine.



8
9
10
# File 'lib/loops/engine.rb', line 8

def initialize
  load_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



2
3
4
# File 'lib/loops/engine.rb', line 2

def config
  @config
end

#global_configObject (readonly)

Returns the value of attribute global_config.



6
7
8
# File 'lib/loops/engine.rb', line 6

def global_config
  @global_config
end

#loops_configObject (readonly)

Returns the value of attribute loops_config.



4
5
6
# File 'lib/loops/engine.rb', line 4

def loops_config
  @loops_config
end

Instance Method Details

#debug_loop!(loop_name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/loops/engine.rb', line 57

def debug_loop!(loop_name)
  @pm = Loops::ProcessManager.new(global_config, Loops.logger)
  loop_config = loops_config[loop_name] || {}

  # Adjust loop config values before starting it in debug mode
  loop_config['workers_number'] = 1
  loop_config['debug_loop'] = true

  # Load loop class
  unless klass = load_loop_class(loop_name, loop_config)
    puts "Can't load loop class!"
    return false
  end

  # Start the loop
  start_loop(loop_name, klass, loop_config)
end

#load_configObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/loops/engine.rb', line 12

def load_config
  # load and parse with erb
  raw_config = File.read(Loops.config_file)
  erb_config = ERB.new(raw_config).result

  @config = YAML.load(erb_config)
  @loops_config  = @config['loops']
  @global_config = {
    'poll_period'    => 1,
    'workers_engine' => 'fork'
  }.merge(@config['global'])

  Loops.logger.default_logfile = @global_config['logger'] || $stdout
  Loops.logger.colorful_logs = @global_config['colorful_logs'] || @global_config['colourful_logs']
end

#start_loops!(loops_to_start = []) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/loops/engine.rb', line 28

def start_loops!(loops_to_start = [])
  @running_loops = []
  @pm = Loops::ProcessManager.new(global_config, Loops.logger)

  # Start all loops
  loops_config.each do |name, config|
    next if config['disabled']
    next unless loops_to_start.empty? || loops_to_start.member?(name)

    klass = load_loop_class(name, config)
    next unless klass

    start_loop(name, klass, config)
    @running_loops << name
  end

  # Do not continue if there is nothing to run
  if @running_loops.empty?
    puts 'WARNING: No loops to run! Exiting...'
    return
  end

  # Start monitoring loop
  setup_signals
  @pm.monitor_workers

  info 'Loops are stopped now!'
end