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

feat: add lifecycle and endpoint customization #203

Merged
merged 10 commits into from
Apr 17, 2023

Conversation

lance
Copy link
Member

@lance lance commented Mar 24, 2023

This is still a work in progress. Enables function developers to export init(), shutdown(), liveness() and readiness() functions from the function file, enabling startup behaviors such as a connection to a database or other persistent store, clean shutdowns, and customizable liveness and readiness checks.

Still needed:

  • Update README with the API
  • Update hack/run.js to provide a better example (and maybe move from hack/run.js to sample/index.js
  • Review types.d.ts to make sure we're doing enough typing
  • Consider any other lifecycle hooks that may be needed
  • Update the CLI to work with Function objects

Fixes: #142

This is still a work in progress. Enables function developers to export
`init()`, `shutdown()`, `liveness()` and `readiness()` functions from
the function file, enabling startup behaviors such as a connection to a
database or other persistent store, clean shutdowns, and customizable
liveness and readiness checks.

Signed-off-by: Lance Ball <lball@redhat.com>
@codecov-commenter
Copy link

codecov-commenter commented Mar 24, 2023

Codecov Report

Patch coverage: 84.61% and project coverage change: -1.49 ⚠️

Comparison is base (8883cc0) 92.39% compared to head (d01c69c) 90.90%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #203      +/-   ##
==========================================
- Coverage   92.39%   90.90%   -1.49%     
==========================================
  Files           9        9              
  Lines         263      286      +23     
==========================================
+ Hits          243      260      +17     
- Misses         20       26       +6     
Impacted Files Coverage Δ
index.js 83.54% <81.81%> (-4.18%) ⬇️
lib/health-check.js 100.00% <100.00%> (ø)
lib/request-handler.js 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Signed-off-by: Lance Ball <lball@redhat.com>
@lance lance requested a review from lholmquist March 24, 2023 13:27
@lance
Copy link
Member Author

lance commented Mar 24, 2023

@lholmquist I've marked it as a draft, but any feedback you have now is welcome.

@lholmquist
Copy link
Member

about a month ago i took a quick look at doing something similar here: lholmquist@628fac0

With the quick glance i just took of this PR, it looks like we had the same thoughts, so that's good :)

i'll try to take a better look at comment today

@lholmquist
Copy link
Member

A thought i just had regarding the shutdown method. The main environment where this framework is running/serving the function is probably going to be in a container, so when the container gets brought down, i wonder how much time a user will have to do something in this method

@lance
Copy link
Member Author

lance commented Mar 24, 2023

i wonder how much time a user will have to do something in this method

Not much that's for sure. I think, however, instead of adding a listener to the server shutdown event, it would be better handled in the way the CLI is doing ON_DEATH. I've played around with some sample functions and this seems like the better approach.

Also cleans up shutdown handling and adds some comments.

Signed-off-by: Lance Ball <lball@redhat.com>
Signed-off-by: Lance Ball <lball@redhat.com>
@lance lance marked this pull request as ready for review March 24, 2023 16:43
@lance
Copy link
Member Author

lance commented Mar 24, 2023

@lholmquist ok I think this is ready for a first look when you have a chance.

Signed-off-by: Lance Ball <lball@redhat.com>
@@ -22,7 +22,39 @@ at `localhost:8080`. The incoming request may be a
just a simple HTTP GET/POST request. The invoked user function can be
`async` but that is not required.

## Function Signatures
## The Function Interface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should probably be documented that you can also change the health checks path using either environment variables are the liveness.path/readiness.path properties in the function

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right - I forgot about that. At the moment, the Function.liveness.path property will override any environment setting. I wonder if it should be the other way around.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, good question. i don't have to strong of feelings either way, but probably leaning more towards the way it currently is. function code overrides env variables

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might also want to add port and logLevel as properties on the Function object so that the behavior is the same in terms of precedence. Default is superseded by configuration (in func.yaml, on the command line, or passed options to start()), which is superseded by environment variables.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm, good question. i don't have to strong of feelings either way, but probably leaning more towards the way it currently is. function code overrides env variables

I think the motivation for env variables overriding function config is that the configuration would typically be set up for production environments, and coded into bash scripts or other pipeline tooling. For local development this can be overridden without changing the production settings by adjusting env vars - therefore no changes to the code.

lib/types.d.ts Outdated Show resolved Hide resolved
lib/types.d.ts Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
Signed-off-by: Lance Ball <lball@redhat.com>
lholmquist
lholmquist previously approved these changes Mar 30, 2023
Copy link
Member

@lholmquist lholmquist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lance except for my previous comments LGTM!

@lance
Copy link
Member Author

lance commented Mar 30, 2023

@lance except for my previous comments LGTM!

Awesome - but please hold off on merging as I have some additional changes locally that I am still working on. I should get those added tomorrow.

Signed-off-by: Lance Ball <lball@redhat.com>
Signed-off-by: Lance Ball <lball@redhat.com>
Signed-off-by: Lance Ball <lball@redhat.com>
@lance
Copy link
Member Author

lance commented Mar 31, 2023

@lholmquist OK, I think this is probably good for a final review. I've updated README.md and the TypeScript types.

module.exports = function healthCheck(opts) {
return (fastify, _, done) => {
// Handle health checks
fastify.get(opts?.readiness?.path || readinessURL, { logLevel: 'warn' }, opts.readiness || callProtect);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these default to warn? or should they be whatever is in the config?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lholmquist done - good catch! Hopefully this PR is good to go now.

Signed-off-by: Lance Ball <lball@redhat.com>
@lance
Copy link
Member Author

lance commented Apr 12, 2023

@lholmquist @matejvasek ptal

Copy link
Member

@lholmquist lholmquist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, i was on PTO last week

LGTM!!

@lholmquist lholmquist merged commit ff20b40 into nodeshift:main Apr 17, 2023
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

Successfully merging this pull request may close these issues.

Support for custom health and liveness checks
4 participants