diff options
| author | Vo Minh Thu <vmt@openerp.com> | 2013-06-21 15:24:11 +0200 |
|---|---|---|
| committer | Vo Minh Thu <vmt@openerp.com> | 2013-06-21 15:24:11 +0200 |
| commit | da38583b821bef77a74f299f2e05352231caddc2 (patch) | |
| tree | 9e113defb2c9ee6bf00c69f05f107b2e99f5d968 /openerpcommand | |
| parent | 74ca07dd1c5b30933005f308643bbe87d99abac3 (diff) | |
[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
Diffstat (limited to 'openerpcommand')
| -rw-r--r-- | openerpcommand/initialize.py | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/openerpcommand/initialize.py b/openerpcommand/initialize.py index 9822dd5f5b2..6261312de7e 100644 --- a/openerpcommand/initialize.py +++ b/openerpcommand/initialize.py @@ -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). |
