Skip to content

Commit

Permalink
[FIX] database: wait_for_connection
Browse files Browse the repository at this point in the history
Now we wait some time, to make sure odoo DB is up, because in some
cases postgres can be just starting and marabunta would try to connect
too soon, getting connection refused error!

[BRANCH] bugfix-wait-db-conn-ala
  • Loading branch information
oerp-odoo committed Jan 6, 2023
1 parent 1bf0dfa commit dda35dd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
12 changes: 8 additions & 4 deletions maraplus/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
from marabunta import parser as parser_orig
from marabunta import model as model_orig
from marabunta import runner as runner_orig
from marabunta import database as database_orig
from marabunta.database import MigrationTable
from marabunta import web as web_orig

from .config import Config, get_args_parser
from .parser import YamlParser
from .model import Version, VersionMode
from .runner import VersionRunner
from .database import Database

TIMEOUT = 10


def migrate(config):
Expand Down Expand Up @@ -41,8 +44,9 @@ def migrate(config):
)
migration = migration_parser.parse()

database = database_orig.Database(config)

database = Database(config)
# Wait to make sure connection is up before using it!
database.wait_for_connection(TIMEOUT)
with database.connect() as lock_connection:
application_lock = core_orig.ApplicationLock(lock_connection)
application_lock.start()
Expand All @@ -63,7 +67,7 @@ def migrate(config):
# migration

try:
table = database_orig.MigrationTable(database)
table = MigrationTable(database)
runner = runner_orig.Runner(config, migration, database, table)
runner.perform()
finally:
Expand Down
27 changes: 27 additions & 0 deletions maraplus/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import time
import psycopg2

from marabunta import database as database_orig


class Database(database_orig.Database):
"""Database subclass to allow waiting for open DB connection."""

def wait_for_connection(self, timeout):
"""Wait for given connection specified amount of seconds.
Args:
timeout (int): number of seconds to wait till connection
opens up.
"""
start_time = time.time()
while (time.time() - start_time) < timeout:
try:
conn = psycopg2.connect(**self.dsn())
conn.close()
return True
except psycopg2.OperationalError:
pass
time.sleep(1)
# Timed out.
return False

0 comments on commit dda35dd

Please sign in to comment.