# Health check

Source : https://webgune.cloud/en-us/docs/controle-de-sante · Mis à jour le 2026-11-20

# Health check

Webgune continuously checks that each instance of your service responds. This check decides when a new version receives traffic, when an instance is taken out of rotation, and when it is restarted. This page describes what is checked and how to write an application that answers it honestly.

## What Webgune checks

For a site or an API, the check is a **TCP connection to the service's port** (the **Container port** in the **Settings** tab). A connection accepted in under one second counts as a success. No HTTP request is sent.

Two checks run in parallel on each instance:

| Check | First attempt | Frequency | After 6 failures in a row |
|----------|---------------|-----------|-------------------------|
| Ready | 5 seconds after startup | every 10 seconds | the instance no longer receives traffic, about one minute after the first failure. It gets traffic again as soon as it responds again |
| Alive | 30 seconds after startup | every 20 seconds | the instance is restarted, about two minutes after the first failure |

A bot or a worker has no port: neither of these checks applies. Webgune only checks that the process is running, and restarts it if it stops.

The check path (HTTP route, delay, number of attempts) cannot be configured.

## During an update

A new version goes live only when all its instances pass the "Ready" check and no old instance remains. In the meantime, the old version keeps serving.

The new version is rejected, and the previous one put back in service:

- right away, if an instance crashes in a loop or its image cannot be downloaded;
- after **15 minutes**, if its instances are still not ready.

The reason for the rejection appears at the end of the deployment log ([Deployments and rollback](/docs/deploiements-rollback)).

## The limit of this check

An accepted connection proves that the program is listening. It does not prove that it works. An application that opens its port, then fails 10 seconds later while connecting to its database, can replace the old version before it falls over.

Conversely, a stuck application (infinite loop, database that no longer responds) that keeps its port open passes the check. Only your own monitoring detects it.

## Open the port when the application is ready

Do all the startup work (connecting to the database, loading the configuration, warming the cache) **before** calling `listen`:

```js
async function main() {
  await db.connect();     // if this step fails, the port never opens
  await loadConfig();     // and the old version stays in service
  const port = Number(process.env.PORT) || 3000;
  app.listen(port, "0.0.0.0");
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});
```

If a step fails, the process exits with an error code, the port does not open, and Webgune keeps the old version.

Same principle in Python with FastAPI: make the connections in the `lifespan`, which Uvicorn runs before accepting connections.

```python
from contextlib import asynccontextmanager
from fastapi import FastAPI

@asynccontextmanager
async def lifespan(app: FastAPI):
    await db.connect()       # an exception here stops startup
    yield
    await db.disconnect()

app = FastAPI(lifespan=lifespan)
```

## Expose a health route

Add a `/healthz` route. Webgune does not call it today, but it serves your external monitoring, diagnosis from the [web terminal](/docs/terminal-exec) (`curl localhost:$PORT/healthz`) and spotting a stuck application. It must:

- answer `200` only when the application is really ready (database reachable, configuration loaded);
- answer in under one second, without authentication;
- do no heavy work.

With Express:

```js
app.get("/healthz", async (req, res) => {
  try {
    await db.query("SELECT 1");
    res.status(200).send("ok");
  } catch {
    res.status(503).send("database unavailable");
  }
});
```

With FastAPI:

```python
from fastapi import HTTPException

@app.get("/healthz")
async def healthz():
    try:
        await db.execute("SELECT 1")
        return {"status": "ok"}
    except Exception:
        raise HTTPException(status_code=503, detail="database unavailable")
```

## Graceful shutdown

When an instance is replaced or moved, it receives the `SIGTERM` signal. It then has a delay to finish its in-flight requests, after which it is stopped by force:

| Switchover strategy (**Settings** tab) | Delay |
|--------------------------------------------|-------|
| Automatic (default) | 30 seconds |
| Immediate | 5 seconds |
| Session end | 30 minutes |

Bots and workers have 30 seconds by default.

```js
const server = app.listen(port, "0.0.0.0");

process.on("SIGTERM", () => {
  server.close(() => process.exit(0));   // refuses new connections, finishes the others
});
```

Without this handling, requests in progress at the moment of the switchover are cut off.

An application started by `npm start` receives the signal through npm. If your application does not stop cleanly with npm, start it directly (`node server.js`) with the [start command](/docs/commande-demarrage).

## Automatic repair

An instance that crashes is restarted, with a delay that grows between restarts if it crashes in a loop. When Webgune recognizes the cause (port different from the configured one, memory exceeded, missing variables), it also tries a targeted fix, up to 3 times, before going back to the last successful version. Details on [Deployments and rollback](/docs/deploiements-rollback).

The **General** tab shows the state of the instances ("2/2 ready"), the restart count and the repair in progress.

## Common errors

| Symptom | Cause | Fix |
|----------|-------|------------|
| Update rolled back to the previous version after 15 minutes | the new version never opened its port | check `PORT` and listening on `0.0.0.0` |
| Update refused right away, "CrashLoopBackOff" | the application crashes at startup | read the **Logs** tab, fix the error |
| The service goes live then crashes | port opened before initialization finished | initialize before `listen` |
| An instance is restarted about every two minutes | it no longer accepts connections (blocking loop, saturation) | read the logs, check memory and CPU |
| The service is never ready | listens on `localhost` instead of `0.0.0.0` | fix the listening address |
| Stop with code 137 | instance memory exceeded | reduce consumption or change plan ([Limits and resources](/docs/limites-ressources)) |
