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 exception handling to LogOrganizationsApiCredentialStatusJob #2330

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions dpc-portal/app/jobs/log_organizations_api_credential_status_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,26 @@ def perform
have_incomplete_or_no_credentials: 0
}
ProviderOrganization.where.not(terms_of_service_accepted_by: nil).find_each do |organization|
credential_status = fetch_credential_status(organization.dpc_api_organization_id)
Rails.logger.info(['Credential status for organization',
{ name: organization.name,
dpc_api_org_id: organization.dpc_api_organization_id,
credential_status: }])
update_organization_aggregate_hash(organizations_credential_aggregate_status, credential_status)
process_organization_credentials(organizations_credential_aggregate_status, organization.name,
organization.dpc_api_organization_id)
end
Rails.logger.info(['Organizations API credential status', organizations_credential_aggregate_status])
end

def process_organization_credentials(organizations_credential_aggregate_status, organization_name,
organization_id)
credential_status = fetch_credential_status(organization_id)
Rails.logger.info(['Credential status for organization',
{ name: organization_name,
dpc_api_org_id: organization_id,
credential_status: }])
update_organization_aggregate_hash(organizations_credential_aggregate_status, credential_status)
rescue StandardError
Rails.logger.error(['Failed to fetch api credential status for organization',
{ name: organization_name,
dpc_api_org_id: organization_id }])
end

def update_organization_aggregate_hash(aggregate_stats, credential_status)
if credential_status[:num_tokens].zero? || credential_status[:num_keys].zero? || credential_status[:num_ips].zero?
aggregate_stats[:have_incomplete_or_no_credentials] += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,31 +60,59 @@

described_class.perform_now
end
end
it 'updates log with 1 organization that has all 3 credentials' do
provider_organization.save!
it 'updates log with 1 organization that has all 3 credentials' do
provider_organization.save!

expect(mock_dpc_client).to receive(:get_client_tokens).and_return(mock_one_token_response).once
expect(mock_dpc_client).to receive(:get_public_keys).and_return({ 'count' => 2 }).once
expect(mock_dpc_client).to receive(:get_ip_addresses).and_return({ 'count' => 3 }).once
allow(Rails.logger).to receive(:info)
expect(Rails.logger).to receive(:info).with(['Organizations API credential status',
{ have_active_credentials: 1,
have_incomplete_or_no_credentials: 0 }])
expect(mock_dpc_client).to receive(:get_client_tokens).and_return(mock_one_token_response).once
expect(mock_dpc_client).to receive(:get_public_keys).and_return({ 'count' => 2 }).once
expect(mock_dpc_client).to receive(:get_ip_addresses).and_return({ 'count' => 3 }).once
allow(Rails.logger).to receive(:info)
expect(Rails.logger).to receive(:info).with(['Organizations API credential status',
{ have_active_credentials: 1,
have_incomplete_or_no_credentials: 0 }])

described_class.perform_now
end
it 'updates log with 1 organization that has partial credentials' do
provider_organization.save!
described_class.perform_now
end
it 'updates log with 1 organization that has partial credentials' do
provider_organization.save!

expect(mock_dpc_client).to receive(:get_client_tokens).and_return(mock_one_token_response).once
expect(mock_dpc_client).to receive(:get_public_keys).and_return({ 'count' => 2 }).once
expect(mock_dpc_client).to receive(:get_ip_addresses).and_return({ 'count' => 0 }).once
allow(Rails.logger).to receive(:info)
expect(Rails.logger).to receive(:info).with(['Organizations API credential status',
{ have_active_credentials: 0,
have_incomplete_or_no_credentials: 1 }])

expect(mock_dpc_client).to receive(:get_client_tokens).and_return(mock_one_token_response).once
expect(mock_dpc_client).to receive(:get_public_keys).and_return({ 'count' => 2 }).once
expect(mock_dpc_client).to receive(:get_ip_addresses).and_return({ 'count' => 0 }).once
allow(Rails.logger).to receive(:info)
expect(Rails.logger).to receive(:info).with(['Organizations API credential status',
{ have_active_credentials: 0,
have_incomplete_or_no_credentials: 1 }])
described_class.perform_now
end
it 'processes next organization successfully when an exception is raised' do
lukey-luke marked this conversation as resolved.
Show resolved Hide resolved
second_organization = create(
:provider_organization,
name: 'Test2',
dpc_api_organization_id: 'bar',
terms_of_service_accepted_by: user,
terms_of_service_accepted_at: 1.day.ago
)
second_organization.save!
provider_organization.save!

described_class.perform_now
allow_any_instance_of(described_class).to receive(:fetch_credential_status).with('foo').and_return(
num_tokens: 1, num_keys: 2, num_ips: 3
)
allow_any_instance_of(described_class).to receive(:fetch_credential_status).with('bar').and_raise(
StandardError.new('something failed inside fetch_credential_status!')
)

allow(Rails.logger).to receive(:error)
expect(Rails.logger).to receive(:error).with(['Failed to fetch api credential status for organization',
{ name: 'Test2',
dpc_api_org_id: 'bar' }])
allow(Rails.logger).to receive(:info)
expect(Rails.logger).to receive(:info).with(['Organizations API credential status',
{ have_active_credentials: 1,
have_incomplete_or_no_credentials: 0 }])
described_class.perform_now
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make sure we are logging failure?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is a bit beyond the scope of the original goals for building the dashboard, but it's an easy lift, so I added a new expect() to this test.

end
end
end
Loading