diff --git a/.github/workflows/judoscale-good_job-test.yml b/.github/workflows/judoscale-good_job-test.yml index 02497470..d36ab30b 100644 --- a/.github/workflows/judoscale-good_job-test.yml +++ b/.github/workflows/judoscale-good_job-test.yml @@ -28,6 +28,11 @@ jobs: ruby: "2.6" - gemfile: Gemfile-activerecord-7-0 ruby: "2.6" + # GoodJob v4+ dropped support for Ruby 2.6/2.7, we test it against these Gemfiles. + - gemfile: Gemfile + ruby: "2.7" + - gemfile: Gemfile-activerecord-7-0 + ruby: "2.7" # AR 6.1 stable doesn't play nice with Ruby 3.2+. Fixes have been backported # https://github.com/rails/rails/pull/46895, but no release is planned. # To avoid testing against 6-1-stable branch, we'll just skip those for now. diff --git a/judoscale-good_job/Gemfile-activerecord-6-1 b/judoscale-good_job/Gemfile-activerecord-6-1 index cb65ad6c..fec73dc2 100644 --- a/judoscale-good_job/Gemfile-activerecord-6-1 +++ b/judoscale-good_job/Gemfile-activerecord-6-1 @@ -4,6 +4,7 @@ gemspec name: "judoscale-good_job" gem "judoscale-ruby", path: "../judoscale-ruby" gem "activerecord", "~> 6.1" +gem "good_job", "~> 3.30" gem "pg" gem "minitest" gem "rake" diff --git a/judoscale-good_job/judoscale-good_job.gemspec b/judoscale-good_job/judoscale-good_job.gemspec index b2cde907..97b104d6 100644 --- a/judoscale-good_job/judoscale-good_job.gemspec +++ b/judoscale-good_job/judoscale-good_job.gemspec @@ -26,5 +26,5 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 2.6.0" spec.add_dependency "judoscale-ruby", Judoscale::VERSION - spec.add_dependency "good_job", ">= 3.0", "< 4.0" + spec.add_dependency "good_job", ">= 3.0", "< 5.0" end diff --git a/judoscale-good_job/lib/judoscale/good_job/metrics_collector.rb b/judoscale-good_job/lib/judoscale/good_job/metrics_collector.rb index c64812f9..8731f875 100644 --- a/judoscale-good_job/lib/judoscale/good_job/metrics_collector.rb +++ b/judoscale-good_job/lib/judoscale/good_job/metrics_collector.rb @@ -9,19 +9,29 @@ module GoodJob class MetricsCollector < Judoscale::JobMetricsCollector include ActiveRecordHelper + def self.good_job_v4? + Gem::Version.new(::GoodJob::VERSION) >= Gem::Version.new("4") + end + + def self.good_job_base_class + good_job_v4? ? ::GoodJob::Job : ::GoodJob::Execution + end + def self.adapter_config Judoscale::Config.instance.good_job end def self.collect?(config) - super && ActiveRecordHelper.table_exists_for_model?(::GoodJob::Execution) + super && ActiveRecordHelper.table_exists_for_model?(good_job_base_class) end def initialize super + @good_job_base_class = self.class.good_job_base_class + queue_names = run_silently do - ::GoodJob::Execution.select("distinct queue_name").map(&:queue_name) + @good_job_base_class.select("distinct queue_name").map(&:queue_name) end self.queues |= queue_names end @@ -32,7 +42,7 @@ def collect # logically we don't need the finished_at condition, but it lets postgres use the indexes oldest_execution_time_by_queue = run_silently do - ::GoodJob::Execution + @good_job_base_class .where(performed_at: nil, finished_at: nil) .group(:queue_name) .pluck(:queue_name, Arel.sql("min(coalesce(scheduled_at, created_at))")) @@ -42,7 +52,7 @@ def collect if track_busy_jobs? busy_count_by_queue = run_silently do - ::GoodJob::Execution.running.group(:queue_name).count + @good_job_base_class.running.group(:queue_name).count end self.queues |= busy_count_by_queue.keys end diff --git a/judoscale-good_job/test/metrics_collector_test.rb b/judoscale-good_job/test/metrics_collector_test.rb index 38dab552..a9510fa9 100644 --- a/judoscale-good_job/test/metrics_collector_test.rb +++ b/judoscale-good_job/test/metrics_collector_test.rb @@ -143,9 +143,14 @@ def clear_enqueued_jobs it "tracks busy jobs when the configuration is enabled" do use_adapter_config :good_job, track_busy_jobs: true do Delayable.set(queue: "default").perform_later - ::GoodJob::Execution.last.update!(performed_at: Time.now.utc) + metrics = nil + ::GoodJob::JobPerformer.new("default").next do |execution| + # Support GoodJob v3 query/scope `GoodJob::Execution.running`, which filters by `performed_at`. + # v4 scope `GoodJob::Job.running` joins with the advisory lock created by `JobPerformer#next` instead. + execution.update!(performed_at: Time.now.utc) - metrics = subject.collect + metrics = subject.collect + end _(metrics.size).must_equal 2 _(metrics[1].value).must_equal 1 @@ -158,9 +163,13 @@ def clear_enqueued_jobs use_config log_level: :debug do use_adapter_config :good_job, track_busy_jobs: true do Delayable.set(queue: "default").perform_later - ::GoodJob::Execution.last.update!(performed_at: Time.now.utc) + ::GoodJob::JobPerformer.new("default").next do |execution| + # Support GoodJob v3 query/scope `GoodJob::Execution.running`, which filters by `performed_at`. + # v4 scope `GoodJob::Job.running` joins with the advisory lock created by `JobPerformer#next` instead. + execution.update!(performed_at: Time.now.utc) - subject.collect + subject.collect + end _(log_string).must_match %r{good_job-qt.default=.+ good_job-busy.default=1} end diff --git a/sample-apps/good_job-multi-db-sample/Gemfile.lock b/sample-apps/good_job-multi-db-sample/Gemfile.lock index 13335f30..2d130533 100644 --- a/sample-apps/good_job-multi-db-sample/Gemfile.lock +++ b/sample-apps/good_job-multi-db-sample/Gemfile.lock @@ -1,21 +1,21 @@ PATH remote: ../../judoscale-good_job specs: - judoscale-good_job (1.7.0) - good_job (>= 3.0, < 4.0) - judoscale-ruby (= 1.7.0) + judoscale-good_job (1.7.1) + good_job (>= 3.0, < 5.0) + judoscale-ruby (= 1.7.1) PATH remote: ../../judoscale-rails specs: - judoscale-rails (1.7.0) - judoscale-ruby (= 1.7.0) + judoscale-rails (1.7.1) + judoscale-ruby (= 1.7.1) railties PATH remote: ../../judoscale-ruby specs: - judoscale-ruby (1.7.0) + judoscale-ruby (1.7.1) GEM remote: https://rubygems.org/ diff --git a/sample-apps/good_job-multi-db-sample/app/views/jobs/index.html.erb b/sample-apps/good_job-multi-db-sample/app/views/jobs/index.html.erb index f2ec0474..41e2f7db 100644 --- a/sample-apps/good_job-multi-db-sample/app/views/jobs/index.html.erb +++ b/sample-apps/good_job-multi-db-sample/app/views/jobs/index.html.erb @@ -1,4 +1,4 @@ -
Judoscale is reporting metrics to https://judoscale-adapter-mock.requestcatcher.com. diff --git a/sample-apps/good_job-multi-db-sample/app/views/layouts/application.html.erb b/sample-apps/good_job-multi-db-sample/app/views/layouts/application.html.erb index 252663a2..274e4f93 100644 --- a/sample-apps/good_job-multi-db-sample/app/views/layouts/application.html.erb +++ b/sample-apps/good_job-multi-db-sample/app/views/layouts/application.html.erb @@ -1,7 +1,7 @@
-