blob: 42344e99be94a1fda6229d6dbd2bb30004d3de87 [file] [log] [blame]
Scott Bakerbba67b62019-01-28 17:38:21 -08001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Zack Williams5c2ea232019-01-30 15:23:01 -070015from __future__ import absolute_import
16
Scott Bakerbba67b62019-01-28 17:38:21 -080017import time
18
Scott Bakerbba67b62019-01-28 17:38:21 -080019from multistructlog import create_logger
Zack Williams5c2ea232019-01-30 15:23:01 -070020from xosconfig import Config
21
Scott Bakerbba67b62019-01-28 17:38:21 -080022
23class Synchronizer(object):
24 def __init__(self):
25 self.log = create_logger(Config().get("logging"))
26
27 def create_model_accessor(self):
Zack Williams5c2ea232019-01-30 15:23:01 -070028 from .modelaccessor import model_accessor
Scott Bakerbba67b62019-01-28 17:38:21 -080029
30 self.model_accessor = model_accessor
31
32 def wait_for_ready(self):
33 models_active = False
34 wait = False
35 while not models_active:
36 try:
Zack Williams5c2ea232019-01-30 15:23:01 -070037 # variable is unused
38 _i = self.model_accessor.Site.objects.first() # noqa: F841
Scott Bakerbba67b62019-01-28 17:38:21 -080039 models_active = True
40 except Exception as e:
41 self.log.info("Exception", e=e)
42 self.log.info("Waiting for data model to come up before starting...")
43 time.sleep(10)
44 wait = True
45
46 if wait:
47 time.sleep(
48 60
49 ) # Safety factor, seeing that we stumbled waiting for the data model to come up.
50
51 def run(self):
52 self.create_model_accessor()
53 self.wait_for_ready()
54
55 # Don't import backend until after the model accessor has been initialized. This is to support sync steps that
56 # use `from xossynchronizer.modelaccessor import ...` and require the model accessor to be initialized before
57 # their code can be imported.
58
Zack Williams5c2ea232019-01-30 15:23:01 -070059 from .backend import Backend
Scott Bakerbba67b62019-01-28 17:38:21 -080060
61 log_closure = self.log.bind(synchronizer_name=Config().get("name"))
Scott Bakerc2fddaa2019-01-30 15:45:03 -080062 backend = Backend(log=log_closure, model_accessor=self.model_accessor)
Scott Bakerbba67b62019-01-28 17:38:21 -080063 backend.run()