Skip to content

Commit

Permalink
Merge pull request #31 from ENCODE-DCC/dev
Browse files Browse the repository at this point in the history
v0.4.1
  • Loading branch information
leepc12 authored Nov 7, 2022
2 parents bee800b + d1217ce commit 46d9396
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion autouri/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
from .s3uri import S3URI

__all__ = ["AbsPath", "AutoURI", "URIBase", "GCSURI", "HTTPURL", "S3URI"]
__version__ = "0.4.0"
__version__ = "0.4.1"
22 changes: 18 additions & 4 deletions autouri/gcsuri.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

from .autouri import AutoURI, URIBase
from .metadata import URIMetadata, get_seconds_from_epoch, parse_md5_str
from .ntp_now import now_utc

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -64,6 +65,14 @@ def add_google_app_creds_to_env(service_account_key_file):


class GCSURILock(BaseFileLock):
"""Class constants:
- LOCK_FILE_EXPIRATION_SEC:
Expiration of a lock file based on its modtime in seconds
If expired then such lock file is ignored.
"""

LOCK_FILE_EXPIRATION_SEC = 1800

def __init__(self, lock_file, timeout=900, poll_interval=10.0, no_lock=False):
super().__init__(lock_file, timeout=timeout)
self._poll_interval = poll_interval
Expand All @@ -80,14 +89,19 @@ def _acquire(self):
u = GCSURI(self._lock_file)
str_id = str(id(self))
try:
if not u.exists:
if (
not u.exists
or now_utc().timestamp() > u.mtime + GCSURILock.LOCK_FILE_EXPIRATION_SEC
):
u.write(str_id, no_lock=True)
time.sleep(self._lock_read_delay)
if u.read() == str_id:
self._lock_file_fd = id(self)

elif u.read() == str_id:
self._lock_file_fd = id(self)

except (Forbidden, NotFound):
raise
except ClientError:
except (ClientError, ValueError):
pass
return None

Expand Down
20 changes: 16 additions & 4 deletions autouri/s3uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
S3 Object versioning must be turned off
"""
import logging
import time
from tempfile import NamedTemporaryFile
from typing import Optional, Tuple

Expand All @@ -13,6 +12,7 @@

from .autouri import AutoURI, URIBase
from .metadata import URIMetadata, get_seconds_from_epoch, parse_md5_str
from .ntp_now import now_utc

logger = logging.getLogger(__name__)

Expand All @@ -28,8 +28,15 @@ class S3URILock(BaseFileLock):
This module first checks if .lock does not exist, then tries to write .lock with id(self).
It waits for a short time (self._lock_read_delay) and checks if written .lock has the same id(self).
self._lock_read_delay is set as poll_interval/10.
Class constants:
- LOCK_FILE_EXPIRATION_SEC:
Expiration of a lock file based on its modtime in seconds
If expired then such lock file is ignored.
"""

LOCK_FILE_EXPIRATION_SEC = 1800

def __init__(self, lock_file, timeout=900, poll_interval=10.0, no_lock=False):
super().__init__(lock_file, timeout=timeout)
self._poll_interval = poll_interval
Expand All @@ -46,11 +53,16 @@ def _acquire(self):
u = S3URI(self._lock_file)
str_id = str(id(self))
try:
if not u.exists:
if (
not u.exists
or now_utc().timestamp() > u.mtime + S3URILock.LOCK_FILE_EXPIRATION_SEC
):
u.write(str_id, no_lock=True)
time.sleep(self._lock_read_delay)
if u.read() == str_id:
self._lock_file_fd = id(self)

elif u.read() == str_id:
self._lock_file_fd = id(self)

except ClientError as e:
status = e.response["ResponseMetadata"]["HTTPStatusCode"]
if status in (403, 404):
Expand Down

0 comments on commit 46d9396

Please sign in to comment.