Appwrite Backend Health Gate
Probe Appwrite's own database, cache, and clock health endpoints on a schedule, and alert Slack before a degraded subsystem turns into an outage.
Classify Trino cluster health from system.runtime.nodes/queries, alert on risk, and optionally kill the oldest stuck query.
A worker that silently drops out of 'active' state, or a query stuck in RUNNING
for an hour because of a bad join plan or a dead downstream connector, both
degrade a Trino cluster the same way: slowly, until someone notices from a
downstream timeout. This blueprint reads Trino's own system connector tables -
system.runtime.nodes for worker state and system.runtime.queries for
in-flight queries - classifies the cluster into a three-state risk level, alerts
on anything but a clean bill of health, and can kill a single stuck query with
Trino's own system.runtime.kill_query() procedure, behind two independent
gates.
periodic_health_check (io.kestra.plugin.core.trigger.Schedule) runs every 10 minutes, always forcing auto_remediate: "false" and dry_run: "true" through its own inputs: override regardless of this flow's defaults. Shipped disabled so you can validate a manual run first.check_node_health (io.kestra.plugin.jdbc.trino.Query, fetchType: FETCH_ONE) counts system.runtime.nodes rows by state = 'active' versus not, populating row.total_nodes, row.active_nodes, row.non_active_nodes.check_long_running_queries (io.kestra.plugin.jdbc.trino.Query, fetchType: FETCH_ONE) counts system.runtime.queries rows in state RUNNING with elapsed time (date_diff('second', started, now())) past long_running_threshold_seconds, and picks the oldest one's query_id through a scalar subquery in the same statement, populating row.stuck_count, row.max_elapsed_seconds, row.oldest_stuck_query_id.evaluate_cluster_visibility (io.kestra.plugin.core.flow.If) branches on total_nodes > 0.else (UNREACHABLE): alert_cluster_unreachable reports that no nodes are visible at all.then (nodes visible): nested evaluate_health_risk (io.kestra.plugin.core.flow.If) branches on non_active_nodes > 0 or active_nodes < min_active_nodes or stuck_count > 0.else (HEALTHY): log_cluster_healthy records a clean check.then (AT_RISK): route_remediation (io.kestra.plugin.core.flow.Switch on auto_remediate) either considers remediation ("true", further gated by check_has_stuck_query - only a stuck query can be killed, not a non-active worker - and then by dry_run through check_dry_run) or only logs that remediation is disabled ("false"). When every gate allows it, kill_stuck_query calls system.runtime.kill_query() on oldest_stuck_query_id only. alert_cluster_at_risk always fires to Slack regardless of which remediation path ran.log_audit_complete always runs last, printing the headline numbers regardless of which branch fired.errors block alerts Slack separately if the flow itself fails - an unreachable coordinator or bad credentials must not read as "no risk" or "healthy".query_id, picked deterministically (the oldest stuck query) - it never kills an arbitrary or unrelated query.auto_remediate, dry_run) between "stuck query found" and "query actually killed".Query driving both system-table introspection and a system-connector procedure call (kill_query), not just catalog data reads.system.runtime.nodes and system.runtime.queries run by hand answer one moment in time; they do not decide a cadence, classify the result into a three-state risk level, pick a single deterministic remediation target, gate a kill behind two independent confirmations, or notify anyone. Kestra supplies the schedule, the HEALTHY/AT_RISK/UNREACHABLE classification as a first-class branch, two independent authorization gates before any query is killed, and an execution history that shows exactly when the cluster degraded and whether anything was done about it.
runtime.nodes, runtime.queries, and the runtime.kill_query() procedure, is built in and requires no extra connector configuration).auto_remediate will ever be set to "true" against queries submitted by other users.Local testing:
docker run -d --name trino-gate -p 8081:8080 trinodb/trino
docker network connect YOUR_KESTRA_NETWORK trino-gate (only needed if the Kestra Worker runs in a separate Docker network than this container; set TRINO_URL to jdbc:trino://trino-gate:8080 from inside that network, or jdbc:trino://localhost:8081 from the host). Host port 8081 is used here specifically so it never collides with Kestra's own UI on 8080.
TRINO_URL: JDBC URL, e.g. jdbc:trino://host:8081 (use TLS on a production port; password authentication requires TLS).TRINO_USERNAME / TRINO_PASSWORD: Trino credentials used by every io.kestra.plugin.jdbc.trino.Query task in this flow.SLACK_WEBHOOK_URL: Slack incoming webhook used by alert_cluster_at_risk, alert_cluster_unreachable, and the errors block.SECRET_, base64-encoded, and read back in flows with {{ secret('NAME') }} - for example SECRET_TRINO_PASSWORD=$(echo -n '<your-local-password>' | base64). This keeps credentials out of the flow YAML but, per Kestra's own documentation, offers no encryption at rest or access control beyond the host environment; use the Enterprise secrets backend for stronger guarantees.min_active_nodes (INT, default 1): active-node floor before non-active nodes alone count as AT_RISK.long_running_threshold_seconds (INT, default 300): RUNNING-query elapsed-time ceiling before a query counts as stuck.auto_remediate (SELECT: "false", "true"; default "false"): must be "true" for remediation to even be considered.dry_run (SELECT: "true", "false"; default "true"): must be explicitly "false", together with auto_remediate: "true", for kill_query() to actually run.outputs.check_node_health.row.total_nodes / .active_nodes / .non_active_nodes: node counts for every run.outputs.check_long_running_queries.row.stuck_count / .max_elapsed_seconds / .oldest_stuck_query_id: stuck-query metrics for every run; oldest_stuck_query_id is absent when stuck_count is 0.tpch table) from another session to put a query into RUNNING for longer than long_running_threshold_seconds.TRINO_URL, TRINO_USERNAME, TRINO_PASSWORD, and SLACK_WEBHOOK_URL secrets.alert_cluster_at_risk fires once the slow query crosses the threshold.auto_remediate: "true" and dry_run: "true" and confirm log_dry_run_remediation describes the kill without running it.auto_remediate: "true" and dry_run: "false" and confirm kill_stuck_query runs, then verify in the Trino Web UI that the query now shows FAILED with the message set by this flow.periodic_health_check once you trust the check; it always runs in the safe auto_remediate: "false" / dry_run: "true" mode regardless of what you leave the flow's own defaults set to.queued_time_ms or analysis_time_ms from system.runtime.queries to catch queries stuck in planning, not just RUNNING.coordinator from system.runtime.nodes to alert with higher severity when the coordinator itself (not a worker) is the non-active node.io.kestra.plugin.core.flow.Loop if more than one Trino cluster needs the same gate.Query task itself and is caught by the errors block, not by evaluate_cluster_visibility - that branch only fires when the coordinator answers but system.runtime.nodes is empty.system.runtime.nodes, system.runtime.queries) and the procedure call (CALL system.runtime.kill_query(...)) both go through the same generic io.kestra.plugin.jdbc.trino.Query task as ordinary SQL text.kill_query() only targets the single oldest_stuck_query_id picked by the scalar subquery. It never iterates or kills every stuck query in one run; a cluster with several stuck queries needs several runs (or a loop extension) to clear them one at a time.auto_remediate and dry_run are independent gates, not a single boolean. Both must be "true"/"false" respectively for kill_query() to run; setting only one leaves the other still blocking.Switch case keys "true"/"false" are quoted strings. auto_remediate renders as the literal string "true" or "false"; unquoted true:/false: YAML map keys would parse as booleans instead and would not match.system.runtime.nodes and system.runtime.queries column names are taken verbatim from NodeSystemTable.java and QuerySystemTable.java in the trinodb/trino source, and kill_query() is taken verbatim from Trino's own system connector documentation - but no Docker container or Kestra engine was run to execute this flow end to end.