[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.