graceful

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: MIT Imports: 7 Imported by: 9

README

graceful

Run Tests codecov Go Report Card Go Reference

Graceful shutdown package when a service is turned off by software function.

Example

Add running job

package main

import (
  "context"
  "log"
  "time"

  "github.com/appleboy/graceful"
)

func main() {
  m := graceful.NewManager()

  // Add job 01
  m.AddRunningJob(func(ctx context.Context) error {
    for {
      select {
      case <-ctx.Done():
        return nil
      default:
        log.Println("working job 01")
        time.Sleep(1 * time.Second)
      }
    }
  })

  // Add job 02
  m.AddRunningJob(func(ctx context.Context) error {
    for {
      select {
      case <-ctx.Done():
        return nil
      default:
        log.Println("working job 02")
        time.Sleep(500 * time.Millisecond)
      }
    }
  })

  <-m.Done()
}

You can also add shutdown jobs.

package main

import (
  "context"
  "log"
  "time"

  "github.com/appleboy/graceful"
)

func main() {
  m := graceful.NewManager()

  // Add job 01
  m.AddRunningJob(func(ctx context.Context) error {
    for {
      select {
      case <-ctx.Done():
        return nil
      default:
        log.Println("working job 01")
        time.Sleep(1 * time.Second)
      }
    }
  })

  // Add job 02
  m.AddRunningJob(func(ctx context.Context) error {
    for {
      select {
      case <-ctx.Done():
        return nil
      default:
        log.Println("working job 02")
        time.Sleep(500 * time.Millisecond)
      }
    }
  })

  // Add shutdown 01
  m.AddShutdownJob(func() error {
    log.Println("shutdown job 01 and wait 1 second")
    time.Sleep(1 * time.Second)
    return nil
  })

  // Add shutdown 02
  m.AddShutdownJob(func() error {
    log.Println("shutdown job 02 and wait 2 second")
    time.Sleep(2 * time.Second)
    return nil
  })

  <-m.Done()
}

Using custom logger, see the zerolog example

m := graceful.NewManager(
  graceful.WithLogger(logger{}),
)

get more information

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger interface {
	Infof(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	Info(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
}

Logger interface is used throughout gorush

func NewEmptyLogger

func NewEmptyLogger() Logger

NewEmptyLogger for simple logger.

Example
l := NewEmptyLogger()
l.Info("test")
l.Infof("test")
l.Error("test")
l.Errorf("test")
l.Fatal("test")
l.Fatalf("test")
Output:

func NewLogger

func NewLogger() Logger

NewLogger for simple logger.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages the graceful shutdown process

func GetManager

func GetManager() *Manager

NewManager get the Manager

func NewManager

func NewManager(opts ...Option) *Manager

NewManager initial the Manager

func NewManagerWithContext added in v0.0.2

func NewManagerWithContext(ctx context.Context, opts ...Option) *Manager

NewManagerWithContext initial the Manager with custom context

func (*Manager) AddRunningJob

func (g *Manager) AddRunningJob(f RunningJob)

AddRunningJob add running task

func (*Manager) AddShutdownJob added in v0.0.2

func (g *Manager) AddShutdownJob(f ShtdownJob)

AddShutdownJob add shutdown task

func (*Manager) Done

func (g *Manager) Done() <-chan struct{}

Done allows the manager to be viewed as a context.Context.

func (*Manager) ShutdownContext added in v0.0.4

func (g *Manager) ShutdownContext() context.Context

ShutdownContext returns a context.Context that is Done at shutdown

type Option

type Option interface {
	Apply(*Options)
}

Option interface for configuration.

func WithContext

func WithContext(ctx context.Context) Option

WithContext custom context

func WithLogger

func WithLogger(logger Logger) Option

WithLogger custom logger

type OptionFunc added in v0.1.0

type OptionFunc func(*Options)

OptionFunc is a function that configures a graceful shutdown.

func (OptionFunc) Apply added in v0.1.0

func (f OptionFunc) Apply(option *Options)

Apply calls f(option)

type Options added in v0.1.0

type Options struct {
	// contains filtered or unexported fields
}

Options for graceful shutdown

type RunningJob

type RunningJob func(context.Context) error

type ShtdownJob added in v0.0.2

type ShtdownJob func() error

Jump to

Keyboard shortcuts

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