etcd

package
v0.0.0-...-b1ea8ad Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2020 License: Apache-2.0 Imports: 25 Imported by: 4

Documentation

Index

Constants

View Source
const (
	KindCommon   = iota
	KindAlone    // 任何时间段只允许单机执行
	KindInterval // 一个任务执行间隔内允许执行一次
)
View Source
const (
	Coll_Node = "node"
)
View Source
const (
	DefaultJobGroup = "default"
)

Variables

This section is empty.

Functions

func DeleteGroupById

func DeleteGroupById(id string) (*client.DeleteResponse, error)

func DeleteJob

func DeleteJob(group, id string) (resp *client.DeleteResponse, err error)

func GetGroups

func GetGroups(nid string) (groups map[string]*Group, err error)

GetGroups 获取包含 nid 的 group 如果 nid 为空,则获取所有的 group

func GetIDFromKey

func GetIDFromKey(key string) string

从 etcd 的 key 中取 id

func GetJobs

func GetJobs() (jobs map[string]*Job, err error)

func GroupKey

func GroupKey(id string) string

func ISNodeAlive

func ISNodeAlive(id string) (bool, error)

func InitEtcdClient

func InitEtcdClient(cfg *conf.Conf) error

func IsValidAsKeyPath

func IsValidAsKeyPath(s string) bool

func JobKey

func JobKey(group, id string) string

func NewEtcdTimeoutContext

func NewEtcdTimeoutContext(c *Client) (context.Context, context.CancelFunc)

NewEtcdTimeoutContext return a new etcdTimeoutContext

func RemoveNode

func RemoveNode(query interface{}) error

func WatchGroups

func WatchGroups() client.WatchChan

func WatchJobs

func WatchJobs() client.WatchChan

func WatchNode

func WatchNode() client.WatchChan

Types

type Client

type Client struct {
	*client.Client
	// contains filtered or unexported fields
}
var (
	DefalutClient      *Client
	ErrValueMayChanged = errors.New("The value has been changed by others on this time.")
)

func (*Client) DelLock

func (c *Client) DelLock(key string) error

func (*Client) Delete

func (c *Client) Delete(key string, opts ...client.OpOption) (*client.DeleteResponse, error)

func (*Client) Get

func (c *Client) Get(key string, opts ...client.OpOption) (*client.GetResponse, error)

func (*Client) GetLock

func (c *Client) GetLock(key string, id client.LeaseID) (bool, error)

func (*Client) Grant

func (c *Client) Grant(ttl int64) (*client.LeaseGrantResponse, error)

func (*Client) KeepAliveOnce

func (c *Client) KeepAliveOnce(id client.LeaseID) (*client.LeaseKeepAliveResponse, error)

func (*Client) Put

func (c *Client) Put(key, val string, opts ...client.OpOption) (*client.PutResponse, error)

func (*Client) PutWithModRev

func (c *Client) PutWithModRev(key, val string, rev int64) (*client.PutResponse, error)

func (*Client) Revoke

func (*Client) Watch

func (c *Client) Watch(key string, opts ...client.OpOption) client.WatchChan

type Cmd

type Cmd struct {
	*Job
	*JobRule
}

func (*Cmd) GetID

func (c *Cmd) GetID() string

func (*Cmd) Run

func (c *Cmd) Run()

type Group

type Group struct {
	ID   string `json:"id"`
	Name string `json:"name"`

	NodeIDs []string `json:"nids"`
}

结点类型分组 注册到 /cronsun/group/<id>

func GetGroupById

func GetGroupById(gid string) (g *Group, err error)

func GetGroupFromKv

func GetGroupFromKv(key, value []byte) (g *Group, err error)

func GetNodeGroups

func GetNodeGroups() (list []*Group, err error)

func (*Group) Check

func (g *Group) Check() error

func (*Group) Included

func (g *Group) Included(nid string) bool

func (*Group) Key

func (g *Group) Key() string

func (*Group) Put

func (g *Group) Put(modRev int64) (*client.PutResponse, error)

type Job

type Job struct {
	ID      string     `json:"id"`
	Name    string     `json:"name"`
	Group   string     `json:"group"`
	Command string     `json:"cmd"`
	User    string     `json:"user"`
	Rules   []*JobRule `json:"rules"`
	Pause   bool       `json:"pause"`   // 可手工控制的状态
	Timeout int64      `json:"timeout"` // 任务执行时间超时设置,大于 0 时有效
	// 设置任务在单个节点上可以同时允许多少个
	// 针对两次任务执行间隔比任务执行时间要长的任务启用
	Parallels int64 `json:"parallels"`
	// 执行任务失败重试次数
	// 默认为 0,不重试
	Retry int `json:"retry"`
	// 执行任务失败重试时间间隔
	// 单位秒,如果不大于 0 则马上重试
	Interval int `json:"interval"`
	// 任务类型
	// 0: 普通任务
	// 1: 单机任务
	// 如果为单机任务,node 加载任务的时候 Parallels 设置 1
	Kind int `json:"kind"`
	// 平均执行时间,单位 ms
	AvgTime int64 `json:"avg_time"`
	// 执行失败发送通知
	FailNotify bool `json:"fail_notify"`
	// 发送通知地址
	To []string `json:"to"`
	// 单独对任务指定日志清除时间
	LogExpiration int `json:"log_expiration"`

	// 执行任务的结点,用于记录 job log
	RunOn    string
	Hostname string
	Ip       string

	// 控制同时执行任务数
	Count *int64 `json:"-"`
	// contains filtered or unexported fields
}

需要执行的 cron cmd 命令 注册到 /cronsun/cmd/groupName/<id>

func GetJob

func GetJob(group, id string) (job *Job, err error)

Note: this function did't check the job.

func GetJobAndRev

func GetJobAndRev(group, id string) (job *Job, rev int64, err error)

func GetJobFromKv

func GetJobFromKv(key, value []byte) (job *Job, err error)

func (*Job) Init

func (j *Job) Init(nodeID, hostname, ip string)

func (*Job) Key

func (j *Job) Key() string

func (*Job) Notify

func (j *Job) Notify(t time.Time, msg string)

func (*Job) String

func (j *Job) String() string

type JobRule

type JobRule struct {
	ID             string   `json:"id"`
	Timer          string   `json:"timer"`
	GroupIDs       []string `json:"gids"`
	NodeIDs        []string `json:"nids"`
	ExcludeNodeIDs []string `json:"exclude_nids"`

	Schedule cron.Schedule `json:"-"`
}

func (*JobRule) Valid

func (rule *JobRule) Valid() error

验证 timer 字段

type Node

type Node struct {
	ID       string `bson:"_id" json:"id"`  // machine id
	PID      string `bson:"pid" json:"pid"` // 进程 pid
	PIDFile  string `bson:"-" json:"-"`
	IP       string `bson:"Ip" json:"Ip"` // node Ip
	Hostname string `bson:"Hostname" json:"Hostname"`

	Version  string    `bson:"version" json:"version"`
	UpTime   time.Time `bson:"up" json:"up"`     // 启动时间
	DownTime time.Time `bson:"down" json:"down"` // 上次关闭时间

	Alived    bool `bson:"alived" json:"alived"` // 是否可用
	Connected bool `bson:"-" json:"connected"`   // 当 Alived 为 true 时有效,表示心跳是否正常
}

执行 cron cmd 的进程 注册到 /cronsun/node/<id>

func GetNodes

func GetNodes() (nodes []*Node, err error)

func GetNodesBy

func GetNodesBy(query interface{}) (nodes []*Node, err error)

func GetNodesByID

func GetNodesByID(id string) (node *Node, err error)

func (*Node) Del

func (n *Node) Del() (*client.DeleteResponse, error)

func (*Node) Down

func (n *Node) Down()

On 结点实例停用后,在 mongoDB 中去掉存活信息

func (*Node) Exist

func (n *Node) Exist() (pid int, err error)

判断 node 是否已注册到 etcd 存在则返回进行 pid,不存在返回 -1

func (*Node) On

func (n *Node) On()

On 结点实例启动后,在 mongoDB 中记录存活信息

func (*Node) Put

func (n *Node) Put(opts ...client.OpOption) (*client.PutResponse, error)

func (*Node) RmOldInfo

func (n *Node) RmOldInfo()

RmOldInfo remove old version(< 0.3.0) node info

func (*Node) String

func (n *Node) String() string

func (*Node) SyncToMgo

func (n *Node) SyncToMgo()

Jump to

Keyboard shortcuts

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