Class: Stormtroopers::Manager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/stormtroopers/manager.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.logger(*args) ⇒ Object



113
114
115
# File 'lib/stormtroopers/manager.rb', line 113

def logger(*args)
  instance.logger(*args)
end

Instance Method Details

#armiesObject



70
71
72
73
74
# File 'lib/stormtroopers/manager.rb', line 70

def armies
  @armies ||= config[:armies].map do |army_config|
    Army.new(army_config)
  end
end

#configObject



76
77
78
# File 'lib/stormtroopers/manager.rb', line 76

def config
  @config ||= HashWithIndifferentAccess.new(YAML.load_file(config_file))
end

#config_fileObject



88
89
90
# File 'lib/stormtroopers/manager.rb', line 88

def config_file
  File.join(working_directory, "config", "stormtroopers.yml")
end

#log_current_statusObject



57
58
59
60
61
62
63
64
# File 'lib/stormtroopers/manager.rb', line 57

def log_current_status
  logger.info "Current status"
  armies.each do |army|
    army.running_troopers_status.each do |status|
      logger.info "#{army.name}: #{status}"
    end
  end
end

#loggerObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/stormtroopers/manager.rb', line 92

def logger
  unless @logger
    log_directory = File.join(working_directory, "log")
    Dir.mkdir(log_directory) unless File.directory?(log_directory)
    @logger = Logger.new(File.join(working_directory, "log", "stormtroopers.log"), 'daily')
    logger.formatter = proc do |severity, datetime, progname, msg|
      "#{datetime.strftime("%Y-%m-%d %H:%M:%S")} #{severity}: #{msg}\n"
    end
  end
  @logger
end

#manageObject

Raises:



20
21
22
23
24
25
26
27
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/stormtroopers/manager.rb', line 20

def manage
  raise AlreadyRunning if managing?
  @managing = true

  Signal.trap("TERM") do
    stop_managing
  end

  Signal.trap("INT") do
    stop_managing
  end

  Signal.trap("USR2") do
    log_current_status
  end

  logger.info "Starting"

  if config[:speed] == "ludicrous"
    logger.info "Ludicrous speed? Sir! we have never gone that fast before, I dont know if the ship can take it! Whats the matter Colonel Sanders? CHICKEN?!"
    puts "Ludicrous speed? Sir! we have never gone that fast before, I dont know if the ship can take it! Whats the matter Colonel Sanders? CHICKEN?!"
    Delayed::Backend::Mongoid::Job.class_eval do
      def self.reserve(worker, max_run_time = Delayed::Worker.max_run_time)
        where(failed_at: nil, locked_at: nil).find_and_modify({"$set" => {locked_at: db_time_now, locked_by: worker.name}}, new: true)
      end
    end
  end

  while managing? do
    assigned = armies.map(&:manage)
    sleep timeout(assigned.include?(true))
  end

  armies.each(&:finish)
  logger.info "Stopped, all running jobs completed"
end

#managing?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/stormtroopers/manager.rb', line 66

def managing?
  @managing || false
end

#stop_managingObject



13
14
15
16
17
18
# File 'lib/stormtroopers/manager.rb', line 13

def stop_managing
  return unless managing?
  logger.info "Stopping, waiting for running jobs to complete"
  log_current_status
  @managing = false
end

#working_directoryObject



80
81
82
83
84
85
86
# File 'lib/stormtroopers/manager.rb', line 80

def working_directory
  if defined?(Rails)
    Rails.root
  else
    Dir.getwd
  end
end