Skip to content
This repository was archived by the owner on Sep 12, 2018. It is now read-only.

Commit 49a3e93

Browse files
committed
Lock out setup_database method
1 parent 10656ba commit 49a3e93

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

docker_registry/lib/index/db.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
from ... import storage
7+
from ... import toolkit
78
from .. import config
89
from . import Index
910
import sqlalchemy
@@ -61,6 +62,7 @@ def __init__(self, database=None):
6162
self._setup_database()
6263
super(SQLAlchemyIndex, self).__init__()
6364

65+
@toolkit.exclusive_lock
6466
def _setup_database(self):
6567
session = self._session()
6668
if self._engine.has_table(table_name=Version.__tablename__):

docker_registry/toolkit.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import distutils.version
55
import functools
66
import logging
7+
import os
78
import random
89
import re
910
import string
@@ -278,6 +279,22 @@ def wrapper(repository, *args, **kwargs):
278279
return wrapper
279280

280281

282+
def exclusive_lock(f):
283+
@functools.wraps(f)
284+
def wrapper(*args, **kwargs):
285+
lock_path = os.path.join(
286+
'/var/lock', 'registry.{0}.lock'.format(f.func_name)
287+
)
288+
if os.path.exists(lock_path):
289+
return
290+
lock_file = open(lock_path, 'w')
291+
lock_file.close()
292+
result = f(*args, **kwargs)
293+
os.remove(lock_path)
294+
return result
295+
return wrapper
296+
297+
281298
def get_repository():
282299
auth = flask.request.headers.get('authorization', '')
283300
if not auth:

0 commit comments

Comments
 (0)