Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RunTaskWithContextCancellationCheck ¶
func RunTaskWithContextCancellationCheck(rootCtx context.Context, task func(cancelCtx context.Context, terminateSignal chan struct{}) error)
RunTaskWithContextCancellationCheck executes a task and provides a mechanism to notify the task of impending cancellation. This allows the task to perform cleanup or error handling before the context is cancelled. The function listens for system signals (SIGTERM, SIGINT) and sends a termination signal to the task, allowing it to gracefully terminate before the context is cancelled.
Parameters:
- rootCtx: The parent context.
- task: A function that represents the task to be executed. It takes a cancelCtx and a termination signal channel as arguments.
Usage:
shutdown.RunTaskWithContextCancellationCheck(context.Background(), func(cancelCtx context.Context, terminateSignal chan struct{}) error { for { select { case <-cancelCtx.Done(): fmt.Println("Context cancelled") return cancelCtx.Err() case <-terminateSignal: fmt.Println("Received termination signal") // Perform cleanup or handle termination return nil } } })
func WaitForShutdown ¶
func WaitForShutdown(rootCtx context.Context, timeoutMilli int64, cleanupCallback func(timeoutCtx context.Context))
WaitForShutdown waits for OS signals (SIGINT, SIGTERM) to gracefully shut down the application. It runs the cleanup code provided by the cleanupCallback function within a context with a specified timeout.
Parameters:
- rootCtx: The parent context.
- timeoutMilli: The timeout duration in milliseconds to wait for the cleanup callback to complete.
- cleanupCallback: A function that contains the cleanup code to execute during shutdown, and that takes a timeoutCtx.
Usage:
shutdown.WaitForShutdown(context.Background(), 5000, func(timeoutCtx context.Context) { // Cleanup code here fmt.Println("Cleaning up resources...") })
Types ¶
This section is empty.