diff --git a/cmd/web.go b/cmd/web.go index 8d8a6aa..0d77918 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -93,12 +93,12 @@ func setEnvironment(ctx *cli.Context) { } switch env { - case "prod": - macaron.Env = macaron.PROD case "test": macaron.Env = macaron.TEST case "dev": macaron.Env = macaron.DEV + default: + macaron.Env = macaron.PROD } } diff --git a/models/migration.go b/models/migration.go index 4320729..9ab26ab 100644 --- a/models/migration.go +++ b/models/migration.go @@ -13,8 +13,9 @@ func (migration *Migration) Exec(dbName string) error { return errors.New("数据库不存在") } setting := new(Setting) + task := new(Task) tables := []interface{}{ - &User{}, &Task{}, &TaskLog{}, &Host{}, setting,&LoginLog{}, + &User{}, task, &TaskLog{}, &Host{}, setting,&LoginLog{}, } for _, table := range tables { exist, err:= Db.IsTableExist(table) @@ -30,6 +31,7 @@ func (migration *Migration) Exec(dbName string) error { } } setting.InitBasicField() + task.CreateTestTask() return nil } diff --git a/models/task.go b/models/task.go index e1bc9c9..a77b918 100644 --- a/models/task.go +++ b/models/task.go @@ -4,6 +4,7 @@ import ( "time" "github.com/ouqiang/gocron/modules/ssh" "github.com/go-xorm/xorm" + "github.com/ouqiang/gocron/modules/utils" ) type TaskProtocol int8 @@ -29,9 +30,9 @@ type Task struct { NotifyType int8 `xorm:"smallint notnull default 0"` // 通知类型 1: 邮件 2: slack NotifyReceiverId string `xorm:"varchar(256) notnull default '' "` // 通知接受者ID, setting表主键ID,多个ID逗号分隔 Remark string `xorm:"varchar(100) notnull default ''"` // 备注 + Status Status `xorm:"tinyint notnull default 0"` // 状态 1:正常 0:停止 Created time.Time `xorm:"datetime notnull created"` // 创建时间 Deleted time.Time `xorm:"datetime deleted"` // 删除时间 - Status Status `xorm:"tinyint notnull default 1"` // 状态 1:正常 0:停止 BaseModel `xorm:"-"` } @@ -58,8 +59,33 @@ func (task *Task) Create() (insertId int, err error) { return } +// 新增测试任务 +func (task *Task) CreateTestTask() { + // HTTP任务 + task.Name = "测试HTTP任务" + task.Protocol = TaskHTTP + task.Spec = "*/30 * * * * *" + // 查询IP地址区域信息 + task.Command = "http://ip.taobao.com/service/getIpInfo.php?ip=117.27.140.253" + task.Status = Enabled + task.Create() + + // 系统命令 + task.Id = 0 + task.Name = "测试系统命令任务" + task.Protocol = TaskLocalCommand + task.Spec = "@every 1m" + task.Status = Enabled + if utils.IsWindows() { + task.Command = "dir" + } else { + task.Command = "ls" + } + task.Create() +} + func (task *Task) UpdateBean(id int) (int64, error) { - return Db.ID(id).Cols("name,spec,protocol,command,timeout,multi,retry_times,host_id,remark,status,notify_status,notify_type,notify_receiver_id").Update(task) + return Db.ID(id).Cols("name,spec,protocol,command,timeout,multi,retry_times,host_id,remark,notify_status,notify_type,notify_receiver_id").Update(task) } // 更新 diff --git a/modules/notify/mail.go b/modules/notify/mail.go index b1576a8..f9ba373 100644 --- a/modules/notify/mail.go +++ b/modules/notify/mail.go @@ -66,7 +66,7 @@ func (mail *Mail) send(mailSetting models.Mail, toUsers []string, msg Message) i += 1 time.Sleep(2 * time.Second) if i < maxTimes { - logger.Error("mail#发送消息失败#%s#消息内容-%s", err.Error(), msg["content"]) + logger.Errorf("mail#发送消息失败#%s#消息内容-%s", err.Error(), msg["content"]) } } } diff --git a/modules/notify/slack.go b/modules/notify/slack.go index 515fb14..226d77f 100644 --- a/modules/notify/slack.go +++ b/modules/notify/slack.go @@ -50,7 +50,7 @@ func (slack *Slack) send(msg Message, slackUrl string, channel string) { i += 1 time.Sleep(2 * time.Second) if i < maxTimes { - logger.Error("slack#发送消息失败#%s#消息内容-%s", resp.Body, msg["content"]) + logger.Errorf("slack#发送消息失败#%s#消息内容-%s", resp.Body, msg["content"]) } } } diff --git a/routers/task/task.go b/routers/task/task.go index 8a93c9e..e8463c9 100644 --- a/routers/task/task.go +++ b/routers/task/task.go @@ -13,6 +13,7 @@ import ( "html/template" "github.com/ouqiang/gocron/routers/base" "github.com/go-macaron/binding" + "strings" ) type TaskForm struct { @@ -26,7 +27,6 @@ type TaskForm struct { RetryTimes int8 HostId int16 Remark string - Status models.Status `binding:"In(1,2)"` NotifyStatus int8 `binding:In(1,2,3)` NotifyType int8 `binding:In(1,2)` NotifyReceiverId string @@ -82,11 +82,6 @@ func Create(ctx *macaron.Context) { // 编辑页面 func Edit(ctx *macaron.Context) { id := ctx.ParamsInt(":id") - hostModel := new(models.Host) - hosts, err := hostModel.List(models.CommonMap{}) - if err != nil || len(hosts) == 0 { - logger.Error(err) - } taskModel := new(models.Task) task, err := taskModel.Detail(id) if err != nil || task.Id != id { @@ -95,11 +90,7 @@ func Edit(ctx *macaron.Context) { } ctx.Data["Task"] = task ctx.Data["Title"] = "编辑" - ctx.Data["Hosts"] = hosts - if len(hosts) > 0 { - ctx.Data["FirstHostName"] = hosts[0].Name - ctx.Data["FirstHostId"] = hosts[0].Id - } + setHostsToTemplate(ctx) ctx.HTML(200, "task/task_form") } @@ -134,12 +125,8 @@ func Store(ctx *macaron.Context, form TaskForm) string { taskModel.Command = form.Command taskModel.Timeout = form.Timeout taskModel.Remark = form.Remark - taskModel.Status = form.Status taskModel.Multi = form.Multi taskModel.RetryTimes = form.RetryTimes - if taskModel.Status != models.Enabled { - taskModel.Status = models.Disabled - } if taskModel.Multi != 1 { taskModel.Multi = 0 } @@ -151,6 +138,10 @@ func Store(ctx *macaron.Context, form TaskForm) string { return json.CommonFailure("请至少选择一个接收者") } if taskModel.Protocol == models.TaskHTTP { + command := strings.ToLower(taskModel.Command) + if !strings.HasPrefix(command, "http://") && !strings.HasPrefix(command, "https://") { + return json.CommonFailure("请输入正确的URL地址") + } if taskModel.Timeout == -1 { return json.CommonFailure("HTTP任务不支持后台运行") } @@ -159,10 +150,15 @@ func Store(ctx *macaron.Context, form TaskForm) string { } } - if taskModel.RetryTimes > 10 || taskModel.RetryTimes < 0 { return json.CommonFailure("任务重试次数取值0-10") } + + + if taskModel.Protocol != models.TaskSSH { + taskModel.HostId = 0 + } + if id == 0 { id, err = taskModel.Create() } else { @@ -172,11 +168,6 @@ func Store(ctx *macaron.Context, form TaskForm) string { return json.CommonFailure("保存失败", err) } - // 任务处于激活状态,加入调度管理 - if (taskModel.Status == models.Enabled) { - addTaskToTimer(id) - } - return json.Success("保存成功", nil) } @@ -280,8 +271,4 @@ func setHostsToTemplate(ctx *macaron.Context) { logger.Error(err) } ctx.Data["Hosts"] = hosts - if len(hosts) > 0 { - ctx.Data["FirstHostName"] = hosts[0].Name - ctx.Data["FirstHostId"] = hosts[0].Id - } } \ No newline at end of file diff --git a/templates/install/create.html b/templates/install/create.html index 6a9617b..1902da5 100644 --- a/templates/install/create.html +++ b/templates/install/create.html @@ -90,9 +90,17 @@ $('.ui.form').form( { onSuccess: function(event, fields) { + swal({ + title: '', + text: "系统安装中.......", + type: 'info', + showConfirmButton: false + }); util.post('/install/store', fields, function(code, message) { swal('安装成功'); - location.href = "/"; + setTimeout(function() { + location.href = "/"; + }, 2000) }); return false; }, diff --git a/templates/manage/mail.html b/templates/manage/mail.html index 1a98524..6679077 100644 --- a/templates/manage/mail.html +++ b/templates/manage/mail.html @@ -16,7 +16,7 @@