Class: Bricolage::JobNetRunner
- Inherits:
-
Object
- Object
- Bricolage::JobNetRunner
- Defined in:
- lib/bricolage/jobnetrunner.rb
Defined Under Namespace
Classes: Options
Constant Summary collapse
- EXIT_SUCCESS =
JobResult::EXIT_SUCCESS
- EXIT_FAILURE =
JobResult::EXIT_FAILURE
- EXIT_ERROR =
JobResult::EXIT_ERROR
Class Method Summary collapse
Instance Method Summary collapse
- #app_name ⇒ Object
- #check_jobs(queue) ⇒ Object
- #clear_queue(opts) ⇒ Object
- #enqueue_jobs(jobnet, queue) ⇒ Object
- #error_exit(msg) ⇒ Object
- #execute_job(ref, queue) ⇒ Object
- #get_queue(opts) ⇒ Object
- #get_queue_file_path(opts) ⇒ Object
-
#initialize ⇒ JobNetRunner
constructor
A new instance of JobNetRunner.
- #list_jobs(queue) ⇒ Object
- #logger ⇒ Object
- #main ⇒ Object
- #make_log_locator(ref, job_start_time) ⇒ Object
- #print_error(msg) ⇒ Object
- #program_name ⇒ Object
- #run_queue(queue) ⇒ Object
- #usage_exit(msg, usage) ⇒ Object
Constructor Details
#initialize ⇒ JobNetRunner
Returns a new instance of JobNetRunner.
27 28 29 30 31 |
# File 'lib/bricolage/jobnetrunner.rb', line 27 def initialize @hooks = ::Bricolage @jobnet_id = nil @jobnet_start_time = Time.now end |
Class Method Details
.main ⇒ Object
22 23 24 25 |
# File 'lib/bricolage/jobnetrunner.rb', line 22 def JobNetRunner.main Application.install_signal_handlers new.main end |
Instance Method Details
#app_name ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/bricolage/jobnetrunner.rb', line 119 def app_name path = @ctx.home_path.realpath while /\A(?:\d+|current|releases)\z/ =~ path.basename.to_s # is Capistrano dirs path = path.dirname end path.basename.to_s end |
#check_jobs(queue) ⇒ Object
142 143 144 145 146 |
# File 'lib/bricolage/jobnetrunner.rb', line 142 def check_jobs(queue) queue.each do |task| Job.load_ref(task.job, @ctx).compile end end |
#clear_queue(opts) ⇒ Object
94 95 96 97 98 |
# File 'lib/bricolage/jobnetrunner.rb', line 94 def clear_queue(opts) if path = get_queue_file_path(opts) FileUtils.rm_f path end end |
#enqueue_jobs(jobnet, queue) ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/bricolage/jobnetrunner.rb', line 127 def enqueue_jobs(jobnet, queue) seq = 1 jobnet.sequential_jobs.each do |ref| queue.enq JobTask.new(ref) seq += 1 end queue.save end |
#error_exit(msg) ⇒ Object
197 198 199 200 |
# File 'lib/bricolage/jobnetrunner.rb', line 197 def error_exit(msg) print_error msg exit 1 end |
#execute_job(ref, queue) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/bricolage/jobnetrunner.rb', line 164 def execute_job(ref, queue) logger.debug "job #{ref}" job_start_time = Time.now job = Job.load_ref(ref, @ctx) job.compile @hooks.run_before_job_hooks(BeforeJobEvent.new(ref)) result = job.execute_in_process(log_locator: make_log_locator(ref, job_start_time)) @hooks.run_after_job_hooks(AfterJobEvent.new(result)) result rescue Exception => ex logger.exception ex logger.error "unexpected error: #{ref} (#{ex.class}: #{ex.})" JobResult.error(ex) end |
#get_queue(opts) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/bricolage/jobnetrunner.rb', line 100 def get_queue(opts) if path = get_queue_file_path(opts) logger.info "queue path: #{path}" FileTaskQueue.restore_if_exist(path) else TaskQueue.new end end |
#get_queue_file_path(opts) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/bricolage/jobnetrunner.rb', line 109 def get_queue_file_path(opts) if opts.queue_path opts.queue_path elsif opts.enable_queue? opts.local_state_dir + 'queue' + "#{app_name}.#{@jobnet_id.tr('/', '.')}" else nil end end |
#list_jobs(queue) ⇒ Object
136 137 138 139 140 |
# File 'lib/bricolage/jobnetrunner.rb', line 136 def list_jobs(queue) queue.each do |task| puts task.job end end |
#logger ⇒ Object
90 91 92 |
# File 'lib/bricolage/jobnetrunner.rb', line 90 def logger @ctx.logger end |
#main ⇒ Object
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 |
# File 'lib/bricolage/jobnetrunner.rb', line 37 def main opts = Options.new(self) @hooks.run_before_option_parsing_hooks(opts) opts.parse!(ARGV) @ctx = Context.for_application(job_path: opts.jobnet_files.first, environment: opts.environment, global_variables: opts.global_variables) opts.(@ctx.) jobnet = RootJobNet.load_auto(@ctx, opts.jobnet_files) @jobnet_id = jobnet.id if opts. puts "jobnet-id=#{@jobnet_id}" puts "jobnet-file=#{opts.jobnet_files.first}" opts.option_pairs.each do |key, value| puts "#{key}=#{value.inspect}" end exit EXIT_SUCCESS end if opts.clear_queue? clear_queue(opts) exit EXIT_SUCCESS end queue = get_queue(opts) if queue.locked? raise ParameterError, "Job queue is still locked. If you are sure to restart jobnet, #{queue.unlock_help}" end unless queue.queued? enqueue_jobs jobnet, queue end if opts.list_jobs? list_jobs queue exit EXIT_SUCCESS end check_jobs queue if opts.check_only? puts "OK" exit EXIT_SUCCESS end @log_locator_builder = LogLocatorBuilder.(@ctx, opts.log_path_format, opts.log_s3_ds, opts.log_s3_key_format) run_queue queue exit EXIT_SUCCESS rescue OptionError => ex raise if $DEBUG usage_exit ex., opts.help rescue ApplicationError => ex raise if $DEBUG error_exit ex. end |
#make_log_locator(ref, job_start_time) ⇒ Object
179 180 181 182 183 184 185 186 |
# File 'lib/bricolage/jobnetrunner.rb', line 179 def make_log_locator(ref, job_start_time) @log_locator_builder.build( job_ref: ref, jobnet_id: @jobnet_id, job_start_time: job_start_time, jobnet_start_time: @jobnet_start_time ) end |
#print_error(msg) ⇒ Object
202 203 204 |
# File 'lib/bricolage/jobnetrunner.rb', line 202 def print_error(msg) $stderr.puts "#{program_name}: error: #{msg}" end |
#program_name ⇒ Object
206 207 208 |
# File 'lib/bricolage/jobnetrunner.rb', line 206 def program_name File.basename($PROGRAM_NAME, '.*') end |
#run_queue(queue) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/bricolage/jobnetrunner.rb', line 148 def run_queue(queue) @hooks.run_before_all_jobs_hooks(BeforeAllJobsEvent.new(@jobnet_id, queue)) queue.consume_each do |task| result = execute_job(task.job, queue) unless result.success? logger.elapsed_time 'jobnet total: ', (Time.now - @jobnet_start_time) logger.error "[job #{task.job}] #{result.}" @hooks.run_after_all_jobs_hooks(AfterAllJobsEvent.new(false, queue)) exit result.status end end @hooks.run_after_all_jobs_hooks(AfterAllJobsEvent.new(true, queue)) logger.elapsed_time 'jobnet total: ', (Time.now - @jobnet_start_time) logger.info "status all green" end |
#usage_exit(msg, usage) ⇒ Object
191 192 193 194 195 |
# File 'lib/bricolage/jobnetrunner.rb', line 191 def usage_exit(msg, usage) print_error msg $stderr.puts usage exit 1 end |