2022-05-15 21:48:23 -07:00
|
|
|
package cron
|
2022-04-02 12:05:32 -07:00
|
|
|
|
|
|
|
import (
|
2022-06-08 21:17:08 -07:00
|
|
|
"context"
|
|
|
|
"database/sql"
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
_ "github.com/lib/pq"
|
2022-04-02 12:05:32 -07:00
|
|
|
"github.com/robfig/cron/v3"
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2024-04-23 07:15:38 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/database"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/services"
|
2022-04-02 12:05:32 -07:00
|
|
|
)
|
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
type Cron struct {
|
2022-06-30 14:54:58 -07:00
|
|
|
Db *database.Queries
|
2024-05-02 17:36:56 -07:00
|
|
|
ctx context.Context
|
2022-06-19 22:02:44 -07:00
|
|
|
timer *cron.Cron
|
2024-05-01 18:26:32 -07:00
|
|
|
repo services.RepositoryService
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2024-05-01 18:26:32 -07:00
|
|
|
func NewScheduler(ctx context.Context, conn *sql.DB) *Cron {
|
2022-06-19 22:02:44 -07:00
|
|
|
c := &Cron{
|
2024-05-02 17:36:56 -07:00
|
|
|
ctx: ctx,
|
2024-05-01 18:26:32 -07:00
|
|
|
repo: services.NewRepositoryService(conn),
|
2022-06-19 22:02:44 -07:00
|
|
|
}
|
|
|
|
timer := cron.New()
|
|
|
|
|
|
|
|
//timer.AddFunc("*/5 * * * *", func() { go CheckCache() })
|
2024-05-01 17:49:38 -07:00
|
|
|
//features := services.GetEnvConfig()
|
|
|
|
|
2024-05-02 17:36:56 -07:00
|
|
|
timer.AddFunc("5 * * * *", func() { go c.CollectRssPosts() })
|
|
|
|
//timer.AddFunc("10 * * * *", c.CollectRedditPosts)
|
|
|
|
//timer.AddFunc("15 * * * *", c.CheckYoutube)
|
|
|
|
//timer.AddFunc("20 * * * *", c.CheckFfxiv)
|
|
|
|
//timer.AddFunc("25 * * * *", c.CheckTwitch)
|
2024-05-01 18:26:32 -07:00
|
|
|
//timer.AddFunc("*/5 * * * *", c.CheckDiscordQueue)
|
2022-07-12 15:28:31 -07:00
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
c.timer = timer
|
|
|
|
return c
|
|
|
|
}
|
2022-04-29 13:02:25 -07:00
|
|
|
|
2022-06-19 22:02:44 -07:00
|
|
|
func (c *Cron) Start() {
|
|
|
|
c.timer.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cron) Stop() {
|
|
|
|
c.timer.Stop()
|
2022-04-29 13:02:25 -07:00
|
|
|
}
|
|
|
|
|
2024-05-01 18:26:32 -07:00
|
|
|
/*
|
|
|
|
func (c *Cron) CheckDiscordQueue() {
|
2022-06-30 14:54:58 -07:00
|
|
|
// Get items from the table
|
|
|
|
queueItems, err := c.Db.ListDiscordQueueItems(*c.ctx, 50)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
for _, queue := range queueItems {
|
2022-06-30 14:54:58 -07:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-07-12 15:28:31 -07:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-06-30 14:54:58 -07:00
|
|
|
// Get the webhhooks to send to
|
2022-07-12 15:28:31 -07:00
|
|
|
for _, sub := range subs {
|
2022-06-30 14:54:58 -07:00
|
|
|
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
|
2022-07-12 15:28:31 -07:00
|
|
|
dwh := output.NewDiscordWebHookMessage(article)
|
|
|
|
msg, err := dwh.GeneratePayload()
|
2022-06-30 14:54:58 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
|
|
|
|
// Send Message(s)
|
|
|
|
for _, i := range endpoints {
|
|
|
|
err = dwh.SendPayload(msg, i)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-30 14:54:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the item from the queue, given we sent our notification.
|
|
|
|
err = c.Db.DeleteDiscordQueueItem(*c.ctx, queue.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-12 15:28:31 -07:00
|
|
|
|
|
|
|
time.Sleep(10 * time.Second)
|
2022-06-08 21:17:08 -07:00
|
|
|
}
|
2022-05-15 21:48:23 -07:00
|
|
|
|
2022-06-08 21:17:08 -07:00
|
|
|
return nil
|
|
|
|
}
|
2024-05-01 18:26:32 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
//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
|
|
|
|
//}
|