Class: NatsWork::JobExecutor
- Inherits:
-
Object
- Object
- NatsWork::JobExecutor
- Defined in:
- lib/natswork/job_executor.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#metrics ⇒ Object
readonly
Returns the value of attribute metrics.
-
#worker_name ⇒ Object
readonly
Returns the value of attribute worker_name.
Instance Method Summary collapse
- #add_error_handler(&block) ⇒ Object
- #add_job_filter(type, &block) ⇒ Object
- #add_middleware(middleware) ⇒ Object
- #execute_job(job_message) ⇒ Object
-
#initialize(worker_name, options = {}) ⇒ JobExecutor
constructor
A new instance of JobExecutor.
- #on_job_failure(&block) ⇒ Object
- #on_job_retry(&block) ⇒ Object
Constructor Details
#initialize(worker_name, options = {}) ⇒ JobExecutor
Returns a new instance of JobExecutor.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/natswork/job_executor.rb', line 12 def initialize(worker_name, = {}) @worker_name = worker_name @logger = [:logger] || Logger.new(namespace: "worker.#{worker_name}") @metrics = [:metrics] || Metrics.global @middleware = [:middleware] || [] @error_handlers = [] @job_filters = { before: [], after: [], around: [] } @skip_instrumentation = [:skip_instrumentation] || false setup_instrumentation unless @skip_instrumentation end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/natswork/job_executor.rb', line 10 def logger @logger end |
#metrics ⇒ Object (readonly)
Returns the value of attribute metrics.
10 11 12 |
# File 'lib/natswork/job_executor.rb', line 10 def metrics @metrics end |
#worker_name ⇒ Object (readonly)
Returns the value of attribute worker_name.
10 11 12 |
# File 'lib/natswork/job_executor.rb', line 10 def worker_name @worker_name end |
Instance Method Details
#add_error_handler(&block) ⇒ Object
124 125 126 |
# File 'lib/natswork/job_executor.rb', line 124 def add_error_handler(&block) @error_handlers << block if block_given? end |
#add_job_filter(type, &block) ⇒ Object
128 129 130 |
# File 'lib/natswork/job_executor.rb', line 128 def add_job_filter(type, &block) @job_filters[type.to_sym] << block if block_given? end |
#add_middleware(middleware) ⇒ Object
120 121 122 |
# File 'lib/natswork/job_executor.rb', line 120 def add_middleware(middleware) @middleware << middleware end |
#execute_job(job_message) ⇒ Object
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 |
# File 'lib/natswork/job_executor.rb', line 28 def execute_job() job_id = ['job_id'] job_class = ['job_class'] arguments = ['arguments'] || [] context = { job_id: job_id, job_class: job_class, worker: @worker_name, queue: ['queue'], retry_count: ['retry_count'] || 0 } filtered_args = filter_sensitive_data(arguments) @logger.info('Starting job', context.merge( arguments: filtered_args, enqueued_at: ['created_at'] )) @metrics.increment('jobs.started', 1, queue: ['queue'], job_class: job_class) result = nil start_time = Time.now begin # Instrument the job execution Instrumentation.instrument('job.execute', context) do # Run before filters run_filters(:before, , context) # Execute the actual job result = @metrics.time('job.execution_time', queue: ['queue'], job_class: job_class) do perform_job(job_class, arguments) end # Run after filters run_filters(:after, , context.merge(result: result)) result end duration_ms = ((Time.now - start_time) * 1000).round(2) @logger.info('Completed job', context.merge( duration_ms: duration_ms, result_class: result.class.name )) @metrics.increment('jobs.completed', 1, queue: ['queue'], job_class: job_class, status: 'success') @metrics.histogram('job.duration', duration_ms, queue: ['queue'], job_class: job_class) result rescue StandardError => e duration_ms = ((Time.now - start_time) * 1000).round(2) error_context = context.merge( duration_ms: duration_ms, error_class: e.class.name, error_message: e., backtrace: e.backtrace&.first(10) ) @logger.error('Job failed', error_context) @metrics.increment('jobs.failed', 1, queue: ['queue'], job_class: job_class, error_class: e.class.name) @metrics.histogram('job.duration', duration_ms, queue: ['queue'], job_class: job_class, status: 'error') # Run error handlers handle_job_error(e, , context) raise e end end |
#on_job_failure(&block) ⇒ Object
136 137 138 |
# File 'lib/natswork/job_executor.rb', line 136 def on_job_failure(&block) add_error_handler(&block) end |
#on_job_retry(&block) ⇒ Object
132 133 134 |
# File 'lib/natswork/job_executor.rb', line 132 def on_job_retry(&block) add_job_filter(:retry, &block) end |