Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add database_connection_pool stats to GoodJob::Process.current_state #1019

Merged
merged 2 commits into from
Jul 27, 2023

Conversation

dixpac
Copy link
Contributor

@dixpac dixpac commented Jul 26, 2023

This commit adds snapshot of database connection pool to a GoodJob::Process.current_state method.

  • size - Total capacity of connection pool
  • active - Number of connections in the pool that are in_use/active

References #1014

This commit adds snapshot of database connection pool to a
`GoodJob::Process.current_stat` method.

* `size` - Total capacity of connection pool
* `active` - Number of connections in the pool that are in_use/active
@dixpac
Copy link
Contributor Author

dixpac commented Jul 26, 2023

Tried to write a test, but it ended up in a large number of stubs, so I'm not sure will it be valuable and stable.

RSpec.describe GoodJob::Process do
  describe '.current_state' do
    before do
      allow(SecureRandom).to receive(:uuid).and_return("test-uuid")
      allow(Socket).to receive(:gethostname).and_return("test-hostname")
      allow(::Process).to receive(:pid).and_return(1234)

     scheduler_instance = instance_double(
       GoodJob::Scheduler,
       stats: { succeeded_executions_count: 10, errored_executions_count: 5 },
       shutdown: nil, 
       shutdown?: true 
      )

      allow(GoodJob::Scheduler).to receive(:instances).and_return([scheduler_instance])


      allow(GoodJob).to receive(:preserve_job_records).and_return(true)
      allow(GoodJob).to receive(:retry_on_unhandled_error).and_return(false)
      allow(GoodJob).to receive_message_chain(:configuration, :enable_cron?).and_return(true)

      connection_pool = instance_double(ActiveRecord::ConnectionAdapters::ConnectionPool, size: 10, connections: [instance_double(ActiveRecord::ConnectionAdapters::AbstractAdapter, in_use?: true)] * 5)
      allow(ActiveRecord::Base).to receive(:connection_pool).and_return(connection_pool)
    end

    it 'returns the correct state' do
      state = GoodJob::Process.current_state

      expect(state).to eq({
        id: "test-uuid",
        hostname: "test-hostname",
        pid: 1234,
        proctitle: $PROGRAM_NAME,
        preserve_job_records: true,
        retry_on_unhandled_error: false,
        schedulers: [{ succeeded_executions_count: 10, errored_executions_count: 5 }],
        cron_enabled: true,
        total_succeeded_executions_count: 10,
        total_errored_executions_count: 5,
        connection_pool: { 
          size: 10,
          active: 5
        }
      })
    end
  end
end

Copy link
Owner

@bensheldon bensheldon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Thank you 🎉

I tweaked the name and added a minimal "doesn't blow up"-style test.

@bensheldon bensheldon changed the title Add connection_pool stats to GoodJob::Process.current_state Add database_connection_pool stats to GoodJob::Process.current_state Jul 27, 2023
@bensheldon bensheldon merged commit bbf5d22 into bensheldon:main Jul 27, 2023
19 checks passed
@bensheldon bensheldon added the enhancement New feature or request label Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging this pull request may close these issues.

2 participants