Skip to content

Commit

Permalink
Merge pull request #9 from rjojjr/refactor-python-client
Browse files Browse the repository at this point in the history
Release Python Client v1.0.1
  • Loading branch information
rjojjr authored Sep 29, 2024
2 parents e57c1fc + c11727a commit 132af58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
23 changes: 19 additions & 4 deletions client-modules/python/src/taskslock/client/tasks_lock_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import requests
import json
from models.task_lock import TaskLock
from logging import getLogger

version = '1.0.0'
version = '1.0.1'

class TasksLockService:
"""Acquires/releases locks from the TasksLockAPI."""
Expand All @@ -13,23 +14,37 @@ def __init__(self,
protocol: str | None = None,
host: str | None = None,
port: int | None = None):
self._protocol = protocol if not protocol is None else host_utils.get_host()
self._protocol = protocol if not protocol is None else host_utils.get_protocol()
self._host = host if not host is None else host_utils.get_host()
self._port = port if not port is None else host_utils.get_port()
self._url = f'{self._protocol}://{self._host}:{str(self._port)}'
self.version = version
self.logger = getLogger(__name__)

def acquire_lock(self, task_name: str, context_id: str, wait_for_lock: bool) -> TaskLock:
"""Acquires lock from the TasksLockAPI."""

task_lock = TaskLock({'taskName': task_name, 'contextId': context_id, 'isLockAcquired': False}, lambda : print(f'Cannot release lock for {task_name}, lock not acquired contextId: {context_id}'))
self.logger.debug(f"acquiring lock for task {task_name} contextId: {context_id}")
response = requests.get(f'{self._url}/tasks-lock/api/v1/acquire?taskName={task_name}&contextId={context_id}&waitForLock={"true" if wait_for_lock else "false"}')
if response.status_code < 300:
body = json.loads(response.content)
return TaskLock(body, lambda: self.release_lock(task_name))
return TaskLock({'taskName': task_name, 'contextId': context_id, 'isLockAcquired': False}, lambda : print(f'Cannot release lock for {task_name}, lock not acquired contextId: {context_id}'))
task_lock = TaskLock(body, lambda: self.release_lock(task_name))
if task_lock.is_locked:
self.logger.debug(f"acquired lock for task {task_name} contextId: {context_id}")
else:
self.logger.warning(f"did not acquire lock for task {task_name} contextId: {context_id}")

return task_lock

def release_lock(self, task_name: str) -> bool:
"""Releases lock from the TasksLockAPI."""
self.logger.debug(f"releasing lock for task {task_name}")

response = requests.get(f'{self._url}/tasks-lock/api/v1/release?taskName={task_name}')
if response.status_code < 300:
self.logger.debug(f"released lock for task {task_name}")
return True

self.logger.warning(f"did not release lock for task {task_name}, received status {response.status_code}")
return False
12 changes: 4 additions & 8 deletions client-modules/python/src/taskslock/client/utils/host_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@


def get_host() -> str:
try:
return os.environ.get('TASKS_LOCK_API_HOST')
except Exception as e:
return 'localhost'
host = os.environ.get('TASKS_LOCK_API_HOST')
return host if host is not None else 'localhost'


def get_protocol() -> str:
try:
return os.environ.get('TASKS_LOCK_API_PROTOCOL')
except Exception as e:
return 'http'
proto = os.environ.get('TASKS_LOCK_API_PROTOCOL')
return proto if proto is not None else 'http'


def get_port() -> int:
Expand Down

0 comments on commit 132af58

Please sign in to comment.