Checking service is up before calling it.

Does anyone know of a way to set a timeout or check if a microservice is
working (up and running)with nameko? I have been using the following
snippet for nameko to run a small service when a user hits a webpage. But
if the microservice is not started it stalls forever.
def start_web_server(template_folder = "/web/templates"):
        app = Flask(__name__)
        app.config.update(dict(
                AMQP_URI= 'pyamqp://guest:guest@localhost',
                NAMEKO_RPC_TIMEOUT=2
                ))

        y = RpcProxy("bot")

        @app.route("/")
        def hello():
                return render_template('index.html')

        @app.route("/service")
        def serve():
                print(y.is_bound)
                print("serve")
                with ClusterRpcProxy(app.config) as rpc:
                        try:
                                rpc.bot.test2()
                        except:
                                print("Could not reach service")

                return render_template('index.html')

This also happens inside the nameko shell. Like when I use
n.rpc.bot.some_func_name() some_func_name can not even exist but it will
stall forever.
If I call a service which doesn't exist I will get an error on the shell
like if I call n.rpc.some_service_that_doesn't_exist.foo(). But if i call
something like n.rpc.bot.some_func_that_does_not_exist() the shell just
stalls(and so my snippet above stalls as well).
Is there any way to get around this problem that I am missing?

The best way to check whether a service is running is to try making an RPC
call to it :slight_smile:

This is the behaviour when services/methods don't exist:

If you call a method that doesn't exist on a running service, you'll get a
MethodNotFound error.
If you call a service that isn't running and has never previously run
you'll get an UnknownService error.
If you call a method on a service that is not *currently* running, the
RpcProxy will wait for a reply (which would be sent when the service comes
back online) or a timeout to expire.

Your situation is the third one, so you need a timeout.

Is the ClusterRpcProxy in your code the one from nameko.standalone.rpc? If
so I think you just need to pass the timeout as a keyword argument:

with ClusterRpcProxy({'AMQP_URI': "amqp://..."}, timeout=2) as rpc:

    ...

You might be using flask-nameko
<https://github.com/jessepollak/flask-nameko&gt; instead, in which case
there's probably some other way to configure timeouts. But certainly a
timeout is what you need, and once it's configured correctly the proxy
won't hang. The reason it hangs in the nameko shell is that the proxy there
doesn't have any RPC timeout configured by default.

···

On Tuesday, June 26, 2018 at 9:11:31 AM UTC+1, John Miragias wrote:

Does anyone know of a way to set a timeout or check if a microservice is
working (up and running)with nameko? I have been using the following
snippet for nameko to run a small service when a user hits a webpage. But
if the microservice is not started it stalls forever.
def start_web_server(template_folder = "/web/templates"):
        app = Flask(__name__)
        app.config.update(dict(
                AMQP_URI= 'pyamqp://guest:guest@localhost',
                NAMEKO_RPC_TIMEOUT=2
                ))

        y = RpcProxy("bot")

        @app.route("/")
        def hello():
                return render_template('index.html')

        @app.route("/service")
        def serve():
                print(y.is_bound)
                print("serve")
                with ClusterRpcProxy(app.config) as rpc:
                        try:
                                rpc.bot.test2()
                        except:
                                print("Could not reach service")

                return render_template('index.html')

This also happens inside the nameko shell. Like when I use
n.rpc.bot.some_func_name() some_func_name can not even exist but it will
stall forever.
If I call a service which doesn't exist I will get an error on the shell
like if I call n.rpc.some_service_that_doesn't_exist.foo(). But if i call
something like n.rpc.bot.some_func_that_does_not_exist() the shell just
stalls(and so my snippet above stalls as well).
Is there any way to get around this problem that I am missing?