AutomationFlowsEmail & Gmail › Bot / Health Monitor & Session Cleanup

Bot / Health Monitor & Session Cleanup

Bot / Health Monitor & Session Cleanup. Uses postgres, telegram. Scheduled trigger; 9 nodes.

Cron / scheduled trigger★★★★☆ complexity9 nodesPostgresTelegram
Email & Gmail Trigger: Cron / scheduled Nodes: 9 Complexity: ★★★★☆ Added:

This workflow follows the Postgres → Telegram recipe pattern — see all workflows that pair these two integrations.

The workflow JSON

Copy or download the full n8n JSON below. Paste it into a new n8n workflow, add your credentials, activate. Full import guide →

Download .json
{
  "name": "Bot / Health Monitor & Session Cleanup",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 10
            }
          ]
        }
      },
      "name": "Cron every 10m",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT bs.id, bs.chat_id, bs.channel, bs.state, bs.updated_at\n  FROM public.bot_sessions bs\n WHERE bs.updated_at < now() - interval '24 hours';"
      },
      "name": "PG stale sessions",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        460,
        180
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "DELETE FROM public.bot_sessions WHERE updated_at < now() - interval '24 hours';"
      },
      "name": "PG cleanup sessions",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        680,
        180
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT COUNT(*) as dead_count FROM public.job_queue WHERE status = 'dead' AND updated_at > now() - interval '10 minutes';"
      },
      "name": "PG dead jobs",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        460,
        360
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT COUNT(*) as pending_count FROM public.job_queue WHERE status = 'pending' AND run_at < now() - interval '5 minutes';"
      },
      "name": "PG stale pending",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        460,
        540
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "sessions_cleaned",
              "value": "={{ $node['PG stale sessions'].json.length || 0 }}"
            },
            {
              "name": "dead_jobs",
              "value": "={{ $json.dead_count }}"
            },
            {
              "name": "stale_pending",
              "value": "={{ $node['PG stale pending'].first().json.pending_count }}"
            }
          ]
        }
      },
      "name": "Set health vars",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3,
      "position": [
        680,
        500
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.dead_jobs }}",
              "operation": "larger",
              "value2": 0
            },
            {
              "value1": "={{ $json.stale_pending }}",
              "operation": "larger",
              "value2": 5
            }
          ]
        }
      },
      "name": "Alert needed?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        900,
        500
      ]
    },
    {
      "parameters": {
        "chatId": "={{$env.TELEGRAM_OWNER_CHAT_ID}}",
        "text": "\u26a0\ufe0f Bot Health Alert\n\n\ud83d\uddd1 \u0421\u0435\u0441\u0441\u0438\u0439 \u043e\u0447\u0438\u0449\u0435\u043d\u043e: {{$json.sessions_cleaned}}\n\ud83d\udc80 Dead jobs: {{$json.dead_jobs}}\n\u23f3 Pending >5min: {{$json.stale_pending}}\n\n\u041f\u0440\u043e\u0432\u0435\u0440\u044c\u0442\u0435 n8n worker (01-job-queue-worker).",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "name": "TG alert owner",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.1,
      "position": [
        1120,
        400
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO public.app_logs (level, source, message, context)\nVALUES ('info', 'n8n-health', 'Health check OK', jsonb_build_object(\n  'sessions_cleaned', '={{ $json.sessions_cleaned || 0 }}',\n  'dead_jobs', '={{ $json.dead_jobs || 0 }}',\n  'stale_pending', '={{ $json.stale_pending || 0 }}'\n));"
      },
      "name": "PG log health",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        1120,
        600
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Cron every 10m": {
      "main": [
        [
          {
            "node": "PG stale sessions",
            "type": "main",
            "index": 0
          },
          {
            "node": "PG dead jobs",
            "type": "main",
            "index": 0
          },
          {
            "node": "PG stale pending",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PG stale sessions": {
      "main": [
        [
          {
            "node": "PG cleanup sessions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PG dead jobs": {
      "main": [
        [
          {
            "node": "Set health vars",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PG stale pending": {
      "main": []
    },
    "Set health vars": {
      "main": [
        [
          {
            "node": "Alert needed?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Alert needed?": {
      "main": [
        [
          {
            "node": "TG alert owner",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "PG log health",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "TG alert owner": {
      "main": [
        [
          {
            "node": "PG log health",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "active": false,
  "tags": [
    "bot",
    "health",
    "ops"
  ]
}

Credentials you'll need

Each integration node will prompt for credentials when you import. We strip credential IDs before publishing — you'll add your own.

Pro

For the full experience including quality scoring and batch install features for each workflow upgrade to Pro

About this workflow

Bot / Health Monitor & Session Cleanup. Uses postgres, telegram. Scheduled trigger; 9 nodes.

Source: https://github.com/alexdmitrievi/Podryad_PRO/blob/main/n8n/workflows/08-bot-health-cleanup.json — original creator credit. Request a take-down →

More Email & Gmail workflows → · Browse all categories →

Related workflows

Workflows that share integrations, category, or trigger type with this one. All free to copy and import.

Email & Gmail

The FamilyFlow Assistant is your n8n-powered 🚀 companion designed to streamline daily parenting tasks, reduce mental load, and bring a bit more organization and fun into your family life. This versati

Start, Email Send, Telegram Trigger +2
Email & Gmail

This template is ideal for educators, HR professionals, and anyone looking to automate testing and collect responses through Telegram, while storing results in a Postgres database.

Telegram, Postgres, Telegram Trigger
Email & Gmail

Workflow1-2-3. Uses httpRequest, gmail, telegram, postgres. Webhook trigger; 34 nodes.

HTTP Request, Gmail, Telegram +2
Email & Gmail

This workflow automates the complete end-to-end processing of daily revenue transactions for finance and accounting teams. It systematically retrieves, validates, and standardizes transaction data fro

HTTP Request, Gmail, Google Drive +2
Email & Gmail

This n8n workflow creates an automated Telegram channel bot that responds to messages with AI-generated text or images using TGPT. The bot monitors a specific Telegram channel and generates responses

HTTP Request, Execute Command, Telegram +1