How to Correctly Exit Jina

In this section, you will learn best practices for shutting down a Flow and exiting Jina correctly.

Feature description and expected outcome

Jina provides several methods to exit this service gracefully. In this way, the Flow is terminated and all occupied resources are released.

Before you start

Make sure that Jina is installed properly as explained in Installation, and you had an active running Flow correctly set up as explained in 101: Basic components <../101.rst> and 102: How basic components work together.

Implementation

In Python

A common way to start a Flow in your Python code is using a with statement. Moving out from the with scope, all resources (including Pods of all kinds) of the Flow will be released immediately.

The source snippet below demonstrates this by defining a Flow named f containing the two names p1 and p2, only. The execution of the pass statement is followed by leaving the scope of the with statement, and releasing the resources for f.

from jina.flow import Flow

f = (Flow.add(name='p1')
         .add(name='p2'))
with f:
    pass

Starting a Flow using the start() method requires you to also call close() method in order to properly shut down the Flow when you do not use it anymore. The source code below demonstrates this in a try-finally block.

from jina.flow import Flow

f = (Flow.add(name='p1')
         .add(name='p2'))
try:
    f.start()
finally:
    f.close()

In the console

If you are running Jina locally, e.g. using the command jina flow, you can use the key combinations Control-c or Command-c to terminate the running Jina process at any time. All Pods created with BasePod will receive this signal and react upon it by shutting down the process accordingly.

Please note container Pods and remote Pods sometimes take longer to shut down. When you open many replicas or many Pods, it may also take some time to release all resources.

Rule of thumb, for an individual Pod/Pea, when you see the output below on the console, then it is already shut down successfully.

[email protected][I]:no update since 2020-04-23 20:31:10, will not save. If you really want to save it, call "touch()" before "save()" to force saving
[email protected][I]:executor says there is nothing to save
[email protected][I]:msg_sent: 0 bytes_sent: 0 KB msg_recv: 0 bytes_recv:0 KB
[email protected][I]:msg_sent: 0 bytes_sent: 0 KB msg_recv: 0 bytes_recv:0 KB
[email protected][S]:terminated

For Flow, when you see the output below from the console, then it is already shut down.

[email protected][S]:terminated
[email protected][I]:msg_sent: 653 bytes_sent: 590 KB msg_recv: 326 bytes_recv:956 KB
[email protected][S]:terminated
[email protected][I]:msg_sent: 653 bytes_sent: 587 KB msg_recv: 326 bytes_recv:948 KB
[email protected][S]:terminated
[email protected][I]:msg_sent: 651 bytes_sent: 583 KB msg_recv: 325 bytes_recv:939 KB
[email protected][S]:terminated
[email protected][I]:msg_sent: 653 bytes_sent: 589 KB msg_recv: 326 bytes_recv:953 KB
[email protected][S]:terminated
[email protected][S]:flow is closed and all resources should be released already, current build level is EMPTY

Using Jina remotely

If you are using Jina remotely (via JinaD), you can find out how to exit correctly via this guide.