task/plugin.go
2025-07-21 13:20:25 +08:00

77 lines
1.5 KiB
Go

package plugin
import (
"container/list"
"sync"
"apigo.cc/gojs"
"apigo.cc/gojs/goja"
"github.com/robfig/cron/v3"
"github.com/ssgo/config"
"github.com/ssgo/log"
"github.com/ssgo/u"
)
const pluginName = "task"
var logger = log.DefaultLogger
type Config struct {
HotLoad *bool
}
var conf = Config{
HotLoad: u.BoolPtr(false),
}
var waitChan chan struct{}
type PluginObject struct {
skipCron *cron.Cron
delayCron *cron.Cron
// asyncCron *cron.Cron
isStarted bool
monitorTaskId cron.EntryID
lock sync.RWMutex
tasks map[string]*Task
taskData map[string]any
taskDataLock sync.RWMutex
taskList map[string]*list.List
taskListLock sync.RWMutex
}
var _pluginObject = &PluginObject{
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{},
}
func init() {
config.LoadConfig(pluginName, &conf)
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()
},
WaitForStop: func() {
if waitChan != nil {
<-waitChan
}
},
TsCode: tsCode,
Desc: "task api",
})
}
func (obj *PluginObject) Config(cfg Config) {
if cfg.HotLoad != nil {
conf.HotLoad = cfg.HotLoad
}
}