[IMP] oe initialize: add a file lock to protect concurrent access to the template db when creating a new db (useful when multiple oe initialize are performed at the same time).

bzr revid: vmt@openerp.com-20130621132411-r6trrt5kd8c4fbca
This commit is contained in:
Vo Minh Thu 2013-06-21 15:24:11 +02:00
parent 74ca07dd1c
commit da38583b82
1 changed files with 28 additions and 1 deletions

View File

@ -1,8 +1,11 @@
"""
Install OpenERP on a new (by default) database.
"""
import contextlib
import errno
import os
import sys
import time
import common
@ -11,7 +14,8 @@ def install_openerp(database_name, create_database_flag, module_names, install_d
config = openerp.tools.config
if create_database_flag:
create_database(database_name)
with lock_file('/tmp/global_openerp_create_database.lock'):
create_database(database_name)
config['init'] = dict.fromkeys(module_names, 1)
@ -23,6 +27,29 @@ def install_openerp(database_name, create_database_flag, module_names, install_d
return registry
# From http://code.activestate.com/recipes/576572/
@contextlib.contextmanager
def lock_file(path, wait_delay=.1, max_try=600):
attempt = 0
while True:
attempt += 1
if attempt > max_try:
raise IOError("Could not lock file %s." % path)
try:
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR)
except OSError, e:
if e.errno != errno.EEXIST:
raise
time.sleep(wait_delay)
continue
else:
break
try:
yield fd
finally:
os.close(fd)
os.unlink(path)
# TODO turn template1 in a parameter
# This should be exposed from openerp (currently in
# openerp/service/web_services.py).