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

Easy way to handle cleanup on user initiated premature termination #463

Closed
RichiCoder1 opened this issue Mar 5, 2019 · 4 comments · Fixed by #752
Closed

Easy way to handle cleanup on user initiated premature termination #463

RichiCoder1 opened this issue Mar 5, 2019 · 4 comments · Fixed by #752

Comments

@RichiCoder1
Copy link
Contributor

I'm not sure of the API shape of this, but it would be nice for gluegun to expose a way either through the toolbox (like https://developer.mozilla.org/en-US/docs/Web/API/AbortController) or in the command definition to have a "cleanup" command that would be run on SIGINT or other user initiated polite terminations. Basically to allow any quick cleanup that might be necessary.

@RichiCoder1 RichiCoder1 changed the title Easy way to handle cleanup Easy way to handle cleanup on user initiated premature termination Mar 5, 2019
@jamonholmgren
Copy link
Member

jamonholmgren commented Mar 5, 2019

Excellent idea. I'd love to hear other's thoughts on it.

@RichiCoder1
Copy link
Contributor Author

I think the right API might be to change

export interface GluegunCommand<TContext extends Toolbox = Toolbox> {
  /** The name of your command */
  name?: string
  /** A tweet-sized summary of your command */
  description?: string
  /** The function for running your command, can be async */
-  run: (toolbox: TContext) => void
+  run: (toolbox: TContext, abortToken: AbortToken) => void
  /** Should your command be shown in the listings  */
  hidden?: boolean
  /** The command path, an array that describes how to get to this command */
  commandPath?: string[]
  /** Potential other names for this command */
  alias?: string | string[]

Where AbortToken is:

class AbortToken extends EventEmitter {
    constructor() {
        super();
    }
}

And run and/or runtime handles wiring up the approriate events.

const token = new AbortToken();
process.on('SIGINT', () => {
    token.emit('abort');
});
//...
if (command.run) {
    await command.run(toolbox, token);
}

with a command being able to do:

run: (toolbox, abortToken) => {
    const otherProcess = exec('do this thing', () => { /* do stuff */ });
    abortToken.on('abort', () => otherProcess.kill());
}

@jamonholmgren
Copy link
Member

I'm not super keen on changing the run function signature. I'd rather hang it on the toolbox itself. Perhaps on meta.

Something like:

run: (toolbox) => {
  const otherProcess = exec('do this thing', () => { /* do stuff */ });
  toolbox.meta.onAbort(() => otherProcess.kill())
}

infinitered-circleci pushed a commit that referenced this issue Feb 4, 2022
# [5.1.0](v5.0.0...v5.1.0) (2022-02-04)

### Features

* **meta:** Adds onAbort callback to toolbox.meta ([#752](#752) by [@ugur-eren](https://github.com/ugur-eren)) ([7e6c21b](7e6c21b)), closes [#463](#463)
@infinitered-circleci
Copy link

🎉 This issue has been resolved in version 5.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging a pull request may close this issue.

3 participants