{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "wf-ttl-monitor",
  "name": "TTL Monitor - Stale Task Watchdog",
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": true,
    "callerPolicy": "workflowsFromSameOwner"
  },
  "tags": [
    {
      "id": "4",
      "name": "monitoring"
    }
  ],
  "nodes": [
    {
      "id": "node-schedule-trigger",
      "name": "Schedule: Every 60 Seconds",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        240,
        300
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "seconds",
              "secondsInterval": 60
            }
          ]
        }
      }
    },
    {
      "id": "node-select-stale",
      "name": "MySQL: SELECT Stale Tasks",
      "type": "n8n-nodes-base.mySql",
      "typeVersion": 2.4,
      "position": [
        480,
        300
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT * FROM stale_tasks;",
        "options": {}
      },
      "credentials": {
        "mySql": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "node-if-has-stale",
      "name": "IF: Has Stale Tasks?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        720,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "condition-count",
              "leftValue": "={{ $input.all().length }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "node-loop-items",
      "name": "Loop Over Stale Tasks",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        960,
        200
      ],
      "parameters": {
        "batchSize": 1,
        "options": {}
      }
    },
    {
      "id": "node-update-blocked",
      "name": "MySQL: UPDATE Status Blocked",
      "type": "n8n-nodes-base.mySql",
      "typeVersion": 2.4,
      "position": [
        1200,
        200
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE tasks SET status = 'blocked', last_heartbeat = NOW() WHERE id = '{{ $json.id }}';",
        "options": {}
      },
      "credentials": {
        "mySql": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "node-telegram-alert",
      "name": "Telegram: Alert Stale Task",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1440,
        200
      ],
      "parameters": {
        "operation": "sendMessage",
        "chatId": "={{ $env.TELEGRAM_CHAT_ID }}",
        "text": "=ALERTA: Tarea estancada detectada\n\nTask ID: {{ $('Loop Over Stale Tasks').item.json.id }}\nAgente: {{ $('Loop Over Stale Tasks').item.json.agent_id }}\nEstado anterior: {{ $('Loop Over Stale Tasks').item.json.status }}\nCreada: {{ $('Loop Over Stale Tasks').item.json.created_at }}\nUltimo heartbeat: {{ $('Loop Over Stale Tasks').item.json.last_heartbeat }}\n\nLa tarea ha sido marcada como BLOCKED automaticamente.",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "node-no-stale",
      "name": "No Stale Tasks",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        960,
        420
      ]
    }
  ],
  "connections": {
    "Schedule: Every 60 Seconds": {
      "main": [
        [
          {
            "node": "MySQL: SELECT Stale Tasks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MySQL: SELECT Stale Tasks": {
      "main": [
        [
          {
            "node": "IF: Has Stale Tasks?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF: Has Stale Tasks?": {
      "main": [
        [
          {
            "node": "Loop Over Stale Tasks",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Stale Tasks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Stale Tasks": {
      "main": [
        [
          {
            "node": "MySQL: UPDATE Status Blocked",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "MySQL: UPDATE Status Blocked",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MySQL: UPDATE Status Blocked": {
      "main": [
        [
          {
            "node": "Telegram: Alert Stale Task",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram: Alert Stale Task": {
      "main": [
        [
          {
            "node": "Loop Over Stale Tasks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}