2019-12-08 03:59:58 -05:00
|
|
|
|
package dao
|
|
|
|
|
|
|
|
|
|
import (
|
2021-01-23 20:41:35 -05:00
|
|
|
|
"fmt"
|
2021-01-08 08:04:50 -05:00
|
|
|
|
"sort"
|
2019-12-09 05:14:31 -05:00
|
|
|
|
"sync"
|
|
|
|
|
|
2019-12-08 03:59:58 -05:00
|
|
|
|
"github.com/patrickmn/go-cache"
|
2021-01-18 20:59:04 -05:00
|
|
|
|
"github.com/robfig/cron/v3"
|
2020-12-19 10:11:16 -05:00
|
|
|
|
"gorm.io/gorm"
|
2019-12-08 03:59:58 -05:00
|
|
|
|
|
2020-11-10 21:07:45 -05:00
|
|
|
|
"github.com/naiba/nezha/model"
|
2021-01-23 20:41:35 -05:00
|
|
|
|
pb "github.com/naiba/nezha/proto"
|
2019-12-08 03:59:58 -05:00
|
|
|
|
)
|
|
|
|
|
|
2021-09-29 07:58:02 -04:00
|
|
|
|
var Version = "v0.10.2" // !!记得修改 README 中的 badge 版本!!
|
2020-12-19 23:18:27 -05:00
|
|
|
|
|
2021-01-23 20:41:35 -05:00
|
|
|
|
var (
|
|
|
|
|
Conf *model.Config
|
|
|
|
|
Cache *cache.Cache
|
|
|
|
|
DB *gorm.DB
|
2021-01-17 09:18:36 -05:00
|
|
|
|
|
2021-01-23 20:41:35 -05:00
|
|
|
|
ServerList map[uint64]*model.Server
|
2021-01-30 04:10:51 -05:00
|
|
|
|
SecretToID map[string]uint64
|
2021-01-23 20:41:35 -05:00
|
|
|
|
ServerLock sync.RWMutex
|
2021-01-18 20:59:04 -05:00
|
|
|
|
|
2021-01-23 20:41:35 -05:00
|
|
|
|
SortedServerList []*model.Server
|
|
|
|
|
SortedServerLock sync.RWMutex
|
|
|
|
|
)
|
2021-01-18 20:59:04 -05:00
|
|
|
|
|
2021-01-08 08:04:50 -05:00
|
|
|
|
func ReSortServer() {
|
2021-01-17 09:18:36 -05:00
|
|
|
|
ServerLock.RLock()
|
|
|
|
|
defer ServerLock.RUnlock()
|
|
|
|
|
SortedServerLock.Lock()
|
|
|
|
|
defer SortedServerLock.Unlock()
|
|
|
|
|
|
2021-01-08 08:04:50 -05:00
|
|
|
|
SortedServerList = []*model.Server{}
|
|
|
|
|
for _, s := range ServerList {
|
|
|
|
|
SortedServerList = append(SortedServerList, s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sort.SliceStable(SortedServerList, func(i, j int) bool {
|
2021-01-20 06:24:59 -05:00
|
|
|
|
if SortedServerList[i].DisplayIndex == SortedServerList[j].DisplayIndex {
|
2021-04-20 07:30:34 -04:00
|
|
|
|
return SortedServerList[i].ID < SortedServerList[j].ID
|
2021-01-20 06:24:59 -05:00
|
|
|
|
}
|
2021-01-08 08:04:50 -05:00
|
|
|
|
return SortedServerList[i].DisplayIndex > SortedServerList[j].DisplayIndex
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-01-23 20:41:35 -05:00
|
|
|
|
|
|
|
|
|
// =============== Cron Mixin ===============
|
|
|
|
|
|
|
|
|
|
var CronLock sync.RWMutex
|
|
|
|
|
var Crons map[uint64]*model.Cron
|
|
|
|
|
var Cron *cron.Cron
|
|
|
|
|
|
2021-06-21 09:30:42 -04:00
|
|
|
|
func ManualTrigger(c *model.Cron) {
|
2021-01-23 20:41:35 -05:00
|
|
|
|
ServerLock.RLock()
|
|
|
|
|
defer ServerLock.RUnlock()
|
|
|
|
|
for j := 0; j < len(c.Servers); j++ {
|
|
|
|
|
if ServerList[c.Servers[j]].TaskStream != nil {
|
|
|
|
|
ServerList[c.Servers[j]].TaskStream.Send(&pb.Task{
|
|
|
|
|
Id: c.ID,
|
|
|
|
|
Data: c.Command,
|
|
|
|
|
Type: model.TaskTypeCommand,
|
|
|
|
|
})
|
|
|
|
|
} else {
|
2021-09-27 09:18:09 -04:00
|
|
|
|
SendNotification(fmt.Sprintf("[任务失败] %s,服务器 %s 离线,无法执行。", c.Name, ServerList[c.Servers[j]].Name), false)
|
2021-01-23 20:41:35 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-21 09:30:42 -04:00
|
|
|
|
|
|
|
|
|
func CronTrigger(cr model.Cron) func() {
|
|
|
|
|
crIgnoreMap := make(map[uint64]bool)
|
|
|
|
|
for j := 0; j < len(cr.Servers); j++ {
|
|
|
|
|
crIgnoreMap[cr.Servers[j]] = true
|
|
|
|
|
}
|
|
|
|
|
return func() {
|
|
|
|
|
ServerLock.RLock()
|
|
|
|
|
defer ServerLock.RUnlock()
|
|
|
|
|
for _, s := range ServerList {
|
|
|
|
|
if cr.Cover == model.CronCoverAll && crIgnoreMap[s.ID] {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if cr.Cover == model.CronCoverIgnoreAll && !crIgnoreMap[s.ID] {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if s.TaskStream != nil {
|
|
|
|
|
s.TaskStream.Send(&pb.Task{
|
|
|
|
|
Id: cr.ID,
|
|
|
|
|
Data: cr.Command,
|
|
|
|
|
Type: model.TaskTypeCommand,
|
|
|
|
|
})
|
|
|
|
|
} else {
|
2021-09-27 09:18:09 -04:00
|
|
|
|
SendNotification(fmt.Sprintf("[任务失败] %s,服务器 %s 离线,无法执行。", cr.Name, s.Name), false)
|
2021-06-21 09:30:42 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|