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,
GetLogger: func(moduleName string) *log.Entry { return log.WithField("module", moduleName) },
RegisterActor: registerAction,
RegisterCron: cronService.AddFunc,
RegisterTemplateFunction: tplFuncs.Register,
SendMessage: sendMessage,
}

10
main.go
View file

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

View file

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