Repository: oppia/oppia

Issue:
Clear the redis memcache in the pre one-off job step
Currently, we ask release coordinators to manually clear the Memcache before running the one-off job on the server. Through this issue, we are expecting the pre-job step to clear the redis memcache i.e, clear the memcache before staring the job.  
Clear the redis memcache in the pre one-off job step
Currently, we ask release coordinators to manually clear the Memcache before running the one-off job on the server. Through this issue, we are expecting the pre-job step to clear the redis memcache i.e, clear the memcache before staring the job.  


Discussion:
As per offline discussion with @brianrodri, we are going to include this in the backend one-off job structure while we are rewriting it for the python3 migration project.

Assigning this to @brianrodri  as per offline discussion.
Hi @oppia/core-maintainers, this issue is not assigned to any project. Can you please update the same? Thanks!
@kevintab95 just to double-check, you had evidence from a recent run that this isn't fixed yet, is that correct?
Hi @seanlip, that's correct, I've documented it in https://github.com/oppia/oppia/issues/16031 and seems like it been fixed now.
@kevintab95 Just to confirm, though: this issue says "clear memcache in the pre-job step" and the issue you pointed to says "flush the cache after a job is run". Aren't these different?
Ah I see, yes, we only clear cache post job run as part of the fix for the issue mentioned in my earlier comment. Clearing cache is still done manually by release coordinators before a job is run.

The issue I had faced was, we ran a job and found that the models created / updated by the job were not fetched by the app because the cache was not updated/invalidated correctly.
Ah ok. So, do you think we should still do this?
From the issue description, it sounds like we will get rid of a manual step that release coordinators have to do currently. So, I would say this issue is worth fixing.
OK, thanks @kevintab95! Sounds good.

@vojtechjelinek, would you be able to do this, please (if straightforward)?  
To do this before a job is run it is pretty straightforward. To do this after a deploy it would be a bit more complicated and I would do it as part of my migration to GH Actionas.
As per offline discussion with @brianrodri, we are going to include this in the backend one-off job structure while we are rewriting it for the python3 migration project.

Assigning this to @brianrodri  as per offline discussion.
Hi @oppia/core-maintainers, this issue is not assigned to any project. Can you please update the same? Thanks!
@kevintab95 just to double-check, you had evidence from a recent run that this isn't fixed yet, is that correct?
Hi @seanlip, that's correct, I've documented it in https://github.com/oppia/oppia/issues/16031 and seems like it been fixed now.
@kevintab95 Just to confirm, though: this issue says "clear memcache in the pre-job step" and the issue you pointed to says "flush the cache after a job is run". Aren't these different?
Ah I see, yes, we only clear cache post job run as part of the fix for the issue mentioned in my earlier comment. Clearing cache is still done manually by release coordinators before a job is run.

The issue I had faced was, we ran a job and found that the models created / updated by the job were not fetched by the app because the cache was not updated/invalidated correctly.
Ah ok. So, do you think we should still do this?
From the issue description, it sounds like we will get rid of a manual step that release coordinators have to do currently. So, I would say this issue is worth fixing.
OK, thanks @kevintab95! Sounds good.

@vojtechjelinek, would you be able to do this, please (if straightforward)?  
To do this before a job is run it is pretty straightforward. To do this after a deploy it would be a bit more complicated and I would do it as part of my migration to GH Actionas.

Resolving patch:
diff --git a/core/jobs/jobs_manager.py b/core/jobs/jobs_manager.py
--- a/core/jobs/jobs_manager.py
+++ b/core/jobs/jobs_manager.py
@@ -24,6 +24,7 @@
 
 from core import feconf
 from core.domain import beam_job_services
+from core.domain import caching_services
 from core.jobs import base_jobs
 from core.jobs import job_options
 from core.jobs.io import cache_io
@@ -129,9 +130,17 @@ def run_job(
     job = job_class(pipeline)
     job_name = job_class.__name__
 
+    # Clear cache before running the job to be sure that the cache
+    # does not affect the job.
+    caching_services.flush_memory_caches()
+
     # NOTE: Exceptions raised within this context are logged and suppressed.
     with _job_bookkeeping_context(job_name) as run_model:
-        _ = job.run() | job_io.PutResults(run_model.id) | cache_io.FlushCache()
+        _ = (
+            job.run()
+            | job_io.PutResults(run_model.id)
+            | cache_io.FlushCache()
+        )
 
         run_result = pipeline.run()
 


Test patch: