-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(count-requests): hotfix 3.18.13 lock instance and await for task …
…before update
- Loading branch information
Hicham
committed
Feb 27, 2024
1 parent
b9d3f6e
commit e8e7ef0
Showing
7 changed files
with
67 additions
and
25 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
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,38 @@ | ||
import functools | ||
|
||
from django.core.cache import cache | ||
|
||
|
||
def locked_instance_task(task): | ||
def acquire_lock(lock_id: str): | ||
return cache.add(lock_id, lock_id, 5*60) | ||
|
||
def release_lock(lock_id: str): | ||
cache.delete(lock_id) | ||
|
||
@functools.wraps(task) | ||
def wrapper(*args, **kwargs): | ||
instance_id = kwargs.get("dm_uuid") | ||
locked = acquire_lock(instance_id) | ||
if locked: | ||
try: | ||
task(*args, **kwargs) | ||
finally: | ||
release_lock(instance_id) | ||
return wrapper | ||
|
||
|
||
def await_celery_task(func): | ||
|
||
def retrieve_lock(lock_id: str): | ||
return cache.get(lock_id) | ||
|
||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs): | ||
dm_id = kwargs.get("uuid") | ||
lock = retrieve_lock(lock_id=dm_id) | ||
while lock is not None: | ||
lock = retrieve_lock(lock_id=dm_id) | ||
else: | ||
return func(*args, **kwargs) | ||
return wrapper |
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