51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package job
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"time"
|
|
|
|
publishqueuedomain "haixun-backend/internal/model/publish_queue/domain/usecase"
|
|
)
|
|
|
|
type PublishQueueSweeper struct {
|
|
queue publishqueuedomain.UseCase
|
|
batchSize int
|
|
}
|
|
|
|
func NewPublishQueueSweeper(queue publishqueuedomain.UseCase, batchSize int) *PublishQueueSweeper {
|
|
if batchSize <= 0 {
|
|
batchSize = 20
|
|
}
|
|
return &PublishQueueSweeper{queue: queue, batchSize: batchSize}
|
|
}
|
|
|
|
func (s *PublishQueueSweeper) Start(ctx context.Context, interval time.Duration) {
|
|
if s == nil || s.queue == nil {
|
|
return
|
|
}
|
|
if interval <= 0 {
|
|
interval = time.Minute
|
|
}
|
|
log.Printf("publish queue sweeper started: interval=%s batch=%d", interval, s.batchSize)
|
|
ticker := time.NewTicker(interval)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
log.Printf("publish queue sweeper stopped")
|
|
return
|
|
case <-ticker.C:
|
|
dispatched, err := s.queue.DispatchDue(ctx, s.batchSize)
|
|
if err != nil {
|
|
log.Printf("publish queue sweeper tick error: %v", err)
|
|
continue
|
|
}
|
|
if dispatched > 0 {
|
|
log.Printf("publish queue sweeper dispatched %d item(s)", dispatched)
|
|
}
|
|
}
|
|
}
|
|
}
|