Skip to content

Commit

Permalink
locking error for multiple simultaneous batou deployments is displaye…
Browse files Browse the repository at this point in the history
…d more beautiful
  • Loading branch information
elikoga committed Oct 15, 2024
1 parent 09b5eec commit c2a4d8f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/batou/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def main(
else:
ACTION = "DEPLOYMENT"
SUCCESS_FORMAT = {"green": True}
with locked(".batou-lock"):
with locked(".batou-lock", exit_on_failure=True):
deployment = Deployment(
environment,
platform,
Expand Down
14 changes: 13 additions & 1 deletion src/batou/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,25 @@ def flush(self):


@contextlib.contextmanager
def locked(filename):
def locked(filename, exit_on_failure=False):
# XXX can we make this not leave files around?
with open(filename, "a+") as lockfile:
try:
fcntl.lockf(lockfile, fcntl.LOCK_EX | fcntl.LOCK_NB)
except IOError:
print("Could not acquire lock {}".format(filename), file=sys.stderr)
if exit_on_failure:
print(
"Another instance of batou may be running, or a stale lock file may exist.",
file=sys.stderr,
)
print(
"If you are sure no other instance is running, you can remove the lock file manually.",
file=sys.stderr,
)
print("Lock file: {}".format(filename), file=sys.stderr)
print("Exiting.", file=sys.stderr)
sys.exit(1)
raise RuntimeError(
'cannot create lock "%s": more than one instance running '
"concurrently?" % lockfile,
Expand Down

0 comments on commit c2a4d8f

Please sign in to comment.