Skip to content

Commit

Permalink
Merge branch 'feat/win-sync-engine' into feat/reload-sync-engine-on-d…
Browse files Browse the repository at this point in the history
…rive-changes
  • Loading branch information
larryrider committed Sep 28, 2023
2 parents fc50401 + 302d133 commit 69a4a7a
Show file tree
Hide file tree
Showing 78 changed files with 1,402 additions and 530 deletions.
10 changes: 10 additions & 0 deletions src/main/fordwardToWindows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ ipcMainDrive.on('FILE_OVERWRITED', (_, payload) => {
});
});

ipcMainDrive.on('FILE_RENAMING', (_, payload) => {
const { nameWithExtension, oldName } = payload;

broadcastToWindows('sync-info-update', {
action: 'RENAMING',
name: nameWithExtension,
oldName,
});
});

ipcMainDrive.on('FILE_RENAMED', (_, payload) => {
const { nameWithExtension } = payload;

Expand Down
1 change: 1 addition & 0 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import './device/handlers';
import './usage/handlers';
import './realtime';
import './tray/tray';
import './tray/handlers';
import './fordwardToWindows';
import './analytics/handlers';
import './platform/handlers';
Expand Down
43 changes: 22 additions & 21 deletions src/main/remote-sync/RemoteSyncManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class RemoteSyncManager {
* Throws an error if there's a sync in progress for this class instance
*/
async startRemoteSync() {
const start = Date.now();
// const start = Date.now();
Logger.info('Starting remote to local sync');

const testPassed = this.smokeTest();
Expand Down Expand Up @@ -102,26 +102,27 @@ export class RemoteSyncManager {
this.changeStatus('SYNC_FAILED');
reportError(error as Error);
} finally {
const totalDuration = Date.now() - start;
// const totalDuration = Date.now() - start;

Logger.info('-----------------');
Logger.info('REMOTE SYNC STATS\n');
// Logger.info('-----------------');
// Logger.info('REMOTE SYNC STATS\n');
Logger.info('Total synced files: ', this.totalFilesSynced);

Logger.info(
`Files sync speed: ${
this.totalFilesSynced / (totalDuration / 1000)
} files/second`
);

Logger.info('Total synced folders: ', this.totalFoldersSynced);
Logger.info(
`Folders sync speed: ${
this.totalFoldersSynced / (totalDuration / 1000)
} folders/second`
);
Logger.info(`Total remote to local sync time: ${totalDuration}ms`);
Logger.info('-----------------');

// Logger.info(
// `Files sync speed: ${
// this.totalFilesSynced / (totalDuration / 1000)
// } files/second`
// );

// Logger.info('Total synced folders: ', this.totalFoldersSynced);
// Logger.info(
// `Folders sync speed: ${
// this.totalFoldersSynced / (totalDuration / 1000)
// } folders/second`
// );
// Logger.info(`Total remote to local sync time: ${totalDuration}ms`);
// Logger.info('-----------------');
}
}

Expand Down Expand Up @@ -387,9 +388,9 @@ export class RemoteSyncManager {
? updatedAtCheckpoint.toISOString()
: undefined,
};
Logger.info(
`Requesting folders with params ${JSON.stringify(params, null, 2)}`
);
// Logger.info(
// `Requesting folders with params ${JSON.stringify(params, null, 2)}`
// );
const response = await this.config.httpClient.get(
`${process.env.NEW_DRIVE_URL}/drive/folders`,
{
Expand Down
12 changes: 6 additions & 6 deletions src/shared/IPC/events/sync-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ type FileUpdatePayload = {
};

export type FolderEvents = {
CREATING_FOLDER: (payload: { name: string }) => void;
FOLDER_CREATING: (payload: { name: string }) => void;
FOLDER_CREATED: (payload: { name: string }) => void;

RENAMING_FOLDER: (payload: { oldName: string; newName: string }) => void;
FOLDER_RENAMING: (payload: { oldName: string; newName: string }) => void;
FOLDER_RENAMED: (payload: { oldName: string; newName: string }) => void;
};

export type FilesEvents = {
UPLOADING_FILE: (payload: FileUpdatePayload) => void;
FILE_UPLOADING: (payload: FileUpdatePayload) => void;
FILE_UPLOADED: (payload: FileUpdatePayload) => void;
FILE_DOWNLOAD_ERROR: (payload: {
name: string;
Expand All @@ -59,7 +59,7 @@ export type FilesEvents = {
error: string;
}) => void;

DOWNLOADING_FILE: (payload: FileUpdatePayload) => void;
FILE_DOWNLOADING: (payload: FileUpdatePayload) => void;
FILE_DOWNLOADED: (payload: FileUpdatePayload) => void;
FILE_UPLOAD_ERROR: (payload: {
name: string;
Expand All @@ -68,7 +68,7 @@ export type FilesEvents = {
error: string;
}) => void;

DELETING_FILE: (payload: {
FILE_DELETING: (payload: {
name: string;
extension: string;
nameWithExtension: string;
Expand All @@ -87,7 +87,7 @@ export type FilesEvents = {
error: string;
}) => void;

RENAMING_FILE: (payload: {
FILE_RENAMING: (payload: {
nameWithExtension: string;
oldName: string;
}) => void;
Expand Down
50 changes: 15 additions & 35 deletions src/workers/sync-engine/BindingManager.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,36 @@
import { VirtualDrive } from 'virtual-drive';
import Logger from 'electron-log';
import { Folder } from './modules/folders/domain/Folder';
import { File } from './modules/files/domain/File';
import { DependencyContainer } from './dependency-injection/DependencyContainer';
import { buildControllers } from './callbacks-controllers/buildControllers';

export class BindingsManager {
private static readonly PROVIDER_NAME = 'Internxt';

constructor(
private readonly drive: VirtualDrive,
private readonly controllers: ReturnType<typeof buildControllers>,
private readonly container: DependencyContainer,
private readonly paths: {
root: string;
icon: string;
}
) {}

private createFolderPlaceholder(folder: Folder) {
// In order to create a folder placeholder it's path must en with /
const folderPath = `${folder.path.value}/`;

this.drive.createItemByPath(folderPath, folder.uuid);
}

public createPlaceHolders(items: Array<File | Folder>) {
items.forEach((item) => {
if (item.isFile()) {
this.drive.createItemByPath(
item.path.value,
item.contentsId,
item.size
);
return;
}

this.createFolderPlaceholder(item);
});
}

async start(version: string, providerId: string) {
await this.stop();

const controllers = buildControllers(this.container);

const callbacks = {
notifyDeleteCallback: (
contentsId: string,
callback: (response: boolean) => void
) => {
this.controllers.deleteFile
controllers.delete
.execute(contentsId)
.then(() => {
Logger.debug('DELETE RESPONSE SUCCESSFUL');
callback(true);
})
.catch((error: Error) => {
Logger.debug('DELETE RESPONSE NOT SUCCESSFUL');
Logger.error(error);
callback(false);
});
Expand All @@ -63,7 +43,7 @@ export class BindingsManager {
contentsId: string,
callback: (response: boolean) => void
) => {
this.controllers.renameOrMoveFile.execute(
controllers.renameOrMoveFile.execute(
absolutePath,
contentsId,
callback
Expand All @@ -73,13 +53,13 @@ export class BindingsManager {
absolutePath: string,
callback: (acknowledge: boolean, id: string) => void
) => {
this.controllers.addFile.execute(absolutePath, callback);
controllers.addFile.execute(absolutePath, callback);
},
fetchDataCallback: (
contentsId: string,
callback: (success: boolean, path: string) => void
) => {
this.controllers.downloadFile
controllers.downloadFile
.execute(contentsId)
.then((path: string) => {
callback(true, path);
Expand Down Expand Up @@ -121,23 +101,23 @@ export class BindingsManager {
},
};

await this.drive.registerSyncRoot(
await this.container.virtualDrive.registerSyncRoot(
BindingsManager.PROVIDER_NAME,
version,
providerId,
callbacks,
this.paths.icon
);

await this.drive.connectSyncRoot();
await this.container.virtualDrive.connectSyncRoot();
}

watch() {
this.drive.watchAndWait(this.paths.root);
this.container.virtualDrive.watchAndWait(this.paths.root);
}

async stop() {
await this.drive.disconnectSyncRoot();
await this.container.virtualDrive.disconnectSyncRoot();
}

cleanUp() {
Expand Down
23 changes: 12 additions & 11 deletions src/workers/sync-engine/callbacks-controllers/buildControllers.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import { DependencyContainer } from '../dependency-injection/DependencyContainer';
import { AddFileController } from './controllers/AddFileController';
import { DeleteFileController } from './controllers/DeleteFileController';
import { AddController } from './controllers/AddController';
import { DeleteController } from './controllers/DeleteController';
import { DownloadFileController } from './controllers/DownloadFileController';
import { RenameOrMoveController } from './controllers/RenameOrMoveController';

export function buildControllers(container: DependencyContainer) {
const addFileController = new AddFileController(
container.contentsUploader,
container.filePathFromAbsolutePathCreator,
container.fileCreator,
const addFileController = new AddController(
container.fileCreationOrchestrator,
container.folderCreator
);

const deleteController = new DeleteController(
container.fileDeleter,
container.fileByPartialSearcher
container.folderDeleter
);

const renameOrMoveFileController = new RenameOrMoveController(
container.filePathFromAbsolutePathCreator,
container.filePathUpdater,
container.fileDeleter
container.folderPathUpdater,
deleteController
);

const deleteFileController = new DeleteFileController(container.fileDeleter);

const downloadFileController = new DownloadFileController(
container.fileFinderByContentsId,
container.contentsDownloader,
Expand All @@ -30,7 +31,7 @@ export function buildControllers(container: DependencyContainer) {
return {
addFile: addFileController,
renameOrMoveFile: renameOrMoveFileController,
deleteFile: deleteFileController,
delete: deleteController,
downloadFile: downloadFileController,
} as const;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import Logger from 'electron-log';
import { CallbackController } from './CallbackController';
import { rawPathIsFolder } from '../helpers/rawPathIsFolder';
import { FolderCreator } from '../../modules/folders/application/FolderCreator';
import { MapObserver } from 'workers/sync-engine/modules/shared/domain/MapObserver';
import { FileCreationOrchestrator } from 'workers/sync-engine/modules/boundaryBridge/application/FileCreationOrchestrator';

type Queue = Map<string, (acknowledge: boolean, id: string) => void>;

export class AddController extends CallbackController {
private readonly filesQueue: Queue;
private readonly foldersQueue: Queue;

private readonly observer: MapObserver;

constructor(
private readonly fileCreationOrchestrator: FileCreationOrchestrator,
private readonly folderCreator: FolderCreator
) {
super();

this.filesQueue = new Map();
this.foldersQueue = new Map();

this.observer = new MapObserver(this.foldersQueue, this.createFiles);
this.observer.startObserving();
}

private createFile = async (
absolutePath: string,
callback: (acknowledge: boolean, id: string) => void
) => {
try {
const contentsId = await this.fileCreationOrchestrator.run(absolutePath);
return callback(true, contentsId);
} catch (error: unknown) {
Logger.error('Error when adding a file: ', error);
callback(false, '');
} finally {
this.filesQueue.delete(absolutePath);
}
};

private createFiles = async () => {
for (const [absolutePath, callback] of this.filesQueue) {
await this.createFile(absolutePath, callback);
}
};

private createFolders = async () => {
for (const [absolutePath, callback] of this.foldersQueue) {
await this.createFolder(absolutePath, callback);
}
};

private createFolder = async (
absolutePath: string,
callback: (acknowledge: boolean, id: string) => void
) => {
Logger.info('Creating folder', absolutePath);
try {
const folder = await this.folderCreator.run(absolutePath);
callback(true, folder.uuid);
} catch (error: unknown) {
Logger.error('Error creating a folder: ', error);
callback(false, '');
} finally {
this.foldersQueue.delete(absolutePath);
}
};

async execute(
absolutePath: string,
callback: (acknowledge: boolean, id: string) => void
): Promise<void> {
if (rawPathIsFolder(absolutePath)) {
this.foldersQueue.set(absolutePath, callback);
await this.createFolders();
return;
}

Logger.debug('File is going to be queued: ', absolutePath);
this.filesQueue.set(absolutePath, callback);
if (this.foldersQueue.size === 0) {
this.createFiles();
}
}
}
Loading

0 comments on commit 69a4a7a

Please sign in to comment.