Add OpenAPI docs, frontend, migrations, and API updates
- OpenAPI: add missing endpoints (add-from-url, subscriptions, public availability) - OpenAPI: CalendarSubscription schema, Subscriptions tag - Frontend app - Migrations: count_for_availability, subscriptions_sync, user_preferences, calendar_settings - Config, rate limit, auth, calendar, booking, ICS, availability, user service updates Made-with: Cursor
This commit is contained in:
@@ -4,13 +4,17 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/calendarapi/internal/repository"
|
||||
"github.com/calendarapi/internal/utils"
|
||||
"github.com/google/uuid"
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
const TypeReminder = "reminder:send"
|
||||
const TypeSubscriptionSync = "subscription:sync"
|
||||
|
||||
type ReminderPayload struct {
|
||||
EventID uuid.UUID `json:"event_id"`
|
||||
@@ -50,10 +54,58 @@ func (s *Scheduler) ScheduleReminder(_ context.Context, eventID, reminderID, use
|
||||
return nil
|
||||
}
|
||||
|
||||
type SubscriptionSyncPayload struct {
|
||||
SubscriptionID string `json:"subscription_id"`
|
||||
}
|
||||
|
||||
func (s *Scheduler) EnqueueSubscriptionSync(ctx context.Context, subscriptionID string) error {
|
||||
payload, err := json.Marshal(SubscriptionSyncPayload{SubscriptionID: subscriptionID})
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal subscription sync payload: %w", err)
|
||||
}
|
||||
task := asynq.NewTask(TypeSubscriptionSync, payload)
|
||||
_, err = s.client.Enqueue(task,
|
||||
asynq.MaxRetry(3),
|
||||
asynq.Queue("subscriptions"),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("enqueue subscription sync: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Scheduler) Close() error {
|
||||
return s.client.Close()
|
||||
}
|
||||
|
||||
// StartSubscriptionEnqueuer runs a goroutine that every interval lists subscriptions due for sync
|
||||
// and enqueues a sync task for each. Call with a cancellable context to stop.
|
||||
func StartSubscriptionEnqueuer(ctx context.Context, queries *repository.Queries, sched *Scheduler, interval time.Duration) {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
subs, err := queries.ListSubscriptionsDueForSync(ctx)
|
||||
if err != nil {
|
||||
log.Printf("subscription enqueuer: list due for sync: %v", err)
|
||||
continue
|
||||
}
|
||||
for _, sub := range subs {
|
||||
subID := utils.FromPgUUID(sub.ID)
|
||||
if err := sched.EnqueueSubscriptionSync(ctx, subID.String()); err != nil {
|
||||
log.Printf("subscription enqueuer: enqueue %s: %v", subID, err)
|
||||
}
|
||||
}
|
||||
if len(subs) > 0 {
|
||||
log.Printf("subscription enqueuer: enqueued %d subscriptions", len(subs))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type NoopScheduler struct{}
|
||||
|
||||
func (NoopScheduler) ScheduleReminder(_ context.Context, _, _, _ uuid.UUID, _ time.Time) error {
|
||||
|
||||
@@ -11,6 +11,11 @@ import (
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
// SubscriptionSyncer performs a subscription sync (used by background worker).
|
||||
type SubscriptionSyncer interface {
|
||||
SyncSubscriptionBackground(ctx context.Context, subscriptionID string) error
|
||||
}
|
||||
|
||||
type ReminderWorker struct {
|
||||
queries *repository.Queries
|
||||
}
|
||||
@@ -41,14 +46,35 @@ func (w *ReminderWorker) HandleReminderTask(ctx context.Context, t *asynq.Task)
|
||||
return nil
|
||||
}
|
||||
|
||||
func StartWorker(redisAddr string, worker *ReminderWorker) *asynq.Server {
|
||||
type SubscriptionSyncWorker struct {
|
||||
syncer SubscriptionSyncer
|
||||
}
|
||||
|
||||
func NewSubscriptionSyncWorker(syncer SubscriptionSyncer) *SubscriptionSyncWorker {
|
||||
return &SubscriptionSyncWorker{syncer: syncer}
|
||||
}
|
||||
|
||||
func (w *SubscriptionSyncWorker) HandleSubscriptionSync(ctx context.Context, t *asynq.Task) error {
|
||||
var payload SubscriptionSyncPayload
|
||||
if err := json.Unmarshal(t.Payload(), &payload); err != nil {
|
||||
return fmt.Errorf("unmarshal subscription sync payload: %w", err)
|
||||
}
|
||||
if err := w.syncer.SyncSubscriptionBackground(ctx, payload.SubscriptionID); err != nil {
|
||||
return fmt.Errorf("sync subscription %s: %w", payload.SubscriptionID, err)
|
||||
}
|
||||
log.Printf("subscription sync completed: %s", payload.SubscriptionID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func StartWorker(redisAddr string, worker *ReminderWorker, subSyncWorker *SubscriptionSyncWorker) *asynq.Server {
|
||||
srv := asynq.NewServer(
|
||||
asynq.RedisClientOpt{Addr: redisAddr},
|
||||
asynq.Config{
|
||||
Concurrency: 10,
|
||||
Queues: map[string]int{
|
||||
"reminders": 6,
|
||||
"default": 3,
|
||||
"reminders": 6,
|
||||
"subscriptions": 2,
|
||||
"default": 3,
|
||||
},
|
||||
RetryDelayFunc: asynq.DefaultRetryDelayFunc,
|
||||
},
|
||||
@@ -56,6 +82,9 @@ func StartWorker(redisAddr string, worker *ReminderWorker) *asynq.Server {
|
||||
|
||||
mux := asynq.NewServeMux()
|
||||
mux.HandleFunc(TypeReminder, worker.HandleReminderTask)
|
||||
if subSyncWorker != nil {
|
||||
mux.HandleFunc(TypeSubscriptionSync, subSyncWorker.HandleSubscriptionSync)
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := srv.Run(mux); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user