Skip to content

Commit

Permalink
HTTP任务支持POST请求 Close ouqiang#19
Browse files Browse the repository at this point in the history
  • Loading branch information
ouqiang committed Jan 27, 2018
1 parent f081d89 commit 50638e6
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 90 deletions.
10 changes: 4 additions & 6 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@ func initModule() {
upgradeIfNeed()

// 初始化定时任务
serviceTask := new(service.Task)
serviceTask.Initialize()
service.ServiceTask.Initialize()
}

// 解析端口
func parsePort(ctx *cli.Context) int {
var port int = DefaultPort
port := DefaultPort
if ctx.IsSet("port") {
port = ctx.Int("port")
}
Expand All @@ -104,7 +103,7 @@ func parseHost(ctx *cli.Context) string {
}

func setEnvironment(ctx *cli.Context) {
var env string = "prod"
env := "prod"
if ctx.IsSet("env") {
env = ctx.String("env")
}
Expand Down Expand Up @@ -147,10 +146,9 @@ func shutdown() {
return
}
logger.Info("应用准备退出")
serviceTask := new(service.Task)
// 停止所有任务调度
logger.Info("停止定时任务调度")
serviceTask.WaitAndExit()
service.ServiceTask.WaitAndExit()
}

// 判断应用是否需要升级, 当存在版本号文件且版本小于app.VersionId时升级
Expand Down
11 changes: 10 additions & 1 deletion models/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,16 @@ func (migration *Migration) upgradeFor140(session *xorm.Session) error {
tableName := TablePrefix + "task"
// task表增加字段
// retry_interval 重试间隔时间(秒)
_, err := session.Exec(fmt.Sprintf("ALTER TABLE %s ADD COLUMN retry_interval SMALLINT NOT NULL DEFAULT 0", tableName))
// http_method http请求方法
sql := fmt.Sprintf(
"ALTER TABLE %s ADD COLUMN retry_interval SMALLINT NOT NULL DEFAULT 0,ADD COLUMN http_method TINYINT NOT NULL DEFAULT 1", tableName)
_, err := session.Exec(sql)

if err != nil {
return err
}



logger.Info("已升级到v1.4\n")

Expand Down
4 changes: 2 additions & 2 deletions models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
type Status int8
type CommonMap map[string]interface{}

var TablePrefix string = ""
var TablePrefix = ""
var Db *xorm.Engine

const (
Expand Down Expand Up @@ -99,7 +99,7 @@ func CreateTmpDb(setting *setting.Setting) (*xorm.Engine, error) {
// 获取数据库引擎DSN mysql,sqlite
func getDbEngineDSN(setting *setting.Setting) string {
engine := strings.ToLower(setting.Db.Engine)
var dsn string = ""
dsn := ""
switch engine {
case "mysql":
dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s",
Expand Down
14 changes: 11 additions & 3 deletions models/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ const (
TaskDependencyStatusWeak TaskDependencyStatus = 2 // 弱依赖
)

type TaskHTTPMethod int8

const (
TaskHTTPMethodGet TaskHTTPMethod = 1;
TaskHttpMethodPost TaskHTTPMethod = 2;
)

// 任务
type Task struct {
Id int `xorm:"int pk autoincr"`
Expand All @@ -38,6 +45,7 @@ type Task struct {
Spec string `xorm:"varchar(64) notnull"` // crontab
Protocol TaskProtocol `xorm:"tinyint notnull index"` // 协议 1:http 2:系统命令
Command string `xorm:"varchar(256) notnull"` // URL地址或shell命令
HttpMethod TaskHTTPMethod `xorm:"tinyint notnull default 1"` // http请求方法
Timeout int `xorm:"mediumint notnull default 0"` // 任务执行超时时间(单位秒),0不限制
Multi int8 `xorm:"tinyint notnull default 1"` // 是否允许多实例运行
RetryTimes int8 `xorm:"tinyint notnull default 0"` // 重试次数
Expand Down Expand Up @@ -84,9 +92,9 @@ func (task *Task) CreateTestTask() {

func (task *Task) UpdateBean(id int) (int64, error) {
return Db.ID(id).
Cols("name,spec,protocol,command,timeout,multi," +
"retry_times,retry_interval,remark,notify_status," +
"notify_type,notify_receiver_id, dependency_task_id, dependency_status, tag").
Cols(`name,spec,protocol,command,timeout,multi,
retry_times,retry_interval,remark,notify_status,
notify_type,notify_receiver_id, dependency_task_id, dependency_status, tag,http_method`).
Update(task)
}

Expand Down
11 changes: 6 additions & 5 deletions modules/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ func ToNumberVersion(versionString string) int {
// 检测目录是否存在
func createDirIfNotExists(path ...string) {
for _, value := range path {
if !utils.FileExist(value) {
err := os.Mkdir(value, 0755)
if err != nil {
logger.Fatal(fmt.Sprintf("创建目录失败:%s", err.Error()))
}
if utils.FileExist(value) {
continue
}
err := os.Mkdir(value, 0755)
if err != nil {
logger.Fatal(fmt.Sprintf("创建目录失败:%s", err.Error()))
}
}
}
3 changes: 1 addition & 2 deletions modules/httpclient/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ func request(req *http.Request, timeout int) ResponseWrapper {
}

func setRequestHeader(req *http.Request) {
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 golang/gocron")
req.Header.Set("User-Agent", "golang/gocron")
}

func createRequestError(err error) ResponseWrapper {
Expand Down
2 changes: 1 addition & 1 deletion modules/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Notifiable interface {
Send(msg Message)
}

var queue chan Message = make(chan Message, 100)
var queue = make(chan Message, 100)

func init() {
go run()
Expand Down
6 changes: 0 additions & 6 deletions modules/rpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ func (s Server) Run(ctx context.Context, req *pb.TaskRequest) (*pb.TaskResponse,
}

func Start(addr string, enableTLS bool, certificate auth.Certificate) {
defer func() {
if err := recover(); err != nil {
grpclog.Println("panic", err)
}
}()

l, err := net.Listen("tcp", addr)
if err != nil {
grpclog.Fatal(err)
Expand Down
14 changes: 1 addition & 13 deletions modules/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package utils
import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/Tang-RoseChild/mahonia"
"math/rand"
"os"
"runtime"
"strings"
"time"
)
Expand Down Expand Up @@ -41,11 +39,6 @@ func RandNumber(max int) int {
return r.Intn(max)
}

// 判断当前系统是否是windows
func IsWindows() bool {
return runtime.GOOS == "windows"
}

// GBK编码转换为UTF8
func GBK2UTF8(s string) (string, bool) {
dec := mahonia.NewDecoder("gbk")
Expand Down Expand Up @@ -99,9 +92,4 @@ func FileExist(file string) bool {
}

return true
}

// 格式化环境变量
func FormatUnixEnv(key, value string) string {
return fmt.Sprintf("export %s=%s; ", key, value)
}
}
2 changes: 1 addition & 1 deletion modules/utils/utils_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func ExecShell(ctx context.Context, command string) (string, error) {
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
var resultChan chan Result = make(chan Result)
resultChan := make(chan Result)
go func() {
output, err := cmd.CombinedOutput()
resultChan <- Result{string(output), err}
Expand Down
5 changes: 2 additions & 3 deletions routers/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ func Store(ctx *macaron.Context, form HostForm) string {
if err != nil {
return json.CommonFailure("刷新任务主机信息失败", err)
}
serviceTask := new(service.Task)
serviceTask.BatchAdd(tasks)
service.ServiceTask.BatchAdd(tasks)
}

return json.Success("保存成功", nil)
Expand Down Expand Up @@ -190,7 +189,7 @@ func Ping(ctx *macaron.Context) string {

// 解析查询参数
func parseQueryParams(ctx *macaron.Context) models.CommonMap {
var params models.CommonMap = models.CommonMap{}
var params = models.CommonMap{}
params["Id"] = ctx.QueryInt("id")
params["Name"] = ctx.QueryTrim("name")
base.ParsePageAndPageSize(ctx, params)
Expand Down
3 changes: 1 addition & 2 deletions routers/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ func Store(ctx *macaron.Context, form InstallForm) string {

app.Installed = true
// 初始化定时任务
serviceTask := new(service.Task)
serviceTask.Initialize()
service.ServiceTask.Initialize()

return json.Success("安装成功", nil)
}
Expand Down
18 changes: 9 additions & 9 deletions routers/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type TaskForm struct {
Spec string
Protocol models.TaskProtocol `binding:"In(1,2)"`
Command string `binding:"Required;MaxSize(256)"`
HttpMethod models.TaskHTTPMethod `binding:"In(1,2)"`
Timeout int `binding:"Range(0,86400)"`
Multi int8 `binding:"In(1,2)"`
RetryTimes int8
Expand Down Expand Up @@ -111,11 +112,11 @@ func Edit(ctx *macaron.Context) {
ctx.HTML(200, "task/task_form")
}

// 保存任务
// 保存任务 todo 拆分为多个方法 快变成意大利面条式代码了
func Store(ctx *macaron.Context, form TaskForm) string {
json := utils.JsonResponse{}
taskModel := models.Task{}
var id int = form.Id
var id = form.Id
nameExists, err := taskModel.NameExist(form.Name, form.Id)
if err != nil {
return json.CommonFailure(utils.FailureContent, err)
Expand All @@ -130,7 +131,7 @@ func Store(ctx *macaron.Context, form TaskForm) string {

taskModel.Name = form.Name
taskModel.Protocol = form.Protocol
taskModel.Command = form.Command
taskModel.Command = strings.TrimSpace(form.Command)
taskModel.Timeout = form.Timeout
taskModel.Tag = form.Tag
taskModel.Remark = form.Remark
Expand All @@ -150,6 +151,7 @@ func Store(ctx *macaron.Context, form TaskForm) string {
if taskModel.NotifyStatus > 0 && taskModel.NotifyReceiverId == "" {
return json.CommonFailure("至少选择一个通知接收者")
}
taskModel.HttpMethod = form.HttpMethod
if taskModel.Protocol == models.TaskHTTP {
command := strings.ToLower(taskModel.Command)
if !strings.HasPrefix(command, "http://") && !strings.HasPrefix(command, "https://") {
Expand Down Expand Up @@ -235,7 +237,7 @@ func Remove(ctx *macaron.Context) string {
taskHostModel := new(models.TaskHost)
taskHostModel.Remove(id)

service.Cron.RemoveJob(strconv.Itoa(id))
service.ServiceTask.Remove(id)

return json.Success(utils.SuccessContent, nil)
}
Expand All @@ -261,8 +263,7 @@ func Run(ctx *macaron.Context) string {
}

task.Spec = "手动运行"
serviceTask := new(service.Task)
serviceTask.Run(task)
service.ServiceTask.Run(task)

return json.Success("任务已开始运行, 请到任务日志中查看结果", nil)
}
Expand All @@ -282,7 +283,7 @@ func changeStatus(ctx *macaron.Context, status models.Status) string {
if status == models.Enabled {
addTaskToTimer(id)
} else {
service.Cron.RemoveJob(strconv.Itoa(id))
service.ServiceTask.Remove(id)
}

return json.Success(utils.SuccessContent, nil)
Expand All @@ -297,8 +298,7 @@ func addTaskToTimer(id int) {
return
}

taskService := service.Task{}
taskService.Add(task)
service.ServiceTask.Add(task)
}

// 解析查询参数
Expand Down
3 changes: 1 addition & 2 deletions routers/tasklog/task_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ func Stop(ctx *macaron.Context) string {
if len(task.Hosts) == 0 {
return json.CommonFailure("任务节点列表为空")
}
serviceTask := new(service.Task)
for _, host := range task.Hosts {
serviceTask.Stop(host.Name, host.Port, id)
service.ServiceTask.Stop(host.Name, host.Port, id)

}

Expand Down
Loading

0 comments on commit 50638e6

Please sign in to comment.