-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatch.rb
159 lines (130 loc) · 4.37 KB
/
dispatch.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# frozen_string_literal: true
module Jobrnr
module Job
# Contains the main event loop
#
# Runs all jobs in the graph to completion.
class Dispatch
require "fileutils"
attr_reader :options
attr_reader :graph
attr_reader :slots
attr_reader :job_queue
attr_reader :pool
attr_reader :stats
attr_reader :plugins
attr_reader :ui
def initialize(options:, graph:, pool:, ui:, stats:, slots:)
@options = options
@graph = graph
@ui = ui
@slots = slots
@stats = stats
@plugins = Jobrnr::Plugins.instance
@job_queue = []
@pool = pool
end
def prerequisites_met?(job)
job.predecessors.all? { |predecessor| predecessor.state.finished? && predecessor.state.passed? }
end
def done?(job_queue, job_pool)
(job_queue.empty? || stop_submission?) && job_pool.empty?
end
def stop_submission?
max_failures_reached || ui.stop_submission?
end
def nothing_todo?(completed, job_queue, slots)
completed.empty? && (
job_queue.empty? ||
slots.available <= 0 ||
stop_submission?
)
end
def ready_to_queue(successors)
successors.select { |successor| !successor.state.queued? && prerequisites_met?(successor) }
end
def enqueue(*jobs)
stats.enqueue(*jobs)
job_queue.push(*jobs)
end
def run
cumulative_completed_instances = []
enqueue(*graph.roots)
FileUtils.mkdir_p(options.output_directory)
until done?(job_queue, pool)
completed = pool.remove_completed
if nothing_todo?(completed, job_queue, slots)
# skip this interval
ui.sleep
next
end
# process completed job instances
cumulative_completed_instances.push(*completed)
completed.each do |job_instance|
ui.post_instance(job_instance)
stats.post_instance(job_instance)
plugins.post_instance(Jobrnr::PostInstanceMessage.new(job_instance, options))
# find new jobs to be queued
if job_instance.success? && job_instance.job.state.finished?
successors_to_queue = ready_to_queue(job_instance.job.successors)
successors_to_queue.each { |successor| successor.state.queue }
enqueue(*successors_to_queue)
end
slots.deallocate(job_instance.slot, options.recycle && job_instance.success?)
end
# launch new job instances
new_instances = stop_submission? ? [] : process_queue(job_queue)
pool.add_and_start(new_instances)
ui.post_interval(stats)
plugins.post_interval(Jobrnr::PostIntervalMessage.new(completed, new_instances, stats, options))
ui.sleep
end
status_code = stats.failed
plugins.post_application(
Jobrnr::PostApplicationMessage.new(
status_code,
cumulative_completed_instances,
stats,
options
)
)
ui.post_application(
early_termination: max_failures_reached && !job_queue.empty?
)
status_code
end
def max_failures_reached
options.max_failures.positive? && stats.failed >= options.max_failures
end
def process_queue(job_queue)
num_jobs_queued = [*job_queue.map(&:state).map(&:to_be_scheduled), 0].reduce(&:+)
num_to_schedule = [num_jobs_queued, slots.available].min
num_to_schedule.times.map do
slot = slots.allocate
job_instance = Jobrnr::Job::Instance.new(
job: job_queue.first,
slot: slot,
log: log_filename(slot)
)
# remove job from queue if all instances scheduled otherwise send to
# the back of the queue to play fair with other jobs
if job_instance.job.state.scheduled?
job_queue.shift
else
job_queue.rotate!
end
plugins.pre_instance(Jobrnr::PreInstanceMessage.new(job_instance, options))
ui.pre_instance(job_instance)
stats.pre_instance
job_instance
end
end
def log_filename(slot)
File.join(
options.output_directory,
slot.to_s
)
end
end
end
end