Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs]: What is the opposite of init_context #3140

Open
1 task done
fanjizhao opened this issue Jan 24, 2024 · 6 comments
Open
1 task done

[Docs]: What is the opposite of init_context #3140

fanjizhao opened this issue Jan 24, 2024 · 6 comments

Comments

@fanjizhao
Copy link

Nuclio version checks

  • I have checked that the issue still exists on the latest versions of the docs here

Location of the documentation

https://nuclio.io/docs/latest/concepts/best-practices-and-common-pitfalls/

Documentation problem

This document mentions init_context to do some work related to creating db connections, but does not mention any function to handle closing the db connections.

Suggested fix for documentation

Please tell us where to close these resources, like db connections, or what is the best practice to handle such things.

@TomerShor
Copy link
Contributor

Hi @fanjizhao ,
We have recently introduced an option to set a termination callback - a function that will be invoked before the function is terminated (either when deleted or when something happens), and enables a graceful shutdown.

See how to use it here: https://docs.nuclio.io/en/stable/reference/runtimes/python/python-reference.html#termination-callback

Notes:

  • This is currently only supported in the Python runtime
  • The function does not accept any arguments

@fanjizhao
Copy link
Author

Hi @fanjizhao , We have recently introduced an option to set a termination callback - a function that will be invoked before the function is terminated (either when deleted or when something happens), and enables a graceful shutdown.

See how to use it here: https://docs.nuclio.io/en/stable/reference/runtimes/python/python-reference.html#termination-callback

Notes:

  • This is currently only supported in the Python runtime
  • The function does not accept any arguments

I really appreciated your answer, which is really helpful. However I found the link you mentioned is different from the one I mentioned, and the document you gave is a little bit newer than the one I used. What is the relation between the two documents?

@rokatyy
Copy link
Contributor

rokatyy commented Jan 24, 2024

@fanjizhao https://nuclio.io/ is our old documentation website. Please refer to https://docs.nuclio.io/ as the source of truth.

@fanjizhao
Copy link
Author

Hi @fanjizhao , We have recently introduced an option to set a termination callback - a function that will be invoked before the function is terminated (either when deleted or when something happens), and enables a graceful shutdown.

See how to use it here: https://docs.nuclio.io/en/stable/reference/runtimes/python/python-reference.html#termination-callback

Notes:

  • This is currently only supported in the Python runtime
  • The function does not accept any arguments

After I read the document about the termination callback, I found it's a good answer to my question. However, I noticed the designing philosophy between init_context and the termination callback is not consistent. Can you please introduce the designing considerations from the developers' perspective?

@TomerShor
Copy link
Contributor

@fanjizhao what do you mean regarding the design philosophy?

The init_context is function is called during function startup, on each inner worker.
So, this will be called once at the beginning, and allow you to do some initialization.

The termination_callback is a function of your choosing, that will be invoked before the function is terminated - either due to some error or due to deleting the function.
This utilizes the fact that k8s (and docker) send SIGTERM signal to tell the container it is about to be killed, and allow for graceful termination.
This whole registering to the signal is done "behind the scenes", and we let the user set a function to be invoked when the signal arrives.

The best option is to set the termination callback in the init_context function, so it will only be set once.

@fanjizhao
Copy link
Author

@fanjizhao what do you mean regarding the design philosophy?

The init_context is function is called during function startup, on each inner worker. So, this will be called once at the beginning, and allow you to do some initialization.

The termination_callback is a function of your choosing, that will be invoked before the function is terminated - either due to some error or due to deleting the function. This utilizes the fact that k8s (and docker) send SIGTERM signal to tell the container it is about to be killed, and allow for graceful termination. This whole registering to the signal is done "behind the scenes", and we let the user set a function to be invoked when the signal arrives.

The best option is to set the termination callback in the init_context function, so it will only be set once.

I mean why don't use the same design for the initializing function and the termination function. For example, init_context and terminate_context functions, or use two functions to set the callbacks, like init_callback and termination_callback. Hope I clarified my meaning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants