Skip to content

Add onTerminate hook to handle cluster termination#8

Merged
jdesboeufs merged 3 commits intomainfrom
copilot/add-onterminate-hook
Oct 15, 2025
Merged

Add onTerminate hook to handle cluster termination#8
jdesboeufs merged 3 commits intomainfrom
copilot/add-onterminate-hook

Conversation

Copy link
Contributor

Copilot AI commented Oct 15, 2025

Overview

This PR adds an optional onTerminate callback to the cluster that is invoked when the cluster terminates, addressing the need to handle cluster termination events.

Motivation

When a cluster terminates (either explicitly via end() or automatically due to failures), there was no way for applications to be notified and perform cleanup operations or logging. This hook provides a clean mechanism to handle these scenarios.

Changes

Core Implementation

  • Added optional onTerminate parameter to createCluster options
  • Modified terminateCluster function to invoke the hook after all nodes are killed
  • Added proper error handling for fire-and-forget termination calls to prevent unhandled promise rejections

Usage

const cluster = await createCluster({
  numNodes: 2,
  onTerminate(reason) {
    console.log(`Cluster terminated: ${reason}`)
    // Perform cleanup operations, logging, etc.
  }
})

The hook receives a reason parameter indicating why the cluster was terminated:

  • 'cluster stopped' - when cluster.end() is called explicitly
  • 'Too many consecutive failures' - when automatic termination occurs due to node startup failures

Testing

Added comprehensive test coverage:

  • Test for explicit termination via end() method
  • Test for automatic termination on too many consecutive failures
  • Both tests verify the hook is called with the correct reason

All existing tests continue to pass, and code coverage is maintained at 91.21%.

Backwards Compatibility

This is a non-breaking change. The onTerminate hook is optional and existing code will continue to work without modification.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add onTerminate hook to handle cluster termination</issue_title>
<issue_description>When a cluster terminates, we have no way to handle this.
We should have an optional onTerminate.</issue_description>

<agent_instructions>Please update unit tests and coverage.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #7


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits October 15, 2025 12:38
Co-authored-by: jdesboeufs <1231232+jdesboeufs@users.noreply.github.com>
Co-authored-by: jdesboeufs <1231232+jdesboeufs@users.noreply.github.com>
Copilot AI changed the title [WIP] Add onTerminate hook to handle cluster termination Add onTerminate hook to handle cluster termination Oct 15, 2025
Copilot AI requested a review from jdesboeufs October 15, 2025 12:46
@jdesboeufs jdesboeufs marked this pull request as ready for review October 15, 2025 13:09
@jdesboeufs jdesboeufs merged commit 48729a3 into main Oct 15, 2025
4 of 5 checks passed
@jdesboeufs jdesboeufs deleted the copilot/add-onterminate-hook branch October 15, 2025 13:10
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.

Add onTerminate hook to handle cluster termination

2 participants