| Atul Agarwal | 5aa710b | 2021-02-26 13:42:09 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # This file is part of OSM Monitoring module |
| 4 | |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # 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, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
| 17 | import pymongo |
| 18 | import time |
| 19 | import socket |
| 20 | import logging |
| 21 | |
| 22 | |
| 23 | def wait_till_core_services_are_ready(config, process_name="osm-mon", commondb_wait_time=5, kafka_wait_time=5): |
| 24 | |
| 25 | logging.debug("wait_till_services_are_ready()") |
| 26 | |
| 27 | if not config: |
| 28 | logging.info("Config information is not available") |
| 29 | return False |
| 30 | |
| 31 | # Check if common-db is ready |
| 32 | while(True): |
| 33 | commondb_url = config.conf["database"].get("uri") |
| 34 | try: |
| 35 | commondb = pymongo.MongoClient(commondb_url) |
| 36 | commondb.server_info() |
| 37 | break |
| 38 | except Exception: |
| 39 | logging.info("{} process is waiting for commondb to come up...".format(process_name)) |
| 40 | time.sleep(commondb_wait_time) |
| 41 | |
| 42 | # Check if kafka is ready |
| 43 | while(True): |
| 44 | port_in_use = False |
| Atul Agarwal | 3e56247 | 2021-03-15 16:57:26 +0000 | [diff] [blame] | 45 | try: |
| 46 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| David Garcia | 03afcaa | 2021-03-17 13:09:42 +0100 | [diff] [blame] | 47 | if ( |
| 48 | s.connect_ex( |
| 49 | ( |
| 50 | config.conf.get("message", {}).get("host", "kafka"), |
| 51 | int(config.conf["message"].get("port")), |
| 52 | ) |
| 53 | ) |
| 54 | == 0 |
| 55 | ): |
| Atul Agarwal | 3e56247 | 2021-03-15 16:57:26 +0000 | [diff] [blame] | 56 | port_in_use = True |
| 57 | except Exception as e: |
| 58 | logging.info("Error when trying to get kafka status.") |
| 59 | logging.debug("Exception when trying to get kafka status: %s", str(e)) |
| Atul Agarwal | 5aa710b | 2021-02-26 13:42:09 +0000 | [diff] [blame] | 60 | if port_in_use: |
| 61 | break |
| 62 | else: |
| 63 | logging.info("{} process is waiting for kafka to come up...".format(process_name)) |
| 64 | time.sleep(kafka_wait_time) |
| 65 | |
| 66 | return True |