config changes
This commit is contained in:
73
main.py
73
main.py
@@ -7,7 +7,7 @@ import os
|
||||
import socket, struct
|
||||
import requests
|
||||
from flask import Flask
|
||||
|
||||
import time
|
||||
import consul
|
||||
|
||||
from logging.config import dictConfig
|
||||
@@ -28,6 +28,18 @@ dictConfig({
|
||||
}
|
||||
})
|
||||
|
||||
def convert_seconds(seconds):
|
||||
if seconds > 0:
|
||||
# generated by ChatGPT
|
||||
days = seconds // (24 * 60 * 60)
|
||||
seconds = seconds % (24 * 60 * 60)
|
||||
hours = seconds // (60 * 60)
|
||||
seconds = seconds % (60 * 60)
|
||||
minutes = seconds // 60
|
||||
seconds = seconds % 60
|
||||
return (f"{days} days {hours} hours {minutes} minutes {seconds} seconds")
|
||||
else:
|
||||
return "never"
|
||||
|
||||
def get_ip():
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
@@ -63,9 +75,21 @@ else:
|
||||
|
||||
BASE_CONSUL_URL = 'http://' + CONSUL + ':8500'
|
||||
|
||||
config = {'default':'config'}
|
||||
prev_config = {'default':'config'}
|
||||
changed = 0
|
||||
configuration = {
|
||||
'default': {
|
||||
'config' : {'default':'config'}
|
||||
, 'time' : int(time.time())
|
||||
}
|
||||
, 'consul' :{
|
||||
'config' : {}
|
||||
, 'time': 0
|
||||
, 'state': {
|
||||
'status': ''
|
||||
, 'time': 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SERVICE_ADDRESS = get_ip()
|
||||
|
||||
PORT = 8080
|
||||
@@ -77,17 +101,17 @@ app = Flask(__name__)
|
||||
|
||||
@app.route('/')
|
||||
def home():
|
||||
global config
|
||||
global prev_config
|
||||
global changed
|
||||
if changed == 1:
|
||||
ad = f'<br>configuration changed. new configuration: {config} | prev config: {prev_config}'
|
||||
if config != {}:
|
||||
prev_config = config
|
||||
changed = 0
|
||||
global configuration
|
||||
message = f"Hello World, <br> I`m {HOSTNAME} from {SERVICE_ADDRESS} {PORT} {UUID} <br> my config: {configuration['default']['config']} |"
|
||||
if configuration['consul']['config'] != configuration['default']['config']:
|
||||
delta_time = convert_seconds(configuration['consul']['time']-configuration['default']['time'])
|
||||
message += f"<br>configuration changed {delta_time} ago. new configuration: {configuration['consul']['config']} | default config: {configuration['default']['config']}"
|
||||
else:
|
||||
ad = f'configuration NOT changed: {config}'
|
||||
return f"Hello World, <br> I`m {HOSTNAME} from {SERVICE_ADDRESS} {PORT} {UUID} <br> my config: {config} | {ad}"
|
||||
message += f'configuration NOT changed'
|
||||
message += '<br>'
|
||||
message += f"Consul request state: {configuration['consul']['state']['status']} was {convert_seconds(int(time.time()) - configuration['consul']['state']['time'])} ago"
|
||||
message += f'<br> <!-- {configuration} -->'
|
||||
return message
|
||||
|
||||
|
||||
@app.route('/health')
|
||||
@@ -95,16 +119,23 @@ def hello_world():
|
||||
data = {
|
||||
'status': 'healthy'
|
||||
}
|
||||
global config
|
||||
global prev_config
|
||||
global changed
|
||||
global configuration
|
||||
try:
|
||||
_, resp = c.kv.get('PythonApp/config', wait='2s')
|
||||
resp_config = resp['Value']
|
||||
if resp_config != prev_config:
|
||||
changed = 1
|
||||
config = resp_config
|
||||
configuration['consul'] = {
|
||||
'config' : resp['Value']
|
||||
, 'time': int(time.time())
|
||||
, 'state': {
|
||||
'status': 'OK'
|
||||
, 'time': int(time.time())
|
||||
}
|
||||
}
|
||||
except Exception as e:
|
||||
configuration['consul']['state'] = {
|
||||
'status': 'Failed to fetch'
|
||||
, 'time': int(time.time())
|
||||
}
|
||||
|
||||
# config = {}
|
||||
app.logger.debug(f'Get config from consul failed: {e}')
|
||||
return json.dumps(data)
|
||||
|
||||
Reference in New Issue
Block a user