[Solved] Restarting flows programmatically

Since v0.20, the deploy menu has a new Restart Flows option. Would it be possible to provide a way to invoke this programmatically from within a flow? For example, in cases where message properties have been used to override node parameters set when a node was deployed (not encouraged, I realize, but sometimes a good idea), it could be useful to be able to recover the initial state of the flow.

Looks like it simply POSTs to the API function flows

        $.ajax({
            url:"flows",
            type: "POST",
            headers: {
                "Node-RED-Deployment-Type":"reload"
            }
        }).done(function(data,textStatus,xhr) {
            if (deployWasEnabled) {
                $("#btn-deploy").removeClass("disabled");
            }
            RED.notify('<p>'+RED._("deploy.successfulRestart")+'</p>',"success");
        }).fail(function(xhr,textStatus,err) {
            if (deployWasEnabled) {
                $("#btn-deploy").removeClass("disabled");
            }
            if (xhr.status === 401) {
                RED.notify(RED._("deploy.deployFailed",{message:RED._("user.notAuthorized")}),"error");
            } else if (xhr.status === 409) {
                resolveConflict(nns, true);
            } else if (xhr.responseText) {
                RED.notify(RED._("deploy.deployFailed",{message:xhr.responseText}),"error");
            } else {
                RED.notify(RED._("deploy.deployFailed",{message:RED._("deploy.errors.noResponse")}),"error");
            }
        }).always(function() {
            deployInflight = false;
            var delta = Math.max(0,300-(Date.now()-startTime));
            setTimeout(function() {
                $(".deploy-button-content").css('opacity',1);
                $(".deploy-button-spinner").hide();
                $("#header-shade").hide();
                $("#editor-shade").hide();
                $("#palette-shade").hide();
                $("#sidebar-shade").hide();
            },delta);
        });

You can do that yourself:

https://nodered.org/docs/api/admin/methods/post/flows/

1 Like

Brilliant! Thanks a lot. I suspected/hoped that this was available in the Admin API, but I didn't know where to look in the docs. It took me a while to work out the DIY part.

No problem. Just note that I had some problems with jQuery's POST function so I used a raw var request = new XMLHttpRequest() in uibuilder. Though that may be why Nick used $.ajax here rather than $.post(). It is a known bug that triggers a CORS error.

In fact the raw query isn't actually any harder to use than jQuery except that you don't get the promises type interface.

1 Like

Noted. I also had some issues with POST, but it seems to work ok in the particular case I need. If it does go sideways, I'll know what to do.

Hey @TotallyInformation or @drmibell, I'm trying to implement this now, but getting a 400 when I try to actually send the reload to the /flows endpoint. Would either of you have an example working flow you could share?

Sorry, but I can't recover my work on this. I ended by solving my original problem in a completely different way. Perhaps @TotallyInformation can help.

Sorry, my comments were about using jQuery's post function that can have some issues. Not actually needed to do programmatic flow restarts myself.

Anyone ever managed to implement that? I have to do the same exact thing, but not sure how to do it exactly. Thanks!

Hmmm ... I tried to do as proposed but get error 401. I do this:

[{"id":"f60dd315.81c64","type":"function","z":"fe0113d0.6b338","name":"reload flows","func":"//\n// url:\"flows\",\n// type: \"POST\",\n// headers: {\n// \"Node-RED-Deployment-Type\":\"reload\"\n// }\n\nmsg.payload = {headers:{\"Node-RED-Deployment-Type\":\"reload\"}};\n\nreturn msg;","outputs":1,"noerr":0,"x":440,"y":660,"wires":[["e83f1e8f.5db03"]]},{"id":"e83f1e8f.5db03","type":"http request","z":"fe0113d0.6b338","name":"","method":"POST","ret":"txt","paytoqs":false,"url":"localhost:1880/flows","tls":"","persist":false,"proxy":"","authType":"","x":680,"y":660,"wires":[["a409437b.1c6a3"]]},{"id":"2021734a.df7a4c","type":"inject","z":"fe0113d0.6b338","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":660,"wires":[["f60dd315.81c64"]]},{"id":"a409437b.1c6a3","type":"debug","z":"fe0113d0.6b338","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":920,"y":660,"wires":[]}]

I finally managed to do this and can programatically restart my flows from within Node Red! Took me a while to work out.

If you are still interested I'll type it up, but I don't want to do it if it's not required any more.