Class: NatsWork::Protocol::MessageBuilder
- Inherits:
-
Object
- Object
- NatsWork::Protocol::MessageBuilder
- Defined in:
- lib/natswork/protocol.rb
Overview
Message builder
Class Method Summary collapse
- .build_job_error(job_id, error, options = {}) ⇒ Object
- .build_job_request(job_class, arguments, options = {}) ⇒ Object
- .build_job_response(job_id, result, options = {}) ⇒ Object
- .build_worker_heartbeat(worker_id, stats, capabilities) ⇒ Object
- .generate_uuid ⇒ Object
Class Method Details
.build_job_error(job_id, error, options = {}) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/natswork/protocol.rb', line 181 def self.build_job_error(job_id, error, = {}) { type: MessageType::JOB_ERROR, version: VERSION, job_id: job_id, error_code: [:error_code] || ErrorCode::EXECUTION_ERROR, error_message: error., error_class: error.class.name, backtrace: error.backtrace&.first(20), metadata: { occurred_at: Time.now.utc.iso8601, worker_id: [:worker_id], worker_language: 'ruby', retry_count: [:retry_count] || 0, retryable: [:retryable] != false, retry_at: [:retry_at] }.compact } end |
.build_job_request(job_class, arguments, options = {}) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/natswork/protocol.rb', line 137 def self.build_job_request(job_class, arguments, = {}) { type: MessageType::JOB_REQUEST, version: VERSION, job_id: [:job_id] || generate_uuid, job_class: job_class, queue: [:queue] || 'default', arguments: arguments, metadata: { created_at: Time.now.utc.iso8601, enqueued_at: [:enqueued_at] || Time.now.utc.iso8601, retry_count: [:retry_count] || 0, max_retries: [:max_retries] || 3, timeout: [:timeout] || 30, language: [:language] || 'ruby', language_version: [:language_version] || RUBY_VERSION, worker_constraints: [:worker_constraints], priority: [:priority] || 0, idempotency_key: [:idempotency_key], correlation_id: [:correlation_id], parent_job_id: [:parent_job_id] }.compact } end |
.build_job_response(job_id, result, options = {}) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/natswork/protocol.rb', line 162 def self.build_job_response(job_id, result, = {}) { type: MessageType::JOB_RESPONSE, version: VERSION, job_id: job_id, status: [:status] || 'success', result: result, metadata: { started_at: [:started_at], completed_at: Time.now.utc.iso8601, duration_ms: [:duration_ms], worker_id: [:worker_id], worker_language: 'ruby', worker_version: VERSION, retry_count: [:retry_count] || 0 }.compact } end |
.build_worker_heartbeat(worker_id, stats, capabilities) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/natswork/protocol.rb', line 201 def self.build_worker_heartbeat(worker_id, stats, capabilities) { type: MessageType::WORKER_HEARTBEAT, version: VERSION, worker_id: worker_id, timestamp: Time.now.utc.iso8601, status: stats[:status] || 'running', stats: { jobs_processed: stats[:jobs_processed] || 0, jobs_failed: stats[:jobs_failed] || 0, active_jobs: stats[:active_jobs] || 0, queues: stats[:queues] || [], concurrency: stats[:concurrency] || 1, memory_usage: stats[:memory_usage], cpu_usage: stats[:cpu_usage], uptime_seconds: stats[:uptime_seconds] }.compact, capabilities: { language: 'ruby', language_version: RUBY_VERSION, protocol_version: VERSION, supported_job_types: capabilities[:supported_job_types], max_job_size: capabilities[:max_job_size] || 10_485_760, # 10MB default features: capabilities[:features] || [] }.compact } end |
.generate_uuid ⇒ Object
229 230 231 232 |
# File 'lib/natswork/protocol.rb', line 229 def self.generate_uuid require 'securerandom' SecureRandom.uuid end |