使用goimports格式化代码

develop
ouqiang 2018-01-30 19:26:04 +08:00
parent 4c940c2088
commit 94c9963e81
31 changed files with 137 additions and 112 deletions

View File

@ -63,9 +63,9 @@ fi
echo '开始编译调度器'
if [[ $OS = 'windows' ]];then
GOOS=$OS GOARCH=$ARCH go build -tags gocron -ldflags '-w'
CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build -tags gocron -ldflags '-w'
else
GOOS=$OS GOARCH=$ARCH go build -tags gocron -ldflags '-w'
CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build -tags gocron -ldflags '-w'
fi
if [[ $? != 0 ]];then
@ -111,4 +111,4 @@ rm $EXEC_NAME
rm -rf $TEMP_DIR
echo '打包完成'
echo '生成压缩文件--' $COMPRESS_FILE
echo '生成压缩文件--' $COMPRESS_FILE

View File

@ -70,9 +70,9 @@ fi
echo '开始编译任务节点'
if [[ $OS = 'windows' ]];then
GOOS=$OS GOARCH=$ARCH go build -tags node -ldflags '-w' -o $EXEC_NAME
CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build -tags node -ldflags '-w' -o $EXEC_NAME
else
GOOS=$OS GOARCH=$ARCH go build -tags node -ldflags '-w' -o $EXEC_NAME
CGO_ENABLED=0 GOOS=$OS GOARCH=$ARCH go build -tags node -ldflags '-w' -o $EXEC_NAME
fi
if [[ $? != 0 ]];then
@ -90,4 +90,4 @@ fi
rm $EXEC_NAME
echo '打包完成'
echo '生成压缩文件--' $COMPRESS_FILE
echo '生成压缩文件--' $COMPRESS_FILE

View File

@ -1,6 +1,10 @@
package cmd
import (
"os"
"os/signal"
"syscall"
"github.com/ouqiang/gocron/models"
"github.com/ouqiang/gocron/modules/app"
"github.com/ouqiang/gocron/modules/logger"
@ -9,9 +13,6 @@ import (
"github.com/ouqiang/gocron/service"
"github.com/urfave/cli"
"gopkg.in/macaron.v1"
"os"
"os/signal"
"syscall"
)
// web服务器默认端口

View File

@ -3,9 +3,10 @@ package models
import (
"errors"
"fmt"
"strconv"
"github.com/go-xorm/xorm"
"github.com/ouqiang/gocron/modules/logger"
"strconv"
)
type Migration struct{}
@ -172,7 +173,7 @@ func (migration *Migration) upgradeFor130(session *xorm.Session) error {
}
// 升级到v1.4版本
func (migration *Migration) upgradeFor140(session *xorm.Session) error {
func (migration *Migration) upgradeFor140(session *xorm.Session) error {
logger.Info("开始升级到v1.4")
tableName := TablePrefix + "task"
@ -180,16 +181,14 @@ func (migration *Migration) upgradeFor140(session *xorm.Session) error {
// retry_interval 重试间隔时间(秒)
// 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)
"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")
return err
}
}

View File

@ -2,6 +2,9 @@ package models
import (
"fmt"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
@ -9,8 +12,6 @@ import (
"github.com/ouqiang/gocron/modules/logger"
"github.com/ouqiang/gocron/modules/setting"
"gopkg.in/macaron.v1"
"strings"
"time"
)
type Status int8

View File

@ -2,9 +2,10 @@ package models
import (
"errors"
"github.com/go-xorm/xorm"
"strings"
"time"
"github.com/go-xorm/xorm"
)
type TaskProtocol int8
@ -31,17 +32,17 @@ const (
type TaskHTTPMethod int8
const (
TaskHTTPMethodGet TaskHTTPMethod = 1;
TaskHttpMethodPost TaskHTTPMethod = 2;
TaskHTTPMethodGet TaskHTTPMethod = 1
TaskHttpMethodPost TaskHTTPMethod = 2
)
// 任务
type Task struct {
Id int `xorm:"int pk autoincr"`
Name string `xorm:"varchar(32) notnull"` // 任务名称
Level TaskLevel `xorm:"tinyint notnull index default 1"` // 任务等级 1: 主任务 2: 依赖任务
Level TaskLevel `xorm:"tinyint notnull index default 1"` // 任务等级 1: 主任务 2: 依赖任务
DependencyTaskId string `xorm:"varchar(64) notnull default ''"` // 依赖任务ID,多个ID逗号分隔
DependencyStatus TaskDependencyStatus `xorm:"tinyint notnull default 1"` // 依赖关系 1:强依赖 主任务执行成功, 依赖任务才会被执行 2:弱依赖
DependencyStatus TaskDependencyStatus `xorm:"tinyint notnull default 1"` // 依赖关系 1:强依赖 主任务执行成功, 依赖任务才会被执行 2:弱依赖
Spec string `xorm:"varchar(64) notnull"` // crontab
Protocol TaskProtocol `xorm:"tinyint notnull index"` // 协议 1:http 2:系统命令
Command string `xorm:"varchar(256) notnull"` // URL地址或shell命令
@ -49,9 +50,9 @@ type Task struct {
Timeout int `xorm:"mediumint notnull default 0"` // 任务执行超时时间(单位秒),0不限制
Multi int8 `xorm:"tinyint notnull default 1"` // 是否允许多实例运行
RetryTimes int8 `xorm:"tinyint notnull default 0"` // 重试次数
RetryInterval int16 `xorm:"smallint notnull default 0"` // 重试间隔时间
NotifyStatus int8 `xorm:"tinyint notnull default 1"` // 任务执行结束是否通知 0: 不通知 1: 失败通知 2: 执行结束通知
NotifyType int8 `xorm:"tinyint notnull default 0"` // 通知类型 1: 邮件 2: slack
RetryInterval int16 `xorm:"smallint notnull default 0"` // 重试间隔时间
NotifyStatus int8 `xorm:"tinyint notnull default 1"` // 任务执行结束是否通知 0: 不通知 1: 失败通知 2: 执行结束通知
NotifyType int8 `xorm:"tinyint notnull default 0"` // 通知类型 1: 邮件 2: slack
NotifyReceiverId string `xorm:"varchar(256) notnull default '' "` // 通知接受者ID, setting表主键ID多个ID逗号分隔
Tag string `xorm:"varchar(32) notnull default ''"`
Remark string `xorm:"varchar(100) notnull default ''"` // 备注

View File

@ -1,8 +1,9 @@
package models
import (
"github.com/go-xorm/xorm"
"time"
"github.com/go-xorm/xorm"
)
type TaskType int8

View File

@ -1,8 +1,9 @@
package models
import (
"github.com/ouqiang/gocron/modules/utils"
"time"
"github.com/ouqiang/gocron/modules/utils"
)
const PasswordSaltLength = 6
@ -78,7 +79,7 @@ func (user *User) Match(username, password string) bool {
}
// 获取用户详情
func (user *User) Find(id int) (error) {
func (user *User) Find(id int) error {
_, err := Db.Id(id).Get(user)
return err

View File

@ -3,13 +3,14 @@ package app
import (
"os"
"github.com/ouqiang/gocron/modules/logger"
"github.com/ouqiang/gocron/modules/setting"
"github.com/ouqiang/gocron/modules/utils"
"fmt"
"io/ioutil"
"strconv"
"strings"
"fmt"
"github.com/ouqiang/gocron/modules/logger"
"github.com/ouqiang/gocron/modules/setting"
"github.com/ouqiang/gocron/modules/utils"
)
var (

View File

@ -2,10 +2,11 @@ package logger
import (
"fmt"
"github.com/cihub/seelog"
"gopkg.in/macaron.v1"
"os"
"runtime"
"github.com/cihub/seelog"
"gopkg.in/macaron.v1"
)
// 日志库

View File

@ -1,13 +1,14 @@
package notify
import (
"strconv"
"strings"
"time"
"github.com/go-gomail/gomail"
"github.com/ouqiang/gocron/models"
"github.com/ouqiang/gocron/modules/logger"
"github.com/ouqiang/gocron/modules/utils"
"strconv"
"strings"
"time"
)
// @author qiang.ou<qingqianludao@gmail.com>

View File

@ -2,8 +2,9 @@ package notify
import (
"fmt"
"github.com/ouqiang/gocron/modules/logger"
"time"
"github.com/ouqiang/gocron/modules/logger"
)
type Message map[string]interface{}

View File

@ -4,13 +4,14 @@ package notify
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/ouqiang/gocron/models"
"github.com/ouqiang/gocron/modules/httpclient"
"github.com/ouqiang/gocron/modules/logger"
"github.com/ouqiang/gocron/modules/utils"
"strconv"
"strings"
"time"
)
type Slack struct{}

View File

@ -5,8 +5,9 @@ import (
"crypto/x509"
"errors"
"fmt"
"google.golang.org/grpc/credentials"
"io/ioutil"
"google.golang.org/grpc/credentials"
)
type Certificate struct {

View File

@ -3,14 +3,15 @@ package client
import (
"errors"
"fmt"
"sync"
"time"
"github.com/ouqiang/gocron/modules/logger"
"github.com/ouqiang/gocron/modules/rpc/grpcpool"
pb "github.com/ouqiang/gocron/modules/rpc/proto"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"time"
"sync"
)
var (
@ -25,7 +26,7 @@ func generateTaskUniqueKey(ip string, port int, id int64) string {
return fmt.Sprintf("%s:%d:%d", ip, port, id)
}
func Stop(ip string, port int , id int64) {
func Stop(ip string, port int, id int64) {
key := generateTaskUniqueKey(ip, port, id)
cancel, ok := taskMap.Load(key)
if !ok {

View File

@ -2,13 +2,14 @@ package grpcpool
import (
"errors"
"strings"
"sync"
"time"
"github.com/ouqiang/gocron/modules/app"
"github.com/ouqiang/gocron/modules/rpc/auth"
"github.com/silenceper/pool"
"google.golang.org/grpc"
"strings"
"sync"
"time"
)
var (

View File

@ -1,6 +1,8 @@
package server
import (
"net"
"github.com/ouqiang/gocron/modules/rpc/auth"
pb "github.com/ouqiang/gocron/modules/rpc/proto"
"github.com/ouqiang/gocron/modules/utils"
@ -8,7 +10,6 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
"net"
)
type Server struct{}

View File

@ -2,6 +2,7 @@ package setting
import (
"errors"
"github.com/ouqiang/gocron/modules/logger"
"github.com/ouqiang/gocron/modules/utils"
"gopkg.in/ini.v1"

View File

@ -2,6 +2,7 @@ package utils
import (
"encoding/json"
"github.com/ouqiang/gocron/modules/logger"
)

View File

@ -3,11 +3,12 @@ package utils
import (
"crypto/md5"
"encoding/hex"
"github.com/Tang-RoseChild/mahonia"
"math/rand"
"os"
"strings"
"time"
"github.com/Tang-RoseChild/mahonia"
)
// 生成长度为length的随机字符串
@ -92,4 +93,4 @@ func FileExist(file string) bool {
}
return true
}
}

View File

@ -4,9 +4,10 @@ package utils
import (
"errors"
"golang.org/x/net/context"
"os/exec"
"syscall"
"golang.org/x/net/context"
)
type Result struct {

View File

@ -4,10 +4,11 @@ package utils
import (
"errors"
"golang.org/x/net/context"
"os/exec"
"strconv"
"syscall"
"golang.org/x/net/context"
)
type Result struct {

View File

@ -2,6 +2,10 @@ package host
import (
"fmt"
"html/template"
"strconv"
"strings"
"github.com/Unknwon/paginater"
"github.com/go-macaron/binding"
"github.com/ouqiang/gocron/models"
@ -13,9 +17,6 @@ import (
"github.com/ouqiang/gocron/routers/base"
"github.com/ouqiang/gocron/service"
"gopkg.in/macaron.v1"
"html/template"
"strconv"
"strings"
)
// Index 主机列表

View File

@ -2,6 +2,8 @@ package install
import (
"fmt"
"strconv"
"github.com/go-macaron/binding"
"github.com/ouqiang/gocron/models"
"github.com/ouqiang/gocron/modules/app"
@ -9,7 +11,6 @@ import (
"github.com/ouqiang/gocron/modules/utils"
"github.com/ouqiang/gocron/service"
"gopkg.in/macaron.v1"
"strconv"
)
// 系统安装

View File

@ -2,12 +2,13 @@ package loginlog
import (
"fmt"
"html/template"
"github.com/Unknwon/paginater"
"github.com/ouqiang/gocron/models"
"github.com/ouqiang/gocron/modules/logger"
"github.com/ouqiang/gocron/routers/base"
"gopkg.in/macaron.v1"
"html/template"
)
func Index(ctx *macaron.Context) {

View File

@ -2,6 +2,7 @@ package manage
import (
"encoding/json"
"github.com/ouqiang/gocron/models"
"github.com/ouqiang/gocron/modules/logger"
"github.com/ouqiang/gocron/modules/utils"

View File

@ -1,6 +1,12 @@
package routers
import (
"html/template"
"net/http"
"strconv"
"strings"
"time"
"github.com/go-macaron/binding"
"github.com/go-macaron/cache"
"github.com/go-macaron/captcha"
@ -18,11 +24,6 @@ import (
"github.com/ouqiang/gocron/routers/tasklog"
"github.com/ouqiang/gocron/routers/user"
"gopkg.in/macaron.v1"
"html/template"
"strconv"
"strings"
"time"
"net/http"
)
// 静态文件目录
@ -220,13 +221,13 @@ func userAuth(ctx *macaron.Context, sess session.Store) {
}
// URL权限验证
func urlAuth(ctx *macaron.Context, sess session.Store) {
func urlAuth(ctx *macaron.Context, sess session.Store) {
if user.IsAdmin(sess) {
return
}
uri := strings.TrimSpace(ctx.Req.URL.Path)
uri = strings.TrimRight(uri, "/")
if (strings.HasPrefix(uri, "/api")) {
if strings.HasPrefix(uri, "/api") {
return
}
// 普通用户允许访问的URL地址

View File

@ -2,6 +2,10 @@ package task
import (
"fmt"
"html/template"
"strconv"
"strings"
"github.com/Unknwon/paginater"
"github.com/go-macaron/binding"
"github.com/jakecoffman/cron"
@ -11,9 +15,6 @@ import (
"github.com/ouqiang/gocron/routers/base"
"github.com/ouqiang/gocron/service"
"gopkg.in/macaron.v1"
"html/template"
"strconv"
"strings"
)
type TaskForm struct {
@ -23,11 +24,11 @@ type TaskForm struct {
DependencyTaskId string
Name string `binding:"Required;MaxSize(32)"`
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)"`
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
RetryInterval int16
HostId string

View File

@ -4,14 +4,15 @@ package tasklog
import (
"fmt"
"html/template"
"github.com/Unknwon/paginater"
"github.com/ouqiang/gocron/models"
"github.com/ouqiang/gocron/modules/logger"
"github.com/ouqiang/gocron/modules/utils"
"github.com/ouqiang/gocron/routers/base"
"gopkg.in/macaron.v1"
"html/template"
"github.com/ouqiang/gocron/service"
"gopkg.in/macaron.v1"
)
func Index(ctx *macaron.Context) {
@ -50,14 +51,14 @@ func Clear(ctx *macaron.Context) string {
}
// 停止运行中的任务
func Stop(ctx *macaron.Context) string {
func Stop(ctx *macaron.Context) string {
id := ctx.QueryInt64("id")
taskId := ctx.QueryInt("task_id")
taskModel := new(models.Task)
task, err := taskModel.Detail(taskId)
json := utils.JsonResponse{}
if err != nil {
return json.CommonFailure("获取任务信息失败#" + err.Error(), err)
return json.CommonFailure("获取任务信息失败#"+err.Error(), err)
}
if task.Protocol != models.TaskRPC {
return json.CommonFailure("仅支持SHELL任务手动停止")
@ -70,7 +71,7 @@ func Stop(ctx *macaron.Context) string {
}
return json.Success("已执行停止操作, 请等待任务退出", nil);
return json.Success("已执行停止操作, 请等待任务退出", nil)
}
// 删除N个月前的日志

View File

@ -1,6 +1,11 @@
package user
import (
"fmt"
"html/template"
"strings"
"github.com/Unknwon/paginater"
"github.com/go-macaron/captcha"
"github.com/go-macaron/session"
"github.com/ouqiang/gocron/models"
@ -8,10 +13,6 @@ import (
"github.com/ouqiang/gocron/modules/utils"
"github.com/ouqiang/gocron/routers/base"
"gopkg.in/macaron.v1"
"github.com/Unknwon/paginater"
"html/template"
"fmt"
"strings"
)
// @author qiang.ou<qingqianludao@gmail.com>
@ -19,17 +20,17 @@ import (
// UserForm 用户表单
type UserForm struct {
Id int
Name string `binding:"Required;MaxSize(32)"` // 用户名
Password string // 密码
ConfirmPassword string // 确认密码
Email string `binding:"Required;MaxSize(50)"` // 邮箱
IsAdmin int8 // 是否是管理员 1:管理员 0:普通用户
Status models.Status
Id int
Name string `binding:"Required;MaxSize(32)"` // 用户名
Password string // 密码
ConfirmPassword string // 确认密码
Email string `binding:"Required;MaxSize(50)"` // 邮箱
IsAdmin int8 // 是否是管理员 1:管理员 0:普通用户
Status models.Status
}
// Index 用户列表页
func Index(ctx *macaron.Context) {
func Index(ctx *macaron.Context) {
queryParams := parseQueryParams(ctx)
userModel := new(models.User)
users, err := userModel.List(queryParams)
@ -59,7 +60,7 @@ func parseQueryParams(ctx *macaron.Context) models.CommonMap {
}
// Create 新增用户页
func Create(ctx *macaron.Context) {
func Create(ctx *macaron.Context) {
userModel := new(models.User)
userModel.Status = models.Enabled
userModel.IsAdmin = 0
@ -129,9 +130,9 @@ func Store(ctx *macaron.Context, form UserForm) string {
}
} else {
_, err = userModel.Update(form.Id, models.CommonMap{
"name": form.Name,
"email": form.Email,
"status": form.Status,
"name": form.Name,
"email": form.Email,
"status": form.Status,
"is_admin": form.IsAdmin,
})
if err != nil {
@ -139,7 +140,6 @@ func Store(ctx *macaron.Context, form UserForm) string {
}
}
return json.Success("保存成功", nil)
}

View File

@ -3,6 +3,12 @@ package service
import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/jakecoffman/cron"
"github.com/ouqiang/gocron/models"
"github.com/ouqiang/gocron/modules/httpclient"
@ -10,11 +16,6 @@ import (
"github.com/ouqiang/gocron/modules/notify"
rpcClient "github.com/ouqiang/gocron/modules/rpc/client"
pb "github.com/ouqiang/gocron/modules/rpc/proto"
"strconv"
"strings"
"sync"
"time"
"net/http"
)
var (
@ -23,7 +24,7 @@ var (
var (
// 定时任务调度管理器
serviceCron *cron.Cron
serviceCron *cron.Cron
// 同一任务是否有实例处于运行中
runInstance Instance
@ -32,7 +33,7 @@ var (
taskCount TaskCount
)
func init() {
func init() {
serviceCron = cron.New()
serviceCron.Start()
taskCount = TaskCount{sync.WaitGroup{}, make(chan bool)}
@ -41,24 +42,24 @@ func init() {
// 任务计数
type TaskCount struct {
wg sync.WaitGroup
wg sync.WaitGroup
exit chan bool
}
func (tc *TaskCount) Add() {
func (tc *TaskCount) Add() {
tc.wg.Add(1)
}
func (tc *TaskCount) Done() {
func (tc *TaskCount) Done() {
tc.wg.Done()
}
func (tc *TaskCount) Exit() {
func (tc *TaskCount) Exit() {
tc.wg.Done()
<-tc.exit
}
func (tc *TaskCount) Wait() {
func (tc *TaskCount) Wait() {
tc.Add()
tc.wg.Wait()
close(tc.exit)
@ -92,7 +93,6 @@ type TaskResult struct {
RetryTimes int8
}
// 初始化任务, 从数据库取出所有任务, 添加到定时任务并运行
func (task Task) Initialize() {
taskModel := new(models.Task)
@ -137,11 +137,11 @@ func (task Task) Add(taskModel models.Task) {
}
// 停止运行中的任务
func (task Task) Stop(ip string, port int, id int64) {
func (task Task) Stop(ip string, port int, id int64) {
rpcClient.Stop(ip, port, id)
}
func (task Task) Remove(id int) {
func (task Task) Remove(id int) {
serviceCron.RemoveJob(strconv.Itoa(id))
}
@ -171,13 +171,13 @@ func (h *HTTPHandler) Run(taskModel models.Task, taskUniqueId int64) (result str
taskModel.Timeout = HttpExecTimeout
}
var resp httpclient.ResponseWrapper
if (taskModel.HttpMethod == models.TaskHTTPMethodGet) {
if taskModel.HttpMethod == models.TaskHTTPMethodGet {
resp = httpclient.Get(taskModel.Command, taskModel.Timeout)
} else {
urlFields := strings.Split(taskModel.Command, "?")
taskModel.Command = urlFields[0]
var params string
if (len(urlFields) >= 2) {
if len(urlFields) >= 2 {
params = urlFields[1]
}
resp = httpclient.PostParams(taskModel.Command, params, taskModel.Timeout)