Skip to content

Commit

Permalink
#1149 | Skip background-sync if we had recently synced within the las…
Browse files Browse the repository at this point in the history
…t half an hour
  • Loading branch information
himeshr committed Oct 26, 2023
1 parent 2d04d0b commit 17f806b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/openchs-android/src/service/SyncTelemetryService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions packages/openchs-android/src/task/Sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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");
Expand All @@ -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")
Expand Down

0 comments on commit 17f806b

Please sign in to comment.