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