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

Listen for SIGTERM too #874

Merged
merged 3 commits into from
Dec 31, 2023
Merged

Listen for SIGTERM too #874

merged 3 commits into from
Dec 31, 2023

Conversation

wms
Copy link
Contributor

@wms wms commented Dec 30, 2023

Install interrupt handler for SIGTERM events, as commonly used by watchers/supervisors.

@todo: the interrupt fn always reports that the signal received was SIGINT even if SIGTERM was used. Parameterize interrupt to correct report received signal?

Motivation

The dev-mode supervision tool tsx sends a SIGTERM to the child process when a change is detected; it would be nice for Effection to catch these too.

Approach

Simply install the interrupt handler in response to SIGTERM events just like SIGINT

Alternate Designs

I've currently got the following baked into my Node app, which works, but would be nice to omit:

/**
 * Shuts down Effection when SIGTERM is received.
 * @TODO Raise PR to bake this into Effection's `main` implementation
 */
function* useSigTerm() {
  const { run } = yield* useScope();
  const handler = () =>
    run(function* () {
      yield* exit(130);
    });

  process.on("SIGTERM", handler);

  yield* ensure(() => {
    process.off("SIGTERM", handler);
  });
}

await main(function* () {
  yield* useSigTerm();
  /* ... */

Possible Drawbacks or Risks

Maybe downstream users have custom behaviour bound to SIGTERM.

TODOs and Open Questions

  • the interrupt fn always reports that the signal received was SIGINT even if SIGTERM was used. Parameterize interrupt to correct report received signal?

Install interrupt handler for `SIGTERM` events, as commonly used by watchers/supervisors.

@todo: the interrupt fn always reports that the signal received was `SIGINT` even if `SIGTERM` was used. Parameterize `interrupt` to correct report received signal?
@cowboyd
Copy link
Member

cowboyd commented Dec 31, 2023

@wms I think this totally makes sense, and in fact, this was the behavior in v2 https://github.com/thefrontside/effection/blob/v2/packages/main/src/node.ts#L41-L42

So I think this would be an improvement for sure. Before merging there are a couple of issues that would have to be addressed.

  1. You're right, we would need to parameterize the interrupt to indicate that it was sigterm
  2. We need a test case. I'd imagine you could replicate this as a start: https://github.com/thefrontside/effection/blob/types-for-all/test/main.test.ts#L5-L22

I really like your work around by the way.!

@wms
Copy link
Contributor Author

wms commented Dec 31, 2023

Thank you sir, but credit goes to you folks in making the API clean enough to make the workaround slot in so nicely and not feel at all hacky. I'll put in a test case + parameterized interrupt handler.

@wms
Copy link
Contributor Author

wms commented Dec 31, 2023

@cowboyd Branch updated to include separate interrupts for each signal and test coverage.

@cowboyd cowboyd merged commit ee71ffc into thefrontside:v3 Dec 31, 2023
1 check passed
@cowboyd
Copy link
Member

cowboyd commented Dec 31, 2023

@wms This looks great, and thanks for taking taking the time to contribute! This should be released some time next week.

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.

2 participants