Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add redis liveness probe #699

Merged
merged 12 commits into from Jul 12, 2023

Conversation

victorgarcia98
Copy link
Contributor

Description

Check the health of redis instance and expose it trough the API endpoint /health/ready

Look & Feel

Request

[GET] http://localhost:5000/api/v3_0/health/ready

Response

{
	"database_redis": false,
	"database_sql": true,
	"status": 503
}

Testing Steps

  1. Configure credentials
echo 'FLEXMEASURES_REDIS_PASSWORD="MyPassWord"' >> ~/.flexmeasures.cfg 
  1. Run FM Server
flexmeasures run
  1. Get and check status
curl localhost:5000/api/v3_0/health/ready

⚙️ Check the response (status=503 and database_redis=false)

{
  "database_redis": false,
  "database_sql": true,
  "status": 503
}
  1. Create Redis instance with Docker
sudo docker run --name my-redis -p 6379:6379 -d redis --requirepass "MyPassWord"
  1. Get and check status
curl localhost:5000/api/v3_0/health/ready

⚙️ Check the response (status=200 and database_redis=true)

{
  "database_redis": true,
  "database_sql": true,
  "status": 200
}

Clean up

sudo docker stop my-redis && sudo docker rm my-redis

Closes #663

Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
@nhoening
Copy link
Contributor

Looks cool!

I tried on our docker compose stack, but the server node is not connecting to redis (only the worker needs it):

± sudo docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS                     PORTS                                       NAMES
2c366a9a86bc   flexmeasures-server   "/bin/sh -c 'pip ins…"   2 minutes ago   Up 2 minutes (unhealthy)   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   flexmeasures-server-1
2d385cb8dcd7   flexmeasures-worker   "flexmeasures jobs r…"   2 minutes ago   Up 2 minutes               5000/tcp                                    flexmeasures-worker-1
fb7bfc9c9023   postgres              "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes               5432/tcp                                    flexmeasures-test-db-1
165d2616189d   postgres              "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes               5432/tcp                                    flexmeasures-dev-db-1
feb39978747b   redis                 "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes               6379/tcp                                    flexmeasures-queue-db-1

I don't have the answer right here, but "health" isn't an atomic concept, I'm afraid.

Maybe we should be able to tell the endpoint what we define as health with some GET variables, e.g. http://localhost:5000/api/v3_0/health/ready?expect_redis=no

@victorgarcia98
Copy link
Contributor Author

Maybe we should be able to tell the endpoint what we define as health with some GET variables, e.g. http://localhost:5000/api/v3_0/health/ready?expect_redis=no

Good idea! Following Issue #663, we could also check if FLEXMEASURES_REDIS_PASSWORD is an empty string, although I like the idea of passing the parameter because we could have a worker and a server working under the same environment.

@nhoening
Copy link
Contributor

nhoening commented Jun 13, 2023

For now, checking FLEXMEASURES_REDIS_PASSWORD seems to be the easiest solution. If the environment has a value here, we should be able to find it.

Unless these nodes operate in different access contexts (e.g. in Kubernetes). Of course, that is rather rare probably, and can be solved by not only using one environment configuration for all nodes. (giving a FM instance access to a credential it shall not be using is not a good security approach, either :)

So I'd argue to go ahead with the simple check.
What do you think?

@nhoening nhoening added this to the 0.15.0 milestone Jun 15, 2023
… database_redis status code into the health information

Signed-off-by: Victor Garcia Reolid <victor@seita.nl>
@victorgarcia98
Copy link
Contributor Author

Unless these nodes operate in different access contexts (e.g. in Kubernetes). Of course, that is rather rare probably, and can be solved by not only using one environment configuration for all nodes. (giving a FM instance access to a credential it shall not be using is not a good security approach, either :)

That's true, we shouldn't have extra env variables if we don't need them.

@nhoening
Copy link
Contributor

@victorgarcia98 Can you add a changelog entry? And then let's merge this PR!

@victorgarcia98 victorgarcia98 merged commit 5508b8c into main Jul 12, 2023
4 checks passed
@victorgarcia98 victorgarcia98 deleted the 663-check-for-redis-db-in-readiness-endpoint branch July 12, 2023 09:35


/api/v3_0/health/ready
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover statement, or starting a list of new endpoints?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check for redis db in /readiness endpoint
3 participants