frigate

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2021 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CANCEL_PROCESS   string = "cancel"
	COMPLETE_PROCESS string = "complete"
)
View Source
const (
	// 未知异常时重启
	UNEXPECTED = "unexpected"
	// 总是自动重启
	TRUE = "true"
	// 不自动重启
	FALSE = "false"
)

Variables

This section is empty.

Functions

func Protect

func Protect(f *Frigate)

对进程发起守护

func RegisterGolangTask

func RegisterGolangTask(name string, task func())

注册golang 任务函数,如果不注册golang函数,接下来 在执行golang任务函数之前需要先对任务函数进行注册 因为golang使用了pthread,所以不能正常使用fork()函数。故而强烈的推荐您,在项目 go没有类似C中的fork调用可以达到在fork之后根据返回的pid然后进入不同的函数的方案。原因主要是:

fork 早出现在只有进程,没有线程的年代 C中是自行控制线程,这样fork之后才不会发生紊乱。一般都是单线程fork之后,才会开始多线程执行。 Go中多线程是runtime自行决定的,所以Go中没有提供单纯的fork,而是fork之后立即就exec执行新的二进制文件

下面使用一个实例程序演示一下 RegisterGolangTask 函数的使用规范: 强烈推荐在init()函数里使用 RegisterGolangTask

func init()  {
	frigate.RegisterGolangTask("childTask", child)
}
func child()  {
	println("child pid := " + strconv.Itoa(syscall.Getpid()))
}
func main()  {
	// 使用刚刚注册的函数名声明一个 frigate
	f := frigate.Create("childTask")
	f.Protect()
	println("parent pid = " + strconv.Itoa(syscall.Getpid()))
	// 等待所有被保护的子进程执行完成后自身退出
	frigate.Done()
}

Types

type ApplyConfig

type ApplyConfig interface {
	/**
	 * 应用配置
	 * return: 0 成功
	 * 			其它失败
	 */
	Apply(cmd *exec.Cmd) error
}

*

  • 应用配置接口
  • 主要用于讲配置数据应用于cmd

type AutoRestartStrategy

type AutoRestartStrategy string

type Frigate

type Frigate struct {
	// config of task logger
	Log *logger.FLogger
	// control groups of resources,it only use on linux
	ControlGroups []*cgroup.ControlGroup

	ProtectTask *ProtectTask
	Strategy    *Strategy
	// 进程信号通道
	SignalChan chan error
}

func Create

func Create(name string) *Frigate

Create 创建守护的任务进程

1. 如果要以golang的函数作为子进程: name 代表 frigate 守护进程的标识,frigate 将使用这个名字创建子进程 注意如果要使用golang函数作为守护进程,那么函数需要提前注册 @see RegisterGolangTask

2. 如果要以可执行程序作为子进程: name 代表可执行程序的绝对路径或者相对路径

func (*Frigate) Apply

func (frigate *Frigate) Apply(cmd *exec.Cmd) (err error)

Apply 应用配置 *

  • 应用子进程配置

func (*Frigate) Start

func (frigate *Frigate) Start() (err error)

Start 启动守护进程 启动守护进程时会使用守护策略参数 启动进程后,会监听进程状态

func (*Frigate) Stop

func (frigate *Frigate) Stop(d time.Duration) (err error)

Stop 用户主动调用,关闭守护进程逻辑

type ProtectTask

type ProtectTask struct {
	// the task run in child process
	Cmd       *exec.Cmd
	Name      string
	Process   *os.Process
	StartTime time.Time
	// contains filtered or unexported fields
}

受保护任务定义

func NewProtectTask

func NewProtectTask(path string) *ProtectTask

创建受守护的进程执行对象 name : 如果是golang 的函数,则代表已经注册的函数名 如果是外部可执行程序,则代表程序绝对路径或相对路径

func (*ProtectTask) Done

func (t *ProtectTask) Done() <-chan error

*

  • 获取进程信号通道

func (*ProtectTask) Start

func (t *ProtectTask) Start() (err error)

Start *

  • 启动进程
  • 启动进程后,需要主动wait,等待子进程结束,接收SINGCHILD信号,否则子进程可能变成僵尸进程

func (*ProtectTask) Stop

func (t *ProtectTask) Stop(d time.Duration) (err error)

Stop 用户主动关闭进程

优先使用 SIGTERM 信号量优雅关闭进程 如果超时后还未能正常关闭进程,则使用 kill 信号强行关闭进程 d 关闭进程最大等大时间

type Runable

type Runable interface {
	// 启动
	Start() (err error)
	// 停止
	Stop(d time.Duration) (err error)
}

Runable 可运行程序接口

可运行的任务接口

type Strategy

type Strategy struct {
	//在 Frigated 启动的时候也自动启动
	AutoStart bool
	// 程序退出后自动重启,可选值:[true,false],默认为 true
	AutoRestart bool
	//启动n秒后没有异常退出,就表示进程正常启动了,默认为10秒
	// 如果这个值为0,则没有启动失败的概念,即只要发现进程未在运行,就触发重启
	Startsecs time.Duration
	// 启动失败自动重试次数,默认是3
	StartRetries int
	// todo 启动进程前,是否尝试关闭同名其它进程
	GraceClose bool
	// 关闭同名进程最大等待时间
	GraceCloseWait time.Duration
	// frigated 被关闭时,是否强杀掉该进程
	Kill bool
	// 启动用户
	User *user.User
	// chroot
	Chroot string
}

@author Wang Weiwei @since 2020/3/24 子任务守护策略

func (*Strategy) Apply

func (s *Strategy) Apply(cmd *exec.Cmd) (err error)

*

  • 在创建子进程时使用守护策略
  • 在创建进程时,子进程默认属于守护进程相同的进程组
  • 子进程默认与父进程是相同用户

Jump to

Keyboard shortcuts

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