taskeeper

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 11, 2020 License: MIT Imports: 22 Imported by: 0

README

taskeeper 进程管理器

类似supervisord的进程管理器

不需要环境支持,只要有命令文件即可,支持多平台

功能设计
  • 对常驻类型进程进行保活
  • 配置定时任务,定时启动脚本
安装
$ mkdir ~/taskeeper
$ export GOPATH=~/taskeeper
$ go get -u github.com/kasiss-liu/taskeeper
$ go install github.com/kasiss-liu/taskeeper/keeper
$ go install github.com/kasiss-liu/taskeeper/keeperctl
配置
# 使用yaml文件作为配置文件
cat github.com/kasiss-liu/taskeeper/config/config.yml
# 主程序日志打印位置 不需要保存日志可以配置为 `/dev/null`
log: ""           //如果配置项为空输出会打印到 stdout

# 服务启动时会开启一个tcp服务,接收管理客户端信号
host: ""          //默认主机 127.0.0.1 如果配置为空 将允许远程控制 否则需要删除host行
port: ""          //默认端口 17101

# 配置工作目录,如果程序运行时遇到相对路径,会以此项作为前缀补充为绝对路径 
workdir: ""

# 常驻进程异常中断重试次数 
# 如果在在5秒内 进程启动次数超过该配置,子命令将不再启动并标记失败
broken_gap: 10

# 命令列表
cmds:
 - 
  //子命令具体地址,建议配置为绝对路径 否则将根据workdir配置进行补充
  cmd: "test/test"
  //命令启动的参数
  args: 
   - "arg1"
   - "arg2"
  //该命令的输出打印位置 如果为空,将打印到主程序的输出位置  
  //如果为相对路径则会进行补充
  output: "test/cmd.test.log" 
 - 
  cmd: "test/cron_test"
  output: "test/cron.test.log"
  //如果该命令是定时任务 需要配置cron表达式
  cron: "* * * * *"
启动
keeper -c config.yml -d

启动参数

keeper -h

Usage of keeper:
  -c string
    	config file in Yaml Format (default "config/config.yml")
  -d	is run in deamonize
  -flog
    	is force to print log
  -pprof
    	show runtime for testing
  -w string
    	keeper work absolute dir

管理客户端

keeperctl 

Usage of keeperctl:
  -cat string
    	cat cmd status
  -h string
    	service hostname : 127.0.0.1
  -p string
    	service port : 17101
  -s string
    	ctl signal 'exit' , 'reload'
# 查看所有配置命令
keeperctl -cat cmdlist
# 查看单个命令运行状态 {cmdid前缀匹配}
keeperctl -cat cmd {cmdId} 
# 查看服务主进程状态 
keeperctl -cat status
# 重载配置
keeperctl -s reload 
# 停止服务
keeperctl -s exit 

Documentation

Index

Constants

View Source
const (
	//ActReload 重新读取配置 并重新启动所有协程
	ActReload = sigReload
	//ActExit 管理进程退出
	ActExit = sigExit
	//ActStart 启动协程
	ActStart = sigStart
	//ActExec 单独执行命令
	ActExec = sigExec
	//ActPause 终端所有任务
	ActPause = sigPause
)
View Source
const (
	ErrResCodeNo  = iota //无错误 0
	ErrResWrgMsg         //消息结构不正确 1
	ErrResUdfCtl         //未定义的操作命令 2
	ErrResMissCmd        //缺少命令ID 3
	ErrResStatNil        //未获取到合法的参数 4
	ErrResWrgSig         //未定义的信号 5
	ErrResCtlSig         //缺少需要重启的命令id 6
)

定义一些常量 错误编号

View Source
const (
	MsgSigCtl  = "ctl"  //控制
	MsgSigStat = "stat" //查询
)

客户端操作命令常量

View Source
const (
	//DefaultBrokenGap 默认的中断容忍间隔
	DefaultBrokenGap int64 = 5
	//DefaultHost 默认的tcp 主机地址
	DefaultHost = "127.0.0.1"
	//DefaultPort 默认的tcp 端口
	DefaultPort = "17101"
	//UnixSysRunDir uinx系的运行目录
	UnixSysRunDir = "/var/run/"
	//UnixSysTmpDir unix系的临时目录
	UnixSysTmpDir = "/tmp/"
)

Variables

View Source
var (
	//SigMap 信号map
	SigMap map[string]int
	//StatArgsMap 信号参数map
	StatArgsMap []string
)
View Source
var (

	//DefaultLogPath 默认主程序日志打印位置
	DefaultLogPath string

	//MainPid 主程序pid
	MainPid int

	//AutoStart 自动启动命令
	AutoStart bool
)
View Source
var (
	//StateCopy 监控服务状态的备份 用于返回客户端查询请求
	StateCopy CopyState
	//StartTime 监控服务启动时间
	StartTime int64
	//ReloadTime 监控服务重载的时间点
	ReloadTime []int64
)
View Source
var ErrMsgMap = []string{
	"success",
	"wrong message",
	"undefined ctl type",
	"miss cmd id",
	"found nil args",
	"undefined signal",
	"undefined restart cmd",
}

ErrMsgMap 错误编号对应的消息数组

Functions

func GetChildPidsFile

func GetChildPidsFile() string

GetChildPidsFile 获取主程序控制的子程序pid文件储存路径

func GetParentDir

func GetParentDir(p string) string

GetParentDir 获取父级目录地址

func GetPidFile

func GetPidFile() string

GetPidFile 获取主程序pid文件的储存路径

func GetTCPAddr

func GetTCPAddr() string

GetTCPAddr 获取tcp启动地址

func Run

func Run()

Run 程序启动 子程序 启动、监控并等待操作信号

func SetWorkDir

func SetWorkDir(dir string) bool

SetWorkDir 外部设置工作目录 如果是绝对路径 直接赋值 如果是相对路径 则按照当前目录为起始获取绝对路径

func Start

func Start(configPath string, deamon, forceLog bool)

Start 启动服务

Types

type CmdStatus

type CmdStatus struct {
	ID         string `json:"id"`               //命令id
	Name       string `json:"name"`             //命令名称
	Pid        int    `json:"pid"`              //命令pid
	Cmd        string `json:"cmd"`              //命令的启动参数
	Output     string `json:"output"`           //命令输出的打印位置
	BkTimes    int    `json:"brokens"`          //中断次数
	LastBkTime string `json:"last_broken_time"` //上一次中断的时间
	IsCron     bool   `json:"is_cron"`          //是否是cron
}

CmdStatus 单个子程序的运行状态信息

type Command

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

Command 执行命令的配置结构 重新封装了cmd

func NewCommand

func NewCommand(cmd string, args []string, output string) *Command

NewCommand 返回一个等待执行的cmd结构体

func (*Command) ID

func (c *Command) ID() string

ID 获取命令字符串Id 创建时随机分配

func (*Command) IsCron

func (c *Command) IsCron() bool

IsCron 验证是否是cron命令

func (*Command) IsPause

func (c *Command) IsPause() bool

IsPause 返回是否暂停

func (*Command) Kill

func (c *Command) Kill() error

Kill 杀死进程

func (*Command) Name

func (c *Command) Name() string

Name 获取命令的名称

func (*Command) Output

func (c *Command) Output() string

Output 获取命令输出打印位置

func (*Command) Pid

func (c *Command) Pid() int

Pid 获取pid

func (*Command) Process

func (c *Command) Process() *os.Process

Process 获取进程结构指针

func (*Command) Release

func (c *Command) Release() error

Release 释放进程资源 释放以后 不能对进程进行任何操作

func (*Command) ResetPid

func (c *Command) ResetPid()

ResetPid 重置命令pid 用于程序退出后标记

func (*Command) SetCron

func (c *Command) SetCron(express string) *Command

SetCron 设置命令为cron命令

func (*Command) SetID

func (c *Command) SetID(id string)

SetID 手动设置一个id

func (*Command) SetName

func (c *Command) SetName(name string) *Command

SetName 设置命令的名称

func (*Command) SetPause

func (c *Command) SetPause()

SetPause 设置命令暂停运行

func (*Command) SetRun

func (c *Command) SetRun()

SetRun 设置命令正常运行

func (*Command) Singal

func (c *Command) Singal(sig os.Signal) error

Singal 向进程传递信号

func (*Command) Start

func (c *Command) Start() int

Start 命令启动

func (*Command) TurnOffCron

func (c *Command) TurnOffCron() bool

TurnOffCron 主动关闭cron

func (*Command) TurnOnCron

func (c *Command) TurnOnCron() bool

TurnOnCron 主动开启cron

func (*Command) Wait

func (c *Command) Wait() (*os.ProcessState, error)

Wait 等待进程执行完毕 进程结束后 会释放进程资源

type CopyState

type CopyState struct {
	TasksNum   int //程序总的运行数量
	RunningNum int //正在运行的命令数
	BrokenNum  int //由于崩溃或结束运行的命令数

	RunningList  map[string]*Command //正在运行的命令map
	BrokenList   map[string]*Command //运行中断的命令map
	BrokenTries  map[string]int      //命令中断后在容忍间隔时间内的重试次数
	BrokenPoints map[string]int64    //命令中断的时间点
	CronState    bool                //是否已经开启cron协程
	SecCronList  map[string]*Command //秒级cron列表
	MinCronList  map[string]*Command //分钟级cron列表
}

CopyState 运行状态结构备份

type ProcessConfig

type ProcessConfig struct {
	//ConfigPath 启动时使用的配置文件
	ConfigPath string `json:"conf_path"`
	//TCPAddr Tcp启动地址
	TCPAddr string `json:"tcp_addr"`
	//PidFile Pid文件地址
	PidFile string `json:"pid_file"`

	//SockFile sock文件存储路径
	SockFile string `json:"sock_file"`
	//ChdFile 子进程pid统一存储路径
	ChdFile string `json:"child_pids"`
	//LogFile 主程序日志打印位置
	LogFile string `json:"log_file"`
	// contains filtered or unexported fields
}

ProcessConfig 主程序配置结构

func ParsePidDesc

func ParsePidDesc() (ProcessConfig, error)

ParsePidDesc 获取主进程的描述信息

type RunningStatus

type RunningStatus struct {
	Pid            int      `json:"main_pid"`          //主程序pid
	StartTime      string   `json:"start_time"`        //主程序启动时间
	ReloadTime     []string `json:"reload_time_list"`  //主程序重载配置时间列表
	TotalTasks     int      `json:"task_total_num"`    //可以启动的子程序总数
	RunningTasks   []string `json:"running_task_list"` //正在运行的子程序命令集合
	TermTasks      []string `json:"term_task_list"`    //中断的子程序命令集合
	RunningSeconds string   `json:"running_seconds"`   //程序运行时间

	CronState   bool     `json:"cron_state"`       //是否已经开启cron协程
	SecCronList []string `json:"second_cron_list"` //秒级cron列表
	MinCronList []string `json:"minute_cron_list"` //分钟级cron列表
}

RunningStatus 服务状态

type State

type State struct {
	TasksNum   int //程序总的运行数量
	RunningNum int //正在运行的命令数
	BrokenNum  int //由于崩溃或结束运行的命令数

	RunningList  map[string]*Command //正在运行的命令map
	BrokenList   map[string]*Command //运行中断的命令map
	BrokenTries  map[string]int      //命令中断后在容忍间隔时间内的重试次数
	BrokenPoints map[string]int64    //命令中断的时间点
	Numlock      sync.Mutex          //操作各数量变更的锁

	CronState   bool                //是否已经开启cron协程
	SecCronList map[string]*Command //秒级cron列表
	MinCronList map[string]*Command //分钟级cron列表
	IsRun       bool                //是否已经开始运行
}

State 状态机

var (
	RunState *State //状态机实例

)

运行时的必要参数

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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