Have got asynchronous publishing working

This commit is contained in:
Leon Mika 2026-02-23 21:35:12 +11:00
parent 3ea5823ca0
commit 4f7058bf36
9 changed files with 100 additions and 31 deletions

View file

@ -0,0 +1,44 @@
package publisher
import (
"context"
"log"
"lmika.dev/lmika/weiro/models"
)
type Queue struct {
publisher *Publisher
pending chan models.Site
}
func NewQueue(publisher *Publisher) *Queue {
return &Queue{
publisher: publisher,
pending: make(chan models.Site, 1),
}
}
func (q *Queue) Queue(site models.Site) bool {
select {
case q.pending <- site:
return true
default:
return false
}
}
func (q *Queue) Start(ctx context.Context) {
go func() {
for {
select {
case site := <-q.pending:
if err := q.publisher.Publish(ctx, site); err != nil {
log.Printf("error publishing site: %v", err)
}
case <-ctx.Done():
return
}
}
}()
}