Class: NatsWork::Protocol::MessageBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/natswork/protocol.rb

Overview

Message builder

Class Method Summary collapse

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, options = {})
  {
    type: MessageType::JOB_ERROR,
    version: VERSION,
    job_id: job_id,
    error_code: options[:error_code] || ErrorCode::EXECUTION_ERROR,
    error_message: error.message,
    error_class: error.class.name,
    backtrace: error.backtrace&.first(20),
    metadata: {
      occurred_at: Time.now.utc.iso8601,
      worker_id: options[:worker_id],
      worker_language: 'ruby',
      retry_count: options[:retry_count] || 0,
      retryable: options[:retryable] != false,
      retry_at: options[: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, options = {})
  {
    type: MessageType::JOB_REQUEST,
    version: VERSION,
    job_id: options[:job_id] || generate_uuid,
    job_class: job_class,
    queue: options[:queue] || 'default',
    arguments: arguments,
    metadata: {
      created_at: Time.now.utc.iso8601,
      enqueued_at: options[:enqueued_at] || Time.now.utc.iso8601,
      retry_count: options[:retry_count] || 0,
      max_retries: options[:max_retries] || 3,
      timeout: options[:timeout] || 30,
      language: options[:language] || 'ruby',
      language_version: options[:language_version] || RUBY_VERSION,
      worker_constraints: options[:worker_constraints],
      priority: options[:priority] || 0,
      idempotency_key: options[:idempotency_key],
      correlation_id: options[:correlation_id],
      parent_job_id: options[: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, options = {})
  {
    type: MessageType::JOB_RESPONSE,
    version: VERSION,
    job_id: job_id,
    status: options[:status] || 'success',
    result: result,
    metadata: {
      started_at: options[:started_at],
      completed_at: Time.now.utc.iso8601,
      duration_ms: options[:duration_ms],
      worker_id: options[:worker_id],
      worker_language: 'ruby',
      worker_version: VERSION,
      retry_count: options[: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_uuidObject



229
230
231
232
# File 'lib/natswork/protocol.rb', line 229

def self.generate_uuid
  require 'securerandom'
  SecureRandom.uuid
end