Skip to content

Commit 7115fa6

Browse files
committed
feat(lock): add wait
Signed-off-by: Chris Snow <[email protected]>
1 parent d3f982e commit 7115fa6

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

hpecp/cli/lock.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,17 @@ def list(
7070
print(json.dumps(response))
7171

7272
@base.intercept_exception
73-
def create(
74-
self,
75-
reason,
76-
):
73+
def create(self, reason, timeout_secs=300):
7774
"""Create a lock."""
78-
print(base.get_client().lock.create(reason), file=sys.stdout)
75+
response = base.get_client().lock.create(reason, timeout_secs)
76+
if response is False:
77+
print(
78+
"Unabled to lock within '{}'".format(timeout_secs),
79+
file=sys.stderr,
80+
)
81+
else:
82+
# reponse contains lock ID
83+
print(response, file=sys.stderr)
7984

8085
@base.intercept_exception
8186
def delete(

hpecp/lock.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ def list(self):
4545
"""Retrieve the locks"""
4646
return self.get()
4747

48-
def create(self, reason=None):
48+
def create(self, reason=None, timeout_secs=300):
4949
"""Create a new lock.
5050
5151
Arguments
5252
---------
5353
reason: str
5454
Provide a reason for the lock.
55+
timeout_secs: int
56+
Time to wait for lock to be successful
5557
5658
Raises
5759
------
@@ -64,7 +66,21 @@ def create(self, reason=None):
6466
data=data,
6567
description="lock/set_lock",
6668
)
67-
return CaseInsensitiveDict(response.headers)["Location"]
69+
id = CaseInsensitiveDict(response.headers)["Location"]
70+
71+
if timeout_secs == 0:
72+
return id
73+
else:
74+
try:
75+
polling.poll(
76+
lambda: self.get()["locked"],
77+
step=10,
78+
poll_forever=False,
79+
timeout=timeout_secs,
80+
)
81+
return id
82+
except polling.TimeoutException:
83+
return False
6884

6985
def delete(self, lock_id):
7086
"""Delete a lock.

0 commit comments

Comments
 (0)