add scheduler onto application struct and set the timer on creation

This commit is contained in:
lyx0 2022-08-23 16:37:45 +02:00
parent ca1f4a35c4
commit 9bf18d3cdf
2 changed files with 11 additions and 9 deletions

View file

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/gempir/go-twitch-irc/v3" "github.com/gempir/go-twitch-irc/v3"
"github.com/go-co-op/gocron"
"github.com/joho/godotenv" "github.com/joho/godotenv"
_ "github.com/lib/pq" _ "github.com/lib/pq"
"github.com/lyx0/nourybot/internal/data" "github.com/lyx0/nourybot/internal/data"
@ -33,6 +34,7 @@ type Application struct {
Logger *zap.SugaredLogger Logger *zap.SugaredLogger
Db *sql.DB Db *sql.DB
Models data.Models Models data.Models
Scheduler *gocron.Scheduler
} }
func main() { func main() {
@ -69,12 +71,15 @@ func main() {
sugar.Fatal(err) sugar.Fatal(err)
} }
s := gocron.NewScheduler(time.UTC)
// Initialize Application // Initialize Application
app := &Application{ app := &Application{
TwitchClient: tc, TwitchClient: tc,
Logger: sugar, Logger: sugar,
Db: db, Db: db,
Models: data.NewModels(db), Models: data.NewModels(db),
Scheduler: s,
} }
// Received a PrivateMessage (normal chat message). // Received a PrivateMessage (normal chat message).
@ -143,6 +148,8 @@ func main() {
common.Send("uudelleenkytkeytynyt", "PepeS", app.TwitchClient) common.Send("uudelleenkytkeytynyt", "PepeS", app.TwitchClient)
}) })
s.StartAsync()
// Actually connect to chat. // Actually connect to chat.
err = app.TwitchClient.Connect() err = app.TwitchClient.Connect()
if err != nil { if err != nil {

View file

@ -6,7 +6,6 @@ import (
"time" "time"
"github.com/gempir/go-twitch-irc/v3" "github.com/gempir/go-twitch-irc/v3"
"github.com/go-co-op/gocron"
"github.com/lyx0/nourybot/internal/data" "github.com/lyx0/nourybot/internal/data"
"github.com/lyx0/nourybot/pkg/common" "github.com/lyx0/nourybot/pkg/common"
) )
@ -39,13 +38,12 @@ func (app *Application) AddTimer(name string, message twitch.PrivateMessage) {
app.Logger.Infow("timer", timer) app.Logger.Infow("timer", timer)
err := app.Models.Timers.Insert(timer) err := app.Models.Timers.Insert(timer)
if err != nil { if err != nil {
reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err) reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err)
common.Send(message.Channel, reply, app.TwitchClient) common.Send(message.Channel, reply, app.TwitchClient)
return return
} else { } else {
app.Scheduler.Tag("pipelines", fmt.Sprintf("%s-%s", message.Channel, name)).Every(repeat).StartAt(time.Now()).Do(app.newTimer, message.Channel, text)
reply := fmt.Sprintf("Successfully added timer %s repeating every %s", name, repeat) reply := fmt.Sprintf("Successfully added timer %s repeating every %s", name, repeat)
common.Send(message.Channel, reply, app.TwitchClient) common.Send(message.Channel, reply, app.TwitchClient)
return return
@ -64,10 +62,8 @@ func (app *Application) InitialTimers() {
app.Logger.Info(timer) app.Logger.Info(timer)
s := gocron.NewScheduler(time.UTC)
// Iterate over the slice of channels and join each. // Iterate over the slice of channels and join each.
for _, v := range timer { for _, v := range timer {
s.TagsUnique()
app.Logger.Infow("Initial timers:", app.Logger.Infow("Initial timers:",
"Name", v.Name, "Name", v.Name,
"Channel", v.Channel, "Channel", v.Channel,
@ -76,12 +72,11 @@ func (app *Application) InitialTimers() {
"V", v, "V", v,
) )
s.Tag("pipelines", fmt.Sprintf("%s-%s", v.Channel, v.Name)).Every(v.Repeat).StartAt(time.Now()).Do(app.xD, v.Channel, v.Text) app.Scheduler.Tag("pipelines", fmt.Sprintf("%s-%s", v.Channel, v.Name)).Every(v.Repeat).StartAt(time.Now()).Do(app.newTimer, v.Channel, v.Text)
} }
s.StartAsync()
} }
func (app *Application) xD(a, b string) { func (app *Application) newTimer(channel, text string) {
common.Send(a, b, app.TwitchClient) common.Send(channel, text, app.TwitchClient)
} }