Skip to content

Commit

Permalink
Fix async not awaited (#484)
Browse files Browse the repository at this point in the history
* Remove unused code

* Fix lifecycle - keep store simple

* Improve config lifecycle, convert to basic locator

* Remove static accessor

* Add separation of concerns

Separate dialog open code from config file load code.

* nit - Doc

* nit - Unused imports

* Fix async not awaited

* Fix async not awaited

* Fix async not awaited - then/catch

* Fix merge error

---------

Co-authored-by: Chenlei Hu <[email protected]>
Co-authored-by: huchenlei <[email protected]>
  • Loading branch information
3 people authored Dec 14, 2024
1 parent 26b29c4 commit 5c55ed8
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/handlers/appInfoHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, ipcMain, shell } from 'electron';
import { app, ipcMain } from 'electron';
import { IPC_CHANNELS } from '../constants';
/**
* Handles static information about the app in IPC channels.
Expand Down
2 changes: 1 addition & 1 deletion src/install/installationValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class InstallationValidator {
// Failed - try the parent dir
const parsed = path.parse(file);
await fs.access(parsed.dir);
shell.openPath(parsed.dir);
await shell.openPath(parsed.dir);
} catch (error) {
// Nothing works. Log, notify, quit.
log.error(
Expand Down
8 changes: 4 additions & 4 deletions src/main-process/appWindow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BrowserWindow, screen, app, shell, ipcMain, Tray, Menu, dialog, MenuItem, type Point } from 'electron';
import { BrowserWindow, screen, app, shell, ipcMain, Tray, Menu, dialog, MenuItem } from 'electron';
import path from 'node:path';
import Store from 'electron-store';
import { AppWindowSettings } from '../store';
Expand Down Expand Up @@ -103,9 +103,9 @@ export class AppWindow {
});
}

public loadComfyUI(serverArgs: ServerArgs) {
public async loadComfyUI(serverArgs: ServerArgs) {
const host = serverArgs.host === '0.0.0.0' ? 'localhost' : serverArgs.host;
this.window.loadURL(`http://${host}:${serverArgs.port}`);
await this.window.loadURL(`http://${host}:${serverArgs.port}`);
}

public openDevTools(): void {
Expand Down Expand Up @@ -146,7 +146,7 @@ export class AppWindow {
} else {
const appResourcesPath = getAppResourcesPath();
const frontendPath = path.join(appResourcesPath, 'ComfyUI', 'web_custom_versions', 'desktop_app');
this.window.loadFile(path.join(frontendPath, 'index.html'), { hash: urlPath });
await this.window.loadFile(path.join(frontendPath, 'index.html'), { hash: urlPath });
}
}

Expand Down
34 changes: 19 additions & 15 deletions src/main-process/comfyDesktopApp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, dialog, ipcMain, type Point } from 'electron';
import { app, dialog, ipcMain } from 'electron';
import log from 'electron-log/main';
import * as Sentry from '@sentry/electron/main';
import { graphics } from 'systeminformation';
Expand Down Expand Up @@ -35,7 +35,7 @@ export class ComfyDesktopApp {
}

public async initialize(): Promise<void> {
this.comfySettings.loadSettings();
await this.comfySettings.loadSettings();
this.registerIPCHandlers();
this.initializeTodesktop();
await this.setupGPUContext();
Expand Down Expand Up @@ -161,8 +161,8 @@ export class ComfyDesktopApp {
await appWindow.loadRenderer('welcome');
}

return new Promise<string>((resolve) => {
ipcMain.on(IPC_CHANNELS.INSTALL_COMFYUI, async (event, installOptions: InstallOptions) => {
return new Promise<string>((resolve, reject) => {
ipcMain.on(IPC_CHANNELS.INSTALL_COMFYUI, (_event, installOptions: InstallOptions) => {
const installWizard = new InstallWizard(installOptions);
useDesktopConfig().set('basePath', installWizard.basePath);

Expand All @@ -171,30 +171,34 @@ export class ComfyDesktopApp {
useDesktopConfig().set('selectedDevice', device);
}

await installWizard.install();
useDesktopConfig().set('installState', 'installed');
appWindow.maximize();
resolve(installWizard.basePath);
installWizard
.install()
.then(() => {
useDesktopConfig().set('installState', 'installed');
appWindow.maximize();
resolve(installWizard.basePath);
})
.catch((reason) => {
reject(reason);
});
});
});
}

async startComfyServer(serverArgs: ServerArgs) {
app.on('before-quit', async () => {
app.on('before-quit', () => {
if (!this.comfyServer) {
return;
}

try {
log.info('Before-quit: Killing Python server');
await this.comfyServer.kill();
} catch (error) {
log.info('Before-quit: Killing Python server');
this.comfyServer.kill().catch((error) => {
log.error('Python server did not exit properly');
log.error(error);
}
});
});
log.info('Server start');
this.appWindow.loadRenderer('server-start');
await this.appWindow.loadRenderer('server-start');

DownloadManager.getInstance(this.appWindow!, getModelsDirectory(this.basePath));

Expand Down
26 changes: 13 additions & 13 deletions src/main-process/comfyServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class ComfyServer {

async start() {
await rotateLogFiles(app.getPath('logs'), 'comfyui', 50);
return new Promise<void>(async (resolve, reject) => {
return new Promise<void>((resolve, reject) => {
const comfyUILog = log.create({ logId: 'comfyui' });
comfyUILog.transports.file.fileName = 'comfyui.log';

Expand Down Expand Up @@ -136,19 +136,19 @@ export class ComfyServer {

this.comfyServerProcess = comfyServerProcess;

try {
await waitOn({
resources: [`${this.baseUrl}/queue`],
timeout: ComfyServer.MAX_FAIL_WAIT,
interval: ComfyServer.CHECK_INTERVAL,
waitOn({
resources: [`${this.baseUrl}/queue`],
timeout: ComfyServer.MAX_FAIL_WAIT,
interval: ComfyServer.CHECK_INTERVAL,
})
.then(() => {
log.info('Python server is ready');
resolve();
})
.catch((error) => {
log.error('Server failed to start:', error);
reject(new Error('Python Server Failed To Start Within Timeout.'));
});

log.info('Python server is ready');
resolve();
} catch (error) {
log.error('Server failed to start:', error);
reject('Python Server Failed To Start Within Timeout.');
}
});
}

Expand Down
26 changes: 15 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,26 @@ if (!gotTheLock) {
log.info('App already running. Exiting...');
app.quit();
} else {
app.on('ready', async () => {
app.on('ready', () => {
log.debug('App ready');

try {
const store = await DesktopConfig.load(shell);
if (!store) throw new Error('Unknown error loading app config on startup.');
} catch (error) {
dialog.showErrorBox('User Data', `Unknown error whilst writing to user data folder:\n\n${error}`);
app.exit(20);
}

await startApp();
startApp().catch((reason) => {
log.error('Unhandled exception in app startup', reason);
app.exit(2020);
});
});
}

async function startApp() {
try {
const store = await DesktopConfig.load(shell);
if (!store) throw new Error('Unknown error loading app config on startup.');
} catch (error) {
dialog.showErrorBox('User Data', `Unknown error whilst writing to user data folder:\n\n${error}`);
app.exit(20);
return;
}

try {
const appWindow = new AppWindow();
appWindow.onClose(() => {
Expand Down Expand Up @@ -93,7 +97,7 @@ async function startApp() {
await comfyDesktopApp.startComfyServer({ host, port, extraServerArgs });
}
appWindow.sendServerStartProgress(ProgressStatus.READY);
appWindow.loadComfyUI({ host, port, extraServerArgs });
await appWindow.loadComfyUI({ host, port, extraServerArgs });
} catch (error) {
appWindow.sendServerStartProgress(ProgressStatus.ERROR);
appWindow.send(IPC_CHANNELS.LOG_MESSAGE, error);
Expand Down
1 change: 0 additions & 1 deletion src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { contextBridge, ipcRenderer } from 'electron';
import { IPC_CHANNELS, ELECTRON_BRIDGE_API, ProgressStatus, DownloadStatus } from './constants';
import type { DownloadState } from './models/DownloadManager';
import path from 'node:path';
import { DesktopConfig } from './store/desktopConfig';

/**
* Open a folder in the system's default file explorer.
Expand Down
1 change: 0 additions & 1 deletion src/store/desktopConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { app, dialog } from 'electron';
import path from 'node:path';
import fs from 'fs/promises';
import type { DesktopSettings } from '.';
import type { TorchDeviceType } from '../preload';

/** Backing ref for the singleton config instance. */
let current: DesktopConfig;
Expand Down
1 change: 0 additions & 1 deletion src/virtualEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { app } from 'electron';
import * as pty from 'node-pty';
import * as os from 'os';
import { getDefaultShell } from './shell/util';
import { DesktopConfig } from './store/desktopConfig';
import type { TorchDeviceType } from './preload';

type ProcessCallbacks = {
Expand Down

0 comments on commit 5c55ed8

Please sign in to comment.