mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-18 05:29:38 -04:00
Merge pull request #2178 from alphagov/cache-has-jobs
Check if any jobs exist before querying jobs
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from collections import defaultdict
|
||||
|
||||
from app.notify_client import NotifyAdminAPIClient, _attach_current_user
|
||||
from app.notify_client import NotifyAdminAPIClient, _attach_current_user, cache
|
||||
|
||||
|
||||
class JobApiClient(NotifyAdminAPIClient):
|
||||
@@ -16,6 +16,8 @@ class JobApiClient(NotifyAdminAPIClient):
|
||||
'sent to dvla'
|
||||
}
|
||||
|
||||
NON_SCHEDULED_JOB_STATUSES = JOB_STATUSES - {'scheduled', 'cancelled'}
|
||||
|
||||
def __init__(self):
|
||||
super().__init__("a" * 73, "b")
|
||||
|
||||
@@ -60,8 +62,38 @@ class JobApiClient(NotifyAdminAPIClient):
|
||||
|
||||
return jobs
|
||||
|
||||
def get_page_of_jobs(self, service_id, page):
|
||||
return self.get_jobs(
|
||||
service_id,
|
||||
statuses=self.NON_SCHEDULED_JOB_STATUSES,
|
||||
page=page,
|
||||
)
|
||||
|
||||
def get_immediate_jobs(self, service_id):
|
||||
return self.get_jobs(
|
||||
service_id,
|
||||
limit_days=7,
|
||||
statuses=self.NON_SCHEDULED_JOB_STATUSES,
|
||||
)['data']
|
||||
|
||||
def get_scheduled_jobs(self, service_id):
|
||||
return sorted(
|
||||
self.get_jobs(service_id, statuses=['scheduled'])['data'],
|
||||
key=lambda job: job['scheduled_for']
|
||||
)
|
||||
|
||||
@cache.set('has_jobs-{service_id}')
|
||||
def has_jobs(self, service_id):
|
||||
return bool(self.get_jobs(service_id)['data'])
|
||||
|
||||
def create_job(self, job_id, service_id, scheduled_for=None):
|
||||
|
||||
self.redis_client.set(
|
||||
'has_jobs-{}'.format(service_id),
|
||||
True,
|
||||
ex=cache.TTL,
|
||||
)
|
||||
|
||||
data = {"id": job_id}
|
||||
|
||||
if scheduled_for:
|
||||
@@ -78,6 +110,7 @@ class JobApiClient(NotifyAdminAPIClient):
|
||||
|
||||
return job
|
||||
|
||||
@cache.delete('has_jobs-{service_id}')
|
||||
def cancel_job(self, service_id, job_id):
|
||||
|
||||
job = self.post(
|
||||
|
||||
Reference in New Issue
Block a user