2025-07-18 15:27:22 +08:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"apigo.cc/gojs"
|
|
|
|
"apigo.cc/gojs/goja"
|
|
|
|
"github.com/robfig/cron/v3"
|
2025-07-21 13:20:25 +08:00
|
|
|
"github.com/ssgo/config"
|
|
|
|
"github.com/ssgo/log"
|
|
|
|
"github.com/ssgo/u"
|
2025-07-18 15:27:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const pluginName = "task"
|
|
|
|
|
2025-07-21 13:20:25 +08:00
|
|
|
var logger = log.DefaultLogger
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
HotLoad *bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var conf = Config{
|
|
|
|
HotLoad: u.BoolPtr(false),
|
|
|
|
}
|
|
|
|
var waitChan chan struct{}
|
|
|
|
|
2025-07-18 15:27:22 +08:00
|
|
|
type PluginObject struct {
|
|
|
|
skipCron *cron.Cron
|
|
|
|
delayCron *cron.Cron
|
|
|
|
// asyncCron *cron.Cron
|
2025-07-21 13:20:25 +08:00
|
|
|
isStarted bool
|
|
|
|
monitorTaskId cron.EntryID
|
|
|
|
lock sync.RWMutex
|
|
|
|
tasks map[string]*Task
|
|
|
|
taskData map[string]any
|
|
|
|
taskDataLock sync.RWMutex
|
2025-07-18 15:27:22 +08:00
|
|
|
|
|
|
|
taskList map[string]*list.List
|
|
|
|
taskListLock sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
var _pluginObject = &PluginObject{
|
2025-07-21 13:20:25 +08:00
|
|
|
skipCron: cron.New(cron.WithSeconds(), cron.WithChain(cron.SkipIfStillRunning(cron.DefaultLogger))),
|
|
|
|
delayCron: cron.New(cron.WithSeconds()),
|
|
|
|
tasks: map[string]*Task{},
|
|
|
|
taskData: map[string]any{},
|
|
|
|
taskList: map[string]*list.List{},
|
2025-07-18 15:27:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2025-07-21 13:20:25 +08:00
|
|
|
config.LoadConfig(pluginName, &conf)
|
2025-07-18 15:27:22 +08:00
|
|
|
tsCode := gojs.MakeTSCode(_pluginObject)
|
|
|
|
mappedObj := gojs.ToMap(_pluginObject)
|
|
|
|
gojs.Register("apigo.cc/gojs/"+pluginName, gojs.Module{
|
|
|
|
ObjectMaker: func(vm *goja.Runtime) gojs.Map {
|
|
|
|
return mappedObj
|
|
|
|
},
|
|
|
|
OnKill: func() {
|
|
|
|
_pluginObject.Stop()
|
|
|
|
},
|
2025-07-21 13:20:25 +08:00
|
|
|
WaitForStop: func() {
|
|
|
|
if waitChan != nil {
|
|
|
|
<-waitChan
|
|
|
|
}
|
|
|
|
},
|
2025-07-18 15:27:22 +08:00
|
|
|
TsCode: tsCode,
|
|
|
|
Desc: "task api",
|
|
|
|
})
|
|
|
|
}
|
2025-07-21 13:20:25 +08:00
|
|
|
|
|
|
|
func (obj *PluginObject) Config(cfg Config) {
|
|
|
|
if cfg.HotLoad != nil {
|
|
|
|
conf.HotLoad = cfg.HotLoad
|
|
|
|
}
|
|
|
|
}
|