module Epi::Jobs

Manages running jobs

Attributes

by_pid[R]
configuration_files[R]

Public Class Methods

beat!() click to toggle source
# File lib/epi/jobs.rb, line 29
def beat!

  # Cancel any scheduled beats
  EventMachine.cancel_timer @next_beat if @next_beat

  # Make sure configuration files have been read
  refresh_config!

  # Snapshot currently running processes
  ProcessStatus.take!

  # Get rid of jobs for config files that have been removed
  clean_configuration_files!

  # Create new jobs
  make_new_jobs!

  # Sync each job with its expectations
  each_value &:sync!

  # Snapshot processes again, so that triggers have access to
  # newly-spawned processes
  ProcessStatus.take!

  # Run job triggers
  each_value &:run_triggers!

  # Write state of each job to data file
  Data.jobs = map { |id, job| [id.to_s, job.state] }.to_h
  Data.save

  # Schedule the next beat
  @next_beat = EventMachine.add_timer(interval) { beat! }
end
interval() click to toggle source
# File lib/epi/jobs.rb, line 21
def interval
  if @interval.nil?
    @interval = (ENV['EPI_INTERVAL'] || 5).to_f
    Epi.logger.info "Polling process status every #{@interval} second#{@interval == 1 ? '' : 's'}"
  end
  @interval
end
job_descriptions() click to toggle source
# File lib/epi/jobs.rb, line 84
def job_descriptions
  configuration_files.values.inject({}) { |all, conf_file| all.merge! conf_file.job_descriptions }
end
refresh_config!() click to toggle source
# File lib/epi/jobs.rb, line 88
def refresh_config!
  Data.configuration_paths.each do |path|
    configuration_files[path] ||= ConfigurationFile.new(path)
  end
  configuration_files.each_value &:read
end
reset!() click to toggle source
# File lib/epi/jobs.rb, line 15
def reset!
  @configuration_files = {}
  @jobs = {}
  @by_pid = {}
end
running_process_count() click to toggle source
# File lib/epi/jobs.rb, line 80
def running_process_count
  each_value.map(&:running_count).reduce(:+) || 0
end
shutdown!(&callback) click to toggle source
# File lib/epi/jobs.rb, line 64
def shutdown!(&callback)
  EventMachine.cancel_timer @next_beat if @next_beat
  ProcessStatus.take!
  remaining = count
  if remaining > 0
    each_value do |job|
      job.shutdown! do
        remaining -= 1
        callback.call if callback && remaining == 0
      end
    end
  else
    callback.call if callback
  end
end

Private Class Methods

clean_configuration_files!() click to toggle source
# File lib/epi/jobs.rb, line 97
def clean_configuration_files!
  to_remove = configuration_files.keys - Data.configuration_paths
  to_remove.each do |path|
    configuration_files.delete(path).job_descriptions.each_key do |job_id|
      job = delete(job_id)
      job.terminate! if job
    end
  end
end
make_new_jobs!() click to toggle source
# File lib/epi/jobs.rb, line 107
def make_new_jobs!
  job_descriptions.each do |name, description|
    self[name] ||= Epi::Job.new(description, Data.jobs[name] || {})
  end
end