From 17f806b06091ce552e6968c17e6a0d9cf7ec7471 Mon Sep 17 00:00:00 2001 From: himeshr Date: Thu, 26 Oct 2023 11:57:12 +0530 Subject: [PATCH] #1149 | Skip background-sync if we had recently synced within the last half an hour --- .../src/service/SyncTelemetryService.js | 6 ++++++ packages/openchs-android/src/task/Sync.js | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/packages/openchs-android/src/service/SyncTelemetryService.js b/packages/openchs-android/src/service/SyncTelemetryService.js index a25579548..9775130d6 100644 --- a/packages/openchs-android/src/service/SyncTelemetryService.js +++ b/packages/openchs-android/src/service/SyncTelemetryService.js @@ -15,6 +15,12 @@ export default class SyncTelemetryService extends BaseService { return !_.isEmpty(results); } + getAllCompletedSyncsSortedByDescSyncEndTime() { + return this.db.objects(SyncTelemetry.schema.name) + .filtered("syncStatus = $0", "complete") + .sorted('syncEndTime', true); + } + getAllCompletedFullSyncsSortedByDescSyncEndTime() { return this.db.objects(SyncTelemetry.schema.name) .filtered("syncStatus = $0 AND syncSource <> $1", "complete", SyncService.syncSources.ONLY_UPLOAD_BACKGROUND_JOB) diff --git a/packages/openchs-android/src/task/Sync.js b/packages/openchs-android/src/task/Sync.js index c3b6e17d2..8e3264795 100644 --- a/packages/openchs-android/src/task/Sync.js +++ b/packages/openchs-android/src/task/Sync.js @@ -18,6 +18,7 @@ import RuleService from '../service/RuleService'; import PrivilegeService from '../service/PrivilegeService'; import {IndividualSearchActionNames as IndividualSearchActions} from '../action/individual/IndividualSearchActions'; import {LandingViewActions} from '../action/LandingViewActions'; +import moment from 'moment'; class Sync extends BaseTask { async execute() { @@ -42,6 +43,12 @@ class Sync extends BaseTask { return false; } await this.initDependencies(); + + if(!this.wasLastCompletedSyncDoneMoreThanHalfAnHourAgo(globalContext)) { + General.logInfo("Sync", 'Skipping auto-sync since we had recently synced within the last half an hour'); + return false; + } + General.logInfo("Sync", "Starting SyncService"); General.logInfo("Sync", "Getting SyncService"); const syncService = globalContext.beanRegistry.getService("syncService"); @@ -68,6 +75,12 @@ class Sync extends BaseTask { } } + wasLastCompletedSyncDoneMoreThanHalfAnHourAgo(globalContext) { + const syncTelemetryService = globalContext.beanRegistry.getService("syncTelemetryService"); + const lastSynced = syncTelemetryService.getAllCompletedSyncsSortedByDescSyncEndTime(); + return _.isEmpty(lastSynced) || moment(lastSynced[0].syncEndTime).add(30, 'minutes').isBefore(moment()); + } + performPostBackgroundSyncActions(dispatchAction, globalContext) { return (updatedSyncSource) => { General.logInfo("Sync", "Sync completed")