Class: LogStruct::Log::GoodJob

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Includes:
Interfaces::AdditionalDataField, Interfaces::CommonFields, MergeAdditionalDataFields, SerializeCommon
Defined in:
lib/log_struct/log/good_job.rb

Overview

GoodJob log entry for structured logging

GoodJob is a PostgreSQL-based ActiveJob backend that provides reliable, scalable job processing for Rails applications. This log class captures GoodJob-specific events including job execution, database operations, error handling, and performance metrics.

Key Features Logged:

  • Job execution lifecycle (enqueue, start, finish, retry)
  • Database-backed job persistence events
  • Error handling and retry logic
  • Job batching and bulk operations
  • Performance metrics and timing data
  • Thread and process information

Usage Examples:

# Job execution logging
LogStruct::Log::GoodJob.new(
  event: Event::Start,
  job_id: "job_123",
  job_class: "UserNotificationJob",
  queue_name: "default",
  execution_time: 1.5
)

# Error logging
LogStruct::Log::GoodJob.new(
  event: Event::Error,
  job_id: "job_123",
  error_class: "StandardError",
  error_message: "Connection failed"
)

Constant Summary collapse

GoodJobEvent =

Valid event types for GoodJob operations

T.type_alias {
  T.any(
    Event::Log,         # General logging
    Event::Enqueue,     # Job queued
    Event::Start,       # Job execution started
    Event::Finish,      # Job completed successfully
    Event::Error,       # Job failed with error
    Event::Schedule     # Job scheduled for future execution
  )
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MergeAdditionalDataFields

#merge_additional_data_fields

Methods included from SerializeCommon

#as_json, #serialize_common

Constructor Details

#initialize(source: T.let(Source::Job, Source::Job), event:, timestamp:, level: T.let(Level::Info, Level), job_id: nil, job_class: nil, queue_name: nil, batch_id: nil, job_label: nil, arguments: nil, executions: nil, exception_executions: nil, execution_time: nil, scheduled_at: nil, error_class: nil, error_message: nil, error_backtrace: nil, process_id: nil, thread_id: nil, priority: nil, cron_key: nil, database_connection_name: nil, wait_time: nil, run_time: nil, finished_at: nil, additional_data: {}) ⇒ void

Parameters:

  • source (Source::Job) (defaults to: T.let(Source::Job, Source::Job))

    Common fields

  • event (GoodJobEvent)
  • timestamp (Time)
  • level (Level) (defaults to: T.let(Level::Info, Level))
  • job_id (String, nil) (defaults to: nil)

    Job identification fields

  • job_class (String, nil) (defaults to: nil)
  • queue_name (String, nil) (defaults to: nil)
  • batch_id (String, nil) (defaults to: nil)
  • job_label (String, nil) (defaults to: nil)
  • arguments (Array<T.untyped>, nil) (defaults to: nil)

    Job execution context

  • executions (Integer, nil) (defaults to: nil)
  • exception_executions (Integer, nil) (defaults to: nil)
  • execution_time (Float, nil) (defaults to: nil)
  • scheduled_at (Time, nil) (defaults to: nil)
  • error_class (String, nil) (defaults to: nil)

    Error information

  • error_message (String, nil) (defaults to: nil)
  • error_backtrace (Array<String>, nil) (defaults to: nil)
  • process_id (Integer, nil) (defaults to: nil)

    GoodJob-specific metadata

  • thread_id (String, nil) (defaults to: nil)
  • priority (Integer, nil) (defaults to: nil)
  • cron_key (String, nil) (defaults to: nil)
  • database_connection_name (String, nil) (defaults to: nil)
  • wait_time (Float, nil) (defaults to: nil)

    Performance and metrics

  • run_time (Float, nil) (defaults to: nil)
  • finished_at (Time, nil) (defaults to: nil)
  • additional_data (Hash{Symbol => T.untyped}) (defaults to: {})

    Additional contextual data



# File ''

const :source, Source::Job, default: T.let(Source::Job, Source::Job)
const :event, GoodJobEvent
const :timestamp, Time, factory: -> { Time.now }
const :level, Level, default: T.let(Level::Info, Level)
const :job_id, T.nilable(String), default: nil
const :job_class, T.nilable(String), default: nil
const :queue_name, T.nilable(String), default: nil
const :batch_id, T.nilable(String), default: nil
const :job_label, T.nilable(String), default: nil
const :arguments, T.nilable(T::Array[T.untyped]), default: nil
const :executions, T.nilable(Integer), default: nil
const :exception_executions, T.nilable(Integer), default: nil
const :execution_time, T.nilable(Float), default: nil
const :scheduled_at, T.nilable(Time), default: nil
const :error_class, T.nilable(String), default: nil
const :error_message, T.nilable(String), default: nil
const :error_backtrace, T.nilable(T::Array[String]), default: nil
const :process_id, T.nilable(Integer), default: nil
const :thread_id, T.nilable(String), default: nil
const :priority, T.nilable(Integer), default: nil
const :cron_key, T.nilable(String), default: nil
const :database_connection_name, T.nilable(String), default: nil
const :wait_time, T.nilable(Float), default: nil
const :run_time, T.nilable(Float), default: nil
const :finished_at, T.nilable(Time), default: nil
const :additional_data, T::Hash[Symbol, T.untyped], default: {}

Instance Attribute Details

#additional_dataHash{Symbol => T.untyped} (readonly)

Additional contextual data

Returns:

  • (Hash{Symbol => T.untyped})


# File ''

const :additional_data, T::Hash[Symbol, T.untyped], default: {}

#argumentsArray<T.untyped>? (readonly)

Job execution context

Returns:

  • (Array<T.untyped>, nil)


# File ''

const :arguments, T.nilable(T::Array[T.untyped]), default: nil

#batch_idString? (readonly)

Returns the value of prop batch_id.

Returns:

  • (String, nil)


# File ''

const :batch_id, T.nilable(String), default: nil

#cron_keyString? (readonly)

Returns the value of prop cron_key.

Returns:

  • (String, nil)


# File ''

const :cron_key, T.nilable(String), default: nil

#database_connection_nameString? (readonly)

Returns the value of prop database_connection_name.

Returns:

  • (String, nil)


# File ''

const :database_connection_name, T.nilable(String), default: nil

#error_backtraceArray<String>? (readonly)

Returns the value of prop error_backtrace.

Returns:

  • (Array<String>, nil)


# File ''

const :error_backtrace, T.nilable(T::Array[String]), default: nil

#error_classString? (readonly)

Error information

Returns:

  • (String, nil)


# File ''

const :error_class, T.nilable(String), default: nil

#error_messageString? (readonly)

Returns the value of prop error_message.

Returns:

  • (String, nil)


# File ''

const :error_message, T.nilable(String), default: nil

#eventGoodJobEvent (readonly)

Returns the value of prop event.

Returns:



# File ''

const :event, GoodJobEvent

#exception_executionsInteger? (readonly)

Returns the value of prop exception_executions.

Returns:

  • (Integer, nil)


# File ''

const :exception_executions, T.nilable(Integer), default: nil

#execution_timeFloat? (readonly)

Returns the value of prop execution_time.

Returns:

  • (Float, nil)


# File ''

const :execution_time, T.nilable(Float), default: nil

#executionsInteger? (readonly)

Returns the value of prop executions.

Returns:

  • (Integer, nil)


# File ''

const :executions, T.nilable(Integer), default: nil

#finished_atTime? (readonly)

Returns the value of prop finished_at.

Returns:

  • (Time, nil)


# File ''

const :finished_at, T.nilable(Time), default: nil

#job_classString? (readonly)

Returns the value of prop job_class.

Returns:

  • (String, nil)


# File ''

const :job_class, T.nilable(String), default: nil

#job_idString? (readonly)

Job identification fields

Returns:

  • (String, nil)


# File ''

const :job_id, T.nilable(String), default: nil

#job_labelString? (readonly)

Returns the value of prop job_label.

Returns:

  • (String, nil)


# File ''

const :job_label, T.nilable(String), default: nil

#levelLevel (readonly)

Returns the value of prop level.

Returns:



# File ''

const :level, Level, default: T.let(Level::Info, Level)

#priorityInteger? (readonly)

Returns the value of prop priority.

Returns:

  • (Integer, nil)


# File ''

const :priority, T.nilable(Integer), default: nil

#process_idInteger? (readonly)

GoodJob-specific metadata

Returns:

  • (Integer, nil)


# File ''

const :process_id, T.nilable(Integer), default: nil

#queue_nameString? (readonly)

Returns the value of prop queue_name.

Returns:

  • (String, nil)


# File ''

const :queue_name, T.nilable(String), default: nil

#run_timeFloat? (readonly)

Returns the value of prop run_time.

Returns:

  • (Float, nil)


# File ''

const :run_time, T.nilable(Float), default: nil

#scheduled_atTime? (readonly)

Returns the value of prop scheduled_at.

Returns:

  • (Time, nil)


# File ''

const :scheduled_at, T.nilable(Time), default: nil

#sourceSource::Job (readonly)

Common fields

Returns:



# File ''

const :source, Source::Job, default: T.let(Source::Job, Source::Job)

#thread_idString? (readonly)

Returns the value of prop thread_id.

Returns:

  • (String, nil)


# File ''

const :thread_id, T.nilable(String), default: nil

#timestampTime (readonly)

Returns the value of prop timestamp.

Returns:

  • (Time)


# File ''

const :timestamp, Time, factory: -> { Time.now }

#wait_timeFloat? (readonly)

Performance and metrics

Returns:

  • (Float, nil)


# File ''

const :wait_time, T.nilable(Float), default: nil

Instance Method Details

#serialize(strict = true) ⇒ Hash{Symbol => T.untyped}

Convert the log entry to a hash for serialization

Parameters:

  • strict (Boolean) (defaults to: true)

Returns:

  • (Hash{Symbol => T.untyped})


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/log_struct/log/good_job.rb', line 112

def serialize(strict = true)
  hash = serialize_common(strict)
  merge_additional_data_fields(hash)

  # Add job identification fields
  hash[LOG_KEYS.fetch(:job_id)] = job_id if job_id
  hash[LOG_KEYS.fetch(:job_class)] = job_class if job_class
  hash[LOG_KEYS.fetch(:queue_name)] = queue_name if queue_name
  hash[:batch_id] = batch_id if batch_id
  hash[:job_label] = job_label if job_label

  # Add execution context
  hash[LOG_KEYS.fetch(:arguments)] = arguments if arguments
  hash[:executions] = executions if executions
  hash[:exception_executions] = exception_executions if exception_executions
  hash[:execution_time] = execution_time if execution_time
  hash[:scheduled_at] = scheduled_at&.iso8601 if scheduled_at

  # Add error information
  hash[LOG_KEYS.fetch(:err_class)] = error_class if error_class
  hash[:error_message] = error_message if error_message
  hash[LOG_KEYS.fetch(:backtrace)] = error_backtrace if error_backtrace

  # Add GoodJob-specific metadata
  hash[LOG_KEYS.fetch(:process_id)] = process_id if process_id
  hash[LOG_KEYS.fetch(:thread_id)] = thread_id if thread_id
  hash[:priority] = priority if priority
  hash[:cron_key] = cron_key if cron_key
  hash[:database_connection_name] = database_connection_name if database_connection_name

  # Add performance metrics
  hash[:wait_time] = wait_time if wait_time
  hash[:run_time] = run_time if run_time
  hash[:finished_at] = finished_at&.iso8601 if finished_at

  hash
end