Interrupt Comfy gens on cancel
| @@ -569,6 +569,17 @@ comfy.post('/generate', jsonParser, async (request, response) => { | ||
| 569 | 569 | const url = new URL(request.body.url); |
| 570 | 570 | url.pathname = '/prompt'; |
| 571 | 571 | |
| 572 | + const controller = new AbortController(); | |
| 573 | + request.socket.removeAllListeners('close'); | |
| 574 | + request.socket.on('close', function () { | |
| 575 | + if (!response.writableEnded && !item) { | |
| 576 | + const interruptUrl = new URL(request.body.url); | |
| 577 | + interruptUrl.pathname = '/interrupt'; | |
| 578 | + fetch(interruptUrl, { method: 'POST', headers: { 'Authorization': getBasicAuthHeader(request.body.auth) } }); | |
| 579 | + } | |
| 580 | + controller.abort(); | |
| 581 | + }); | |
| 582 | + | |
| 572 | 583 | const promptResult = await fetch(url, { |
| 573 | 584 | method: 'POST', |
| 574 | 585 | body: request.body.prompt, |
| @@ -594,6 +605,9 @@ comfy.post('/generate', jsonParser, async (request, response) => { | ||
| 594 | 605 | } |
| 595 | 606 | await delay(100); |
| 596 | 607 | } |
| 608 | + if (item.status.status_str === 'error') { | |
| 609 | + throw new Error('ComfyUI generation did not succeed.'); | |
| 610 | + } | |
| 597 | 611 | const imgInfo = Object.keys(item.outputs).map(it => item.outputs[it].images).flat()[0]; |
| 598 | 612 | const imgUrl = new URL(request.body.url); |
| 599 | 613 | imgUrl.pathname = '/view'; |
| @@ -605,6 +619,7 @@ comfy.post('/generate', jsonParser, async (request, response) => { | ||
| 605 | 619 | const imgBuffer = await imgResponse.buffer(); |
| 606 | 620 | return response.send(imgBuffer.toString('base64')); |
| 607 | 621 | } catch (error) { |
| 622 | + console.log(error); | |
| 608 | 623 | return response.sendStatus(500); |
| 609 | 624 | } |
| 610 | 625 | }); |