-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support identical cron schedule (#87)
* Retry on lock being unavailable * Refactor locking to return plain error * Collect LockedTime in stats * Add test case * Add documentation for LOCK_TIMEOUT * Log in case lock needs to be awaited * Release resources created for awaiting lock
- Loading branch information
Showing
13 changed files
with
90 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2022 - Offen Authors <[email protected]> | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/gofrs/flock" | ||
) | ||
|
||
// lock opens a lockfile at the given location, keeping it locked until the | ||
// caller invokes the returned release func. In case the lock is currently blocked | ||
// by another execution, it will repeatedly retry until the lock is available | ||
// or the given timeout is exceeded. | ||
func (s *script) lock(lockfile string) (func() error, error) { | ||
start := time.Now() | ||
defer func() { | ||
s.stats.LockedTime = time.Now().Sub(start) | ||
}() | ||
|
||
retry := time.NewTicker(5 * time.Second) | ||
defer retry.Stop() | ||
deadline := time.NewTimer(s.c.LockTimeout) | ||
defer deadline.Stop() | ||
|
||
fileLock := flock.New(lockfile) | ||
|
||
for { | ||
acquired, err := fileLock.TryLock() | ||
if err != nil { | ||
return noop, fmt.Errorf("lock: error trying lock: %w", err) | ||
} | ||
if acquired { | ||
if s.encounteredLock { | ||
s.logger.Info("Acquired exclusive lock on subsequent attempt, ready to continue.") | ||
} | ||
return fileLock.Unlock, nil | ||
} | ||
|
||
if !s.encounteredLock { | ||
s.logger.Infof( | ||
"Exclusive lock was not available on first attempt. Will retry until it becomes available or the timeout of %s is exceeded.", | ||
s.c.LockTimeout, | ||
) | ||
s.encounteredLock = true | ||
} | ||
|
||
select { | ||
case <-retry.C: | ||
continue | ||
case <-deadline.C: | ||
return noop, errors.New("lock: timed out waiting for lockfile to become available") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,8 @@ type script struct { | |
file string | ||
stats *Stats | ||
|
||
encounteredLock bool | ||
|
||
c *Config | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
BACKUP_FILENAME="other.tar.gz" | ||
BACKUP_CRON_EXPRESSION="*/1 * * * *" |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters