daemon

package
v0.0.0-...-3eafb10 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package daemon 实现应用进程启动命令、后台启动、信号处理、热重启的代码支持。

启动命令

app --command=start/status/stop/restart/deamon/disable --pidfile=/run/run/pidfile

command:

start	写入pid前台启动
daemon	写入pid后台启动
status	读取pid判断进程存在
stop	读取pid发送syscall.SIGTERM信号(15)
restart	读取pid发送syscall.SIGUSR2信号(12)
disable	跳过启动命令处理

后台启动

func main() {
	daemon.StartDaemon()
}

信号处理

热重启

使用command组件或kill命令发送SIGUSR2信号。

父进程接受SIGUSR2信号后,传递当前Listen FD和ppid后台启动子进程; 子进程启动初始化后完成向父进程发送SIGTERM信号关闭父进程。

Index

Constants

View Source
const (
	CommandStart   = "start"
	CommandDaemon  = "daemon"
	CommandStatus  = "status"
	CommandStop    = "stop"
	CommandRestart = "restart"
	CommandDisable = "disable"
)

Variables

This section is empty.

Functions

func AppRestart

func AppRestart(ctx context.Context) error

func AppStop

func AppStop(ctx context.Context) error

func NewParseDaemon

func NewParseDaemon(app *eudore.App) eudore.ConfigParseFunc

NewParseCommand 函数创建Command配置解析函数。

func NewParseRestart

func NewParseRestart() eudore.ConfigParseFunc

func StartDaemon

func StartDaemon(envs ...string)

StartDaemon 函数直接后台启动程序。

Types

type Command

type Command struct {
	Command string
	Pidfile string
	Args    []string
	Envs    []string
	Print   func(string, ...any)
}

Command is a command parser that performs the corresponding behavior based on the current command.

Command 对象是一个命令解析器,根据当前命令执行对应行为。

func (*Command) Daemon

func (cmd *Command) Daemon(ctx context.Context) error

Daemon Start the process in the background. If it is not started in the background, create a background process.

Daemon 函数后台启动进程。若不是后台启动,则创建一个后台进程。

func (*Command) ExecSignal

func (cmd *Command) ExecSignal(sig os.Signal) error

ExecSignal 函数向pidfile内的进程发送指定命令。

func (*Command) Reload

func (cmd *Command) Reload() error

Reload 函数调用系统命令,想进程发送信号syscall.SIGUSR1。

func (*Command) Restart

func (cmd *Command) Restart() error

Restart 函数调用系统命令,想进程发送信号syscall.SIGUSR2。

func (*Command) Run

func (cmd *Command) Run(ctx context.Context) (err error)

Run 方法启动Command解析。

func (*Command) Start

func (cmd *Command) Start(ctx context.Context) error

Start execute the startup function and write the pid to the file.

Start 函数执行启动函数,并将pid写入文件。

func (*Command) Status

func (cmd *Command) Status() error

Status 函数调用系统命令,想进程发送信号 0。

func (*Command) Stop

func (cmd *Command) Stop() error

Stop 函数调用系统命令,想进程发送信号syscall.SIGTERM。

func (*Command) Wait

func (cmd *Command) Wait(p int)

type Signal

type Signal struct {
	sync.Mutex
	Chan  chan os.Signal
	Funcs map[os.Signal][]SignalFunc
}

func (*Signal) Handle

func (sig *Signal) Handle(ctx context.Context, s os.Signal) error

func (*Signal) Notify

func (sig *Signal) Notify()

func (*Signal) Register

func (sig *Signal) Register(s os.Signal, fn SignalFunc)

func (*Signal) Run

func (sig *Signal) Run(ctx context.Context)

type SignalFunc

type SignalFunc func(context.Context) error

Signal handle func.

Jump to

Keyboard shortcuts

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