Provide central cron service to plugins

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-08-25 00:44:49 +02:00
parent 984223e6ac
commit 47f314af4e
Signed by: luzifer
GPG Key ID: 0066F03ED215AD7D
3 changed files with 14 additions and 2 deletions

View File

@ -36,6 +36,7 @@ func getRegistrationArguments() plugins.RegistrationArguments {
FormatMessage: formatMessage, FormatMessage: formatMessage,
GetLogger: func(moduleName string) *log.Entry { return log.WithField("module", moduleName) }, GetLogger: func(moduleName string) *log.Entry { return log.WithField("module", moduleName) },
RegisterActor: registerAction, RegisterActor: registerAction,
RegisterCron: cronService.AddFunc,
RegisterTemplateFunction: tplFuncs.Register, RegisterTemplateFunction: tplFuncs.Register,
SendMessage: sendMessage, SendMessage: sendMessage,
} }

10
main.go
View File

@ -9,6 +9,7 @@ import (
"github.com/go-irc/irc" "github.com/go-irc/irc"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/Luzifer/go_helpers/v2/str" "github.com/Luzifer/go_helpers/v2/str"
@ -35,6 +36,8 @@ var (
config *configFile config *configFile
configLock = new(sync.RWMutex) configLock = new(sync.RWMutex)
cronService *cron.Cron
sendMessage func(m *irc.Message) error sendMessage func(m *irc.Message) error
store = newStorageFile(false) store = newStorageFile(false)
@ -73,12 +76,13 @@ func init() {
func main() { func main() {
var err error var err error
cronService = cron.New()
twitchClient = twitch.New(cfg.TwitchClient, cfg.TwitchToken)
if err = loadPlugins(cfg.PluginDir); err != nil { if err = loadPlugins(cfg.PluginDir); err != nil {
log.WithError(err).Fatal("Unable to load plugins") log.WithError(err).Fatal("Unable to load plugins")
} }
twitchClient = twitch.New(cfg.TwitchClient, cfg.TwitchToken)
if err = loadConfig(cfg.Config); err != nil { if err = loadConfig(cfg.Config); err != nil {
log.WithError(err).Fatal("Initial config load failed") log.WithError(err).Fatal("Initial config load failed")
} }
@ -107,6 +111,8 @@ func main() {
autoMessageTicker = time.NewTicker(time.Second) autoMessageTicker = time.NewTicker(time.Second)
) )
cronService.Start()
ircDisconnected <- struct{}{} ircDisconnected <- struct{}{}
for { for {

View File

@ -2,6 +2,7 @@ package plugins
import ( import (
"github.com/go-irc/irc" "github.com/go-irc/irc"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -22,6 +23,8 @@ type (
ActorRegistrationFunc func(ActorCreationFunc) ActorRegistrationFunc func(ActorCreationFunc)
CronRegistrationFunc func(spec string, cmd func()) (cron.EntryID, error)
LoggerCreationFunc func(moduleName string) *log.Entry LoggerCreationFunc func(moduleName string) *log.Entry
MsgFormatter func(tplString string, m *irc.Message, r *Rule, fields map[string]interface{}) (string, error) MsgFormatter func(tplString string, m *irc.Message, r *Rule, fields map[string]interface{}) (string, error)
@ -36,6 +39,8 @@ type (
GetLogger LoggerCreationFunc GetLogger LoggerCreationFunc
// RegisterActor is used to register a new IRC rule-actor implementing the Actor interface // RegisterActor is used to register a new IRC rule-actor implementing the Actor interface
RegisterActor ActorRegistrationFunc RegisterActor ActorRegistrationFunc
// RegisterCron is a method to register cron functions in the global cron instance
RegisterCron CronRegistrationFunc
// RegisterTemplateFunction can be used to register a new template functions // RegisterTemplateFunction can be used to register a new template functions
RegisterTemplateFunction TemplateFuncRegister RegisterTemplateFunction TemplateFuncRegister
// SendMessage can be used to send a message not triggered by an event // SendMessage can be used to send a message not triggered by an event