Files
Dr.Lt.Data 307bec483e Enable a disabled node pack requested through cm-api before restarting (#3525)
When an extension asks through cm.try-install-custom-node for a node
pack that is disabled, the user is asked whether to enable it and
restart. On confirmation the handler posted to
/customnode/toggle_active, which the server does not have, did not
look at the answer, and restarted: ComfyUI came back with the pack
still disabled.

- Enable the pack as the Custom Nodes Manager does: find its row in
  /customnode/getlist, put it in the queue, start the queue, and wait
  for the queue to report the pack
- Restart only when the queue reports success; otherwise show why the
  pack was not enabled
- Leave a queue that is already running alone
- End the wait when the started queue reports without the pack, or
  does not report within a minute; a listener left waiting would take
  a later report for the same row and restart ComfyUI unasked
- Show the reason when a request made while enabling gets no answer
- Show "Rebooting..." when the restart request gets no answer, which
  is what happens when ComfyUI restarts
- Remove the branch for a pack that is not installed: the server does
  not send this request for one, and the route it posted to does not
  exist
- Leave the queue listener of the Manager menu when the menu has not
  been created; a queue started from here made it raise
- Add browser tests of the handler and of the menu listener
- Add a CHANGELOG entry

The same change for manager-v4 is #3526.
2026-11-14 23:42:36 +09:00

142 lines
4.7 KiB
JavaScript

import { api } from "../../scripts/api.js";
import { app } from "../../scripts/app.js";
import { sleep, customConfirm, customAlert, handle403Response, show_message, sanitizeHTML, md5 } from "./common.js";
// Enable a disabled node pack as the Enable button of the Custom Nodes Manager does: find the pack
// in the node list, put it in the queue, and wait until the queue has finished it.
// Returns undefined when the pack was enabled, or the reason when it was not.
async function enableNodePack(target) {
const list = await api.fetchApi(`/customnode/getlist?mode=cache&skip_update=true`);
if(list.status != 200) {
return `Failed to get the custom node list (${list.status}).`;
}
const { channel, node_packs } = await list.json();
const key = Object.keys(node_packs).find((k) => {
const pack = node_packs[k];
return pack.state == 'disabled' && pack.files?.some((url) => target.files.includes(url));
});
if(key == undefined) {
return 'It is not in the custom node list as a disabled node pack. Please enable it in the Custom Nodes Manager.';
}
const ui_id = md5(key); // the id of this pack's row in the Custom Nodes Manager
const status = await api.fetchApi('/manager/queue/status');
if((await status.json()).is_processing) {
return 'There are already tasks in progress. Please try again after it is completed.';
}
// Once the queue is started its next report ends the wait; it is without the pack when
// something else has emptied the queue.
let listener, started = false;
const result = new Promise((resolve) => {
listener = (event) => {
if(event.detail.status != 'done') {
return;
}
if(ui_id in event.detail.nodepack_result) {
resolve(event.detail.nodepack_result[ui_id]);
}
else if(started) {
resolve('The queue finished without this node pack. Please try again.');
}
};
});
// A node pack from the registry is enabled while the install request is handled, and reported then.
api.addEventListener("cm-queue-status", listener);
try {
await api.fetchApi('/manager/queue/reset', { method: 'POST' });
const response = await api.fetchApi(`/manager/queue/install`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
...node_packs[key],
channel,
mode: 'cache',
ui_id,
skip_post_install: true
})
});
if(response.status == 403) {
return 'This action is not allowed with this security level configuration.';
}
if(response.status != 200) {
return `The request was refused (${response.status}). Please check the terminal log.`;
}
started = true;
await api.fetchApi('/manager/queue/start', { method: 'POST' });
// A report that does not reach the page would leave the listener in place, and a later report
// for the same row, such as its Disable, would then restart ComfyUI unasked.
const message = await Promise.race([result, sleep(60000).then(() =>
'The queue did not report the result. Please check the node pack in the Custom Nodes Manager.')]);
if(message != 'success') {
return String(message);
}
}
finally {
api.removeEventListener("cm-queue-status", listener);
}
}
async function tryInstallCustomNode(event) {
const target = event.detail.target;
let msg = '-= [ComfyUI Manager] extension installation request =-\n\n';
msg += `The '${event.detail.sender}' extension requires the installation of the '${target.title}' extension. `;
if(target.installed == 'Disabled') {
msg += 'However, the extension is currently disabled. Would you like to enable it and reboot?'
}
else {
msg += 'However, it seems that the extension is in an import-fail state or is not compatible with the current version. Please address this issue.';
}
msg += `\n\nRequest message:\n${event.detail.msg}`;
// The server sends this request only for a node pack that is installed: it is disabled, or it failed to load.
if(target.installed != 'Disabled') {
customAlert(msg);
return;
}
const res = await customConfirm(msg);
if(res) {
const title = sanitizeHTML(String(target.title));
await sleep(300);
app.ui.dialog.show(`Enabling... '${title}'`);
const failure = await enableNodePack(target).catch((exception) => String(exception));
if(failure != undefined) {
show_message(`Failed to enable '${title}'.<BR><BR>${sanitizeHTML(failure)}`);
return false;
}
let response;
try {
response = await api.fetchApi("/manager/reboot", { method: 'POST' });
}
catch(exception) {
// The server restarts without answering.
}
if(response?.status == 403) {
await handle403Response(response);
return false;
}
await sleep(300);
app.ui.dialog.show(`Rebooting...`);
}
}
api.addEventListener("cm-api-try-install-customnode", tryInstallCustomNode);