Skip to content

Commit

Permalink
Use distributed lock for multiple worker processes
Browse files Browse the repository at this point in the history
  • Loading branch information
mreid-tt committed Apr 29, 2023
1 parent 736ecdf commit 1240c5e
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions spkrepo/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import re
import shutil
import threading
import redis
from functools import wraps

from flask import Blueprint, _request_ctx_stack, current_app, request
Expand All @@ -29,14 +29,13 @@
from ..utils import SPK

api = Blueprint("api", __name__)
redis_client = redis.Redis(host='localhost', port=6379, db=0)
redis_lock = redis_client.lock('my_lock')

# regexes
firmware_re = re.compile(r"^(?P<version>\d\.\d)-(?P<build>\d{3,6})$")
version_re = re.compile(r"^(?P<upstream_version>.*)-(?P<version>\d+)$")

# Create two locks
package_lock = threading.Lock()
version_lock = threading.Lock()

def api_auth_required(f):
@wraps(f)
Expand Down Expand Up @@ -135,7 +134,10 @@ def post(self):
data_path = current_app.config["DATA_PATH"]

# Package
with package_lock:
try:
# Acquire the Redis lock
redis_lock.acquire()

create_package = False
package = Package.find(spk.info["package"])
if package is None:
Expand All @@ -158,9 +160,16 @@ def post(self):
# Add package to database
db.session.add(package)
db.session.commit()

finally:
# Release the Redis lock
redis_lock.release()

# Version
with version_lock:
try:
# Acquire the Redis lock
redis_lock.acquire()

create_version = False
match = version_re.match(spk.info["version"])
if not match:
Expand Down Expand Up @@ -247,6 +256,10 @@ def post(self):
db.session.add(version)
db.session.commit()

finally:
# Release the Redis lock
redis_lock.release()

# Build
if version.id:
# check for conflicts
Expand Down

0 comments on commit 1240c5e

Please sign in to comment.