Check if any jobs exist before querying jobs

At the moment the dashboard does two API calls to find out if a service
has:

1. Scheduled jobs
2. Normal jobs

API calls are slow because they are synchronous, go over the network and
touch the database. We can’t cache these API calls because:
- a scheduled job could become a normal job at any time
- the statistics on a normal job are constantly updating

However there are plenty of services which don’t have any jobs, and
probably never will. And finding out if a service has any jobs is
reliably cacheable (because as soon as a service creates its first job
it has some jobs).

So this commit:
- refactors the way we get scheduled/normal jobs into the job_api_client
  to make the view a bit slimmer
- makes an additional, Redis-wrapped call to find out if any jobs exist
  before trying to get the jobs

This should result in a speedup on the dashboard, and can be used in the
future if there’s anywhere else we want to show or hide something
depending on whether a service has created any jobs (I have some ideas).
This commit is contained in:
Chris Hill-Scott
2018-07-20 10:18:51 +01:00
parent c1b2f63671
commit 505de52d38
5 changed files with 122 additions and 27 deletions

View File

@@ -2,7 +2,7 @@ import copy
import json
from datetime import datetime
from functools import partial
from unittest.mock import ANY, call
from unittest.mock import call
import pytest
from bs4 import BeautifulSoup
@@ -573,9 +573,9 @@ def test_should_show_upcoming_jobs_on_dashboard(
):
response = logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
first_call = mock_get_jobs.call_args_list[0]
assert first_call[0] == (SERVICE_ONE_ID,)
assert first_call[1]['statuses'] == ['scheduled']
second_call = mock_get_jobs.call_args_list[1]
assert second_call[0] == (SERVICE_ONE_ID,)
assert second_call[1]['statuses'] == ['scheduled']
assert response.status_code == 200
@@ -702,10 +702,10 @@ def test_should_show_recent_jobs_on_dashboard(
):
response = logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
second_call = mock_get_jobs.call_args_list[1]
assert second_call[0] == (SERVICE_ONE_ID,)
assert second_call[1]['limit_days'] == 7
assert 'scheduled' not in second_call[1]['statuses']
third_call = mock_get_jobs.call_args_list[2]
assert third_call[0] == (SERVICE_ONE_ID,)
assert third_call[1]['limit_days'] == 7
assert 'scheduled' not in third_call[1]['statuses']
assert response.status_code == 200
@@ -1266,11 +1266,14 @@ def test_should_show_all_jobs_with_valid_statuses(
logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
first_call = mock_get_jobs.call_args_list[0]
# first call - scheduled jobs only
assert first_call == call(ANY, statuses=['scheduled'])
# second call - everything but scheduled and cancelled
# first call - checking for any jobs
assert first_call == call(SERVICE_ONE_ID)
second_call = mock_get_jobs.call_args_list[1]
assert second_call == call(ANY, limit_days=ANY, statuses={
# second call - scheduled jobs only
assert second_call == call(SERVICE_ONE_ID, statuses=['scheduled'])
# third call - everything but scheduled and cancelled
third_call = mock_get_jobs.call_args_list[2]
assert third_call == call(SERVICE_ONE_ID, limit_days=7, statuses={
'pending',
'in progress',
'finished',