Module: Epi::Jobs

Extended by:
Forwardable
Defined in:
lib/epi/jobs.rb

Overview

Manages running jobs

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.by_pidObject (readonly)

Returns the value of attribute by_pid.



13
14
15
# File 'lib/epi/jobs.rb', line 13

def by_pid
  @by_pid
end

.configuration_filesObject (readonly)

Returns the value of attribute configuration_files.



13
14
15
# File 'lib/epi/jobs.rb', line 13

def configuration_files
  @configuration_files
end

Class Method Details

.beat!Object



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
# File 'lib/epi/jobs.rb', line 21

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(5) { beat! } # TODO: make interval configurable
end

.job_descriptionsObject



76
77
78
# File 'lib/epi/jobs.rb', line 76

def job_descriptions
  configuration_files.values.inject({}) { |all, conf_file| all.merge! conf_file.job_descriptions }
end

.refresh_config!Object



80
81
82
83
84
85
# File 'lib/epi/jobs.rb', line 80

def refresh_config!
  Data.configuration_paths.each do |path|
    configuration_files[path] ||= ConfigurationFile.new(path)
  end
  configuration_files.each_value &:read
end

.reset!Object



15
16
17
18
19
# File 'lib/epi/jobs.rb', line 15

def reset!
  @configuration_files = {}
  @jobs = {}
  @by_pid = {}
end

.running_process_countObject



72
73
74
# File 'lib/epi/jobs.rb', line 72

def running_process_count
  each_value.map(&:running_count).reduce(:+) || 0
end

.shutdown!(&callback) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/epi/jobs.rb', line 56

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