-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutex.py
38 lines (32 loc) · 964 Bytes
/
mutex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import boto3
from typing import Optional
from constants import TABLE_NAME, MUTEX_KEY , PARTITION_KEY
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(TABLE_NAME)
def get_mutex() -> bool:
try:
table.put_item(
Item={
PARTITION_KEY: MUTEX_KEY,
"locked": True
},
ConditionExpression="attribute_not_exists(locationID)"
)
return True
except dynamodb.meta.client.exceptions.ConditionalCheckFailedException:
return False
def release_mutex() -> Optional[bool]:
response = table.delete_item(
Key={
PARTITION_KEY: MUTEX_KEY
},
ConditionExpression="attribute_exists({}) AND locked = :locked".format(PARTITION_KEY),
ExpressionAttributeValues={
":locked": True
},
ReturnValues="ALL_OLD"
)
if "Attributes" in response:
return True
else:
return None