{
  "name": "MZPanel: site down -> restart service -> Slack",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 1
            }
          ]
        }
      },
      "id": "14b41e82-b45c-59ef-874a-01e9e9ebf78f",
      "name": "Every minute",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "657f27a0-c0dd-5a11-b2d2-bf162d6674bd",
              "name": "apiBase",
              "value": "https://api.mzpanel.com",
              "type": "string"
            },
            {
              "id": "539d55a7-4a0f-50f3-b4ee-eb893fba4cfd",
              "name": "eventTypes",
              "value": "site.down",
              "type": "string"
            },
            {
              "id": "846ff79b-98f3-552f-afc6-c9c1c3190cf5",
              "name": "service",
              "value": "nginx",
              "type": "string"
            },
            {
              "id": "507c95e8-6b0c-5c3c-9b66-5f1ae06e3a20",
              "name": "slackWebhookUrl",
              "value": "https://hooks.slack.com/services/REPLACE/ME",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "fd71e4d2-ec17-598a-8683-369dc645d199",
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Build the event-feed URL. The cursor lives in workflow static data, which n8n\n// keeps only for ACTIVE workflows (production runs), not manual test runs.\nconst s = $('Settings').first().json;\nconst state = $getWorkflowStaticData('global');\nconst base = s.apiBase + '/v1/events?types=' + encodeURIComponent(s.eventTypes);\nreturn [{ json: { url: state.cursor ? base + '&after=' + encodeURIComponent(state.cursor) : base + '&limit=1' } }];\n"
      },
      "id": "8440af68-3dc5-5bc4-9222-ac4bac2381a5",
      "name": "Build poll URL",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{ $json.url }}",
        "options": {},
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "id": "0245af0e-6bea-541a-9381-c9a0623a01d8",
      "name": "Poll events",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        660,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "mzpanel-api-token",
          "name": "MZPanel API token"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// First run: remember the newest event id as the baseline and act on nothing.\n// Later runs: GET /v1/events?after=<cursor> answers newer events oldest first\n// plus nextCursor, which is stored for the next run.\nconst state = $getWorkflowStaticData('global');\nconst body = $input.first().json;\nconst events = Array.isArray(body.events) ? body.events : [];\nif (!state.cursor) {\n  if (events.length > 0) state.cursor = events[0].id;\n  return [];\n}\nif (body.nextCursor) state.cursor = body.nextCursor;\nreturn events.map((e) => ({ json: e }));\n"
      },
      "id": "e47d5806-bcef-5785-b29c-9fee07f9517a",
      "name": "New events only",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Settings').first().json.apiBase }}/v1/servers/{{ $json.serverId }}/jobs?wait=60",
        "options": {
          "response": {
            "response": {
              "neverError": true,
              "fullResponse": true
            }
          }
        },
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ type: 'service.action', action: 'restart', name: $('Settings').first().json.service }) }}"
      },
      "id": "bde37237-a2d8-571a-8a01-c54ba8331f98",
      "name": "Restart service",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1100,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "mzpanel-api-token",
          "name": "MZPanel API token"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Settings').first().json.slackWebhookUrl }}",
        "options": {},
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ text: ':rotating_light: ' + $('New events only').item.json.subject + ' is down. Restarted ' + $('Settings').first().json.service + ' on server ' + $('New events only').item.json.serverId + ': ' + ($json.statusCode === 200 ? ($json.body.status + ' (exit ' + $json.body.exitCode + ')') : ('HTTP ' + $json.statusCode + ' ' + JSON.stringify($json.body))) }) }}"
      },
      "id": "f8766e54-b03a-537c-bc90-8807eac279b9",
      "name": "Post to Slack",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1320,
        300
      ]
    }
  ],
  "connections": {
    "Every minute": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Build poll URL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build poll URL": {
      "main": [
        [
          {
            "node": "Poll events",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Poll events": {
      "main": [
        [
          {
            "node": "New events only",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "New events only": {
      "main": [
        [
          {
            "node": "Restart service",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Restart service": {
      "main": [
        [
          {
            "node": "Post to Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false,
    "mzpanel": "Polls GET /v1/events for site.down (every plan, Read token for the poll; the restart needs Operate), restarts the service with POST /v1/servers/{id}/jobs?wait=60 {type: service.action}, posts the result to a Slack incoming webhook."
  }
}
