132 lines
2.8 KiB
Go
132 lines
2.8 KiB
Go
package cron
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
_ "github.com/lib/pq"
|
|
"github.com/robfig/cron/v3"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/database"
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/services"
|
|
)
|
|
|
|
type Cron struct {
|
|
Db *database.Queries
|
|
ctx *context.Context
|
|
timer *cron.Cron
|
|
repo services.RepositoryService
|
|
}
|
|
|
|
func NewScheduler(ctx context.Context, conn *sql.DB) *Cron {
|
|
c := &Cron{
|
|
ctx: &ctx,
|
|
repo: services.NewRepositoryService(conn),
|
|
}
|
|
timer := cron.New()
|
|
|
|
//timer.AddFunc("*/5 * * * *", func() { go CheckCache() })
|
|
//features := services.GetEnvConfig()
|
|
|
|
timer.AddFunc("5 * * * *", c.CollectRssPosts)
|
|
//timer.AddFunc("5 1-23 * * *", c.CollectRedditPosts)
|
|
//timer.AddFunc("10 1-23 * * *", c.CheckYoutube)
|
|
//timer.AddFunc("5 5,10,15,20 * * *", c.CheckFfxiv)
|
|
//timer.AddFunc("15 1-23 * * *", c.CheckTwitch)
|
|
//timer.AddFunc("*/5 * * * *", c.CheckDiscordQueue)
|
|
|
|
c.timer = timer
|
|
return c
|
|
}
|
|
|
|
func (c *Cron) Start() {
|
|
c.timer.Start()
|
|
}
|
|
|
|
func (c *Cron) Stop() {
|
|
c.timer.Stop()
|
|
}
|
|
|
|
/*
|
|
func (c *Cron) CheckDiscordQueue() {
|
|
// Get items from the table
|
|
queueItems, err := c.Db.ListDiscordQueueItems(*c.ctx, 50)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, queue := range queueItems {
|
|
// Get the articleByID
|
|
article, err := c.Db.GetArticleByID(*c.ctx, queue.Articleid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var endpoints []string
|
|
// List Subscription by SourceID
|
|
subs, err := c.Db.ListSubscriptionsBySourceId(*c.ctx, article.Sourceid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// if no one is subscribed to it, remove it from the index.
|
|
if len(subs) == 0 {
|
|
log.Printf("No subscriptions found bound to '%v' so it was removed.", article.Sourceid)
|
|
err = c.Db.DeleteDiscordQueueItem(*c.ctx, queue.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
continue
|
|
}
|
|
|
|
// Get the webhhooks to send to
|
|
for _, sub := range subs {
|
|
webhook, err := c.Db.GetDiscordWebHooksByID(*c.ctx, sub.Discordwebhookid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// store them in an array
|
|
endpoints = append(endpoints, webhook.Url)
|
|
}
|
|
|
|
// Create Discord Message
|
|
dwh := output.NewDiscordWebHookMessage(article)
|
|
msg, err := dwh.GeneratePayload()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Send Message(s)
|
|
for _, i := range endpoints {
|
|
err = dwh.SendPayload(msg, i)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Remove the item from the queue, given we sent our notification.
|
|
err = c.Db.DeleteDiscordQueueItem(*c.ctx, queue.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
time.Sleep(10 * time.Second)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
*/
|
|
|
|
//func (c *Cron) addToDiscordQueue(Id uuid.UUID) error {
|
|
// err := c.Db.CreateDiscordQueue(*c.ctx, database.CreateDiscordQueueParams{
|
|
// ID: uuid.New(),
|
|
// Articleid: Id,
|
|
// })
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
// return nil
|
|
//}
|