[server] Release previous locks taken by host on statup

This commit is contained in:
Neeraj Gupta 2024-03-11 11:15:08 +05:30 committed by Neeraj Gupta
parent 38e8f7c8d7
commit c32f0a28f1
3 changed files with 21 additions and 1 deletions

View file

@ -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,

View file

@ -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)
}

View file

@ -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 {