# Checking service is up before calling it.

**URL:** <https://discourse.nameko.io/t/checking-service-is-up-before-calling-it/261>\
**Category:** googlegroup\
**Created:** [June 26, 2018, 8:11am UTC](https://discourse.nameko.io/t/checking-service-is-up-before-calling-it/261 "2018-06-26T08:11:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Miragias](https://avatars.discourse-cdn.com/v4/letter/j/cdc98d/32.png) [@John\_Miragias](https://discourse.nameko.io/u/John_Miragias)\
**Post date:** [June 26, 2018, 8:11am UTC](https://discourse.nameko.io/t/checking-service-is-up-before-calling-it/261/1 "2018-06-26T08:11:31Z")

</div>

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"):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app = Flask(\_\_name\_\_)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app.config.update(dict(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AMQP\_URI= 'pyamqp://guest:guest@localhost',  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NAMEKO\_RPC\_TIMEOUT=2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;))

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y = RpcProxy("bot")

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@app.route("/")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def hello():  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return render\_template('index.html')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@app.route("/service")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def serve():  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(y.is\_bound)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("serve")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with ClusterRpcProxy(app.config) as rpc:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rpc.bot.test2()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;except:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Could not reach service")

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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?

---

<div class="post-metadata">

**Author:** ![mattbennett](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.nameko.io/mattbennett/32/13_2.png) [@mattbennett](https://discourse.nameko.io/u/mattbennett)\
**Post date:** [June 26, 2018, 1:43pm UTC](https://discourse.nameko.io/t/checking-service-is-up-before-calling-it/261/2 "2018-06-26T13:43:38Z")

</div>

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:

&nbsp;&nbsp;&nbsp;&nbsp;...

You might be using flask-nameko  
\<[https://github.com/jessepollak/flask-nameko&gt](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"):  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app = Flask(\_\_name\_\_)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app.config.update(dict(  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AMQP\_URI= 'pyamqp://guest:guest@localhost',  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NAMEKO\_RPC\_TIMEOUT=2  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;))
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y = RpcProxy("bot")
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@app.route("/")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def hello():  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return render\_template('index.html')
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@app.route("/service")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def serve():  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(y.is\_bound)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("serve")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with ClusterRpcProxy(app.config) as rpc:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rpc.bot.test2()  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;except:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Could not reach service")
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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?
