kkdaemon

package module
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2024 License: Apache-2.0 Imports: 13 Imported by: 3

README

goth-daemon

daemon process management with timer job support and stop all when kill signal raised.

HOWTO

// register daemon
kkdaemon.RegisterDaemon(daemons.DaemonSetupEnvironment)
kkdaemon.RegisterDaemon(daemons.DaemonSetupLogger)

// start daemon service
kkdaemon.Start()

// wait process killed
kkdaemon.ShutdownFuture().Await()

// or trigger `GracefullyShutdown` manually
go func(){
    <-time.After(time.Minute)
    kkdaemon.ShutdownGracefully()
}()

Define Daemon

DefaultDaemon

use default daemon to execute function when daemon service Start and Stop

type DefaultDaemonExample struct {
kkdaemon.DefaultTimerDaemon
}

func (d *DefaultDaemonExample) Registered() error {
// init func
return nil
}

func (d *DefaultDaemonExample) Start() {
// do when start
}

func (d *DefaultDaemonExample) Stop(sig os.Signal) {
// do when stop
}

TimerDaemon

timer daemon provide Loop func to be execute every Interval duration

type TimerDaemonExample struct {
kkdaemon.DefaultTimerDaemon
}

func (d *TimerDaemonExample) Registered() error {
// init func
return nil
}

func (d *TimerDaemonExample) Start() {
// do when start
}

func (d *TimerDaemonExample) Interval() time.Duration {
// run every minute
return time.Minute
}

func (d *TimerDaemonExample) Loop() error {
// do something
return nil
}

func (d *TimerDaemonExample) Stop(sig os.Signal) {
// do when stop
}

SchedulerDaemon

scheduler daemon use cron syntax to manage daemon Loop when be executed.

type SchedulerDaemonExample struct {
kkdaemon.DefaultSchedulerDaemon
}

func (d *SchedulerDaemonExample) Registered() error {
// init func
return nil
}

func (d *SchedulerDaemonExample) Start() {
// do when start
}

func (d *SchedulerDaemonExample) When() kkdaemon.CronSyntax {
// run every two minute
return "*/2 * * * *"
}

func (d *SchedulerDaemonExample) Loop() error {
// do something
return nil
}

func (d *SchedulerDaemonExample) Stop(sig os.Signal) {
// do when stop
}

Documentation

Index

Constants

View Source
const StateRun = int32(2)
View Source
const StateStart = int32(1)
View Source
const StateStop = int32(3)
View Source
const StateWait = int32(0)

Variables

View Source
var DefaultService = NewDaemonService()

Functions

func IsShutdown added in v1.1.3

func IsShutdown() bool

func RegisterDaemon added in v1.1.0

func RegisterDaemon(daemon Daemon) error

func RegisterSimpleDaemon added in v1.3.0

func RegisterSimpleDaemon(name string, startFunc func(), stopFunc func(sig os.Signal)) error

func ShutdownFuture added in v1.3.0

func ShutdownFuture() concurrent.Future

func ShutdownGracefully added in v1.2.0

func ShutdownGracefully()

func Start

func Start() error

func StartDaemon added in v1.4.0

func StartDaemon(name string) error

func Stop

func Stop(sig os.Signal) error

func StopDaemon added in v1.4.0

func StopDaemon(name string, sig os.Signal) error

func UnregisterDaemon added in v1.3.0

func UnregisterDaemon(name string) error

Types

type CronSyntax added in v1.3.0

type CronSyntax string

func (CronSyntax) Next added in v1.3.0

func (c CronSyntax) Next(from time.Time) time.Time

type Daemon added in v1.1.0

type Daemon interface {
	Registered() error
	State() int32
	Start()
	Stop(sig os.Signal)
	Name() string
	// contains filtered or unexported methods
}

type DaemonEntity added in v1.1.0

type DaemonEntity struct {
	Name   string
	Daemon Daemon
	Order  int
	Next   time.Time
}

func GetDaemon added in v1.3.0

func GetDaemon(name string) *DaemonEntity

type DaemonService added in v1.3.0

type DaemonService struct {
	// daemons map
	DaemonMap sync.Map
	// stop all daemon when get kill signal, default: `true`
	StopWhenKill bool
	// contains filtered or unexported fields
}

func NewDaemonService added in v1.3.0

func NewDaemonService() *DaemonService

func (*DaemonService) GetDaemon added in v1.3.0

func (s *DaemonService) GetDaemon(name string) *DaemonEntity

func (*DaemonService) IsShutdown added in v1.3.0

func (s *DaemonService) IsShutdown() bool

func (*DaemonService) RegisterDaemon added in v1.3.0

func (s *DaemonService) RegisterDaemon(daemon Daemon) error

func (*DaemonService) ShutdownFuture added in v1.3.0

func (s *DaemonService) ShutdownFuture() concurrent.Future

func (*DaemonService) ShutdownGracefully added in v1.3.0

func (s *DaemonService) ShutdownGracefully()

func (*DaemonService) Start added in v1.3.0

func (s *DaemonService) Start() error

func (*DaemonService) StartDaemon added in v1.4.0

func (s *DaemonService) StartDaemon(entity *DaemonEntity) kkpanic.Caught

func (*DaemonService) Stop added in v1.3.0

func (s *DaemonService) Stop(sig os.Signal) error

func (*DaemonService) StopDaemon added in v1.4.0

func (s *DaemonService) StopDaemon(entity *DaemonEntity, sig os.Signal) kkpanic.Caught

func (*DaemonService) UnregisterDaemon added in v1.3.0

func (s *DaemonService) UnregisterDaemon(name string) error

type DefaultDaemon added in v1.1.0

type DefaultDaemon struct {
	Params map[string]interface{}
	// contains filtered or unexported fields
}

func (*DefaultDaemon) Name added in v1.1.6

func (d *DefaultDaemon) Name() string

func (*DefaultDaemon) Registered added in v1.1.5

func (d *DefaultDaemon) Registered() error

func (*DefaultDaemon) Start added in v1.1.0

func (d *DefaultDaemon) Start()

func (*DefaultDaemon) State added in v1.2.18

func (d *DefaultDaemon) State() int32

func (*DefaultDaemon) Stop added in v1.1.0

func (d *DefaultDaemon) Stop(sig os.Signal)

type DefaultSchedulerDaemon added in v1.3.0

type DefaultSchedulerDaemon struct {
	DefaultDaemon
}

func (*DefaultSchedulerDaemon) Loop added in v1.3.0

func (d *DefaultSchedulerDaemon) Loop() error

func (*DefaultSchedulerDaemon) When added in v1.3.0

type DefaultTimerDaemon added in v1.2.7

type DefaultTimerDaemon struct {
	DefaultDaemon
}

func (*DefaultTimerDaemon) Interval added in v1.2.18

func (d *DefaultTimerDaemon) Interval() time.Duration

func (*DefaultTimerDaemon) Loop added in v1.2.7

func (d *DefaultTimerDaemon) Loop() error

type Looper added in v1.3.0

type Looper interface {
	Loop() error
}

type PanicResult

type PanicResult struct {
	Daemon Daemon
	Caught kkpanic.Caught
}

type SchedulerDaemon added in v1.3.0

type SchedulerDaemon interface {
	Daemon
	Looper
	When() CronSyntax
}

type TimerDaemon added in v1.2.7

type TimerDaemon interface {
	Daemon
	Looper
	Interval() time.Duration
}

Jump to

Keyboard shortcuts

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