Documentation ¶
Index ¶
- Variables
- func GetFirstLocalIpAddress() (ipv4 string, err error)
- type BaseFields
- type BaseResponse
- type Category
- type EtcdLock
- func (etcdLock *EtcdLock) GetLeaseID() string
- func (etcdLock *EtcdLock) KeepAliveOnce(secret string) (err error)
- func (etcdLock *EtcdLock) ResetTimer(duration time.Duration, secret string) (err error)
- func (etcdLock *EtcdLock) SetAutoKillTicker(callback func())
- func (etcdLock *EtcdLock) TryLock() (err error)
- func (etcdLock *EtcdLock) UnLock()
- type Job
- type JobEtcd
- type JobEvent
- type JobExecute
- type JobExecuteInfo
- type JobExecuteLog
- type JobExecuteResult
- type JobKill
- type JobSchedulePlan
- type KillEtcd
- type Lock
- type LockRequest
- type LockResponse
- type Worker
Constants ¶
This section is empty.
Variables ¶
var EtcdJobsLockDir = "/cronjob/lock/"
Functions ¶
Types ¶
type BaseFields ¶
type BaseResponse ¶
type BaseResponse struct { Status string `json:"status"` // 状态 Message string `json:"message"` // 消息内容 }
通用的响应消息
type Category ¶
type Category struct { // gorm.Model BaseFields EtcdKey string `gorm:"size:200" json:"etcd_key"` // etcd中保存的key Name string `gorm:"size:40;NOT NULL;UNIQUE_INDEX" json:"name"` // 分类名称 Description string `gorm:"size:512;NOT NULL" json:"description"` // 分类的描述信息 CheckCmd string `gorm:"size:512;" json:"check_cmd""` // 检查是否可以执行本来计划任务的命令:eg:ls `which bash` SetupCmd string `gorm:"size:512;" json:"setup_cmd"` // worker节点初始化的时候执行的命令, eg: pip install requests TearDownCmd string `gorm:"size:512;" json:"tear_down_cmd"` // worker节点退出的时候需要执行的命令, eg: pip uninstall requests IsActive bool `gorm:"type:boolean" json:"is_active"` // 分类的状态,如果是true才可以执行 }
计划任务的分类:默认是Default 执行不同类型的计划任务,需要不同的环境准备 环境准备可以通过Command来处理 当worker启动的时候:会添加default的分类 执行分类相关的命令: 1. 先执行checkCmd的命令:成功就跳过, 2. 未成功就执行SetupCmd,再执行CheckCmd 3. 当worker关闭的时候,执行TearDownCmd的命令。
type EtcdLock ¶
type EtcdLock struct { CreatedAt time.Time `json:"created_at"` // 创建时间 Name string `json:"name"` // 锁的名字,eg:jobs/:category/:name Duration int64 `json:"duration"` // 关联的租约秒 LeaseID clientv3.LeaseID `json:"lease_id"` // 租约ID IsLocked bool `json:"is_locked"` // 释放上锁成功,也可根据leaseID释放不为0判断 NeedKillChan chan bool `json:"-"` // 是否需要杀掉当前上锁的程序 Description string `json:"description"` // 备注信息 ResetTimes int `json:"reset_times"` // 重置定时器的次数 Secret string `json:"secret"` // 设置个秘钥,续租的时候需要提供 // contains filtered or unexported fields }
etcd分布式锁 计划任务执行的时候需要锁:/crontab/lock/jobs/:category/:name client --> server: TryLocker: 申请锁,判断是否成功 KeepAliveOnce: 续租一次,要想让key持续ok,那么需要让不断的发起续租请求
func NewEtcdLock ¶
func NewEtcdLock(name string, duration int64, kv clientv3.KV, lease clientv3.Lease) (etcdLock *EtcdLock, err error)
实例化一个etcdLock duration是关联的租约的时间秒数
func (*EtcdLock) KeepAliveOnce ¶
发起一次续租 注意:假如租约关联的key如果被删除了,租约还存在【还在有效期内】 那么我们发起续租租约会成功,但是key却已经删除了
func (*EtcdLock) ResetTimer ¶
重置timer 比如:timer:5,4,3,2,1,0 假如倒数到2了,你执行一下ResetTimer(10 * time.Second), 立刻变成:10,9,8,7...了
func (*EtcdLock) SetAutoKillTicker ¶
func (etcdLock *EtcdLock) SetAutoKillTicker(callback func())
上锁成功后,是否想自动释放 比如:上锁成功了,然后需要客户端主动发送续租信息 超时后,自动kill续租的相关操作
type Job ¶
type Job struct { BaseFields EtcdKey string `gorm:"size:100" json:"etcd_key, omitempty"` // ETCD中保存的key Category *Category `gorm:"ForeignKey:CategoryID" json:"category"` // Job的分类 CategoryID uint `gorm:"INDEX;NOT NULL" json:"category_id"` // 分类的ID Name string `gorm:"size:256" json:"name"` // 任务的名称 Time string `gorm:"size:100;NOT NULL" json:"time"` // 计划任务的时间 Command string `gorm:"size:256;NOT NULL" json:"command"` // 任务的命令 Description string `gorm:"size:512" json:"description,omitempty"` // Job描述 IsActive bool `gorm:"type:boolean" json:"is_active"` // 是否激活,激活才执行 SaveOutput bool `gorm:"type:boolean" json:"save_output"` // 是否记录输出 Timeout int `json:"timeout"` // 超时时间,默认是0不超时,单位为秒 }
定时任务 比如:每三十分钟执行一次的任务 */30 * * * * echo `date` >> /var/log/test.log
type JobEtcd ¶
type JobEtcd struct { ID uint `json:"id"` CreatedAt time.Time `json:"created_at"` Category string `json:"category"` Name string `json:"name"` Time string `json:"time"` Command string `json:"command"` Description string `json:"description"` IsActive bool `json:"is_active"` SaveOutput bool `json:"save_output"` Timeout int `json:"timeout"` }
保存去Eetcd中的
func (*JobEtcd) ToJobExecutePlan ¶
func (job *JobEtcd) ToJobExecutePlan() (jobSchedulePlan *JobSchedulePlan, err error)
JobEtcd转换成JobExecutePlan 每次etcd监听到job的put操作的时候,就需要把Job信息转换成jobSchedulePlan
type JobExecute ¶
type JobExecute struct { BaseFields Worker string `gorm:"size:100" json:"worker"` // 执行者 Category string `gorm:"size:100" json:"category"` // 任务类型 Name string `gorm:"size:100" json:"name"` // 任务名字 JobID int `gorm:"INDEX;NOT NULL" json:"job_id"` // 计划任务ID Command string `gorm:"NOT NULL" json:"command"` // 执行的命令 Status string `gorm:"size:100;NOT NULL" json:"status"` // 任务执行状态 PlanTime time.Time `gorm:"NOT NULL" json:"plan_time"` // 计划时间 ScheduleTime time.Time `json:"schedule_time"` // 调度时间 StartTime time.Time `json:"start_time"` // 开始时间 EndTime time.Time `json:"end_time"` // 任务结束时间 LogID string `json:"log_id"` // 执行结果保存的ObjectID }
任务调度前创建JobExecute 任务执行完后再写入JobExecuteLog信息 Status:start、doing、cancel、done
type JobExecuteInfo ¶
type JobExecuteInfo struct { Job *JobEtcd `json:"job"` // 任务信息 JobExecuteID uint `json:"job_execute_id"` // 任务执行ID PlanTime time.Time `json:"plan_time"` // 计划调度的时间 ExecuteTime time.Time `json:"execute_time"` // 实际执行的时间 ExecuteCtx context.Context `json:"-"` // 执行job的上下文 ExceteCancelFun context.CancelFunc `json:"-"` // 执行执行job的取消函数 Status string `json:"status"` // 执行信息的状态:start、timeout、kill、success、error、done }
Job执行信息
type JobExecuteLog ¶
type JobExecuteLog struct { JobExecuteID uint `json:"job_execute_id" bson:"job_execute_id"` // 任务执行ID Output string `json:"output" bson:"output"` // 执行任务输出结果 Error string `json:"error" bson:"error"` // 任务错误信息 Success bool `json:"success" bson:"success"` // 执行是否成功:当有错误日志的时候,就是未成功 }
执行日志结果,写入到Mongodb中
type JobExecuteResult ¶
type JobExecuteResult struct { ExecuteID uint // 任务执行的ID ExecuteInfo *JobExecuteInfo // 执行信息: 消费结果的时候,会根据这个来判断是否执行了 IsExecuted bool // 是否有执行 Output []byte // Job执行输出结果 Error string // 脚本错误原因 StartTime time.Time // 启动时间 EndTime time.Time // 结束时间 Status string // 执行状态:start、finish、cancel、success、error、timeout }
Job执行结果
type JobKill ¶
type JobKill struct { BaseFields EtcdKey string `gorm:"size:100" json:"etcd_key"` // 保存在Etcd中的Key Category string `gorm:"size:100" json:"category"` // 要杀掉的Job的分类 JobID uint `gorm:"INDEX;NOT NULL" json:"job_id"` // 杀掉的Job的ID Killed bool `gorm:"type:boolean" json:"killed"` // 是否已经杀死 FinishedAt *time.Time `gorm:"NULL" json:"finished_at"` // 完成时间 Result string `gorm:"size:512" json:"result"` // 处理结果 }
func (*JobKill) ToEtcdDataStruct ¶
type JobSchedulePlan ¶
type JobSchedulePlan struct { Job *JobEtcd // 计划任务 Expression *cronexpr.Expression // 解析好的cronexpr表达式 NextTime time.Time // 下次执行时间 }
Job调度计划
type Lock ¶
type Lock struct { Name string `json:"name"` // 锁的名字 TTL int64 `json:"ttl"` // 锁的时间:time to live的简写 Password string `json:"password"` // 锁的密码 LeaseID int64 `json:"lease_id"` // 租约ID IsActive bool `json:"is_active"` // 是否有效 }
锁
type LockRequest ¶
type LockRequest struct { ID int `json:"id"` // 发起上锁请求的序号 Name string `json:"name"` // 锁的名字 Secret string `json:"secret"` // 锁的秘钥 }
LockRequest
type LockResponse ¶
type LockResponse struct { ID int `json:"id"` // 发起上锁请求的序号 Success bool `json:"success"` // 是否成功 Name string `json:"name"` // 锁的名字 Secret string `json:"secret"` // 锁的秘钥 Message string `json:"message"` // 消息内容 }
通过http获取锁的响应结果