From 852db161c990913174ca73b2e80d059f62028d91 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Thu, 2 May 2024 17:36:56 -0700 Subject: [PATCH] changed how cron uses context --- internal/services/cron/collectors.go | 30 ++++++++++++++-------------- internal/services/cron/scheduler.go | 14 ++++++------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/internal/services/cron/collectors.go b/internal/services/cron/collectors.go index 6e0ed32..4d4a93d 100644 --- a/internal/services/cron/collectors.go +++ b/internal/services/cron/collectors.go @@ -10,7 +10,7 @@ import ( func (c *Cron) CollectRssPosts() { log.Println("Starting ") - sources, err := c.repo.Sources.ListBySource(*c.ctx, 0, 1000, domain.SourceCollectorRss) + sources, err := c.repo.Sources.ListBySource(c.ctx, 0, 1000, domain.SourceCollectorRss) if err != nil { log.Println(err) } @@ -27,12 +27,12 @@ func (c *Cron) CollectRssPosts() { } for _, article := range articles { - _, err := c.repo.Articles.GetByUrl(*c.ctx, article.Url) + _, err := c.repo.Articles.GetByUrl(c.ctx, article.Url) if err == nil { continue } - rowsCreated, err := c.repo.Articles.CreateFromEntity(*c.ctx, article) + rowsCreated, err := c.repo.Articles.CreateFromEntity(c.ctx, article) if err != nil { log.Println(err) } @@ -48,7 +48,7 @@ func (c *Cron) CollectRssPosts() { } func (c *Cron) CollectRedditPosts() { - sources, err := c.repo.Sources.ListBySource(*c.ctx, 0, 1000, domain.SourceCollectorReddit) + sources, err := c.repo.Sources.ListBySource(c.ctx, 0, 1000, domain.SourceCollectorReddit) if err != nil { log.Printf("[Reddit] No sources found to query - %v\r", err) } @@ -67,12 +67,12 @@ func (c *Cron) CollectRedditPosts() { redditArticles := rc.ConvertToArticles(raw) for _, article := range redditArticles { - _, err := c.repo.Articles.GetByUrl(*c.ctx, article.Url) + _, err := c.repo.Articles.GetByUrl(c.ctx, article.Url) if err == nil { continue } - rowsAdded, err := c.repo.Articles.CreateFromEntity(*c.ctx, article) + rowsAdded, err := c.repo.Articles.CreateFromEntity(c.ctx, article) if err != nil { log.Printf("Failed to add a new reddit article to the database: %s", err) } @@ -86,7 +86,7 @@ func (c *Cron) CollectRedditPosts() { } func (c *Cron) CollectYoutubePosts() { - sources, err := c.repo.Sources.ListBySource(*c.ctx, 0, 1000, domain.SourceCollectorYoutube) + sources, err := c.repo.Sources.ListBySource(c.ctx, 0, 1000, domain.SourceCollectorYoutube) if err != nil { log.Printf("[Youtube] No sources found to query - %v\r", err) } @@ -104,12 +104,12 @@ func (c *Cron) CollectYoutubePosts() { } for _, article := range raw { - _, err := c.repo.Articles.GetByUrl(*c.ctx, article.Url) + _, err := c.repo.Articles.GetByUrl(c.ctx, article.Url) if err == nil { continue } - rowsAdded, err := c.repo.Articles.CreateFromEntity(*c.ctx, article) + rowsAdded, err := c.repo.Articles.CreateFromEntity(c.ctx, article) if err != nil { log.Printf("Failed to add a new youtube article to the database: %s", err) } @@ -127,7 +127,7 @@ func (c *Cron) CollectYoutubePosts() { } func (c *Cron) CollectFfxivPosts() { - sources, err := c.repo.Sources.ListBySource(*c.ctx, 0, 1000, domain.SourceCollectorFfxiv) + sources, err := c.repo.Sources.ListBySource(c.ctx, 0, 1000, domain.SourceCollectorFfxiv) if err != nil { log.Printf("[FFXIV] No sources found to query - %v\r", err) } @@ -144,12 +144,12 @@ func (c *Cron) CollectFfxivPosts() { } for _, article := range items { - _, err := c.repo.Articles.GetByUrl(*c.ctx, article.Url) + _, err := c.repo.Articles.GetByUrl(c.ctx, article.Url) if err == nil { continue } - rowsAdded, err := c.repo.Articles.CreateFromEntity(*c.ctx, article) + rowsAdded, err := c.repo.Articles.CreateFromEntity(c.ctx, article) if err != nil { log.Printf("Failed to add a new FFXIV article to the database: %s", err) } @@ -167,7 +167,7 @@ func (c *Cron) CollectFfxivPosts() { } func (c *Cron) CollectTwitchPosts() { - sources, err := c.repo.Sources.ListBySource(*c.ctx, 0, 1000, domain.SourceCollectorTwitch) + sources, err := c.repo.Sources.ListBySource(c.ctx, 0, 1000, domain.SourceCollectorTwitch) if err != nil { log.Printf("[Twitch] No sources found to query - %v\r", err) } @@ -196,12 +196,12 @@ func (c *Cron) CollectTwitchPosts() { } for _, article := range items { - _, err := c.repo.Articles.GetByUrl(*c.ctx, article.Url) + _, err := c.repo.Articles.GetByUrl(c.ctx, article.Url) if err == nil { continue } - rowsAdded, err := c.repo.Articles.CreateFromEntity(*c.ctx, article) + rowsAdded, err := c.repo.Articles.CreateFromEntity(c.ctx, article) if err != nil { log.Printf("Failed to add a new Twitch article to the database: %s", err) } diff --git a/internal/services/cron/scheduler.go b/internal/services/cron/scheduler.go index 594bec4..41d3e75 100644 --- a/internal/services/cron/scheduler.go +++ b/internal/services/cron/scheduler.go @@ -13,14 +13,14 @@ import ( type Cron struct { Db *database.Queries - ctx *context.Context + ctx context.Context timer *cron.Cron repo services.RepositoryService } func NewScheduler(ctx context.Context, conn *sql.DB) *Cron { c := &Cron{ - ctx: &ctx, + ctx: ctx, repo: services.NewRepositoryService(conn), } timer := cron.New() @@ -28,11 +28,11 @@ func NewScheduler(ctx context.Context, conn *sql.DB) *Cron { //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 * * * *", func() { go c.CollectRssPosts() }) + //timer.AddFunc("10 * * * *", c.CollectRedditPosts) + //timer.AddFunc("15 * * * *", c.CheckYoutube) + //timer.AddFunc("20 * * * *", c.CheckFfxiv) + //timer.AddFunc("25 * * * *", c.CheckTwitch) //timer.AddFunc("*/5 * * * *", c.CheckDiscordQueue) c.timer = timer