Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanVicens committed Apr 15, 2024
1 parent de6d71b commit bb05ea5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
50 changes: 24 additions & 26 deletions src/apps/main/remote-sync/RemoteSyncManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export class RemoteSyncManager {
private totalFoldersSynced = 0;
private lastSyncingFinishedTimestamp: Date | null = null;

private static SIX_HOURS = 6 * 60 * 60 * 1000;

constructor(
private db: {
files: DatabaseCollectionAdapter<DriveFile>;
Expand Down Expand Up @@ -241,8 +239,8 @@ export class RemoteSyncManager {
* @param syncConfig Config to execute the sync with
* @returns
*/
private async syncRemoteFiles(syncConfig: SyncConfig) {
const lastFilesSyncAt = await helpers.getLastFilesSyncAt();
private async syncRemoteFiles(syncConfig: SyncConfig, from?: Date) {
const lastFilesSyncAt = from ?? helpers.getLastFilesSyncAt();
try {
Logger.info(
`Syncing files updated from ${
Expand All @@ -253,13 +251,16 @@ export class RemoteSyncManager {
lastFilesSyncAt
);

let lastFileSynced = null;

for (const remoteFile of result) {
// eslint-disable-next-line no-await-in-loop
await this.createOrUpdateSyncedFileEntry(remoteFile);
const fileUpdatedAt = new Date(remoteFile.updatedAt);

helpers.saveLastFilesSyncAt(fileUpdatedAt, SYNC_OFFSET_MS);
this.totalFilesSynced++;
lastFileSynced = remoteFile;
}

if (!hasMore) {
Expand All @@ -269,10 +270,13 @@ export class RemoteSyncManager {
return;
}
Logger.info('Retrieving more files for sync');
await this.syncRemoteFiles({
retry: 1,
maxRetries: syncConfig.maxRetries,
});
await this.syncRemoteFiles(
{
retry: 1,
maxRetries: syncConfig.maxRetries,
},
lastFileSynced ? new Date(lastFileSynced.updatedAt) : undefined
);
} catch (error) {
Logger.error('Remote files sync failed with error: ', error);

Expand Down Expand Up @@ -300,8 +304,8 @@ export class RemoteSyncManager {
* @param syncConfig Config to execute the sync with
* @returns
*/
private async syncRemoteFolders(syncConfig: SyncConfig) {
const lastFoldersSyncAt = await helpers.getLastFoldersSyncAt();
private async syncRemoteFolders(syncConfig: SyncConfig, from?: Date) {
const lastFoldersSyncAt = from ?? helpers.getLastFoldersSyncAt();
try {
Logger.info(
`Syncing folders updated from ${
Expand All @@ -312,6 +316,8 @@ export class RemoteSyncManager {
lastFoldersSyncAt
);

let lastFolderSynced = null;

for (const remoteFolder of result) {
// eslint-disable-next-line no-await-in-loop
await this.createOrUpdateSyncedFolderEntry(remoteFolder);
Expand All @@ -320,6 +326,7 @@ export class RemoteSyncManager {
Logger.info(`Saving folders updatedAt ${foldersUpdatedAt}`);
helpers.saveLastFoldersSyncAt(foldersUpdatedAt, SYNC_OFFSET_MS);
this.totalFoldersSynced++;
lastFolderSynced = remoteFolder;
}

if (!hasMore) {
Expand All @@ -329,10 +336,13 @@ export class RemoteSyncManager {
}

Logger.info('Retrieving more folders for sync');
await this.syncRemoteFolders({
retry: 1,
maxRetries: syncConfig.maxRetries,
});
await this.syncRemoteFolders(
{
retry: 1,
maxRetries: syncConfig.maxRetries,
},
lastFolderSynced ? new Date(lastFolderSynced.updatedAt) : undefined
);
} catch (error) {
Logger.error('Remote folders sync failed with error: ', error);
reportError(error as Error, {
Expand Down Expand Up @@ -363,12 +373,6 @@ export class RemoteSyncManager {
hasMore: boolean;
result: RemoteSyncedFile[];
}> {
if (updatedAtCheckpoint) {
updatedAtCheckpoint.setTime(
updatedAtCheckpoint.getTime() - RemoteSyncManager.SIX_HOURS
);
}

const params = {
limit: this.config.fetchFilesLimitPerRequest,
offset: 0,
Expand Down Expand Up @@ -438,12 +442,6 @@ export class RemoteSyncManager {
hasMore: boolean;
result: RemoteSyncedFolder[];
}> {
if (updatedAtCheckpoint) {
updatedAtCheckpoint.setTime(
updatedAtCheckpoint.getTime() - RemoteSyncManager.SIX_HOURS
);
}

const params = {
limit: this.config.fetchFilesLimitPerRequest,
offset: 0,
Expand Down
15 changes: 13 additions & 2 deletions src/apps/main/remote-sync/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Store from 'electron-store';

const SIX_HOURS_IN_MILLISECONDS = 6 * 60 * 60 * 1000;

let store: Store<{
lastFilesSyncAt?: string;
lastFoldersSyncAt?: string;
Expand All @@ -23,12 +25,17 @@ export const getRemoteSyncStore = () => {
};

export const clearRemoteSyncStore = () => getRemoteSyncStore().clear();

export function getLastFilesSyncAt(): Date | undefined {
const value = getRemoteSyncStore().get('lastFilesSyncAt');

if (!value) return undefined;

return new Date(value);
const date = new Date(value);

date.setTime(date.getTime() - SIX_HOURS_IN_MILLISECONDS);

return date;
}

export function saveLastFilesSyncAt(date: Date, offsetMs: number): Date {
Expand All @@ -44,7 +51,11 @@ export function getLastFoldersSyncAt(): Date | undefined {

if (!value) return undefined;

return new Date(value);
const date = new Date(value);

date.setTime(date.getTime() - SIX_HOURS_IN_MILLISECONDS);

return date;
}

export function saveLastFoldersSyncAt(date: Date, offsetMs: number): Date {
Expand Down

0 comments on commit bb05ea5

Please sign in to comment.