From 30b562223a0ee296878d4fbcc0c9185601652276 Mon Sep 17 00:00:00 2001 From: ouqiang Date: Fri, 10 Mar 2017 18:08:45 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=BE=AA=E7=8E=AF=E4=BE=9D?= =?UTF-8?q?=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/web.go | 14 +++++++------- models/model.go | 3 ++- service/task.go | 25 ------------------------- utils/app/app.go | 22 ++-------------------- utils/cron_task.go | 13 ++++++++++--- utils/utils.go | 27 +++++++++++++++++++++++++++ 6 files changed, 48 insertions(+), 56 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index 906c4ff..29b829d 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -6,9 +6,9 @@ import ( "github.com/go-macaron/gzip" "github.com/go-macaron/session" "github.com/go-macaron/csrf" - "scheduler/utils/app" + "scheduler/utils" "fmt" - "os" + "scheduler/utils/app" ) // web服务器默认端口 @@ -31,7 +31,7 @@ var CmdWeb = cli.Command{ func run(ctx *cli.Context) { // 检测环境 - app.CheckEnv() + utils.CheckEnv() // 启动定时任务 runScheduler() m := macaron.Classic() @@ -44,15 +44,15 @@ func run(ctx *cli.Context) { } // 定时任务调度 -func runScheduler() { - fmt.Println("hello world") - os.Exit(1) -} +func runScheduler() {} // 路由注册 func registerRouter(m *macaron.Macaron) { // 所有GET方法,自动注册HEAD方法 m.SetAutoHead(true) + m.Get("/", func(ctx *macaron.Context) (string) { + return "go home" + }) } // 中间件注册 diff --git a/models/model.go b/models/model.go index a48dcb9..7684fd6 100644 --- a/models/model.go +++ b/models/model.go @@ -10,7 +10,7 @@ import ( "scheduler/utils/app" ) -var Db *xorm.Engine = nil +var Db *xorm.Engine func init() { if app.Installed { @@ -35,6 +35,7 @@ const ( MaxPageSize = 1000 // 每次最多取多少条 ) +// 创建Db func createDb() *xorm.Engine{ config,err := setting.Read() if err != nil { diff --git a/service/task.go b/service/task.go index 5c7c25c..8a1f18b 100644 --- a/service/task.go +++ b/service/task.go @@ -7,33 +7,8 @@ import ( "io/ioutil" "strconv" "time" - "sync" ) -type Host struct { - sync.RWMutex - hosts []models.Host -} - -var host Host = &Host{ - sync.RWMutex{}, - hosts: initHosts(), -} - -func GetHosts() []models.Host { - host.RLock() - defer host.RUnlock() - - return host.hosts -} - -func SetHosts(h []models.Host) { - host.Lock() - defer host.Unlock() - - host.hosts = h -} - func initHosts() []models.Host { // 获取所有主机 hostModel := new(models.Host) diff --git a/utils/app/app.go b/utils/app/app.go index 3b5c1c0..d811900 100644 --- a/utils/app/app.go +++ b/utils/app/app.go @@ -2,8 +2,6 @@ package app import ( "os" - "scheduler/utils" - "runtime" ) var ( @@ -12,7 +10,7 @@ var ( LogDir string // 日志目录 DataDir string // 数据目录,存放session文件等 AppConfig string // 应用配置文件 - Installed bool = isInstalled() // 应用是否安装过 + Installed bool // 应用是否安装过 ) func init() { @@ -26,23 +24,7 @@ func init() { DataDir = AppDir + "/data" AppConfig = AppDir + "/app.ini" checkDirExists(ConfDir, LogDir, DataDir) -} - - -// 检测环境 -func CheckEnv() { - // ansible不支持安装在windows上, windows只能作为被控机 - if runtime.GOOS == "windows" { - panic("不支持在windows上运行") - } - _, err := utils.ExecShell("ansible", "--version") - if err != nil { - panic(err) - } - _, err = utils.ExecShell("ansible-playbook", "--version") - if err != nil { - panic("ansible-playbook not found") - } + Installed = isInstalled() } // 判断应用是否安装过 diff --git a/utils/cron_task.go b/utils/cron_task.go index 9b1f7f4..b0969cc 100644 --- a/utils/cron_task.go +++ b/utils/cron_task.go @@ -3,18 +3,25 @@ package utils import ( "github.com/robfig/cron" "errors" + "scheduler/utils/app" ) // todo map并发访问加锁 -var DefaultCronTask = &CronTask{ - make(map[string]*cron.Cron), -} +var DefaultCronTask CronTask; type CronTask struct { tasks map[string]*cron.Cron } +func init() { + if app.Installed { + DefaultCronTask = CronTask{ + make(map[string]*cron.Cron), + } + } +} + // 新增定时任务,如果name存在,则添加失败 func(cronTask *CronTask) Add(name string, spec string, cmd func() ) error { if name == "" || spec == "" || cmd == nil { diff --git a/utils/utils.go b/utils/utils.go index 76ae4b9..0bee367 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -7,8 +7,35 @@ import ( "crypto/md5" "encoding/hex" "log" + "os" + "runtime" + "scheduler/utils/app" ) +// 检测环境 +func CheckEnv() { + // ansible不支持安装在windows上, windows只能作为被控机 + if runtime.GOOS == "windows" { + panic("不支持在windows上运行") + } + _, err := ExecShell("ansible", "--version") + if err != nil { + panic(err) + } + _, err = ExecShell("ansible-playbook", "--version") + if err != nil { + panic("ansible-playbook not found") + } +} + +// 创建安装锁文件 +func CreateInstallLock() { + _, err := os.Create(app.ConfDir + "/install.lock") + if err != nil { + RecordLog("创建安装锁文件失败") + } +} + // 执行shell命令 func ExecShell(command string, args... string) (string, error) { result, err := exec.Command(command, args...).CombinedOutput()