title: Automatic restart of workflows in node-red
body: <p>I asked this question also in the node-red forum, but there is not much activity (compared to stackoverflow). So I try it here again. The motivation of my question is, that I sometime loose the connection to an OPC-UA-Server that requires restart of the flow to work again. I want to do this automatically in background, because the rare restart of the flow is no big issue - so I can live with it.</p>
  <p>There is a thread <a href="https://discourse.nodered.org/t/how-to-restart-flows-automatically/12993" rel="nofollow noreferrer">https://discourse.nodered.org/t/how-to-restart-flows-automatically/12993</a> that is not really finished or solved.</p>
  <p>I implemented the last example, but get error 400, so automatic restart of flow doesn't work. Is there any fool proof description how to do this?</p>
  <p>EDIT:</p>
  <p>This is the code:</p>
  <pre><code>    [
      {
          &quot;id&quot;: &quot;3623f8cf.b4d2b8&quot;,
          &quot;type&quot;: &quot;subflow&quot;,
          &quot;name&quot;: &quot;restart flows&quot;,
          &quot;info&quot;: &quot;&quot;,
          &quot;category&quot;: &quot;&quot;,
          &quot;in&quot;: [
              {
                  &quot;x&quot;: 40,
                  &quot;y&quot;: 80,
                  &quot;wires&quot;: [
                      {
                          &quot;id&quot;: &quot;f24fbf79.77a74&quot;
                      }
                  ]
              }
          ],
          &quot;out&quot;: [
              {
                  &quot;x&quot;: 960,
                  &quot;y&quot;: 80,
                  &quot;wires&quot;: [
                      {
                          &quot;id&quot;: &quot;cb4d3992.931138&quot;,
                          &quot;port&quot;: 0
                      }
                  ]
              }
          ],
          &quot;env&quot;: [
              {
                  &quot;name&quot;: &quot;user&quot;,
                  &quot;type&quot;: &quot;str&quot;,
                  &quot;value&quot;: &quot;&quot;
              },
              {
                  &quot;name&quot;: &quot;password&quot;,
                  &quot;type&quot;: &quot;str&quot;,
                  &quot;value&quot;: &quot;&quot;
              }
          ],
          &quot;color&quot;: &quot;#FF8888&quot;,
          &quot;inputLabels&quot;: [
              &quot;Injection&quot;
          ],
          &quot;icon&quot;: &quot;node-red/alert.svg&quot;
      },
      {
          &quot;id&quot;: &quot;f24fbf79.77a74&quot;,
          &quot;type&quot;: &quot;function&quot;,
          &quot;z&quot;: &quot;3623f8cf.b4d2b8&quot;,
          &quot;name&quot;: &quot;Request Token&quot;,
          &quot;func&quot;: &quot;var user = env.get('user');\nvar pass = env.get('password');\n\nif(user === undefined || user === null)\n{\n    user = 'adminuser';\n}\nif(pass === undefined || pass === null)\n{\n    pass = 'adminpass';\n}\n\nmsg.payload = {\n    \&quot;client_id\&quot;: \&quot;node-red-editor\&quot;,\n    \&quot;grant_type\&quot;: \&quot;password\&quot;,\n    \&quot;scope\&quot;: \&quot;*\&quot;,\n    \&quot;username\&quot;: user,\n    \&quot;password\&quot;: pass\n}\nreturn msg;\n\n/*\nmsg.payload = {\n    \&quot;client_id\&quot;: \&quot;node-red-editor\&quot;,\n    \&quot;grant_type\&quot;: \&quot;password\&quot;,\n    \&quot;scope\&quot;: \&quot;*\&quot;,\n    \&quot;username\&quot;: \&quot;type or username\&quot;,\n    \&quot;password\&quot;: \&quot;type your password\&quot;\n}\nreturn msg;\n*/&quot;,
          &quot;outputs&quot;: 1,
          &quot;noerr&quot;: 0,
          &quot;x&quot;: 200,
          &quot;y&quot;: 80,
          &quot;wires&quot;: [
              [
                  &quot;3aedf561.e16f4a&quot;
              ]
          ]
      },
      {
          &quot;id&quot;: &quot;3aedf561.e16f4a&quot;,
          &quot;type&quot;: &quot;http request&quot;,
          &quot;z&quot;: &quot;3623f8cf.b4d2b8&quot;,
          &quot;name&quot;: &quot;Token&quot;,
          &quot;method&quot;: &quot;POST&quot;,
          &quot;ret&quot;: &quot;txt&quot;,
          &quot;paytoqs&quot;: false,
          &quot;url&quot;: &quot;http://servername:port/auth/token&quot;,
          &quot;tls&quot;: &quot;&quot;,
          &quot;persist&quot;: false,
          &quot;proxy&quot;: &quot;&quot;,
          &quot;authType&quot;: &quot;&quot;,
          &quot;x&quot;: 390,
          &quot;y&quot;: 80,
          &quot;wires&quot;: [
              [
                  &quot;388fe2f5.639eee&quot;
              ]
          ]
      },
      {
          &quot;id&quot;: &quot;388fe2f5.639eee&quot;,
          &quot;type&quot;: &quot;function&quot;,
          &quot;z&quot;: &quot;3623f8cf.b4d2b8&quot;,
          &quot;name&quot;: &quot;Confirm token&quot;,
          &quot;func&quot;: &quot;// get the status of the request\nvar status = msg.statusCode;\nvar token = '';\nmsg.headers ={};\n\n//let node = feedback;\n\nswitch(status){\n    case 200:\n        node.log(\&quot;Secure restart\&quot;);\n        token = JSON.parse(msg.payload);\n        token = 'Bearer '+token.access_token;\n        msg.headers = {\n            \&quot;Authorization\&quot;: token,\n            \&quot;Node-RED-Deployment-Type\&quot;:\&quot;reload\&quot;\n        }\n        msg.payload =\&quot;\&quot;;\n        break;\n    case 204:\n        node.log(\&quot;Secure without restart\&quot;);\n        global.set('result','\tSuccess - with no further content');\n        break;\n    case 400:\n        node.warn(\&quot;Bad request\&quot;);\n        break;\n    case 401:\n        node.warn(\&quot;Not authorized\&quot;);\n        break;\n    case 403:\n        node.warn(\&quot;Forbidden\&quot;);\n        break;\n    case 404:\n        node.log(\&quot;Unsecure restart\&quot;);\n        msg.headers = {\n            \&quot;Node-RED-Deployment-Type\&quot;:\&quot;reload\&quot;\n        }\n        break;\n    case 409:\n        node.warn(\&quot;Version mismatch\&quot;);\n        break;\n    case 500:\n        node.error(\&quot;Server Error\&quot;);\n        break;\n    default:\n        node.warn(\&quot;Unknown Error\&quot;);\n       break;\n}\n\nmsg.payload = \&quot;\&quot;;\n\nsetTimeout(function() { node.send(msg) }, 10000);\n\nreturn null;&quot;,
          &quot;outputs&quot;: 1,
          &quot;noerr&quot;: 0,
          &quot;x&quot;: 580,
          &quot;y&quot;: 80,
          &quot;wires&quot;: [
              [
                  &quot;cb4d3992.931138&quot;
              ]
          ],
          &quot;info&quot;: &quot;Restart of Node-Red flows.\nWill check if the action needs security or not.\nNote: if the first 5 attemps return a statuscode 403 'forbidden'\nthen the server will break and only way to recover is then to\nrestart the service&quot;
      },
      {
          &quot;id&quot;: &quot;cb4d3992.931138&quot;,
          &quot;type&quot;: &quot;http request&quot;,
          &quot;z&quot;: &quot;3623f8cf.b4d2b8&quot;,
          &quot;name&quot;: &quot;Restart&quot;,
          &quot;method&quot;: &quot;POST&quot;,
          &quot;ret&quot;: &quot;txt&quot;,
          &quot;paytoqs&quot;: false,
          &quot;url&quot;: &quot;http://servername:port/flows&quot;,
          &quot;tls&quot;: &quot;&quot;,
          &quot;persist&quot;: false,
          &quot;proxy&quot;: &quot;&quot;,
          &quot;authType&quot;: &quot;&quot;,
          &quot;x&quot;: 780,
          &quot;y&quot;: 80,
          &quot;wires&quot;: [
              []
          ]
      }
      ]
  </code></pre>
  <p>The settings.js file is as follows:</p>
  <pre><code>adminAuth: {
      type: &quot;credentials&quot;,
      users: [
  {
          username: &quot;adminuser&quot;,
          password: &quot;adminpass&quot;,
          permissions: &quot;*&quot;
      },
  </code></pre>
  <p>Hope this helps.</p>