mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-21 14:59:26 -04:00
merge from main
This commit is contained in:
@@ -209,7 +209,7 @@
|
|||||||
"filename": "tests/app/aws/test_s3.py",
|
"filename": "tests/app/aws/test_s3.py",
|
||||||
"hashed_secret": "67a74306b06d0c01624fe0d0249a570f4d093747",
|
"hashed_secret": "67a74306b06d0c01624fe0d0249a570f4d093747",
|
||||||
"is_verified": false,
|
"is_verified": false,
|
||||||
"line_number": 27,
|
"line_number": 29,
|
||||||
"is_secret": false
|
"is_secret": false
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -384,5 +384,5 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"generated_at": "2024-09-10T18:12:39Z"
|
"generated_at": "2024-09-27T16:42:53Z"
|
||||||
}
|
}
|
||||||
|
|||||||
4
.github/workflows/checks.yml
vendored
4
.github/workflows/checks.yml
vendored
@@ -54,7 +54,7 @@ jobs:
|
|||||||
- name: Check for dead code
|
- name: Check for dead code
|
||||||
run: make dead-code
|
run: make dead-code
|
||||||
- name: Run tests with coverage
|
- name: Run tests with coverage
|
||||||
run: poetry run coverage run --omit=*/notifications_utils/*,*/migrations/* -m pytest --maxfail=10
|
run: poetry run coverage run --omit=*/migrations/*,*/tests/* -m pytest --maxfail=10
|
||||||
env:
|
env:
|
||||||
SQLALCHEMY_DATABASE_TEST_URI: postgresql://user:password@localhost:5432/test_notification_api
|
SQLALCHEMY_DATABASE_TEST_URI: postgresql://user:password@localhost:5432/test_notification_api
|
||||||
NOTIFY_E2E_TEST_EMAIL: ${{ secrets.NOTIFY_E2E_TEST_EMAIL }}
|
NOTIFY_E2E_TEST_EMAIL: ${{ secrets.NOTIFY_E2E_TEST_EMAIL }}
|
||||||
@@ -63,7 +63,7 @@ jobs:
|
|||||||
NOTIFY_E2E_TEST_PASSWORD: ${{ secrets.NOTIFY_E2E_TEST_PASSWORD }}
|
NOTIFY_E2E_TEST_PASSWORD: ${{ secrets.NOTIFY_E2E_TEST_PASSWORD }}
|
||||||
- name: Check coverage threshold
|
- name: Check coverage threshold
|
||||||
# TODO get this back up to 95
|
# TODO get this back up to 95
|
||||||
run: poetry run coverage report -m --fail-under=95
|
run: poetry run coverage report -m --fail-under=91
|
||||||
|
|
||||||
validate-new-relic-config:
|
validate-new-relic-config:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|||||||
10
.github/workflows/deploy.yml
vendored
10
.github/workflows/deploy.yml
vendored
@@ -70,15 +70,7 @@ jobs:
|
|||||||
cf_password: ${{ secrets.CLOUDGOV_PASSWORD }}
|
cf_password: ${{ secrets.CLOUDGOV_PASSWORD }}
|
||||||
cf_org: gsa-tts-benefits-studio
|
cf_org: gsa-tts-benefits-studio
|
||||||
cf_space: notify-staging
|
cf_space: notify-staging
|
||||||
push_arguments: >-
|
cf_command: "push -f manifest.yml --vars-file deploy-config/staging.yml --var var-name=${{ secrets.DANGEROUS_SALT }} --var var-name=${{ secrets.SECRET_KEY }} --var var-name=${{ secrets.ADMIN_CLIENT_SECRET }} --var var-name=${{ secrets.NEW_RELIC_LICENSE_KEY }} --var var-name=${{ secrets.NOTIFY_E2E_TEST_EMAIL }} --var var-name=${{ secrets.NOTIFY_E2E_TEST_PASSWORD }} --var LOGIN_DOT_GOV_REGISTRATION_URL=\"${{ env.LOGIN_DOT_GOV_REGISTRATION_URL }}\" --strategy rolling"
|
||||||
--vars-file deploy-config/staging.yml
|
|
||||||
--var DANGEROUS_SALT="$DANGEROUS_SALT"
|
|
||||||
--var SECRET_KEY="$SECRET_KEY"
|
|
||||||
--var ADMIN_CLIENT_SECRET="$ADMIN_CLIENT_SECRET"
|
|
||||||
--var NEW_RELIC_LICENSE_KEY="$NEW_RELIC_LICENSE_KEY"
|
|
||||||
--var NOTIFY_E2E_TEST_EMAIL="$NOTIFY_E2E_TEST_EMAIL"
|
|
||||||
--var NOTIFY_E2E_TEST_PASSWORD="$NOTIFY_E2E_TEST_PASSWORD"
|
|
||||||
--var LOGIN_DOT_GOV_REGISTRATION_URL="$LOGIN_DOT_GOV_REGISTRATION_URL"
|
|
||||||
|
|
||||||
- name: Check for changes to templates.json
|
- name: Check for changes to templates.json
|
||||||
id: changed-templates
|
id: changed-templates
|
||||||
|
|||||||
5
Makefile
5
Makefile
@@ -81,9 +81,10 @@ test: ## Run tests and create coverage report
|
|||||||
poetry run black .
|
poetry run black .
|
||||||
poetry run flake8 .
|
poetry run flake8 .
|
||||||
poetry run isort --check-only ./app ./tests
|
poetry run isort --check-only ./app ./tests
|
||||||
poetry run coverage run --omit=*/notifications_utils/*,*/migrations/* -m pytest --maxfail=10
|
poetry run coverage run --omit=*/migrations/*,*/tests/* -m pytest --maxfail=10
|
||||||
|
|
||||||
poetry run coverage report -m --fail-under=95
|
## TODO set this back to 95 asap
|
||||||
|
poetry run coverage report -m --fail-under=91
|
||||||
poetry run coverage html -d .coverage_cache
|
poetry run coverage html -d .coverage_cache
|
||||||
|
|
||||||
.PHONY: py-lock
|
.PHONY: py-lock
|
||||||
|
|||||||
187
app/aws/s3.py
187
app/aws/s3.py
@@ -1,13 +1,13 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
from multiprocessing import Manager
|
||||||
|
|
||||||
import botocore
|
import botocore
|
||||||
from boto3 import Session
|
from boto3 import Session
|
||||||
from expiringdict import ExpiringDict
|
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
|
|
||||||
from app import redis_store
|
|
||||||
from app.clients import AWS_CLIENT_CONFIG
|
from app.clients import AWS_CLIENT_CONFIG
|
||||||
from notifications_utils import aware_utcnow
|
from notifications_utils import aware_utcnow
|
||||||
|
|
||||||
@@ -16,17 +16,30 @@ NEW_FILE_LOCATION_STRUCTURE = "{}-service-notify/{}.csv"
|
|||||||
|
|
||||||
# Temporarily extend cache to 7 days
|
# Temporarily extend cache to 7 days
|
||||||
ttl = 60 * 60 * 24 * 7
|
ttl = 60 * 60 * 24 * 7
|
||||||
JOBS = ExpiringDict(max_len=20000, max_age_seconds=ttl)
|
manager = Manager()
|
||||||
|
job_cache = manager.dict()
|
||||||
|
|
||||||
|
|
||||||
JOBS_CACHE_HITS = "JOBS_CACHE_HITS"
|
|
||||||
JOBS_CACHE_MISSES = "JOBS_CACHE_MISSES"
|
|
||||||
|
|
||||||
# Global variable
|
# Global variable
|
||||||
s3_client = None
|
s3_client = None
|
||||||
s3_resource = None
|
s3_resource = None
|
||||||
|
|
||||||
|
|
||||||
|
def set_job_cache(job_cache, key, value):
|
||||||
|
job_cache[key] = (value, time.time() + 8 * 24 * 60 * 60)
|
||||||
|
|
||||||
|
|
||||||
|
def clean_cache():
|
||||||
|
current_time = time.time()
|
||||||
|
keys_to_delete = []
|
||||||
|
for key, (_, expiry_time) in job_cache.items():
|
||||||
|
if expiry_time < current_time:
|
||||||
|
keys_to_delete.append(key)
|
||||||
|
|
||||||
|
for key in keys_to_delete:
|
||||||
|
del job_cache[key]
|
||||||
|
|
||||||
|
|
||||||
def get_s3_client():
|
def get_s3_client():
|
||||||
global s3_client
|
global s3_client
|
||||||
if s3_client is None:
|
if s3_client is None:
|
||||||
@@ -88,7 +101,6 @@ def get_bucket_name():
|
|||||||
|
|
||||||
|
|
||||||
def cleanup_old_s3_objects():
|
def cleanup_old_s3_objects():
|
||||||
|
|
||||||
bucket_name = get_bucket_name()
|
bucket_name = get_bucket_name()
|
||||||
|
|
||||||
s3_client = get_s3_client()
|
s3_client = get_s3_client()
|
||||||
@@ -100,9 +112,15 @@ def cleanup_old_s3_objects():
|
|||||||
while True:
|
while True:
|
||||||
for obj in response.get("Contents", []):
|
for obj in response.get("Contents", []):
|
||||||
if obj["LastModified"] <= time_limit:
|
if obj["LastModified"] <= time_limit:
|
||||||
current_app.logger.info(
|
|
||||||
f"#delete-old-s3-objects Wanting to delete: {obj['LastModified']} {obj['Key']}"
|
try:
|
||||||
)
|
remove_csv_object(obj["Key"])
|
||||||
|
current_app.logger.info(
|
||||||
|
f"#delete-old-s3-objects Deleted: {obj['LastModified']} {obj['Key']}"
|
||||||
|
)
|
||||||
|
except botocore.exceptions.ClientError:
|
||||||
|
current_app.logger.exception(f"Couldn't delete {obj['Key']}")
|
||||||
|
|
||||||
if "NextContinuationToken" in response:
|
if "NextContinuationToken" in response:
|
||||||
response = s3_client.list_objects_v2(
|
response = s3_client.list_objects_v2(
|
||||||
Bucket=bucket_name,
|
Bucket=bucket_name,
|
||||||
@@ -116,36 +134,70 @@ def cleanup_old_s3_objects():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def get_s3_files():
|
def get_job_id_from_s3_object_key(key):
|
||||||
|
object_arr = key.split("/")
|
||||||
|
job_id = object_arr[1] # get the job_id
|
||||||
|
job_id = job_id.replace(".csv", "") # we just want the job_id
|
||||||
|
return job_id
|
||||||
|
|
||||||
|
|
||||||
|
def read_s3_file(bucket_name, object_key, s3res):
|
||||||
|
"""
|
||||||
|
This method runs during the 'regenerate job cache' task.
|
||||||
|
Note that in addition to retrieving the jobs and putting them
|
||||||
|
into the cache, this method also does some pre-processing by
|
||||||
|
putting a list of all phone numbers into the cache as well.
|
||||||
|
|
||||||
|
This means that when the report needs to be regenerated, it
|
||||||
|
can easily find the phone numbers in the cache through job_cache[<job_id>_phones]
|
||||||
|
and the personalization through job_cache[<job_id>_personalisation], which
|
||||||
|
in theory should make report generation a lot faster.
|
||||||
|
|
||||||
|
We are moving processing from the front end where the user can see it
|
||||||
|
in wait time, to this back end process.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
job_id = get_job_id_from_s3_object_key(object_key)
|
||||||
|
if job_cache.get(job_id) is None:
|
||||||
|
object = (
|
||||||
|
s3res.Object(bucket_name, object_key)
|
||||||
|
.get()["Body"]
|
||||||
|
.read()
|
||||||
|
.decode("utf-8")
|
||||||
|
)
|
||||||
|
set_job_cache(job_cache, job_id, object)
|
||||||
|
set_job_cache(job_cache, f"{job_id}_phones", extract_phones(object))
|
||||||
|
set_job_cache(
|
||||||
|
job_cache,
|
||||||
|
f"{job_id}_personalisation",
|
||||||
|
extract_personalisation(object),
|
||||||
|
)
|
||||||
|
|
||||||
|
except LookupError:
|
||||||
|
# perhaps our key is not formatted as we expected. If so skip it.
|
||||||
|
current_app.logger.exception("LookupError #notify-admin-1200")
|
||||||
|
|
||||||
|
|
||||||
|
def get_s3_files():
|
||||||
|
"""
|
||||||
|
We're using the ThreadPoolExecutor here to speed up the retrieval of S3
|
||||||
|
csv files for scaling needs.
|
||||||
|
"""
|
||||||
bucket_name = current_app.config["CSV_UPLOAD_BUCKET"]["bucket"]
|
bucket_name = current_app.config["CSV_UPLOAD_BUCKET"]["bucket"]
|
||||||
objects = list_s3_objects()
|
object_keys = list_s3_objects()
|
||||||
|
|
||||||
s3res = get_s3_resource()
|
s3res = get_s3_resource()
|
||||||
current_app.logger.info(
|
current_app.logger.info(
|
||||||
f"JOBS cache length before regen: {len(JOBS)} #notify-admin-1200"
|
f"job_cache length before regen: {len(job_cache)} #notify-admin-1200"
|
||||||
)
|
)
|
||||||
for object in objects:
|
try:
|
||||||
# We put our csv files in the format "service-{service_id}-notify/{job_id}"
|
with ThreadPoolExecutor() as executor:
|
||||||
try:
|
executor.map(lambda key: read_s3_file(bucket_name, key, s3res), object_keys)
|
||||||
object_arr = object.split("/")
|
except Exception:
|
||||||
job_id = object_arr[1] # get the job_id
|
current_app.logger.exception("Connection pool issue")
|
||||||
job_id = job_id.replace(".csv", "") # we just want the job_id
|
|
||||||
if JOBS.get(job_id) is None:
|
|
||||||
object = (
|
|
||||||
s3res.Object(bucket_name, object)
|
|
||||||
.get()["Body"]
|
|
||||||
.read()
|
|
||||||
.decode("utf-8")
|
|
||||||
)
|
|
||||||
if "phone number" in object.lower():
|
|
||||||
JOBS[job_id] = object
|
|
||||||
except LookupError:
|
|
||||||
# perhaps our key is not formatted as we expected. If so skip it.
|
|
||||||
current_app.logger.exception("LookupError #notify-admin-1200")
|
|
||||||
|
|
||||||
current_app.logger.info(
|
current_app.logger.info(
|
||||||
f"JOBS cache length after regen: {len(JOBS)} #notify-admin-1200"
|
f"job_cache length after regen: {len(job_cache)} #notify-admin-1200"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -303,20 +355,6 @@ def get_job_from_s3(service_id, job_id):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def incr_jobs_cache_misses():
|
|
||||||
if not redis_store.get(JOBS_CACHE_MISSES):
|
|
||||||
redis_store.set(JOBS_CACHE_MISSES, 1)
|
|
||||||
else:
|
|
||||||
redis_store.incr(JOBS_CACHE_MISSES)
|
|
||||||
|
|
||||||
|
|
||||||
def incr_jobs_cache_hits():
|
|
||||||
if not redis_store.get(JOBS_CACHE_HITS):
|
|
||||||
redis_store.set(JOBS_CACHE_HITS, 1)
|
|
||||||
else:
|
|
||||||
redis_store.incr(JOBS_CACHE_HITS)
|
|
||||||
|
|
||||||
|
|
||||||
def extract_phones(job):
|
def extract_phones(job):
|
||||||
job = job.split("\r\n")
|
job = job.split("\r\n")
|
||||||
first_row = job[0]
|
first_row = job[0]
|
||||||
@@ -325,7 +363,7 @@ def extract_phones(job):
|
|||||||
phone_index = 0
|
phone_index = 0
|
||||||
for item in first_row:
|
for item in first_row:
|
||||||
# Note: may contain a BOM and look like \ufeffphone number
|
# Note: may contain a BOM and look like \ufeffphone number
|
||||||
if "phone number" in item.lower():
|
if item.lower() in ["phone number", "\\ufeffphone number"]:
|
||||||
break
|
break
|
||||||
phone_index = phone_index + 1
|
phone_index = phone_index + 1
|
||||||
|
|
||||||
@@ -349,6 +387,8 @@ def extract_phones(job):
|
|||||||
|
|
||||||
|
|
||||||
def extract_personalisation(job):
|
def extract_personalisation(job):
|
||||||
|
if isinstance(job, dict):
|
||||||
|
job = job[0]
|
||||||
job = job.split("\r\n")
|
job = job.split("\r\n")
|
||||||
first_row = job[0]
|
first_row = job[0]
|
||||||
job.pop(0)
|
job.pop(0)
|
||||||
@@ -364,18 +404,15 @@ def extract_personalisation(job):
|
|||||||
|
|
||||||
|
|
||||||
def get_phone_number_from_s3(service_id, job_id, job_row_number):
|
def get_phone_number_from_s3(service_id, job_id, job_row_number):
|
||||||
# We don't want to constantly pull down a job from s3 every time we need a phone number.
|
job = job_cache.get(job_id)
|
||||||
# At the same time we don't want to store it in redis or the db
|
|
||||||
# So this is a little recycling mechanism to reduce the number of downloads.
|
|
||||||
job = JOBS.get(job_id)
|
|
||||||
if job is None:
|
if job is None:
|
||||||
current_app.logger.info(f"job {job_id} was not in the cache")
|
current_app.logger.info(f"job {job_id} was not in the cache")
|
||||||
job = get_job_from_s3(service_id, job_id)
|
job = get_job_from_s3(service_id, job_id)
|
||||||
# Even if it is None, put it here to avoid KeyErrors
|
# Even if it is None, put it here to avoid KeyErrors
|
||||||
JOBS[job_id] = job
|
set_job_cache(job_cache, job_id, job)
|
||||||
incr_jobs_cache_misses()
|
|
||||||
else:
|
else:
|
||||||
incr_jobs_cache_hits()
|
# skip expiration date from cache, we don't need it here
|
||||||
|
job = job[0]
|
||||||
|
|
||||||
if job is None:
|
if job is None:
|
||||||
current_app.logger.error(
|
current_app.logger.error(
|
||||||
@@ -383,24 +420,16 @@ def get_phone_number_from_s3(service_id, job_id, job_row_number):
|
|||||||
)
|
)
|
||||||
return "Unavailable"
|
return "Unavailable"
|
||||||
|
|
||||||
# If we look in the JOBS cache for the quick lookup dictionary of phones for a given job
|
phones = extract_phones(job)
|
||||||
# and that dictionary is not there, create it
|
set_job_cache(job_cache, f"{job_id}_phones", phones)
|
||||||
if JOBS.get(f"{job_id}_phones") is None:
|
|
||||||
JOBS[f"{job_id}_phones"] = extract_phones(job)
|
|
||||||
|
|
||||||
# If we can find the quick dictionary, use it
|
# If we can find the quick dictionary, use it
|
||||||
if JOBS.get(f"{job_id}_phones") is not None:
|
phone_to_return = phones[job_row_number]
|
||||||
phone_to_return = JOBS.get(f"{job_id}_phones").get(job_row_number)
|
if phone_to_return:
|
||||||
if phone_to_return:
|
return phone_to_return
|
||||||
return phone_to_return
|
|
||||||
else:
|
|
||||||
current_app.logger.warning(
|
|
||||||
f"Was unable to retrieve phone number from lookup dictionary for job {job_id}"
|
|
||||||
)
|
|
||||||
return "Unavailable"
|
|
||||||
else:
|
else:
|
||||||
current_app.logger.error(
|
current_app.logger.warning(
|
||||||
f"Was unable to construct lookup dictionary for job {job_id}"
|
f"Was unable to retrieve phone number from lookup dictionary for job {job_id}"
|
||||||
)
|
)
|
||||||
return "Unavailable"
|
return "Unavailable"
|
||||||
|
|
||||||
@@ -409,14 +438,15 @@ def get_personalisation_from_s3(service_id, job_id, job_row_number):
|
|||||||
# We don't want to constantly pull down a job from s3 every time we need the personalisation.
|
# We don't want to constantly pull down a job from s3 every time we need the personalisation.
|
||||||
# At the same time we don't want to store it in redis or the db
|
# At the same time we don't want to store it in redis or the db
|
||||||
# So this is a little recycling mechanism to reduce the number of downloads.
|
# So this is a little recycling mechanism to reduce the number of downloads.
|
||||||
job = JOBS.get(job_id)
|
job = job_cache.get(job_id)
|
||||||
if job is None:
|
if job is None:
|
||||||
|
current_app.logger.info(f"job {job_id} was not in the cache")
|
||||||
job = get_job_from_s3(service_id, job_id)
|
job = get_job_from_s3(service_id, job_id)
|
||||||
JOBS[job_id] = job
|
# Even if it is None, put it here to avoid KeyErrors
|
||||||
incr_jobs_cache_misses()
|
set_job_cache(job_cache, job_id, job)
|
||||||
else:
|
else:
|
||||||
incr_jobs_cache_hits()
|
# skip expiration date from cache, we don't need it here
|
||||||
|
job = job[0]
|
||||||
# If the job is None after our attempt to retrieve it from s3, it
|
# If the job is None after our attempt to retrieve it from s3, it
|
||||||
# probably means the job is old and has been deleted from s3, in
|
# probably means the job is old and has been deleted from s3, in
|
||||||
# which case there is nothing we can do. It's unlikely to run into
|
# which case there is nothing we can do. It's unlikely to run into
|
||||||
@@ -428,14 +458,11 @@ def get_personalisation_from_s3(service_id, job_id, job_row_number):
|
|||||||
)
|
)
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
# If we look in the JOBS cache for the quick lookup dictionary of personalisations for a given job
|
set_job_cache(job_cache, f"{job_id}_personalisation", extract_personalisation(job))
|
||||||
# and that dictionary is not there, create it
|
|
||||||
if JOBS.get(f"{job_id}_personalisation") is None:
|
|
||||||
JOBS[f"{job_id}_personalisation"] = extract_personalisation(job)
|
|
||||||
|
|
||||||
# If we can find the quick dictionary, use it
|
# If we can find the quick dictionary, use it
|
||||||
if JOBS.get(f"{job_id}_personalisation") is not None:
|
if job_cache.get(f"{job_id}_personalisation") is not None:
|
||||||
personalisation_to_return = JOBS.get(f"{job_id}_personalisation").get(
|
personalisation_to_return = job_cache.get(f"{job_id}_personalisation")[0].get(
|
||||||
job_row_number
|
job_row_number
|
||||||
)
|
)
|
||||||
if personalisation_to_return:
|
if personalisation_to_return:
|
||||||
|
|||||||
@@ -446,6 +446,11 @@ def regenerate_job_cache():
|
|||||||
s3.get_s3_files()
|
s3.get_s3_files()
|
||||||
|
|
||||||
|
|
||||||
|
@notify_celery.task(name="clean-job-cache")
|
||||||
|
def clean_job_cache():
|
||||||
|
s3.clean_cache()
|
||||||
|
|
||||||
|
|
||||||
@notify_celery.task(name="delete-old-s3-objects")
|
@notify_celery.task(name="delete-old-s3-objects")
|
||||||
def delete_old_s3_objects():
|
def delete_old_s3_objects():
|
||||||
s3.cleanup_old_s3_objects()
|
s3.cleanup_old_s3_objects()
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ AWS_CLIENT_CONFIG = Config(
|
|||||||
"addressing_style": "virtual",
|
"addressing_style": "virtual",
|
||||||
},
|
},
|
||||||
use_fips_endpoint=True,
|
use_fips_endpoint=True,
|
||||||
|
# This is the default but just for doc sake
|
||||||
|
# there may come a time when increasing this helps
|
||||||
|
# with job cache management.
|
||||||
|
max_pool_connections=10,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -251,7 +251,7 @@ class Config(object):
|
|||||||
},
|
},
|
||||||
"delete_old_s3_objects": {
|
"delete_old_s3_objects": {
|
||||||
"task": "delete-old-s3-objects",
|
"task": "delete-old-s3-objects",
|
||||||
"schedule": crontab(minute="*/5"),
|
"schedule": crontab(hour=7, minute=10),
|
||||||
"options": {"queue": QueueNames.PERIODIC},
|
"options": {"queue": QueueNames.PERIODIC},
|
||||||
},
|
},
|
||||||
"regenerate-job-cache": {
|
"regenerate-job-cache": {
|
||||||
@@ -269,6 +269,11 @@ class Config(object):
|
|||||||
"expires": 60,
|
"expires": 60,
|
||||||
}, # Ensure it doesn't run if missed
|
}, # Ensure it doesn't run if missed
|
||||||
},
|
},
|
||||||
|
"clean-job-cache": {
|
||||||
|
"task": "clean-job-cache",
|
||||||
|
"schedule": crontab(hour=2, minute=11),
|
||||||
|
"options": {"queue": QueueNames.PERIODIC},
|
||||||
|
},
|
||||||
"cleanup-unfinished-jobs": {
|
"cleanup-unfinished-jobs": {
|
||||||
"task": "cleanup-unfinished-jobs",
|
"task": "cleanup-unfinished-jobs",
|
||||||
"schedule": crontab(hour=4, minute=5),
|
"schedule": crontab(hour=4, minute=5),
|
||||||
|
|||||||
@@ -36,17 +36,14 @@ def send_sms_to_provider(notification):
|
|||||||
Get data for recipient, template,
|
Get data for recipient, template,
|
||||||
notification and send it to sns.
|
notification and send it to sns.
|
||||||
"""
|
"""
|
||||||
# we no longer store the personalisation in the db,
|
# Take this path for report generation, where we know
|
||||||
# need to retrieve from s3 before generating content
|
# everything is in the cache.
|
||||||
# However, we are still sending the initial verify code through personalisation
|
personalisation = get_personalisation_from_s3(
|
||||||
# so if there is some value there, don't overwrite it
|
notification.service_id,
|
||||||
if not notification.personalisation:
|
notification.job_id,
|
||||||
personalisation = get_personalisation_from_s3(
|
notification.job_row_number,
|
||||||
notification.service_id,
|
)
|
||||||
notification.job_id,
|
notification.personalisation = personalisation
|
||||||
notification.job_row_number,
|
|
||||||
)
|
|
||||||
notification.personalisation = personalisation
|
|
||||||
|
|
||||||
service = SerialisedService.from_id(notification.service_id)
|
service = SerialisedService.from_id(notification.service_id)
|
||||||
message_id = None
|
message_id = None
|
||||||
|
|||||||
@@ -39,6 +39,12 @@ def init_app(app):
|
|||||||
for logger_instance, handler in product(warning_loggers, handlers):
|
for logger_instance, handler in product(warning_loggers, handlers):
|
||||||
logger_instance.addHandler(handler)
|
logger_instance.addHandler(handler)
|
||||||
logger_instance.setLevel(logging.WARNING)
|
logger_instance.setLevel(logging.WARNING)
|
||||||
|
|
||||||
|
# Suppress specific loggers to prevent leaking sensitive info
|
||||||
|
logging.getLogger("boto3").setLevel(logging.ERROR)
|
||||||
|
logging.getLogger("botocore").setLevel(logging.ERROR)
|
||||||
|
logging.getLogger("urllib3").setLevel(logging.ERROR)
|
||||||
|
|
||||||
app.logger.info("Logging configured")
|
app.logger.info("Logging configured")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
from datetime import timedelta
|
||||||
from os import getenv
|
from os import getenv
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -8,6 +9,7 @@ from app.aws.s3 import (
|
|||||||
cleanup_old_s3_objects,
|
cleanup_old_s3_objects,
|
||||||
file_exists,
|
file_exists,
|
||||||
get_job_from_s3,
|
get_job_from_s3,
|
||||||
|
get_job_id_from_s3_object_key,
|
||||||
get_personalisation_from_s3,
|
get_personalisation_from_s3,
|
||||||
get_phone_number_from_s3,
|
get_phone_number_from_s3,
|
||||||
get_s3_file,
|
get_s3_file,
|
||||||
@@ -31,15 +33,30 @@ def single_s3_object_stub(key="foo", last_modified=None):
|
|||||||
|
|
||||||
|
|
||||||
def test_cleanup_old_s3_objects(mocker):
|
def test_cleanup_old_s3_objects(mocker):
|
||||||
|
"""
|
||||||
|
Currently we are going to delete s3 objects if they are more than 14 days old,
|
||||||
|
because we want to delete all jobs older than 7 days, and jobs can be scheduled
|
||||||
|
three days in advance, and on top of that we want to leave a little cushion for
|
||||||
|
the time being. This test shows that a 3 day old job ("B") is not deleted,
|
||||||
|
whereas a 30 day old job ("A") is.
|
||||||
|
"""
|
||||||
mocker.patch("app.aws.s3.get_bucket_name", return_value="Bucket")
|
mocker.patch("app.aws.s3.get_bucket_name", return_value="Bucket")
|
||||||
|
|
||||||
mock_s3_client = mocker.Mock()
|
mock_s3_client = mocker.Mock()
|
||||||
mocker.patch("app.aws.s3.get_s3_client", return_value=mock_s3_client)
|
mocker.patch("app.aws.s3.get_s3_client", return_value=mock_s3_client)
|
||||||
|
mock_remove_csv_object = mocker.patch("app.aws.s3.remove_csv_object")
|
||||||
|
lastmod30 = aware_utcnow() - timedelta(days=30)
|
||||||
|
lastmod3 = aware_utcnow() - timedelta(days=3)
|
||||||
|
|
||||||
mock_s3_client.list_objects_v2.return_value = {
|
mock_s3_client.list_objects_v2.return_value = {
|
||||||
"Contents": [{"Key": "A", "LastModified": aware_utcnow()}]
|
"Contents": [
|
||||||
|
{"Key": "A", "LastModified": lastmod30},
|
||||||
|
{"Key": "B", "LastModified": lastmod3},
|
||||||
|
]
|
||||||
}
|
}
|
||||||
cleanup_old_s3_objects()
|
cleanup_old_s3_objects()
|
||||||
mock_s3_client.list_objects_v2.assert_called_with(Bucket="Bucket")
|
mock_s3_client.list_objects_v2.assert_called_with(Bucket="Bucket")
|
||||||
|
mock_remove_csv_object.assert_called_once_with("A")
|
||||||
|
|
||||||
|
|
||||||
def test_get_s3_file_makes_correct_call(notify_api, mocker):
|
def test_get_s3_file_makes_correct_call(notify_api, mocker):
|
||||||
@@ -95,13 +112,27 @@ def test_get_s3_file_makes_correct_call(notify_api, mocker):
|
|||||||
def test_get_phone_number_from_s3(
|
def test_get_phone_number_from_s3(
|
||||||
mocker, job, job_id, job_row_number, expected_phone_number
|
mocker, job, job_id, job_row_number, expected_phone_number
|
||||||
):
|
):
|
||||||
mocker.patch("app.aws.s3.redis_store")
|
|
||||||
get_job_mock = mocker.patch("app.aws.s3.get_job_from_s3")
|
get_job_mock = mocker.patch("app.aws.s3.get_job_from_s3")
|
||||||
get_job_mock.return_value = job
|
get_job_mock.return_value = job
|
||||||
phone_number = get_phone_number_from_s3("service_id", job_id, job_row_number)
|
phone_number = get_phone_number_from_s3("service_id", job_id, job_row_number)
|
||||||
assert phone_number == expected_phone_number
|
assert phone_number == expected_phone_number
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"key, expected_job_id",
|
||||||
|
[
|
||||||
|
("service-blahblahblah-notify/abcde.csv", "abcde"),
|
||||||
|
(
|
||||||
|
"service-x-notify/4c99f361-4ed7-49b1-bd6f-02fe0c807c53.csv",
|
||||||
|
"4c99f361-4ed7-49b1-bd6f-02fe0c807c53",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_get_job_id_from_s3_object_key(key, expected_job_id):
|
||||||
|
actual_job_id = get_job_id_from_s3_object_key(key)
|
||||||
|
assert actual_job_id == expected_job_id
|
||||||
|
|
||||||
|
|
||||||
def mock_s3_get_object_slowdown(*args, **kwargs):
|
def mock_s3_get_object_slowdown(*args, **kwargs):
|
||||||
error_response = {
|
error_response = {
|
||||||
"Error": {
|
"Error": {
|
||||||
@@ -160,7 +191,6 @@ def test_get_job_from_s3_exponential_backoff_file_not_found(mocker):
|
|||||||
def test_get_personalisation_from_s3(
|
def test_get_personalisation_from_s3(
|
||||||
mocker, job, job_id, job_row_number, expected_personalisation
|
mocker, job, job_id, job_row_number, expected_personalisation
|
||||||
):
|
):
|
||||||
mocker.patch("app.aws.s3.redis_store")
|
|
||||||
get_job_mock = mocker.patch("app.aws.s3.get_job_from_s3")
|
get_job_mock = mocker.patch("app.aws.s3.get_job_from_s3")
|
||||||
get_job_mock.return_value = job
|
get_job_mock.return_value = job
|
||||||
personalisation = get_personalisation_from_s3("service_id", job_id, job_row_number)
|
personalisation = get_personalisation_from_s3("service_id", job_id, job_row_number)
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user