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