Class: Postburner::TrackedJob

Inherits:
Job show all
Defined in:
app/models/postburner/tracked_job.rb

Overview

Job wrapper for executing tracked ActiveJob instances.

TrackedJob is a Postburner::Job subclass that deserializes and executes ActiveJob instances that opted-in to PostgreSQL tracking via the ‘Postburner::Tracked` concern.

When an ActiveJob with ‘tracked` is enqueued, the adapter:

  1. Creates a TrackedJob record in PostgreSQL

  2. Stores the ActiveJob data in the ‘args` JSONB column

  3. Queues a minimal payload to Beanstalkd with the TrackedJob ID

When the worker executes the job:

  1. Loads the TrackedJob record

  2. Deserializes the ActiveJob from ‘args`

  3. Executes it with full audit trail (logs, timing, errors)

Examples:

# User's ActiveJob (opts in to tracking)
class ProcessPayment < ApplicationJob
  include Postburner::Tracked
  tracked

  def perform(payment_id)
    log "Processing payment #{payment_id}"
    # ...
  end
end

# ActiveJob adapter creates TrackedJob
ProcessPayment.perform_later(123)
# => Creates TrackedJob record, queues to Beanstalkd

# Worker executes
Postburner::Job.perform(tracked_job.id)
# => Deserializes ProcessPayment job and executes with audit trail

Instance Attribute Summary

Attributes inherited from Job

#args, #attempt_count, #attempting_at, #attempts, #bkid, #duration, #errata, #error_count, #id, #lag, #log_count, #logs, #processed_at, #processing_at, #queued_at, #removed_at, #run_at, #sid, #type

Instance Method Summary collapse

Methods inherited from Job

#bk, #bk!, #bk=, #destroy, #destroy!, find_sti_class, #orphaned?

Methods included from Statistics

#elapsed_ms, #intended_at, #stats

Methods included from Execution

#perform!

Methods included from Insertion

#queue!, #requeue!, #will_insert?

Methods included from Commands

#delete!, #extend!, #kick!, #remove!

Methods included from Logging

#log, #log!, #log_exception, #log_exception!

Methods included from Properties

#priority, #queue_name, #retry_delay_for_attempt, #should_retry?, #ttr, #tube_name

Instance Method Details

#perform(args) ⇒ void

This method returns an undefined value.

Executes the wrapped ActiveJob instance.

Deserializes the ActiveJob from args, restores metadata, sets up the bidirectional link for logging, and executes the job.

Parameters:

  • args (Hash)

    JSONB args containing serialized ActiveJob data

Raises:

  • (Exception)

    Any exception raised by the ActiveJob is logged and re-raised



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
# File 'app/models/postburner/tracked_job.rb', line 52

def perform(args)
  # Extract ActiveJob metadata from args
  job_class = args['job_class'].constantize
  arguments = ::ActiveJob::Arguments.deserialize(args['arguments'])

  # Instantiate the ActiveJob
  job = job_class.new(*arguments)

  # Restore ActiveJob metadata
  job.job_id = args['job_id']
  job.queue_name = args['queue_name']
  job.priority = args['priority']
  job.executions = args['executions'] || 0
  job.exception_executions = args['exception_executions'] || {}
  job.locale = args['locale']
  job.timezone = args['timezone']

  if args['enqueued_at']
    job.enqueued_at = Time.iso8601(args['enqueued_at'])
  end

  # Give the ActiveJob access to this Postburner::Job for logging
  if job.respond_to?(:postburner_job=)
    job.postburner_job = self
  end

  # Execute the job (ActiveJob handles retry_on/discard_on)
  # Exceptions are caught by Postburner::Job#perform! and logged to errata
  job.perform_now
end