2024-04-08 23:18:09 +07:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2024-04-09 21:49:23 +07:00
|
|
|
"context"
|
2024-04-08 23:18:09 +07:00
|
|
|
"database/sql"
|
2024-04-09 21:49:23 +07:00
|
|
|
"fmt"
|
2024-04-08 23:18:09 +07:00
|
|
|
|
2024-04-09 21:49:23 +07:00
|
|
|
"github.com/robfig/cron/v3"
|
|
|
|
"github.com/teivah/broadcast"
|
2024-04-10 22:38:19 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/api/bmessage"
|
2024-04-14 00:32:55 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/api/reddit"
|
2024-04-09 23:17:23 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/config"
|
2024-04-08 23:18:09 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/db/queries"
|
2024-04-09 21:49:23 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/pkg/errs"
|
|
|
|
"github.com/tigorlazuardi/redmage/pkg/log"
|
2024-04-08 23:18:09 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
type API struct {
|
2024-04-09 21:49:23 +07:00
|
|
|
queries *queries.Queries
|
|
|
|
db *sql.DB
|
|
|
|
|
|
|
|
scheduler *cron.Cron
|
|
|
|
scheduleMap map[cron.EntryID]queries.Subreddit
|
|
|
|
|
2024-04-11 16:04:13 +07:00
|
|
|
downloadBroadcast *broadcast.Relay[bmessage.ImageDownloadMessage]
|
2024-04-09 23:17:23 +07:00
|
|
|
|
|
|
|
config *config.Config
|
2024-04-14 00:32:55 +07:00
|
|
|
|
|
|
|
imageSemaphore chan struct{}
|
|
|
|
subredditSemaphore chan struct{}
|
|
|
|
|
|
|
|
reddit *reddit.Reddit
|
|
|
|
}
|
|
|
|
|
|
|
|
type Dependencies struct {
|
|
|
|
Queries *queries.Queries
|
|
|
|
DB *sql.DB
|
|
|
|
Config *config.Config
|
|
|
|
Reddit *reddit.Reddit
|
2024-04-09 21:49:23 +07:00
|
|
|
}
|
|
|
|
|
2024-04-14 00:32:55 +07:00
|
|
|
func New(deps Dependencies) *API {
|
2024-04-09 21:49:23 +07:00
|
|
|
return &API{
|
2024-04-14 00:32:55 +07:00
|
|
|
queries: deps.Queries,
|
|
|
|
db: deps.DB,
|
|
|
|
scheduler: cron.New(),
|
|
|
|
scheduleMap: make(map[cron.EntryID]queries.Subreddit, 8),
|
|
|
|
downloadBroadcast: broadcast.NewRelay[bmessage.ImageDownloadMessage](),
|
|
|
|
config: deps.Config,
|
|
|
|
imageSemaphore: make(chan struct{}, deps.Config.Int("download.concurrency.images")),
|
|
|
|
subredditSemaphore: make(chan struct{}, deps.Config.Int("download.concurrency.subreddits")),
|
|
|
|
reddit: deps.Reddit,
|
2024-04-09 21:49:23 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) StartScheduler(ctx context.Context) error {
|
|
|
|
subreddits, err := api.queries.SubredditsGetAll(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrapw(err, "failed to get all subreddits")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, subreddit := range subreddits {
|
2024-04-12 01:32:06 +07:00
|
|
|
err := api.scheduleSubreddit(subreddit)
|
2024-04-09 21:49:23 +07:00
|
|
|
if err != nil {
|
2024-04-12 01:32:06 +07:00
|
|
|
log.New(ctx).Err(err).Error(
|
|
|
|
fmt.Sprintf("failed to start scheduler for subreddit '%s'", subreddit.Name),
|
|
|
|
"subreddit", subreddit,
|
|
|
|
)
|
2024-04-09 21:49:23 +07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-04-08 23:18:09 +07:00
|
|
|
}
|
2024-04-12 01:32:06 +07:00
|
|
|
|
|
|
|
func (api *API) scheduleSubreddit(subreddit queries.Subreddit) error {
|
|
|
|
id, err := api.scheduler.AddFunc(subreddit.Schedule, func() {
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
api.scheduleMap[id] = subreddit
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|