Browse Source

SQL: only create tables in scheduler

If the scheduler and web processes are started simultaneously,
table creation can error out.  Therefore, only create tables
within the scheduler process.

The scheduler is the only process that calls the onLoad method.

Change-Id: Ibb72e5e1af0cdd0db51744767c853318516dc22d
tags/3.6.1
James E. Blair 7 months ago
parent
commit
41298d51ad
1 changed files with 14 additions and 2 deletions
  1. 14
    2
      zuul/driver/sql/sqlconnection.py

+ 14
- 2
zuul/driver/sql/sqlconnection.py View File

@@ -161,7 +161,6 @@ class SQLConnection(BaseConnection):
161 161
                 self.dburi,
162 162
                 poolclass=sqlalchemy.pool.QueuePool,
163 163
                 pool_recycle=self.connection_config.get('pool_recycle', 1))
164
-            self._migrate()
165 164
 
166 165
             # If we want the objects returned from query() to be
167 166
             # usable outside of the session, we need to expunge them
@@ -174,7 +173,6 @@ class SQLConnection(BaseConnection):
174 173
                                                     autoflush=False)
175 174
             self.session = orm.scoped_session(self.session_factory)
176 175
 
177
-            self.tables_established = True
178 176
         except sa.exc.NoSuchModuleError:
179 177
             self.log.exception(
180 178
                 "The required module for the dburi dialect isn't available. "
@@ -205,6 +203,20 @@ class SQLConnection(BaseConnection):
205 203
             tag = {'table_prefix': self.table_prefix}
206 204
             alembic.command.upgrade(config, 'head', tag=tag)
207 205
 
206
+    def onLoad(self):
207
+        try:
208
+            self._migrate()
209
+            self.tables_established = True
210
+        except sa.exc.NoSuchModuleError:
211
+            self.log.exception(
212
+                "The required module for the dburi dialect isn't available. "
213
+                "SQL connection %s will be unavailable." %
214
+                self.connection_name)
215
+        except sa.exc.OperationalError:
216
+            self.log.exception(
217
+                "Unable to connect to the database or establish the required "
218
+                "tables. Connection %s is disabled" % self)
219
+
208 220
     def _setup_models(self):
209 221
         Base = declarative_base(metadata=sa.MetaData())
210 222
 

Loading…
Cancel
Save