From c32f0a28f1f0f4a47b1405bda4ed06214c6d592c Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:15:08 +0530 Subject: [PATCH] [server] Release previous locks taken by host on statup --- server/cmd/museum/main.go | 2 +- server/pkg/controller/lock/lock.go | 8 ++++++++ server/pkg/repo/tasklock.go | 12 ++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/server/cmd/museum/main.go b/server/cmd/museum/main.go index a5fc5fa4f..800bf9aa9 100644 --- a/server/cmd/museum/main.go +++ b/server/cmd/museum/main.go @@ -682,7 +682,7 @@ func main() { publicAPI.GET("/offers/black-friday", offerHandler.GetBlackFridayOffers) setKnownAPIs(server.Routes()) - + lockController.ReleaseHostLock() setupAndStartBackgroundJobs(objectCleanupController, replicationController3) setupAndStartCrons( userAuthRepo, publicCollectionRepo, twoFactorRepo, passkeysRepo, fileController, taskLockingRepo, emailNotificationCtrl, diff --git a/server/pkg/controller/lock/lock.go b/server/pkg/controller/lock/lock.go index ea7d8bf73..3746ff679 100644 --- a/server/pkg/controller/lock/lock.go +++ b/server/pkg/controller/lock/lock.go @@ -56,3 +56,11 @@ func (c *LockController) ReleaseLock(lockID string) { log.Errorf("Error while releasing lock %v: %s", lockID, err) } } + +func (c *LockController) ReleaseHostLock() { + count, err := c.TaskLockingRepo.ReleaseLocksBy(c.HostName) + if err != nil { + log.Errorf("Error while releasing host lock: %s", err) + } + log.Infof("Released %d locks held by %s", *count, c.HostName) +} diff --git a/server/pkg/repo/tasklock.go b/server/pkg/repo/tasklock.go index 3aa593e13..ce6cca8f4 100644 --- a/server/pkg/repo/tasklock.go +++ b/server/pkg/repo/tasklock.go @@ -71,6 +71,18 @@ func (repo *TaskLockRepository) ReleaseLock(name string) error { return stacktrace.Propagate(err, "") } +func (repo *TaskLockRepository) ReleaseLocksBy(lockedBy string) (*int64, error) { + result, err := repo.DB.Exec(`DELETE FROM task_lock WHERE locked_by = $1`, lockedBy) + if err != nil { + return nil, stacktrace.Propagate(err, "") + } + rowsAffected, err := result.RowsAffected() + if err != nil { + return nil, stacktrace.Propagate(err, "") + } + return &rowsAffected, nil +} + func (repo *TaskLockRepository) CleanupExpiredLocks() error { result, err := repo.DB.Exec(`DELETE FROM task_lock WHERE lock_until < $1`, time.Microseconds()) if err != nil {