Skip to content

Commit

Permalink
Move resync behaviour into separate peer_sync function.
Browse files Browse the repository at this point in the history
Run in finally block to always resync.
  • Loading branch information
rtibbles committed Aug 24, 2021
1 parent f7190e7 commit b0fe8aa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
11 changes: 0 additions & 11 deletions kolibri/core/auth/management/commands/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from kolibri.core.auth.models import dataset_cache
from kolibri.core.auth.sync_event_hook_utils import register_sync_event_handlers
from kolibri.core.logger.utils.data import bytes_for_humans
from kolibri.core.public.utils import schedule_new_sync
from kolibri.core.tasks.exceptions import UserCancelledError
from kolibri.core.tasks.management.commands.base import AsyncCommand
from kolibri.core.utils.lock import db_lock
Expand Down Expand Up @@ -82,12 +81,6 @@ def add_arguments(self, parser):
action="store_true",
help="do not create a facility and temporary superuser",
)
parser.add_argument(
"--resync-interval",
type=int,
default=None,
help="Seconds to schedule a new sync",
)
# parser.add_argument("--scope-id", type=str, default=FULL_FACILITY)

def handle_async(self, *args, **options): # noqa C901
Expand All @@ -103,7 +96,6 @@ def handle_async(self, *args, **options): # noqa C901
no_pull,
noninteractive,
no_provision,
resync_interval,
) = (
options["baseurl"],
options["facility"],
Expand All @@ -115,7 +107,6 @@ def handle_async(self, *args, **options): # noqa C901
options["no_pull"],
options["noninteractive"],
options["no_provision"],
options["resync_interval"],
)

PORTAL_SYNC = baseurl == DATA_PORTAL_SYNCING_BASE_URL
Expand Down Expand Up @@ -278,8 +269,6 @@ def handle_async(self, *args, **options): # noqa C901
self.job.save_meta()

dataset_cache.deactivate()
if user_id and resync_interval:
schedule_new_sync(baseurl, user_id, resync_interval)
logger.info("Syncing has been completed.")

@contextmanager
Expand Down
24 changes: 23 additions & 1 deletion kolibri/core/public/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ def get_device_info(version=DEVICE_INFO_VERSION):
return info


def peer_sync(**kwargs):
try:
call_command("sync", **kwargs)
except Exception:
logger.error(
"Error syncing user {} to server {}".format(
kwargs["user"], kwargs["baseurl"]
)
)
raise
finally:
# schedule a new sync
schedule_new_sync(
kwargs["baseurl"], kwargs["user"], interval=kwargs["resync_interval"]
)


def startpeerusersync(
server, user_id, resync_interval=OPTIONS["Deployment"]["SYNC_INTERVAL"]
):
Expand Down Expand Up @@ -118,7 +135,7 @@ def startpeerusersync(
job_data["resync_interval"] = resync_interval
JOB_ID = hashlib.md5("{}::{}".format(server, user).encode()).hexdigest()
job_data["job_id"] = JOB_ID
job = queue.enqueue(call_command, "sync", **job_data)
job = queue.enqueue(peer_sync, **job_data)

return job

Expand Down Expand Up @@ -269,6 +286,11 @@ def handle_server_sync_response(response, server, user):

def schedule_new_sync(server, user, interval=OPTIONS["Deployment"]["SYNC_INTERVAL"]):
# reschedule the process for a new sync
logging.info(
"Requeueing to sync with server {} for user {} in {} seconds".format(
server, user, interval
)
)
dt = datetime.timedelta(seconds=interval)
JOB_ID = hashlib.md5("{}:{}".format(server, user).encode()).hexdigest()
job = Job(request_soud_sync, server, user, job_id=JOB_ID)
Expand Down

0 comments on commit b0fe8aa

Please sign in to comment.