-
Notifications
You must be signed in to change notification settings - Fork 42
Shutdown/Cancel interface, "Deactivate" #71
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b2d5b8b
Shutdown/Cancel interface
maxdml 3607d0f
should not need this
maxdml ed2f64c
comment
maxdml db5f224
deactivate just stops the wf scheduler + rebalance shutdown/cancel re…
maxdml 5fde81e
comments
maxdml f450782
no cancel for now, just shutdown
maxdml 3740223
shutdown systemdb last
maxdml File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,8 +71,8 @@ type DBOSContext interface { | |
| context.Context | ||
|
|
||
| // Context Lifecycle | ||
| Launch() error // Launch the DBOS runtime including system database, queues, admin server, and workflow recovery | ||
| Cancel() // Gracefully shutdown the DBOS runtime, waiting for workflows to complete and cleaning up resources | ||
| Launch() error // Launch the DBOS runtime including system database, queues, admin server, and workflow recovery | ||
| Shutdown(timeout time.Duration) // Gracefully shutdown all DBOS runtime components with ordered cleanup sequence | ||
|
|
||
| // Workflow operations | ||
| RunAsStep(_ DBOSContext, fn StepFunc, opts ...StepOption) (any, error) // Execute a function as a durable step within a workflow | ||
|
|
@@ -239,7 +239,7 @@ func (c *dbosContext) GetApplicationID() string { | |
| } | ||
|
|
||
| // NewDBOSContext creates a new DBOS context with the provided configuration. | ||
| // The context must be launched with Launch() before use and should be shut down with Cancel(). | ||
| // The context must be launched with Launch() before use and should be shut down with Shutdown(). | ||
| // This function initializes the DBOS system database, sets up the queue sub-system, | ||
| // and prepares the workflow registry. | ||
| // | ||
|
|
@@ -253,7 +253,7 @@ func (c *dbosContext) GetApplicationID() string { | |
| // if err != nil { | ||
| // log.Fatal(err) | ||
| // } | ||
| // defer ctx.Cancel() | ||
| // defer ctx.Shutdown(30*time.Second) | ||
| // | ||
| // if err := ctx.Launch(); err != nil { | ||
| // log.Fatal(err) | ||
|
|
@@ -372,62 +372,86 @@ func (c *dbosContext) Launch() error { | |
| return nil | ||
| } | ||
|
|
||
| // Cancel gracefully shuts down the DBOS runtime by canceling the context, waiting for | ||
| // all workflows to complete, and cleaning up system resources including the database | ||
| // connection pool, queue runner, workflow scheduler, and admin server. | ||
| // All workflows and steps contexts will be canceled, which one can check using their context's Done() method. | ||
| // Shutdown gracefully shuts down the DBOS runtime by performing a complete, ordered cleanup | ||
| // of all system components. The shutdown sequence includes: | ||
| // | ||
| // This method blocks until all workflows finish and all resources are properly cleaned up. | ||
| // It should be called when the application is shutting down to ensure data consistency. | ||
| func (c *dbosContext) Cancel() { | ||
| // 1. Calls Cancel to stop workflows and cancel the context | ||
| // 2. Waits for the queue runner to complete processing | ||
| // 3. Stops the workflow scheduler and waits for scheduled jobs to finish | ||
| // 4. Shuts down the system database connection pool and notification listener | ||
| // 5. Shuts down the admin server | ||
| // 6. Marks the context as not launched | ||
| // | ||
| // Each step respects the provided timeout. If any component doesn't shut down within the timeout, | ||
| // a warning is logged and the shutdown continues to the next component. | ||
| // | ||
| // Shutdown is a permanent operation and should be called when the application is terminating. | ||
| func (c *dbosContext) Shutdown(timeout time.Duration) { | ||
| c.logger.Info("Shutting down DBOS context") | ||
|
|
||
| // Cancel the context to signal all resources to stop | ||
| c.ctxCancelFunc(errors.New("DBOS shutdown initiated")) | ||
| c.ctxCancelFunc(errors.New("DBOS cancellation initiated")) | ||
|
|
||
| // Wait for all workflows to finish | ||
| c.logger.Info("Waiting for all workflows to finish") | ||
| c.workflowsWg.Wait() | ||
| c.logger.Info("All workflows completed") | ||
| done := make(chan struct{}) | ||
maxdml marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| go func() { | ||
| c.workflowsWg.Wait() | ||
| close(done) | ||
| }() | ||
| select { | ||
| case <-done: | ||
| c.logger.Info("All workflows completed") | ||
| case <-time.After(timeout): | ||
| // For now just log a warning: eventually we might want Cancel to return an error. | ||
| c.logger.Warn("Timeout waiting for workflows to complete", "timeout", timeout) | ||
| } | ||
|
|
||
| // Close the pool and the notification listener if started | ||
| if c.systemDB != nil { | ||
| c.logger.Info("Shutting down system database") | ||
| c.systemDB.shutdown(c) | ||
| c.systemDB = nil | ||
| // Wait for queue runner to finish | ||
| if c.queueRunner != nil && c.launched.Load() { | ||
| c.logger.Info("Waiting for queue runner to complete") | ||
| select { | ||
| case <-c.queueRunner.completionChan: | ||
| c.logger.Info("Queue runner completed") | ||
| c.queueRunner = nil | ||
| case <-time.After(timeout): | ||
| c.logger.Warn("Timeout waiting for queue runner to complete", "timeout", timeout) | ||
|
Comment on lines
+415
to
+418
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we cannot safely set the pointer to |
||
| } | ||
| } | ||
|
|
||
| if c.launched.Load() { | ||
| // Wait for queue runner to finish | ||
| <-c.queueRunner.completionChan | ||
| c.logger.Info("Queue runner completed") | ||
|
|
||
| if c.workflowScheduler != nil { | ||
| c.logger.Info("Stopping workflow scheduler") | ||
| ctx := c.workflowScheduler.Stop() | ||
| // Wait for all running jobs to complete with 5-second timeout | ||
| timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second) | ||
| defer cancel() | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| c.logger.Info("All scheduled jobs completed") | ||
| case <-timeoutCtx.Done(): | ||
| c.logger.Warn("Timeout waiting for jobs to complete. Moving on", "timeout", "5s") | ||
| } | ||
| // Stop the workflow scheduler and wait until all scheduled workflows are done | ||
| if c.workflowScheduler != nil && c.launched.Load() { | ||
| c.logger.Info("Stopping workflow scheduler") | ||
| ctx := c.workflowScheduler.Stop() | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| c.logger.Info("All scheduled jobs completed") | ||
| c.workflowScheduler = nil | ||
| case <-time.After(timeout): | ||
| c.logger.Warn("Timeout waiting for jobs to complete. Moving on", "timeout", timeout) | ||
| } | ||
| } | ||
|
|
||
| if c.adminServer != nil { | ||
| c.logger.Info("Shutting down admin server") | ||
| err := c.adminServer.Shutdown(c) | ||
| if err != nil { | ||
| c.logger.Error("Failed to shutdown admin server", "error", err) | ||
| } else { | ||
| c.logger.Info("Admin server shutdown complete") | ||
| } | ||
| c.adminServer = nil | ||
| // Shutdown the admin server | ||
| if c.adminServer != nil && c.launched.Load() { | ||
| c.logger.Info("Shutting down admin server") | ||
| err := c.adminServer.Shutdown(timeout) | ||
| if err != nil { | ||
| c.logger.Error("Failed to shutdown admin server", "error", err) | ||
| } else { | ||
| c.logger.Info("Admin server shutdown complete") | ||
| } | ||
| c.adminServer = nil | ||
| } | ||
|
|
||
| // Close the system database | ||
| if c.systemDB != nil { | ||
| c.logger.Info("Shutting down system database") | ||
| c.systemDB.shutdown(c, timeout) | ||
| c.systemDB = nil | ||
| } | ||
|
|
||
| c.launched.Store(false) | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out that
Shutdownonhttp.Serveris non blocking and graceful, i.e., it doesn't terminate existing connections. Waiting on the goroutine that started it to be done is a clean(er) way to wait for the server's full termination.