api: started cron scheduler

This commit is contained in:
Tigor Hutasuhut 2024-04-25 13:52:38 +07:00
parent e5bfe7a29a
commit 86c3a0d699
2 changed files with 8 additions and 3 deletions

View file

@ -17,6 +17,7 @@ import (
"github.com/tigorlazuardi/redmage/pkg/errs" "github.com/tigorlazuardi/redmage/pkg/errs"
"github.com/tigorlazuardi/redmage/pkg/log" "github.com/tigorlazuardi/redmage/pkg/log"
"github.com/ThreeDotsLabs/watermill"
watermillSql "github.com/ThreeDotsLabs/watermill-sql/v3/pkg/sql" watermillSql "github.com/ThreeDotsLabs/watermill-sql/v3/pkg/sql"
"github.com/ThreeDotsLabs/watermill/message" "github.com/ThreeDotsLabs/watermill/message"
watermillSqlite "github.com/walterwanderley/watermill-sqlite" watermillSqlite "github.com/walterwanderley/watermill-sqlite"
@ -89,12 +90,15 @@ func New(deps Dependencies) *API {
publisher: publisher, publisher: publisher,
} }
api.startSubredditDownloadPubsub(ch) if err := api.StartScheduler(ctx); err != nil {
panic(err)
}
api.StartSubredditDownloadPubsub(ch)
return api return api
} }
func (api *API) StartScheduler(ctx context.Context) error { func (api *API) StartScheduler(ctx context.Context) error {
subreddits, err := models.Subreddits.Query(ctx, api.db, nil).All() subreddits, err := models.Subreddits.Query(ctx, api.db, models.SelectWhere.Subreddits.Enable.EQ(1)).All()
if err != nil { if err != nil {
return errs.Wrapw(err, "failed to get all subreddits") return errs.Wrapw(err, "failed to get all subreddits")
} }
@ -115,6 +119,7 @@ func (api *API) StartScheduler(ctx context.Context) error {
func (api *API) scheduleSubreddit(subreddit *models.Subreddit) error { func (api *API) scheduleSubreddit(subreddit *models.Subreddit) error {
id, err := api.scheduler.AddFunc(subreddit.Schedule, func() { id, err := api.scheduler.AddFunc(subreddit.Schedule, func() {
_ = api.publisher.Publish(downloadTopic, message.NewMessage(watermill.NewUUID(), []byte(subreddit.Name)))
}) })
if err != nil { if err != nil {
return errs.Wrap(err) return errs.Wrap(err)

View file

@ -11,7 +11,7 @@ import (
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
) )
func (api *API) startSubredditDownloadPubsub(messages <-chan *message.Message) { func (api *API) StartSubredditDownloadPubsub(messages <-chan *message.Message) {
for msg := range messages { for msg := range messages {
api.subredditSemaphore <- struct{}{} api.subredditSemaphore <- struct{}{}
go func(msg *message.Message) { go func(msg *message.Message) {