68 lines
1.2 KiB
Go
68 lines
1.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"high-loud-apps-labs/internal/domain"
|
|
)
|
|
|
|
const advisoryLockKey int64 = 0x4D5455534904
|
|
|
|
type Reconciler struct {
|
|
attachments domain.AttachmentRepository
|
|
storage domain.ObjectStorage
|
|
pool *pgxpool.Pool
|
|
gracePeriod time.Duration
|
|
batchSize int
|
|
}
|
|
|
|
func NewReconciler(
|
|
attachments domain.AttachmentRepository,
|
|
storage domain.ObjectStorage,
|
|
pool *pgxpool.Pool,
|
|
gracePeriod time.Duration,
|
|
) *Reconciler {
|
|
if gracePeriod <= 0 {
|
|
gracePeriod = time.Hour
|
|
}
|
|
return &Reconciler{
|
|
attachments: attachments,
|
|
storage: storage,
|
|
pool: pool,
|
|
gracePeriod: gracePeriod,
|
|
batchSize: 100,
|
|
}
|
|
}
|
|
|
|
func (r *Reconciler) Run(ctx context.Context, interval time.Duration) {
|
|
if interval <= 0 {
|
|
interval = 5 * time.Minute
|
|
}
|
|
ticker := time.NewTicker(interval)
|
|
defer ticker.Stop()
|
|
|
|
log.Printf("reconciler: started, interval=%s grace=%s", interval, r.gracePeriod)
|
|
|
|
r.tick(ctx)
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
log.Println("reconciler: stopped")
|
|
return
|
|
case <-ticker.C:
|
|
r.tick(ctx)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (r *Reconciler) tick(ctx context.Context) {
|
|
if err := ctx.Err(); err != nil {
|
|
return
|
|
}
|
|
}
|