Module: Postburner::Statistics

Extended by:
ActiveSupport::Concern
Included in:
Job
Defined in:
app/concerns/postburner/statistics.rb

Overview

Concern providing statistics and metrics methods for Postburner jobs.

Provides methods for calculating timing metrics, retrieving job statistics, and determining intended execution times for scheduled jobs.

Examples:

Get job statistics

job.stats
# => { id: 5, bkid: 1, queue: "default", ... }

Check elapsed time

job.elapsed_ms  # => 1234.567

Instance Method Summary collapse

Instance Method Details

#elapsed_msFloat?

Returns elapsed time in milliseconds since job execution started.

Calculates milliseconds between attempting_at and current time. Used to track how long a job has been executing.

Examples:

elapsed_ms  # => 1234.567 (job has been running for ~1.2 seconds)

Returns:

  • (Float, nil)

    Milliseconds since attempting_at, or nil if not yet attempting

See Also:

  • #log


100
101
102
103
# File 'app/concerns/postburner/statistics.rb', line 100

def elapsed_ms
  return unless self.attempting_at
  (Time.current - self.attempting_at) * 1000
end

#intended_atTime

Returns the intended execution time for the job.

Returns run_at if scheduled, otherwise returns queued_at. Used for calculating lag (delay between intended and actual execution).

Examples:

job.queue!(delay: 1.hour)
job.intended_at  # => 1 hour from now (run_at)

job.queue!
job.intended_at  # => now (queued_at)

Returns:

  • (Time)

    Intended execution timestamp

See Also:

  • #lag


121
122
123
# File 'app/concerns/postburner/statistics.rb', line 121

def intended_at
  self.run_at ? self.run_at : self.queued_at
end

#statsHash

Returns job statistics including Beanstalkd job state.

Fetches current job state from Beanstalkd and returns combined statistics about the job’s PostgreSQL record and its current Beanstalkd status.

Examples:

job.stats
# => {
#   id: 5,
#   bkid: 1,
#   queue: "sleep-jobs",
#   tube: "postburner.development.sleep-jobs",
#   watched: false,
#   beanstalk: {
#     id: 1,
#     tube: "postburner.development.sleep-jobs",
#     state: "ready",
#     pri: 50,
#     age: 1391,
#     delay: 0,
#     ttr: 120,
#     time_left: 0,
#     file: 0,
#     reserves: 0,
#     timeouts: 0,
#     releases: 0,
#     buries: 0,
#     kicks: 0
#   }
# }

Returns:

  • (Hash)

    Statistics hash with the following keys:

    • id: PostgreSQL job ID

    • bkid: Beanstalkd job ID

    • queue: Queue name configured for this job class (e.g., ‘sleep-jobs’)

    • tube: Derived tube name with environment prefix (e.g., ‘postburner.development.sleep-jobs’)

    • watched: Boolean indicating if tube is in configured watch list

    • beanstalk: Hash of Beanstalkd job statistics:

      • id: Beanstalkd job ID

      • tube: Tube name where job resides

      • state: Job state (ready, reserved, delayed, buried)

      • pri: Priority (0 = highest, 4294967295 = lowest)

      • age: Seconds since job was created

      • delay: Seconds remaining before job becomes ready (0 if ready now)

      • ttr: Time-to-run in seconds (time allowed for job processing)

      • time_left: Seconds remaining before job times out (0 if not reserved)

      • file: Binlog file number containing job

      • reserves: Number of times job has been reserved

      • timeouts: Number of times job has timed out during processing

      • releases: Number of times job has been released back to ready

      • buries: Number of times job has been buried

      • kicks: Number of times job has been kicked from buried/delayed

Raises:

  • (Beaneater::NotFoundError)

    if job no longer exists in Beanstalkd



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/concerns/postburner/statistics.rb', line 74

def stats
  # Get configured watched tubes (expanded with environment prefix)
  watched = Postburner.watched_tube_names.include?(self.tube_name)

  {
    id: self.id,
    bkid: self.bkid,
    queue: queue_name,
    tube: tube_name,
    watched: watched,
    beanstalk: self.bk.stats.to_h.symbolize_keys,
  }
end