diff --git a/src/main/index.ts b/src/main/index.ts index 3b5edf6..81b4e91 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -162,8 +162,6 @@ app.whenReady().then(() => { // explicitly with Cmd + Q. app.on('window-all-closed', () => { if (process.platform !== 'darwin') { - stopWatchingDirectory() - killZImageProcess() app.quit() } }) @@ -177,6 +175,8 @@ app.on('before-quit', async (event) => { console.log('Quitting...') return } + console.log('Stopping watching directory...') + stopWatchingDirectory() console.log('Killing child process before quitting...') event.preventDefault() isQuitting = true diff --git a/src/main/zimage.ts b/src/main/zimage.ts index 7ad121e..2fa5766 100644 --- a/src/main/zimage.ts +++ b/src/main/zimage.ts @@ -7,6 +7,7 @@ import { readdir } from 'node:fs/promises' import { join, normalize } from 'node:path' import { IpcChannelOn } from '@shared/const/ipc' +import kill from 'tree-kill' import { getCorePath } from './getPath' import { PRESET_MODELS } from './modelManager' @@ -143,7 +144,9 @@ export async function runZImageCommand(_event: IpcMainEvent, options: ZImageOpti args.push('-g', `${options.gpuId}`) } - console.log(`[Batch ${i + 1}/${count}] Executing ZImage:`, getCorePath(), args) + const current_start_args = `[Batch ${i + 1}/${count}] Executing ZImage: ${getCorePath()} ${args.join(' ')} \n\n` + console.log(current_start_args) + _event.sender.send(IpcChannelOn.COMMAND_STDOUT, current_start_args) // Run and wait await runSingleZImage(_event, args, getCorePath()) @@ -159,9 +162,28 @@ export async function runZImageCommand(_event: IpcMainEvent, options: ZImageOpti export async function killZImageProcess(_event?: IpcMainEvent): Promise { isBatchStopped = true - if (zImageChild) { - console.log('Killing ZImage process...') - zImageChild.kill() - zImageChild = null + if (!zImageChild || !zImageChild.pid) { + console.log('ZImage process not running or no PID found.') + return } + + const pid = zImageChild.pid + console.log(`Killing ZImage process tree with PID: ${pid}`) + + return new Promise((resolve) => { + kill(pid, 'SIGKILL', (err) => { + if (err) { + console.error(`Failed to kill process tree: ${err.message}`) + } + else { + console.log('ZImage process tree killed successfully') + } + + // Ensure reference is cleared if it matches + if (zImageChild && zImageChild.pid === pid) { + zImageChild = null + } + resolve() + }) + }) } diff --git a/src/renderer/src/store/zimageStore.ts b/src/renderer/src/store/zimageStore.ts index b96b5a4..013f5fa 100644 --- a/src/renderer/src/store/zimageStore.ts +++ b/src/renderer/src/store/zimageStore.ts @@ -131,10 +131,6 @@ export const useZImageStore = defineStore( } } - isGenerating.value = true - logs.value = '' - // Don't clear generatedImages - keep history - if (!selectedModel.value) { return { success: false, @@ -142,6 +138,9 @@ export const useZImageStore = defineStore( } } + isGenerating.value = true + logs.value = '' + const options: ZImageOptions = { prompt: prompt.value, negativePrompt: negativePrompt.value, @@ -156,6 +155,9 @@ export const useZImageStore = defineStore( modelDir: modelFolder.value, } + console.log('[Store] Starting generation with options:', JSON.stringify(options, null, 2)) + logs.value += `${JSON.stringify(options, null, 2)}\n` + // Listeners const onStdout = (_event: any, data: string): void => { logs.value += data