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/gzip"
|
||||||
"github.com/go-macaron/session"
|
"github.com/go-macaron/session"
|
||||||
"github.com/go-macaron/csrf"
|
"github.com/go-macaron/csrf"
|
||||||
"scheduler/utils/app"
|
"scheduler/utils"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"scheduler/utils/app"
|
||||||
)
|
)
|
||||||
|
|
||||||
// web服务器默认端口
|
// web服务器默认端口
|
||||||
|
@ -31,7 +31,7 @@ var CmdWeb = cli.Command{
|
||||||
|
|
||||||
func run(ctx *cli.Context) {
|
func run(ctx *cli.Context) {
|
||||||
// 检测环境
|
// 检测环境
|
||||||
app.CheckEnv()
|
utils.CheckEnv()
|
||||||
// 启动定时任务
|
// 启动定时任务
|
||||||
runScheduler()
|
runScheduler()
|
||||||
m := macaron.Classic()
|
m := macaron.Classic()
|
||||||
|
@ -44,15 +44,15 @@ func run(ctx *cli.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 定时任务调度
|
// 定时任务调度
|
||||||
func runScheduler() {
|
func runScheduler() {}
|
||||||
fmt.Println("hello world")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 路由注册
|
// 路由注册
|
||||||
func registerRouter(m *macaron.Macaron) {
|
func registerRouter(m *macaron.Macaron) {
|
||||||
// 所有GET方法,自动注册HEAD方法
|
// 所有GET方法,自动注册HEAD方法
|
||||||
m.SetAutoHead(true)
|
m.SetAutoHead(true)
|
||||||
|
m.Get("/", func(ctx *macaron.Context) (string) {
|
||||||
|
return "go home"
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 中间件注册
|
// 中间件注册
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"scheduler/utils/app"
|
"scheduler/utils/app"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Db *xorm.Engine = nil
|
var Db *xorm.Engine
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if app.Installed {
|
if app.Installed {
|
||||||
|
@ -35,6 +35,7 @@ const (
|
||||||
MaxPageSize = 1000 // 每次最多取多少条
|
MaxPageSize = 1000 // 每次最多取多少条
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 创建Db
|
||||||
func createDb() *xorm.Engine{
|
func createDb() *xorm.Engine{
|
||||||
config,err := setting.Read()
|
config,err := setting.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -7,33 +7,8 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"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 {
|
func initHosts() []models.Host {
|
||||||
// 获取所有主机
|
// 获取所有主机
|
||||||
hostModel := new(models.Host)
|
hostModel := new(models.Host)
|
||||||
|
|
|
@ -2,8 +2,6 @@ package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"scheduler/utils"
|
|
||||||
"runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -12,7 +10,7 @@ var (
|
||||||
LogDir string // 日志目录
|
LogDir string // 日志目录
|
||||||
DataDir string // 数据目录,存放session文件等
|
DataDir string // 数据目录,存放session文件等
|
||||||
AppConfig string // 应用配置文件
|
AppConfig string // 应用配置文件
|
||||||
Installed bool = isInstalled() // 应用是否安装过
|
Installed bool // 应用是否安装过
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -26,23 +24,7 @@ func init() {
|
||||||
DataDir = AppDir + "/data"
|
DataDir = AppDir + "/data"
|
||||||
AppConfig = AppDir + "/app.ini"
|
AppConfig = AppDir + "/app.ini"
|
||||||
checkDirExists(ConfDir, LogDir, DataDir)
|
checkDirExists(ConfDir, LogDir, DataDir)
|
||||||
}
|
Installed = isInstalled()
|
||||||
|
|
||||||
|
|
||||||
// 检测环境
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断应用是否安装过
|
// 判断应用是否安装过
|
||||||
|
|
|
@ -3,18 +3,25 @@ package utils
|
||||||
import (
|
import (
|
||||||
"github.com/robfig/cron"
|
"github.com/robfig/cron"
|
||||||
"errors"
|
"errors"
|
||||||
|
"scheduler/utils/app"
|
||||||
)
|
)
|
||||||
|
|
||||||
// todo map并发访问加锁
|
// todo map并发访问加锁
|
||||||
|
|
||||||
var DefaultCronTask = &CronTask{
|
var DefaultCronTask CronTask;
|
||||||
make(map[string]*cron.Cron),
|
|
||||||
}
|
|
||||||
|
|
||||||
type CronTask struct {
|
type CronTask struct {
|
||||||
tasks map[string]*cron.Cron
|
tasks map[string]*cron.Cron
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if app.Installed {
|
||||||
|
DefaultCronTask = CronTask{
|
||||||
|
make(map[string]*cron.Cron),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 新增定时任务,如果name存在,则添加失败
|
// 新增定时任务,如果name存在,则添加失败
|
||||||
func(cronTask *CronTask) Add(name string, spec string, cmd func() ) error {
|
func(cronTask *CronTask) Add(name string, spec string, cmd func() ) error {
|
||||||
if name == "" || spec == "" || cmd == nil {
|
if name == "" || spec == "" || cmd == nil {
|
||||||
|
|
|
@ -7,8 +7,35 @@ import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"log"
|
"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命令
|
// 执行shell命令
|
||||||
func ExecShell(command string, args... string) (string, error) {
|
func ExecShell(command string, args... string) (string, error) {
|
||||||
result, err := exec.Command(command, args...).CombinedOutput()
|
result, err := exec.Command(command, args...).CombinedOutput()
|
||||||
|
|
Loading…
Reference in New Issue