From d6e258789561eef59283c4a0912931e5d47ff3cf Mon Sep 17 00:00:00 2001 From: Anders Mikkelsen Date: Tue, 13 Jun 2023 22:44:29 +0200 Subject: [PATCH] added metrics --- lib/good_job/metrics.rb | 14 ++++++++++++++ lib/good_job/scheduler.rb | 11 +++++++++++ 2 files changed, 25 insertions(+) create mode 100644 lib/good_job/metrics.rb diff --git a/lib/good_job/metrics.rb b/lib/good_job/metrics.rb new file mode 100644 index 000000000..69e92c5f2 --- /dev/null +++ b/lib/good_job/metrics.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true +module GoodJob # :nodoc: + class Metrics + attr_accessor :failed_count, :succeeded_count + + def increment_failed + self.failed_count += 1 + end + + def increment_succeeded + self.succeeded_count += 1 + end + end +end diff --git a/lib/good_job/scheduler.rb b/lib/good_job/scheduler.rb index 500f2d150..adaaf7bee 100644 --- a/lib/good_job/scheduler.rb +++ b/lib/good_job/scheduler.rb @@ -3,6 +3,7 @@ require "concurrent/executor/timer_set" require "concurrent/scheduled_task" require "concurrent/utility/processor_counter" +require 'good_job/stats' module GoodJob # :nodoc: # @@ -88,6 +89,7 @@ def initialize(performer, max_threads: nil, max_cache: nil, warm_cache_on_initia @executor_options[:name] = name @cleanup_tracker = CleanupTracker.new(cleanup_interval_seconds: cleanup_interval_seconds, cleanup_interval_jobs: cleanup_interval_jobs) + @metrics = ::GoodJob::Metrics.new create_executor warm_cache if warm_cache_on_initialize end @@ -203,6 +205,12 @@ def task_observer(time, output, thread_error) instrument("finished_job_task", { result: output, error: thread_error, time: time }) return unless output + if error + @metrics.increment_failed + else + @metrics.increment_succeeded + end + @cleanup_tracker.increment if @cleanup_tracker.cleanup? cleanup @@ -222,6 +230,9 @@ def stats max_cache: @max_cache, active_cache: cache_count, available_cache: remaining_cache_count, + succeeded_count: @metrics.succeeded_count, + failed_count: @metrics.failed_count, + total_count: @metrics.succeeded_count + @metrics.failed_count } end