Browse Source

Merge branch 'improvement/logging' of sixfab-power/agent into dev

pull/10/head^2
bisguzar 1 month ago
parent
commit
deb817ee92
4 changed files with 52 additions and 32 deletions
  1. 0
    7
      agent.py
  2. 19
    15
      core/__init__.py
  3. 21
    0
      core/helpers/logger.py
  4. 12
    10
      core/modules/recovery.py

+ 0
- 7
agent.py View File

@@ -1,5 +1,4 @@
import os
import logging

from core import Agent
from core.modules import fixer
@@ -7,12 +6,6 @@ from configparser import ConfigParser

__version__ = "0.2.0"


# is_debugger_true = os.getenv('ENABLE_PMS_AGENT_DEBUG')
# is_debugger_true = True if is_debugger_true == "True" else False
is_debugger_true = True # debug is always enabled for now.

logging.basicConfig(level=logging.DEBUG if is_debugger_true else logging.CRITICAL)
environments_object = ConfigParser()
environments_object.read("/opt/sixfab/.env")
pms_environments = environments_object["pms"]

+ 19
- 15
core/__init__.py View File

@@ -1,6 +1,8 @@
import os
import time
import json
import logging
import logging.handlers
import subprocess
import paho.mqtt.client as mqtt

@@ -14,10 +16,12 @@ from .modules.set_configurations import update_timezone

from .helpers.configs import config_object_to_string
from .helpers import network
from .helpers.logger import initialize_logger

MQTT_HOST = "power.sixfab.com"
MQTT_PORT = 1883

logger = initialize_logger()

class Agent(object):
def __init__(
@@ -65,7 +69,7 @@ class Agent(object):
ping_host = network.get_host_by_addr(ping_addr)

if not self.is_connected:
logging.debug("[LOOP] Network online, starting mqtt agent")
logger.debug("[LOOP] Network online, starting mqtt agent")
self.client.connect(
self.configs["environments"].get("MQTT_HOST", MQTT_HOST),
MQTT_PORT,
@@ -81,7 +85,7 @@ class Agent(object):
continue

if self.is_connected:
logging.debug("[LOOP] Network ofline, blocking mqtt agent")
logger.debug("[LOOP] Network ofline, blocking mqtt agent")
self.is_connected = False
self.client.loop_stop()
self.client.disconnect()
@@ -96,7 +100,7 @@ class Agent(object):
continue

try:
logging.debug("[FEEDER] Starting, locking")
logger.debug("[FEEDER] Starting, locking")
with self.lock_thread:
self.client.publish(
"/device/{token}/feed".format(token=self.token),
@@ -106,7 +110,7 @@ class Agent(object):
)
),
)
logging.debug("[FEEDER] Done, releasing setters")
logger.debug("[FEEDER] Done, releasing setters")

time.sleep(self.configs["feeder_interval"])
except:
@@ -120,9 +124,9 @@ class Agent(object):
self.PMSAPI.softReboot()
self.PMSAPI.sendSystemTemp()
except Exception as e:
logging.debug("[ROUTINE WORKER] Error occured, trying again in 15secs")
logger.debug("[ROUTINE WORKER] Error occured, trying again in 15secs")
else:
logging.debug("[ROUTINE WORKER] Metrics sent to hat")
logger.debug("[ROUTINE WORKER] Metrics sent to hat")

time.sleep(15)

@@ -151,14 +155,14 @@ class Agent(object):
"\n")]

if is_ntp_synchronized == 'yes':
logging.debug("NTP synchronized, updating timezone")
logger.debug("NTP synchronized, updating timezone")

with self.lock_thread:
logging.debug("Setting RTC timezone to " + timezone)
logger.debug("Setting RTC timezone to " + timezone)
update_timezone(self.PMSAPI, timezone)

return True
logging.debug("Waiting for NTP synchronization")
logger.debug("Waiting for NTP synchronization")
time.sleep(15)

def _lock_feeder_for_firmware_update(self):
@@ -180,18 +184,18 @@ class Agent(object):
command_data = message.get("data", {})

if "connected" in message:
logging.error(
"\033[33m[CONNECTION] \033[0m status message recieved from broker")
logger.info(
"[CONNECTION] status message recieved from broker")
if not message["connected"]:
logging.error(
"\033[33m[CONNECTION] \033[0m looks like broker thinks we are disconnected, sending status message again")
logger.warning(
"[CONNECTION] looks like broker thinks we are disconnected, sending status message again")
self.client.publish(
"/device/{}/status".format(self.token),
json.dumps({"connected": True}),
retain=True,
)
logging.error(
"\033[33m[CONNECTION] \033[0m status changed to true")
logger.info(
"[CONNECTION] status changed to true")

return


+ 21
- 0
core/helpers/logger.py View File

@@ -0,0 +1,21 @@
import os
import logging
import logging.handlers


def initialize_logger():
logging_file_path = os.path.expanduser("~")+"/.sixfab/"
if not os.path.exists(logging_file_path):
os.mkdir(logging_file_path)

logger = logging.getLogger("agent")
logger.setLevel(logging.DEBUG)

formatter = logging.Formatter("%(asctime)s %(levelname)-8s %(message)s")
log_file_handler = logging.handlers.TimedRotatingFileHandler(filename=logging_file_path+"agent-log", when="midnight", backupCount=3)
log_file_handler.setFormatter(formatter)

logger.addHandler(log_file_handler)

return logger

+ 12
- 10
core/modules/recovery.py View File

@@ -3,30 +3,32 @@ import logging
from pms_api import SixfabPMS
from pms_api.exceptions import CRCCheckFailed

logger = logging.getLogger("agent")

def try_until_get(api, function):
try_count = 1
while True:
if try_count > 5:
logging.error("\033[33m[{}] \033[0m tried for 3 times and couldn't get response".format(function))
logger.error("[{}] tried for 3 times and couldn't get response".format(function))
raise OverflowError("")

try:
resp = getattr(api, function)()
except CRCCheckFailed:
logging.error("\033[33m[{}] \033[0m crc check failed, reinitializing api".format(function))
logger.error("[{}] crc check failed, reinitializing api".format(function))
del api
api = SixfabPMS()
except TypeError:
logging.error("\033[33m[{}] \033[0m TypeError raised, clearing pipe".format(function))
logger.error("[{}] TypeError raised, clearing pipe".format(function))
api.clearPipe()
except Exception as e:
logging.error("\033[33m[{}] \033[0m unknown exception raised".format(function))
logger.error("[{}] unknown exception raised".format(function))
else:
return resp
finally:
try_count += 1
logging.error("[{}] trying again".format(function))
logger.error("[{}] trying again".format(function))
time.sleep(0.5)


@@ -35,25 +37,25 @@ def try_until_done(api, function, *args, **kwargs):

while True:
if try_count > 5:
logging.error("\033[33m[{}] \033[0m tried for 3 times and couldn't get response".format(function))
logger.error("[{}] tried for 3 times and couldn't get response".format(function))
raise OverflowError("")
try:
resp = getattr(api, function)(*args, **kwargs)
except CRCCheckFailed:
logging.error("\033[33m[{}] \033[0m crc check failed, reinitializing api".format(function))
logger.error("[{}] crc check failed, reinitializing api".format(function))
del api
api = SixfabPMS()
except TypeError:
logging.error("\033[33m[{}] \033[0m TypeError raised, clearing pipe".format(function))
logger.error("[{}] TypeError raised, clearing pipe".format(function))
api.clearPipe()
except Exception as e:
logging.error("\033[33m[{}] \033[0m unknown exception raised".format(function))
logger.error("[{}] unknown exception raised".format(function))
else:
return resp
finally:
try_count += 1


logging.error("[{}] trying again".format(function))
logger.error("[{}] trying again".format(function))
time.sleep(0.5)

Loading…
Cancel
Save