Class: Stormtroopers::Manager
- Inherits:
-
Object
- Object
- Stormtroopers::Manager
- Includes:
- Singleton
- Defined in:
- lib/stormtroopers/manager.rb
Class Method Summary collapse
Instance Method Summary collapse
- #armies ⇒ Object
- #config ⇒ Object
- #config_file ⇒ Object
- #log_current_status ⇒ Object
- #logger ⇒ Object
- #manage ⇒ Object
- #managing? ⇒ Boolean
- #stop_managing ⇒ Object
- #working_directory ⇒ Object
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
#armies ⇒ Object
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 |
#config ⇒ Object
76 77 78 |
# File 'lib/stormtroopers/manager.rb', line 76 def config @config ||= HashWithIndifferentAccess.new(YAML.load_file(config_file)) end |
#config_file ⇒ Object
88 89 90 |
# File 'lib/stormtroopers/manager.rb', line 88 def config_file File.join(working_directory, "config", "stormtroopers.yml") end |
#log_current_status ⇒ Object
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 |
#logger ⇒ Object
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 |
#manage ⇒ Object
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
66 67 68 |
# File 'lib/stormtroopers/manager.rb', line 66 def managing? @managing || false end |
#stop_managing ⇒ Object
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_directory ⇒ Object
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 |