Skip to content

Commit

Permalink
Merge pull request learningequality#10318 from rtibbles/no_job_found_…
Browse files Browse the repository at this point in the history
…no_cry

Improve cleanup of automated LOD peer to peer syncing tasks
  • Loading branch information
rtibbles authored Mar 24, 2023
2 parents ac29dc1 + d243227 commit 80ab68f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
18 changes: 16 additions & 2 deletions kolibri/core/auth/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@
from kolibri.core.public.constants.user_sync_statuses import SYNC
from kolibri.core.serializers import HexOnlyUUIDField
from kolibri.core.tasks.decorators import register_task
from kolibri.core.tasks.exceptions import JobNotFound
from kolibri.core.tasks.exceptions import UserCancelledError
from kolibri.core.tasks.job import JobStatus
from kolibri.core.tasks.job import State
from kolibri.core.tasks.main import job_storage
from kolibri.core.tasks.permissions import IsAdminForJob
from kolibri.core.tasks.permissions import IsSuperAdmin
from kolibri.core.tasks.permissions import NotProvisioned
from kolibri.core.tasks.utils import get_current_job
from kolibri.core.tasks.validation import JobValidator
from kolibri.core.utils.urls import reverse_remote
from kolibri.utils.conf import KOLIBRI_HOME
Expand Down Expand Up @@ -465,6 +468,9 @@ def peerusersync(command, **kwargs):
kwargs["user"], kwargs["baseurl"]
)
)
elif isinstance(e, UserCancelledError):
# In this instance we are cancelling the task, and we should not reschedule
resync_interval = None
else:
logger.error(
"Error syncing user {} to server {}".format(
Expand All @@ -477,6 +483,10 @@ def peerusersync(command, **kwargs):
if cleanup and command == "resumesync":
# for resume we should have sync_session_id kwarg
queue_soud_sync_cleanup(kwargs["sync_session_id"])
job = get_current_job()
if job and job.storage.check_job_canceled(job.job_id):
# If the job is canceled, then do not attempt to resync
resync_interval = None
if resync_interval:
# schedule a new sync
schedule_new_sync(
Expand Down Expand Up @@ -526,8 +536,12 @@ def stoppeerusersync(server, user_id):

# clear jobs with matching ID
job_id = hashlib.md5("{}::{}".format(server, user_id).encode()).hexdigest()
job_storage.clear(job_id=job_id)
job_storage.cancel(job_id)
try:
job_storage.cancel(job_id)
job_storage.clear(job_id=job_id)
except JobNotFound:
# No job to clean up, we're done!
pass

# skip if we couldn't find one for resume
if sync_session is None:
Expand Down
6 changes: 5 additions & 1 deletion kolibri/core/tasks/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,11 @@ def restart_job(self, job_id):
)

def check_job_canceled(self, job_id):
job = self.get_job(job_id)
try:
job = self.get_job(job_id)
except JobNotFound:
return True

return job.state == State.CANCELED or job.state == State.CANCELING

def cancel(self, job_id):
Expand Down

0 comments on commit 80ab68f

Please sign in to comment.