From c73167de347856f6a795130641ad220a62cef138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=BE=D0=BB=D0=B8=D0=BA=D0=BE=D0=B2=20Maximillian?= =?UTF-8?q?=20=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Fri, 23 Oct 2026 08:53:44 +0300 Subject: [PATCH] feat(service):lab4 add Reconciler scaffold and Run loop --- backend/internal/service/reconciler.go | 67 ++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 backend/internal/service/reconciler.go diff --git a/backend/internal/service/reconciler.go b/backend/internal/service/reconciler.go new file mode 100644 index 00000000..978a52f4 --- /dev/null +++ b/backend/internal/service/reconciler.go @@ -0,0 +1,67 @@ +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 + } +}