Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 1 | # 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 Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import |
| 16 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 17 | import time |
| 18 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 19 | from multistructlog import create_logger |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 20 | from xosconfig import Config |
| 21 | |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 22 | |
| 23 | class Synchronizer(object): |
| 24 | def __init__(self): |
| 25 | self.log = create_logger(Config().get("logging")) |
| 26 | |
| 27 | def create_model_accessor(self): |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 28 | from .modelaccessor import model_accessor |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 29 | |
| 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 Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 37 | # variable is unused |
| 38 | _i = self.model_accessor.Site.objects.first() # noqa: F841 |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 39 | 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 Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 59 | from .backend import Backend |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 60 | |
| 61 | log_closure = self.log.bind(synchronizer_name=Config().get("name")) |
Scott Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 62 | backend = Backend(log=log_closure, model_accessor=self.model_accessor) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 63 | backend.run() |