Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #49

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
902de9a
Update .gitignore
ehwjh2010 Jul 22, 2022
9c181fd
feat: 存储使用Storage接口
ehwjh2010 Jul 22, 2022
fa8133b
feat: 添加任务列表, 支持动态添加任务
ehwjh2010 Jul 23, 2022
695e1d6
feat: 移除列表
ehwjh2010 Jul 23, 2022
5d506e8
feat: 添加登录, 获取任务列表, 新增任务, 删除任务, 任务存储接口化
ehwjh2010 Jul 24, 2022
8128102
Merge pull request #1 from ehwjh2010/develop
ehwjh2010 Jul 24, 2022
46925e3
feat: 删除example
ehwjh2010 Jul 24, 2022
57b8221
feat: 删除example
ehwjh2010 Jul 24, 2022
cd410fa
Merge branch 'develop'
ehwjh2010 Jul 24, 2022
59cfdfe
feat: 优化运行任务与任务列表
ehwjh2010 Jul 25, 2022
aeb0146
feat: 优化登录与任务列表
ehwjh2010 Jul 26, 2022
3656dc2
feat: 添加JobId字段, 以及相等方法
ehwjh2010 Jul 26, 2022
7fa0ef7
feat: 优化Storager接口
ehwjh2010 Jul 26, 2022
536c9c2
feat: storage删除taskName字段
ehwjh2010 Jul 26, 2022
442a0ed
feat: 挑战Storager接口
ehwjh2010 Jul 26, 2022
7b34ffa
Merge pull request #2 from ehwjh2010/develop
ehwjh2010 Jul 26, 2022
3a5da33
feat: 修复寻找处理器bug
ehwjh2010 Jul 26, 2022
10c831a
Merge pull request #3 from ehwjh2010/develop
ehwjh2010 Jul 26, 2022
a110d64
feat: 修改扫描时间
ehwjh2010 Jul 28, 2022
7fe915f
Merge pull request #4 from ehwjh2010/develop
ehwjh2010 Jul 28, 2022
f2e8fe6
feat: 支持运行任务
ehwjh2010 Sep 13, 2022
b544066
Merge pull request #5 from ehwjh2010/develop
ehwjh2010 Sep 13, 2022
2908d67
feat: 添加运行任务接口
ehwjh2010 Sep 14, 2022
b5f08a8
feat: 优化接口
ehwjh2010 Sep 14, 2022
30dc34f
feat: 添加常用常量
ehwjh2010 Oct 8, 2022
ade3bd5
feat: 类型转换
ehwjh2010 Oct 8, 2022
cbd9edc
feat: 查询优化
ehwjh2010 Oct 8, 2022
322655b
feat: 使用日志打印报错堆栈
ehwjh2010 Feb 13, 2023
2af28da
feat: 任务执行忽略过期时间
ehwjh2010 Mar 2, 2023
d1f16f9
feat: 支持同时执行同一个执行器
ehwjh2010 Mar 27, 2023
ee5c557
feat: 删除测试打印
ehwjh2010 Mar 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@

# Dependency directories (remove the comment below to include it)
# vendor/
go.sum
.idea/
120 changes: 120 additions & 0 deletions auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package xxl

import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"sync"
"time"
)

type Auth interface {
Login() ([]*http.Cookie, error)
}

type AuthImpl struct {
Addr string
UserName string
Password string

mu sync.Mutex
closeChan chan struct{}
cookies []*http.Cookie
}

func NewAuth(userName, password, addr string, interval time.Duration) (Auth, func(), error) {
auth := &AuthImpl{
UserName: userName,
Password: password,
Addr: addr,
closeChan: make(chan struct{}),
}

auth.CronReplaceCookie(interval)

return auth, func() {
auth.closeChan <- struct{}{}
}, nil
}

const loginUrl = "/login"

var LoginErr = errors.New("login failed")

// Login 登录
func (a *AuthImpl) Login() ([]*http.Cookie, error) {
if len(a.cookies) == 0 {

a.mu.Lock()
defer a.mu.Unlock()

if len(a.cookies) > 0 {
return a.cookies, nil
}

cookies, err := a.login()

if err != nil {
return nil, err
}

a.cookies = cookies
}

return a.cookies, nil
}

func (a *AuthImpl) CronReplaceCookie(interval time.Duration) {

go func(t time.Duration) {
ticker := time.NewTicker(t)

ReplaceXxlJonCookieLoop:
for {
select {
case <-ticker.C:
cookies, err := a.login()
if err != nil {
continue
}

a.cookies = cookies
case <-a.closeChan:
break ReplaceXxlJonCookieLoop
}
}
}(interval)
}

func (a *AuthImpl) login() ([]*http.Cookie, error) {
values := url.Values{}
values.Add("userName", a.UserName)
values.Add("password", a.Password)

resp, err := http.PostForm(a.Addr+loginUrl, values)
if err != nil {
return nil, err
}

defer resp.Body.Close()

all, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

response := &res{}
if err = json.Unmarshal(all, response); err != nil {
return nil, err
}

if response.Code == FailureCode {
return nil, LoginErr
}

cookies := resp.Cookies()

return cookies, nil
}
39 changes: 39 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,42 @@ const (
SuccessCode = 200
FailureCode = 500
)

type ScheduleType string

const (
FixRate ScheduleType = "FIX_RATE"
Cron ScheduleType = "CRON"
)

type MisfireStrategy string

const (
DoNothing MisfireStrategy = "DO_NOTHING" // 什么都不做
FireOnceNow MisfireStrategy = "FIRE_ONCE_NOW" // 立即执行
)

type ExecutorBlockStrategy string

const (
SerialExecution ExecutorBlockStrategy = "SERIAL_EXECUTION" // 串行
DiscardLater ExecutorBlockStrategy = "DISCARD_LATER" // 丢弃后续调度
CoverEarly ExecutorBlockStrategy = "COVER_EARLY" // 覆盖之前调度
)

type TriggerStatus int32

const (
Total TriggerStatus = -1 // 全部
Stop TriggerStatus = 0 // 停止
Running TriggerStatus = 1 // 运行
)

type ExecutorRouteStrategy string

const (
First ExecutorRouteStrategy = "FIRST" // 第一个
Last ExecutorBlockStrategy = "LAST" // 最后一个
Round ExecutorBlockStrategy = "ROUND" // 轮询
Random ExecutorRouteStrategy = "RANDOM" // 随机
)
98 changes: 98 additions & 0 deletions dto.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package xxl

import "time"

//通用响应
type res struct {
Code int64 `json:"code"` // 200 表示正常、其他失败
Expand Down Expand Up @@ -89,3 +91,99 @@ type LogResContent struct {
LogContent string `json:"logContent"` // 本次请求日志内容
IsEnd bool `json:"isEnd"` // 日志是否全部加载完
}

/********************任务参数*******************/

type AddJob struct {
JobGroup int64 `json:"jobGroup"` // 执行器ID
JobDesc string `json:"jobDesc"` // 任务描述
Author string `json:"author"` // 作者
TriggerStatus int32 `json:"triggerStatus"` // 任务状态
AlarmEmail string `json:"alarmEmail"` // 多个邮件使用逗号分隔
ScheduleType string `json:"scheduleType"` // 调度类型, CRON 定时, FIX_RATE 固定速率
ScheduleConf string `json:"scheduleConf"` // 必传
ScheduleConfCron string `json:"schedule_conf_CRON"` // 不清楚该字段功能
CronGenDisplay string `json:"cronGen_display"` // CRON类型, 与scheduleConf一致, 否则不传
ScheduleConfFixDelay string `json:"schedule_conf_FIX_DELAY"` // 不清楚该字段功能
ScheduleConfFixRate string `json:"schedule_conf_FIX_RATE"` // FIX_RATE类型, 与scheduleConf一致, 否则不传
GlueType string `json:"glueType"` // 运行模式, BEAN
ExecutorHandler string `json:"executorHandler"` // JobHandler, 任务名
ExecutorParam string `json:"executorParam"` // 执行参数
ExecutorRouteStrategy string `json:"executorRouteStrategy"` // 路由策略, ROUND 轮询, FIRST 第一个, LAST 最后一个
ChildJobId string `json:"childJobId"` // 子任务ID, 做个任务使用逗号隔开
MisfireStrategy string `json:"misfireStrategy"` // 调度过期策略, DO_NOTHING 忽略, FIRE_ONCE_NOW 立即执行一次
ExecutorBlockStrategy string `json:"executorBlockStrategy"` // 阻塞处理策略, SERIAL_EXECUTION 串行, DISCARD_LATER 丢弃后续调度, COVER_EARLY 覆盖之前调度
ExecutorTimeout int64 `json:"executorTimeout"` // 执行超时时间, 单位: s
ExecutorFailRetryCount int32 `json:"executorFailRetryCount"` // 失败重试次数
GlueRemark string `json:"glueRemark"` // 值为: GLUE代码初始化
GlueSource string `json:"glueSource"` // 源码
}

/********************添加任务响应*******************/

type AddJobResp struct {
Code int `json:"code"`
Msg interface{} `json:"msg"`
Content string `json:"content"`
}

/********************添加任务参数*******************/

type JobInfo struct {
Id int `json:"id"`
JobGroup int `json:"jobGroup"`
JobDesc string `json:"jobDesc"`
AddTime time.Time `json:"addTime"`
UpdateTime time.Time `json:"updateTime"`
Author string `json:"author"`
AlarmEmail string `json:"alarmEmail"`
ScheduleType string `json:"scheduleType"`
ScheduleConf string `json:"scheduleConf"`
MisfireStrategy string `json:"misfireStrategy"`
ExecutorRouteStrategy string `json:"executorRouteStrategy"`
ExecutorHandler string `json:"executorHandler"`
ExecutorParam string `json:"executorParam"`
ExecutorBlockStrategy string `json:"executorBlockStrategy"`
ExecutorTimeout int `json:"executorTimeout"`
ExecutorFailRetryCount int `json:"executorFailRetryCount"`
GlueType string `json:"glueType"`
GlueSource string `json:"glueSource"`
GlueRemark string `json:"glueRemark"`
GlueUpdatetime time.Time `json:"glueUpdatetime"`
ChildJobId string `json:"childJobId"`
TriggerStatus int `json:"triggerStatus"`
TriggerLastTime int `json:"triggerLastTime"`
TriggerNextTime int `json:"triggerNextTime"`
}

/********************查询任务参数*******************/

type QueryJob struct {
JobGroup int64 `json:"jobGroup"` // 执行器ID
TriggerStatus int32 `json:"triggerStatus"` // 直接使用-1
JobDesc string `json:"jobDesc"` // 任务描述
ExecutorHandler string `json:"executorHandler"` // 任务名
Author string `json:"author"` // 作者
Start int32 `json:"start"` // 偏移量, 默认是0
Length int32 `json:"length"` // 每页数量
}

/********************运行任务参数*******************/

type RunJob struct {
Id int64 `json:"id"`
}

/********************查询任务响应*******************/

type JobList struct {
RecordsFiltered int `json:"recordsFiltered"`
Data []JobInfo `json:"data"`
RecordsTotal int `json:"recordsTotal"`
}

/********************删除任务参数*******************/

type DeleteJob struct {
Id int64 `json:"id"`
}
45 changes: 0 additions & 45 deletions example/main.go

This file was deleted.

11 changes: 0 additions & 11 deletions example/task/panic.go

This file was deleted.

12 changes: 0 additions & 12 deletions example/task/test.go

This file was deleted.

29 changes: 0 additions & 29 deletions example/task/test2.go

This file was deleted.

Loading