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-05-23 13:49:37 +07:00
|
|
|
"sync"
|
2024-04-08 23:18:09 +07:00
|
|
|
|
2024-04-24 13:01:13 +07:00
|
|
|
"github.com/stephenafamo/bob"
|
2024-04-09 21:49:23 +07:00
|
|
|
"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-05-14 12:08:34 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/api/scheduler"
|
2024-04-09 23:17:23 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/config"
|
2024-04-09 21:49:23 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/pkg/log"
|
2024-05-14 12:08:34 +07:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2024-04-25 12:31:20 +07:00
|
|
|
|
|
|
|
"github.com/ThreeDotsLabs/watermill/message"
|
2024-04-08 23:18:09 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
type API struct {
|
2024-05-03 23:40:05 +07:00
|
|
|
db bob.Executor
|
|
|
|
sqldb *sql.DB
|
2024-04-09 21:49:23 +07:00
|
|
|
|
2024-05-14 12:08:34 +07:00
|
|
|
scheduler *scheduler.Scheduler
|
2024-04-09 21:49:23 +07:00
|
|
|
|
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
|
|
|
|
2024-05-04 19:39:02 +07:00
|
|
|
imageSemaphore chan struct{}
|
2024-04-14 00:32:55 +07:00
|
|
|
|
|
|
|
reddit *reddit.Reddit
|
2024-04-25 12:31:20 +07:00
|
|
|
|
|
|
|
subscriber message.Subscriber
|
|
|
|
publisher message.Publisher
|
2024-05-23 13:49:37 +07:00
|
|
|
|
|
|
|
mu *sync.Mutex
|
2024-04-14 00:32:55 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
type Dependencies struct {
|
2024-05-03 21:02:31 +07:00
|
|
|
DB *sql.DB
|
|
|
|
Config *config.Config
|
|
|
|
Reddit *reddit.Reddit
|
|
|
|
Publisher message.Publisher
|
|
|
|
Subscriber message.Subscriber
|
2024-04-09 21:49:23 +07:00
|
|
|
}
|
|
|
|
|
2024-04-25 13:05:41 +07:00
|
|
|
const downloadTopic = "subreddit_download"
|
|
|
|
|
2024-04-14 00:32:55 +07:00
|
|
|
func New(deps Dependencies) *API {
|
2024-05-03 21:02:31 +07:00
|
|
|
ch, err := deps.Subscriber.Subscribe(context.Background(), downloadTopic)
|
2024-04-25 13:05:41 +07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-05-14 12:08:34 +07:00
|
|
|
|
2024-04-25 13:05:41 +07:00
|
|
|
api := &API{
|
2024-05-04 19:39:02 +07:00
|
|
|
db: bob.New(deps.DB),
|
|
|
|
sqldb: deps.DB,
|
|
|
|
downloadBroadcast: broadcast.NewRelay[bmessage.ImageDownloadMessage](),
|
|
|
|
config: deps.Config,
|
|
|
|
imageSemaphore: make(chan struct{}, deps.Config.Int("download.concurrency.images")),
|
|
|
|
reddit: deps.Reddit,
|
|
|
|
subscriber: deps.Subscriber,
|
|
|
|
publisher: deps.Publisher,
|
2024-05-23 13:49:37 +07:00
|
|
|
mu: &sync.Mutex{},
|
2024-04-09 21:49:23 +07:00
|
|
|
}
|
2024-04-25 13:05:41 +07:00
|
|
|
|
2024-05-14 12:08:34 +07:00
|
|
|
api.scheduler = scheduler.New(api.scheduleRun)
|
|
|
|
|
|
|
|
if err := api.scheduler.Sync(context.Background(), api.db); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-05-14 18:49:45 +07:00
|
|
|
go api.scheduler.Start()
|
2024-05-14 12:08:34 +07:00
|
|
|
|
2024-04-25 20:22:05 +07:00
|
|
|
go api.StartSubredditDownloadPubsub(ch)
|
2024-04-25 13:05:41 +07:00
|
|
|
return api
|
2024-04-09 21:49:23 +07:00
|
|
|
}
|
|
|
|
|
2024-05-14 12:08:34 +07:00
|
|
|
func (api *API) scheduleRun(subreddit string) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), api.config.Duration("scheduler.timeout"))
|
|
|
|
defer cancel()
|
2024-04-09 21:49:23 +07:00
|
|
|
|
2024-05-14 12:08:34 +07:00
|
|
|
ctx, span := tracer.Start(ctx, "*API.scheduleRun")
|
|
|
|
defer span.End()
|
|
|
|
span.SetAttributes(attribute.String("subreddit", subreddit))
|
2024-04-09 21:49:23 +07:00
|
|
|
|
2024-05-14 12:08:34 +07:00
|
|
|
log.New(ctx).Info("api: schedule run", "subreddit", subreddit)
|
2024-04-12 01:32:06 +07:00
|
|
|
|
2024-05-14 12:08:34 +07:00
|
|
|
err := api.PubsubStartDownloadSubreddit(ctx, PubsubStartDownloadSubredditParams{Subreddit: subreddit})
|
2024-04-12 01:32:06 +07:00
|
|
|
if err != nil {
|
2024-05-14 12:08:34 +07:00
|
|
|
log.New(ctx).Err(err).Error("api: failed to start download subreddit", "subreddit", subreddit)
|
2024-04-12 01:32:06 +07:00
|
|
|
}
|
2024-05-13 21:10:48 +07:00
|
|
|
}
|