common

package
v0.0.0-...-a4b71fe Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 任务保存目录
	JOB_SAVE_DIR = "/cron/jobs/"

	// 任务强杀目录
	JOB_KILLER_DIR = "/cron/killer/"

	// 任务锁目录
	JOB_LOCK_DIR = "/cron/lock/"

	// 服务注册目录
	JOB_WORKER_DIR = "/cron/workers/"

	// 保存任务事件
	JOB_EVENT_SAVE = 1

	// 删除任务事件
	JOB_EVENT_DELETE = 2

	// 强杀任务事件
	JOB_EVENT_KILL = 3
)

Variables

View Source
var (
	ERR_LOCK_ALREADY_REQUIRED = errors.New("锁已被占用")

	ERR_NO_LOCAL_IP_FOUND = errors.New("没有找到网卡IP")
)

Functions

func BuildResponse

func BuildResponse(errno int, msg string, data interface{}) (resp []byte, err error)

应答方法

func ExtractJobName

func ExtractJobName(jobKey string) string

从etcd的key中提取任务名 /cron/jobs/job10抹掉/cron/jobs/

func ExtractKillerName

func ExtractKillerName(killerKey string) string

从 /cron/killer/job10提取job10

func ExtractWorkerIP

func ExtractWorkerIP(regKey string) string

提取worker的IP

func InitMongo

func InitMongo(config MongoConfig) (*mongo.Client, error)

Types

type Etcd

type Etcd struct {
	Client  *clientv3.Client
	Kv      clientv3.KV
	Lease   clientv3.Lease
	Watcher clientv3.Watcher
}

func NewEtcd

func NewEtcd(etcdConfig *EtcdConfig) (*Etcd, error)

func (*Etcd) DeleteKey

func (etcd *Etcd) DeleteKey(key string) (*clientv3.DeleteResponse, error)

func (*Etcd) GetKeyValue

func (etcd *Etcd) GetKeyValue(key string) (*clientv3.GetResponse, error)

func (*Etcd) PutKeyValue

func (etcd *Etcd) PutKeyValue(key, value string) (*clientv3.PutResponse, error)

func (*Etcd) PutKeyWithLease

func (etcd *Etcd) PutKeyWithLease(key string, timeout int64) error

type EtcdConfig

type EtcdConfig struct {
	EtcdEndpoints   []string
	EtcdDialTimeout time.Duration
}

type Job

type Job struct {
	Name     string `json:"name"`     //  任务名
	Command  string `json:"command"`  // shell命令
	CronExpr string `json:"cronExpr"` // cron表达式
}

定时任务

func UnpackJob

func UnpackJob(value []byte) (ret *Job, err error)

反序列化Job

type JobEvent

type JobEvent struct {
	EventType int //  SAVE, DELETE
	Job       *Job
}

变化事件

func BuildJobEvent

func BuildJobEvent(eventType int, job *Job) (jobEvent *JobEvent)

任务变化事件有2种:1)更新任务 2)删除任务

type JobExecuteInfo

type JobExecuteInfo struct {
	Job        *Job               // 任务信息
	PlanTime   time.Time          // 理论上的调度时间
	RealTime   time.Time          // 实际的调度时间
	CancelCtx  context.Context    // 任务command的context
	CancelFunc context.CancelFunc //  用于取消command执行的cancel函数
}

任务执行状态

func BuildJobExecuteInfo

func BuildJobExecuteInfo(jobSchedulePlan *JobSchedulePlan) (jobExecuteInfo *JobExecuteInfo)

构造执行状态信息

type JobExecuteResult

type JobExecuteResult struct {
	ExecuteInfo *JobExecuteInfo // 执行状态
	Output      []byte          // 脚本输出
	Err         error           // 脚本错误原因
	StartTime   time.Time       // 启动时间
	EndTime     time.Time       // 结束时间
}

任务执行结果

type JobLog

type JobLog struct {
	JobName      string `json:"jobName" bson:"jobName"`           // 任务名字
	Command      string `json:"command" bson:"command"`           // 脚本命令
	Err          string `json:"err" bson:"err"`                   // 错误原因
	Output       string `json:"output" bson:"output"`             // 脚本输出
	PlanTime     int64  `json:"planTime" bson:"planTime"`         // 计划开始时间
	ScheduleTime int64  `json:"scheduleTime" bson:"scheduleTime"` // 实际调度时间
	StartTime    int64  `json:"startTime" bson:"startTime"`       // 任务执行开始时间
	EndTime      int64  `json:"endTime" bson:"endTime"`           // 任务执行结束时间
}

任务执行日志

type JobLogFilter

type JobLogFilter struct {
	JobName string `bson:"jobName"`
}

任务日志过滤条件

type JobSchedulePlan

type JobSchedulePlan struct {
	Job      *Job                 // 要调度的任务信息
	Expr     *cronexpr.Expression // 解析好的cronexpr表达式
	NextTime time.Time            // 下次调度时间
}

任务调度计划

func BuildJobSchedulePlan

func BuildJobSchedulePlan(job *Job) (jobSchedulePlan *JobSchedulePlan, err error)

构造任务执行计划

type LogBatch

type LogBatch struct {
	Logs []interface{} // 多条日志
}

日志批次

type MongoConfig

type MongoConfig struct {
	ConnectTimeOut time.Duration
	Uri            string
}

type Response

type Response struct {
	Errno int         `json:"errno"`
	Msg   string      `json:"msg"`
	Data  interface{} `json:"data"`
}

HTTP接口应答

type SortLogByStartTime

type SortLogByStartTime struct {
	SortOrder int `bson:"startTime"` // {startTime: -1}
}

任务日志排序规则

Jump to

Keyboard shortcuts

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