Search code examples
pythonfalconframework

Falcon API - check if the server is running


I have built a very simple API in Falcon that post results of my machine learning model. See below:


import falcon
import heureka_predikce


class MLmodel:
    def on_post(self, req, resp):

        reg_list = req.media
        response_list = []

        for vstup in reg_list:
            response_list.append(heureka_predikce.heureka_predikuj(vstup))

        resp.text = response_list
        resp.status = falcon.HTTP_OK


my_model = MLmodel()

app = application = falcon.API()
app.add_route("/predictionApi/v1/predict", my_model)

But I would like to check if the server is running. That is, I would like to include some new method into MLmodel that does a "health check". I am a beginner with APIs, but is there a recommended way, how to do this? I think that it should be relatively easy, but I cannot find anything myself... Thanks


Solution

  • To check if server is up and running, usually what we do is send a request at an endpoint and server will return something indicating that it's working.

    As you said you will need to add a method in MLmodel for the same reason, it can be done but it's not the recommended way as your MLmodel is handling predict endpoint.

    To do a health check, you can create a new endpoint with new class which will accept a get request and will respond you as soon it is invoked.

    For Ex.:

    class Ping:
        def on_get(self, req, resp):
            resp.text = "pong"
            resp.status = falcon.HTTP_OK
    

    And then just add route for same resource using: app.add_route("/predictionApi/v1/ping", Ping())

    Now for each ping request, you will get pong response.