aws_s3_lock#

This module provides a distributed lock using AWS S3 backend.

Requirements:

python>=3.7
boto3

Usage:

import boto3
from aws_s3_lock.py import Lock, Vault, get_utc_now, AlreadyLockedError

s3_client = boto3.client("s3")

# define a vault backend
vault = Vault(bucket="my-bucket", key="my-task.json", expire=900, wait=1.0)

# acquire the lock before doing any task
lock = vault.acquire(s3_client) # or vault.acquire(s3_client, owner="alice")

# do yor task that requires the distributed lock here
...

# release the lock after the task is done
vault.release(s3_client, lock)
exception aws_ops_alpha.vendor.aws_s3_lock.AlreadyLockedError[source]#
class aws_ops_alpha.vendor.aws_s3_lock.Lock(expire: int, lock_time: str, release_time: Union[str, NoneType], owner: Union[str, NoneType])[source]#
is_locked(utc_now: datetime, expect_owner: str) bool[source]#

From the expect_owner point of view, is the lock locked?

  • if the lock has no owner, then it is NOT LOCKED

  • if the lock’s owner matches the expected owner, then it is NOT LOCKED

  • if the lock’s owner is not the expected owner:
    • if the lock is expired, then it is NOT LOCKED

    • if the lock is not expired, then it is LOCKED

class aws_ops_alpha.vendor.aws_s3_lock.Vault(bucket: str, key: str, expire: int, wait: float = 1.0)[source]#

A vault is an S3 object to store a lock.

Parameters:
  • bucket – the S3 bucket.

  • key – the S3 key.

  • expire – how long the lock will expire in seconds.

  • wait – how long we should wait after we thought we successfully acquired the lock, before actually doing any task.

acquire(s3_client: S3Client, owner: Optional[str] = None) Lock[source]#

The owner tries to acquire the lock.

  • If failed, raise AlreadyLockedError.

  • If the lock is already owned by the owner, and still not expired, then

    refresh lock_time.

Parameters:
  • s3_client

  • owner – the owner of the lock. If None, then a random uuid will be used.

release(s3_client: S3Client, lock: Lock) Lock[source]#

Release the lock. Set the owner as None and update release time.

Parameters:
  • s3_client

  • lock – the lock to release.