Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 1 | #!/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 Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 17 | import time |
| 18 | |
| 19 | from xosconfig import Config |
| 20 | from multistructlog import create_logger |
| 21 | |
| 22 | class 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 Baker | 4839dec | 2019-02-27 16:50:37 -0800 | [diff] [blame] | 36 | _i = self.model_accessor.Site.objects.first() |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 37 | 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 Baker | c2fddaa | 2019-01-30 15:45:03 -0800 | [diff] [blame] | 60 | backend = Backend(log=log_closure, model_accessor=self.model_accessor) |
Scott Baker | bba67b6 | 2019-01-28 17:38:21 -0800 | [diff] [blame] | 61 | backend.run() |
| 62 | |
| 63 | |
| 64 | if __name__ == "__main__": |
| 65 | main() |