From 39e031db3b4f614dc31954af1e06fc46abd9f616 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 7 Aug 2025 14:01:41 -0700 Subject: [PATCH 1/2] fix race condition --- app/aws/s3.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/app/aws/s3.py b/app/aws/s3.py index 37ffe7aa3..ee5963f17 100644 --- a/app/aws/s3.py +++ b/app/aws/s3.py @@ -39,7 +39,7 @@ def set_job_cache(key, value): def get_job_cache(key): - + key = str(key) ret = job_cache.get(key) return ret @@ -53,14 +53,15 @@ def len_job_cache(): 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) - current_app.logger.debug( - f"Deleting the following keys from the job_cache: {keys_to_delete}" - ) with job_cache_lock: + for key, (_, expiry_time) in job_cache.items(): + if expiry_time < current_time: + keys_to_delete.append(key) + + current_app.logger.debug( + f"Deleting the following keys from the job_cache: {keys_to_delete}" + ) for key in keys_to_delete: del job_cache[key] @@ -524,11 +525,13 @@ def extract_personalisation(job): def get_phone_number_from_s3(service_id, job_id, job_row_number): + job = get_job_cache(job_id) if job is None: job = get_job_from_s3(service_id, job_id) # Even if it is None, put it here to avoid KeyErrors set_job_cache(job_id, job) + current_app.logger.info(f"HAD TO SET JOB CACHE for {job_id}") else: # skip expiration date from cache, we don't need it here job = job[0] @@ -566,6 +569,7 @@ def get_personalisation_from_s3(service_id, job_id, job_row_number): # 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 = get_job_cache(job_id) + if job is None: job = get_job_from_s3(service_id, job_id) # Even if it is None, put it here to avoid KeyErrors @@ -587,6 +591,7 @@ def get_personalisation_from_s3(service_id, job_id, job_row_number): personalisation = get_job_cache(f"{job_id}_personalisation") if personalisation is None: set_job_cache(f"{job_id}_personalisation", extract_personalisation(job)) + current_app.logger.info("HAD TO EXTRACT PERSONALIZATION") return get_job_cache(f"{job_id}_personalisation")[0].get(job_row_number) From c87653523b484bf2b85d24c42686b23fada2ea22 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 7 Aug 2025 14:04:27 -0700 Subject: [PATCH 2/2] remove debug --- app/aws/s3.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/aws/s3.py b/app/aws/s3.py index ee5963f17..fb8e17ed9 100644 --- a/app/aws/s3.py +++ b/app/aws/s3.py @@ -531,7 +531,6 @@ def get_phone_number_from_s3(service_id, job_id, job_row_number): job = get_job_from_s3(service_id, job_id) # Even if it is None, put it here to avoid KeyErrors set_job_cache(job_id, job) - current_app.logger.info(f"HAD TO SET JOB CACHE for {job_id}") else: # skip expiration date from cache, we don't need it here job = job[0] @@ -591,7 +590,6 @@ def get_personalisation_from_s3(service_id, job_id, job_row_number): personalisation = get_job_cache(f"{job_id}_personalisation") if personalisation is None: set_job_cache(f"{job_id}_personalisation", extract_personalisation(job)) - current_app.logger.info("HAD TO EXTRACT PERSONALIZATION") return get_job_cache(f"{job_id}_personalisation")[0].get(job_row_number)