mirror of https://github.com/ouqiang/gocron
解决循环依赖
parent
8c93bb3c93
commit
30b562223a
14
cmd/web.go
14
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"
|
||||
})
|
||||
}
|
||||
|
||||
// 中间件注册
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
||||
// 判断应用是否安装过
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue