terminator

package module
v0.0.0-...-73f4b8f Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

GO Terminator - Graceful Process Termination Utility

Go Report Card Go Reference Test Cases License

The go-terminator package provides a utility in Go for gracefully terminating processes by closing registered resources when a termination signal is received.

Read Docs

Table of Contents

Introduction

When building long-running applications or services, it's important to ensure that resources are properly cleaned up when the process is terminated. The go-terminator package helps manage the graceful termination of your application by providing a simple mechanism to register resources that need to be closed when the process receives termination signals.

Installation

To use the go-terminator package, import it in your Go code:

go get "github.com/RohanPoojary/go-terminator"

Usage

Creating a Terminator

To create a new instance of the terminator, you need to specify the signals that should trigger the termination. The terminator listens for these signals and closes the registered resources when a signal is received.


import (
	"os"
	"github.com/RohanPoojary/go-terminator"
)

func main() {
	closeSignals := []os.Signal{os.Interrupt, os.Kill}
	term := terminator.NewTerminator(closeSignals)
}
Adding Resources

Resources that need to be closed gracefully can be registered with the terminator using the Add and AddWithTimeout methods. These methods take the resource name, a closing function, and an optional timeout duration.


term.Add("Database Connection", func(ctx context.Context) error {
	// Close the database connection gracefully.
	return db.Close()
})

term.AddWithTimeout("File Writer", func(ctx context.Context) error {
	// Close the file writer gracefully, allowing a maximum of 5 seconds for closure.
	return fileWriter.Close()
}, 5*time.Second)
Setting Callback

You can set a callback function that will be executed after all registered resources are closed. This can be useful for performing any final tasks or logging.


term.SetCallback(func(result terminator.TerminationResult) {
	fmt.Println("Termination completed. Result:", result)
})
Waiting for Termination

The Wait method allows you to wait for the termination process to complete with a specified timeout duration. Usually wrapped inside defer after initialisation.


success := term.Wait(10 * time.Second)
if success {
	fmt.Println("Termination completed successfully.")
} else {
	fmt.Println("Termination timed out.")
}
TerminationResult Structure

The TerminationResult structure provides information about the termination process:

  • Signal: The termination signal received.
  • Result: A slice of TerminationResultData containing information about each closed resource.

Complete Example


package main

import (
	"context"
	"fmt"
	"os"
	"time"

	"github.com/RohanPoojary/go-terminator"
)

func main() {
	// Create a new terminator instance with the specified termination signals.
	closeSignals := []os.Signal{os.Interrupt, os.Kill}
	term := terminator.NewTerminator(closeSignals)

	defer func() {
		fmt.Println("Waiting for signal upto 10 seconds...")
		// Wait for the termination process to complete.
		success := term.Wait(10 * time.Second)
		if success {
			fmt.Println("Termination completed successfully.")
		} else {
			fmt.Println("Termination timed out.")
		}
	}()

	// Register resources to be closed gracefully.
	term.Add("Database Connection", func(ctx context.Context) error {
		// Simulate closing the database connection.
		fmt.Println("Closing database connection...")
		time.Sleep(2 * time.Second)
		fmt.Println("Database connection closed.")
		return nil
	})

	term.Add("File Writer", func(ctx context.Context) error {
		// Simulate closing the file writer.
		fmt.Println("Closing file writer...")
		time.Sleep(1 * time.Second)
		fmt.Println("File writer closed.")
		return nil
	})

	// Set a callback function to execute after resources are closed.
	term.SetCallback(func(result terminator.TerminationResult) {
		fmt.Println("Termination completed. Result:", result)
	})

	// Your running application ...

	fmt.Println("Exiting the program.")
}


Contributing

Contributions are welcome! If you find any issues or have suggestions, please open an issue or submit a pull request on the GitHub repository.

License

This project is licensed under the Open Source Apache License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CloseFunc

type CloseFunc func(context.Context) error

CloseFunc defines the function signature for closing a resource.

type TerminationResult

type TerminationResult struct {

	// Termination signal received
	Signal os.Signal

	// Number of resources that failed or timed out
	FailedOrTimeoutCount int

	// Result data for each terminated resource
	Result []TerminationResultData
}

TerminationResult contains the overall result of the termination process.

type TerminationResultData

type TerminationResultData struct {

	// Name of the terminated resource
	Name string

	// Error that occurred during termination, if any
	Error error

	// Termination status of the process
	Status TerminationStatus
}

TerminationResultData holds information about the result of terminating a resource.

type TerminationStatus

type TerminationStatus string

TerminationStatus represents the status of termination (success or failure).

const (

	// SUCCESS indicates that the resource was closed successfully.
	SUCCESS TerminationStatus = "SUCCESS"

	// FAILED indicates that the resource failed to close.
	FAILED TerminationStatus = "FAILED"
)

type Terminator

type Terminator interface {

	// Add registers a resource to be closed without a timeout.
	Add(name string, close CloseFunc)

	// AddWithTimeout registers a resource to be closed with a specified timeout.
	AddWithTimeout(name string, close CloseFunc, timeout time.Duration)

	// SetCallback sets the callback function to be executed after all resources are closed.
	SetCallback(callback func(TerminationResult))

	// Wait waits for the termination process to complete within the specified timeout duration.
	Wait(timeout time.Duration) bool
}

Terminator is the interface that provides methods for managing resource termination.

func NewTerminator

func NewTerminator(closeSignals []os.Signal) Terminator

NewTerminator creates a new instance of the terminator.

Jump to

Keyboard shortcuts

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