Class: MiddleManagement::Manager

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

Class Method Summary collapse

Class Method Details

.enforce_number_of_current_jobs(_num_workers_last_set_at = nil, _last_enforcement_job_set_for = nil) ⇒ Object

ToDo: Figure out what to do about caching worker counts



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/middle_management/manager.rb', line 8

def self.enforce_number_of_current_jobs(_num_workers_last_set_at = nil, _last_enforcement_job_set_for = nil)
  self.current_worker_count = self.get_heroku_client.info(MiddleManagement::Config::HEROKU_APP)[:workers].to_i
  # Do nothing if our worker figures are up-to-date
  return if !self.num_jobs_changes_worker_count?(self.current_jobs_count)
  
  # Load cached values if they're supplied. This is to prevent different instances
  # from re-querying the heroku API.
  self.num_workers_last_set_at = _num_workers_last_set_at unless _num_workers_last_set_at.nil?
  self.last_enforcement_job_set_for = _last_enforcement_job_set_for unless _last_enforcement_job_set_for.nil?
  
  # Set number of workers if we haven't set it recently
  if self.num_workers_last_set_at.nil? || self.num_workers_last_set_at < 10.seconds.ago || self.current_worker_count.nil? || self.current_worker_count == 0
    self.set_num_workers(self.calculate_needed_worker_count(self.current_jobs_count))
    self.num_workers_last_set_at = Time.now
  else
    # Schedule job with cached data if we don't have one scheduled
    if self.last_enforcement_job_set_for.nil? || Time.now > self.last_enforcement_job_set_for # Prevent multiple jobs within our window
      self.last_enforcement_job_set_for = Time.now + 10.seconds
      self.delay(:run_at => self.last_enforcement_job_set_for).enforce_number_of_current_jobs(self.num_workers_last_set_at, self.last_enforcement_job_set_for)
      # Make sure we have at least one worker running to cover the job.
      self.set_num_workers(1) if self.current_worker_count == 0
    end
  end
end

.track_completionObject



37
38
39
# File 'lib/middle_management/manager.rb', line 37

def self.track_completion
  self.current_jobs_count -= 1
end

.track_creationObject



33
34
35
# File 'lib/middle_management/manager.rb', line 33

def self.track_creation
  self.current_jobs_count += 1
end