Checking service is up before calling it.

34 views
Skip to first unread message

John Miragias

unread,
Jun 26, 2018, 4:11:31 AM6/26/18
to nameko-dev
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?
 

Matt Yule-Bennett

unread,
Jun 26, 2018, 9:43:38 AM6/26/18
to nameko-dev
The best way to check whether a service is running is to try making an RPC call to it :)

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 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.
Reply all
Reply to author
Forward
0 new messages