Skip to content

Commit

Permalink
feat/ change in download wait
Browse files Browse the repository at this point in the history
  • Loading branch information
ArceDanielShok committed May 22, 2024
1 parent e9f12d4 commit 641e1cf
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 46 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "internxt-drive",
"version": "2.0.9-qa",
"version": "2.0.9",
"author": "Internxt <[email protected]>",
"description": "Internxt Drive client UI",
"license": "AGPL-3.0",
Expand Down Expand Up @@ -28,7 +28,7 @@
"test:e2e": "playwright test --config=src/test",
"type-check": "tsc --noEmit --pretty --skipLibCheck",
"prepare": "husky install",
"reload-virtual-drive": "cd release/app && npm run reload-virtual-drive && cd ../.. && npm i",
"reload-virtual-drive": "cd release/app && npm run reload-virtual-drive && cd ../.. && yarn",
"start:reload-bindings": "npm run reload-virtual-drive && npm run start"
},
"lint-staged": {
Expand Down Expand Up @@ -95,7 +95,8 @@
"win": {
"target": [
"nsis"
]
],
"certificateSubjectName": "Internxt Universal Technologies SL"
},
"linux": {
"target": [
Expand Down
2 changes: 1 addition & 1 deletion release/app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "internxt-drive",
"version": "2.0.9-qa",
"version": "2.0.9",
"description": "Internxt Drive client UI",
"main": "./dist/main/main.js",
"author": "Internxt <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion src/apps/main/auth/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let isLoggedIn: boolean;
export function setIsLoggedIn(value: boolean) {
isLoggedIn = value;

getWidget()?.webContents.send('user-logged-in-changed', value);
getWidget()?.webContents?.send('user-logged-in-changed', value);
}

setIsLoggedIn(!!getUser());
Expand Down
13 changes: 8 additions & 5 deletions src/apps/main/background-processes/sync-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function healthCheck() {
resolve();
});

const millisecondsToWait = 2_000;
const millisecondsToWait = 8_000;

setTimeout(() => {
reject(
Expand Down Expand Up @@ -72,9 +72,12 @@ function scheduleHeathCheck() {
});

healthCheckSchedule = nodeSchedule.scheduleJob('*/30 * * * * *', async () => {
const workerIsPending = checkSyncEngineInProcess(5_000);
Logger.debug('Health check', workerIsPending ? 'Worker is pending' : 'Worker is running');
if(!workerIsPending) {
const workerIsPending = checkSyncEngineInProcess(15_000);
Logger.debug(
'Health check',
workerIsPending ? 'Worker is pending' : 'Worker is running'
);
if (!workerIsPending) {
await relaunchOnFail();
}
});
Expand Down Expand Up @@ -254,7 +257,7 @@ export function fallbackSyncEngine() {
export async function sendUpdateFilesInSyncPending(): Promise<string[]> {
try {
if (worker?.webContents && !worker?.isDestroyed()) {
worker?.webContents.send('UPDATE_UNSYNC_FILE_IN_SYNC_ENGINE_PROCESS');
worker?.webContents.send('UPDATE_UNSYNC_FILE_IN_SYNC_ENGINE_PROCESS');
}
return [];
} catch (err) {
Expand Down
1 change: 0 additions & 1 deletion src/apps/main/fordwardToWindows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ ipcMainDrive.on('FILE_PREPARING', (_, payload) => {

ipcMainDrive.on('FILE_DOWNLOADED', (_, payload) => {
const { nameWithExtension } = payload;
setIsProcessing(false);
broadcastToWindows('sync-info-update', {
action: 'DOWNLOADED',
name: nameWithExtension,
Expand Down
52 changes: 31 additions & 21 deletions src/apps/sync-engine/BindingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export class BindingsManager {
private static readonly PROVIDER_NAME = 'Internxt';
private progressBuffer = 0;
private controllers: IControllers;
private processingResolve?: (unknown?: unknown) => void;
private processingReject?: (unknown?: unknown) => void;

constructor(
private readonly container: DependencyContainer,
private readonly paths: {
Expand Down Expand Up @@ -145,6 +148,10 @@ export class BindingsManager {
finished = result.finished;
Logger.debug('callback result', result);

if (result.progress > 1 || result.progress < 0) {
throw new Error('Result progress is not between 0 and 1');
}

if (finished && result.progress === 0) {
throw new Error('Result progress is 0');
} else if (this.progressBuffer == result.progress) {
Expand All @@ -165,29 +172,28 @@ export class BindingsManager {
});
}
this.progressBuffer = 0;

await this.controllers.notifyPlaceholderHydrationFinished.execute(
contentsId
);
} catch (error) {
Logger.error('notify: ', error);
Sentry.captureException(error);
await this.container.virtualDrive.closeDownloadMutex();
await callback(false, '');
// if (this.processingReject) this.processingReject(error);
}
fs.unlinkSync(path);

await new Promise((resolve) => {
setTimeout(() => {
Logger.debug('timeout');
resolve(true);
}, 500);
});
await this.controllers.notifyPlaceholderHydrationFinished.execute(
contentsId
);
if (this.processingResolve) this.processingResolve();

fs.unlinkSync(path);
ipcRenderer.send('CHECK_SYNC');
Logger.debug('[Fetch Data Callback] Finish...', path);
} catch (error) {
Logger.error(error);
Sentry.captureException(error);
callback(false, '');
await callback(false, '');
if (this.processingResolve) this.processingResolve();
await this.container.virtualDrive.closeDownloadMutex();
ipcRenderer.send('CHECK_SYNC');
}
},
notifyMessageCallback: (
Expand Down Expand Up @@ -288,21 +294,25 @@ export class BindingsManager {
try {
Logger.debug('[Handle Hydrate Callback] Preparing begins', task.path);

// Crear una promesa que será resuelta por fetchDataCallback
const processingPromise = new Promise((resolve, reject) => {
this.processingResolve = resolve;
this.processingReject = reject;
});

await this.container.virtualDrive.hydrateFile(task.path);

await new Promise((resolve) => {
setTimeout(() => {
Logger.debug('timeout');
resolve(true);
}, 1000);
});
Logger.debug('hydrate result');
// Esperar hasta que fetchDataCallback resuelva o rechace la promesa
await processingPromise;

Logger.debug('[Handle Hydrate Callback] Finish begins', task.path);
} catch (error) {
Logger.error(`error hydrating file ${task.path}`);
Logger.error(error);
Sentry.captureException(error);
}
},

handleDehydrate: async (task: QueueItem) => {
try {
Logger.debug('Dehydrate', task);
Expand Down Expand Up @@ -330,7 +340,7 @@ export class BindingsManager {
queueManager,
logWatcherPath
);
// queueManager.processAll();
queueManager.processAll();
}

async stop() {
Expand Down
75 changes: 61 additions & 14 deletions src/apps/sync-engine/dependency-injection/common/QueueManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ export type QueueHandler = {
handleChangeSize: HandleAction;
};

// const queueFilePath = path.join(__dirname, 'queue.json');
export class QueueManager implements IQueueManager {
private _queue: QueueItem[] = [];

private isProcessing = false;

// private queueFilePath = queueFilePath;

actions: HandleActions;

constructor(handlers: QueueHandler) {
Expand All @@ -30,12 +33,42 @@ export class QueueManager implements IQueueManager {
changeSize: handlers.handleChangeSize,
change: handlers.handleChange || (() => Promise.resolve()),
};

// this.loadQueueFromFile();
}

// private saveQueueToFile(): void {
// const queue = this._queue.filter((item) => item.type !== 'hydrate');
// fs.writeFileSync(this.queueFilePath, JSON.stringify(queue, null, 2));
// Logger.debug('Queue saved to file.');
// }

// private loadQueueFromFile(): void {
// try {
// if (fs.existsSync(this.queueFilePath)) {
// const data = fs.readFileSync(this.queueFilePath, 'utf-8');
// this._queue = JSON.parse(data);
// Logger.debug('Queue loaded from file.');
// }
// } catch (error) {
// Logger.error('Failed to load queue from file:', error);
// }
// }

public enqueue(task: QueueItem): void {
Logger.debug(`Task enqueued: ${JSON.stringify(task)}`);
// const existingTask = this._queue.find(
// (item) => item.path === task.path && item.type === task.type
// );
// if (existingTask) {
// Logger.debug('Task already exists in queue. Skipping.');
// this.processAll();

// return;
// }
this._queue.push(task);
this.sortQueue();
// this.saveQueueToFile();
if (!this.isProcessing) {
this.processAll();
}
Expand All @@ -61,23 +94,37 @@ export class QueueManager implements IQueueManager {
Logger.debug('No tasks in queue.');
return;
}

const task = this._queue.shift();
if (!task) return;

// this.saveQueueToFile();

Logger.debug(`Processing task: ${JSON.stringify(task)}`);
switch (task.type) {
case 'add':
return await this.actions.add(task);
case 'hydrate':
return await this.actions.hydrate(task);
case 'dehydrate':
return await this.actions.dehydrate(task);
case 'change':
return await this.actions.change(task);
case 'changeSize':
return await this.actions.changeSize(task);
default:
Logger.debug('Unknown task type.');
break;

try {
switch (task.type) {
case 'add':
await this.actions.add(task);
break;
case 'hydrate':
await this.actions.hydrate(task);
break;
case 'dehydrate':
await this.actions.dehydrate(task);
break;
case 'change':
await this.actions.change(task);
break;
case 'changeSize':
await this.actions.changeSize(task);
break;
default:
Logger.debug('Unknown task type.');
break;
}
} catch (error) {
Logger.error('Failed to process task:', task);
}
}

Expand Down

0 comments on commit 641e1cf

Please sign in to comment.