Skip to content

Commit

Permalink
Support for Rails 7.1
Browse files Browse the repository at this point in the history
- Remove outdated versions of Rails (5.2, 6.0) and Ruby (2.5, 2.6) from test matrix due to reaching end of life
- Add new versions of Rails (7.1) and Ruby (3.2) to the test matrix
- Use separate gemfiles to enforce correct Rails version
- Parse `event.payload[:job].enqueued_at` only when needed. In Rails 7.1 it is an instance of `Time` which makes Time.parse fail (see rails/rails#48066)

Co-Authored-By: Jacopo Beschi <beschi.jacopo@gmail.com>
  • Loading branch information
bronislav and intrip committed Oct 31, 2023
1 parent 4339aa2 commit 2af52d3
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 29 deletions.
23 changes: 6 additions & 17 deletions .github/workflows/test.yml
Expand Up @@ -17,36 +17,25 @@ jobs:
strategy:
fail-fast: false
matrix:
include:
- ruby: "3.1"
rails: "HEAD"
- ruby: "3.0"
rails: "7.0"
- ruby: "2.7"
rails: "6.1"
- ruby: "2.6"
rails: "6.0"
- ruby: "2.5"
rails: "5.2"
rails: ["6.1", "7.0", "7.1"]
ruby: ["2.7", "3.0", "3.1", "3.2"]
container:
image: ruby:${{ matrix.ruby }}
env:
CI: true
RAILS_VERSION: ${{ matrix.rails }}
BUNDLE_GEMFILE: gemfiles/rails_${{ matrix.rails }}.gemfile
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: vendor/bundle
key: bundle-${{ matrix.ruby }}-${{ hashFiles('**/*.gemspec') }}-${{ hashFiles('**/Gemfile') }}
key: bundle-${{ matrix.ruby }}-${{ hashFiles('**/*.gemspec') }}-${{ hashFiles(format('**/gemfiles/rails_{0}.gemfile', matrix.rails)) }}
restore-keys: |
bundle-${{ matrix.ruby }}-${{ hashFiles('**/*.gemspec') }}-${{ hashFiles('**/Gemfile') }}
bundle-${{ matrix.ruby }}-${{ hashFiles('**/*.gemspec') }}-${{ hashFiles(format('**/gemfiles/rails_{0}.gemfile', matrix.rails)) }}
bundle-${{ matrix.ruby }}-
- name: Upgrade Bundler to 2.0 (for older Rubies)
run: gem install bundler -v '~> 2.0'
- name: Bundle install
run: |
bundle config path vendor/bundle
bundle config path ../vendor/bundle
bundle install
bundle update
- name: Run RSpec
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Expand Up @@ -181,6 +181,7 @@ GEM
zeitwerk (2.6.1)

PLATFORMS
ruby
x86_64-darwin-21
x86_64-linux

Expand Down
9 changes: 9 additions & 0 deletions gemfiles/rails_6.1.gemfile
@@ -0,0 +1,9 @@
# frozen_string_literal: true

source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

gem "rails", "~> 6.1.0"

# Specify your gem's dependencies in yabeda-activejob.gemspec.
gemspec path: "../"
9 changes: 9 additions & 0 deletions gemfiles/rails_7.0.gemfile
@@ -0,0 +1,9 @@
# frozen_string_literal: true

source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

gem "rails", "~> 7.0.0"

# Specify your gem's dependencies in yabeda-activejob.gemspec.
gemspec path: "../"
9 changes: 9 additions & 0 deletions gemfiles/rails_7.1.gemfile
@@ -0,0 +1,9 @@
# frozen_string_literal: true

source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

gem "rails", "~> 7.1.0"

# Specify your gem's dependencies in yabeda-activejob.gemspec.
gemspec path: "../"
2 changes: 1 addition & 1 deletion lib/yabeda/activejob.rb
Expand Up @@ -96,7 +96,7 @@ def self.job_latency(event)
enqueue_time = event.payload[:job].enqueued_at
return nil unless enqueue_time.present?

enqueue_time = Time.parse(enqueue_time).utc
enqueue_time = Time.parse(enqueue_time).utc unless enqueue_time.is_a?(Time)
perform_at_time = Time.parse(event.end.to_s).utc
(perform_at_time - enqueue_time)
end
Expand Down
20 changes: 9 additions & 11 deletions spec/yabeda/activejob_spec.rb
Expand Up @@ -7,7 +7,9 @@
include ActionDispatch::IntegrationTest::Behavior
include ActiveJob::TestHelper

before { ActiveJob::Base.queue_adapter = :inline }
before do |ex|
ActiveJob::Base.queue_adapter = ex.metadata[:queue_adapter] || :inline
end

after { described_class.after_event_block = proc {} }

Expand Down Expand Up @@ -47,19 +49,17 @@
.with(be_between(0.005, 0.05))
end

it "measures job latency" do
ActiveJob::Base.queue_adapter = :test
it "measures job latency", queue_adapter: :test do
expect { HelloJob.perform_later }.to have_enqueued_job.on_queue("default")
sleep(1)
expect { perform_enqueued_jobs }.to measure_yabeda_histogram(Yabeda.activejob.latency)
.with_tags(queue: "default", activejob: "HelloJob", executions: "1")
.with(be_between(1, 2))
.with(kind_of(Float))
end

context "when enqueued_at is not present" do
it "does not measure job latency" do
ActiveJob::Base.queue_adapter = :test
expect_any_instance_of(HelloJob).to receive(:enqueued_at).and_return(nil).twice # rubocop:disable RSpec/AnyInstance
it "does not measure job latency", queue_adapter: :test do
allow_any_instance_of(HelloJob).to receive(:enqueued_at).and_return(nil)

expect { HelloJob.perform_later }.to have_enqueued_job.on_queue("default")
sleep(1)
expect { perform_enqueued_jobs }.not_to measure_yabeda_histogram(Yabeda.activejob.latency)
Expand Down Expand Up @@ -111,9 +111,7 @@
end
end

context "when job is enqueued" do
before { ActiveJob::Base.queue_adapter = :test }

context "when job is enqueued", queue_adapter: :test do
it "increments enqueued job counter" do
expect do
HelloJob.perform_later
Expand Down

0 comments on commit 2af52d3

Please sign in to comment.