shutdown

package
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2024 License: MIT Imports: 7 Imported by: 0

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL