moan0s / matrix-registration-bot

A bot that is used to create and manage registration tokens for a matrix server
GNU Affero General Public License v3.0
64 stars 10 forks source link

Check if service is still healthy #25

Open Babalion opened 1 year ago

Babalion commented 1 year ago

I suggest adding a health check to the docker-compose.yml file. I can create a pull request for this, but I am unsure which would be the best way to check if the service is still healthy.

I am grateful for any help!

moan0s commented 1 year ago

Hi, do you have an example how this would look like? Sorry, never did this before

Babalion commented 1 year ago

Of course!

In a common matrix-synapse docker-compose.yml one can add the following healthcheck:

healthcheck:
  test: ["CMD", "curl", "-fSs", "http://localhost:8008/health"]
  interval: 15s
  timeout: 5s
  retries: 3
  start_period: 5s

The command specified after test will be executed by the docker-daemon in the given interval. After specified retries or timeout the docker-daemon will mark the service as unhealthy. It is now up to the server administrator how to proceed with unhealthy services (for example send a notification, restart the container etc.).

So the question is, what command could be executed regularly in the bot container that succeeds when everything is fine and fails when something has gone wrong?

moan0s commented 1 year ago

Sounds good & sensible. Sadly with the framework that we use does not allow to easily implement a /health. I will try to think of something

Babalion commented 1 year ago

We don't need a site like the /health in matrix-synapse. Any shell command is fine. We can for example curl for a variable in a json response. I've also seen healthchecks where they simply ping an arbitrary URL of the service.

So what would you check first, to be sure everything is fine?

Maybe the source code does not need to be changed at all... What I noticed was, that the bot sometimes does not respond to a message. I had to restart the container then. Maybe this is something one could check in a healthcheck?

moan0s commented 1 year ago

Sorry for the late response. I was thinking about this a lot and I think there is no easy way to check the status/health of the bot. The easiest I could think of, was to check the presence status of the bot like this

curl -H 'Authorization: Bearer syt_access_token_of_the_bot_itself'  https://synapse.hyteck.de/_matrix/client/v3/presence/@test-registration-bot:hyteck.de/status

whicht returns either {"presence":"online","last_active_ago":1957,"currently_active":true} or {"presence":"offline","last_active_ago":52746674}

red0888 commented 1 year ago

Sorry for the late response. I was thinking about this a lot and I think there is no easy way to check the status/health of the bot. The easiest I could think of, was to check the presence status of the bot like this

curl -H 'Authorization: Bearer syt_access_token_of_the_bot_itself'  https://synapse.hyteck.de/_matrix/client/v3/presence/@test-registration-bot:hyteck.de/status

whicht returns either {"presence":"online","last_active_ago":1957,"currently_active":true} or {"presence":"offline","last_active_ago":52746674}

That works too imo