Update dependencies, improve CI workflow, and enhance code formatting
- Updated the `sientia-dataops-library` dependency version from 1.4.3 to 1.4.6 in `requirements.txt`. - Modified the GitHub Actions workflow to install development and runtime dependencies separately, improving clarity and organization. - Added code formatting and linting checks using Ruff, along with type checking using mypy, to ensure code quality. - Updated `.gitignore` to include additional cache directories and log files. - Refactored code in various files for consistency in string formatting and improved logging messages.
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import os
|
||||
import asyncio
|
||||
import os
|
||||
import signal
|
||||
import traceback
|
||||
from threading import Event
|
||||
from time import sleep, time
|
||||
from time import time
|
||||
|
||||
from prometheus_client import start_http_server
|
||||
|
||||
@@ -11,7 +11,7 @@ import ingestor.metrics as metrics
|
||||
from ingestor.ingestor import Ingestor
|
||||
|
||||
exit_signal = Event()
|
||||
POD_ID = os.getenv("HOSTNAME", "localhost")
|
||||
POD_ID = os.getenv('HOSTNAME', 'localhost')
|
||||
|
||||
|
||||
async def main():
|
||||
@@ -43,30 +43,27 @@ async def main():
|
||||
try:
|
||||
await ingestor.prepare_ingestor()
|
||||
except Exception as e:
|
||||
metrics.APP_ERRORS_TOTAL.labels(
|
||||
pod_id=POD_ID).inc() # Increment errors
|
||||
ingestor.logger.error(f"Failed to prepare ingestor: {e}")
|
||||
metrics.APP_ERRORS_TOTAL.labels(pod_id=POD_ID).inc() # Increment errors
|
||||
ingestor.logger.error(f'Failed to prepare ingestor: {e}')
|
||||
exit_signal.set()
|
||||
ingestor.logger.info("Ingestor prepared. Starting main loop.")
|
||||
ingestor.logger.info('Ingestor prepared. Starting main loop.')
|
||||
|
||||
while not exit_signal.is_set():
|
||||
start_time = time() # Start loop timer
|
||||
try:
|
||||
await ingestor.loop()
|
||||
metrics.APP_LOOP_COUNT.labels(
|
||||
pod_id=POD_ID).inc() # Increment loop counter
|
||||
metrics.APP_LOOP_COUNT.labels(pod_id=POD_ID).inc() # Increment loop counter
|
||||
|
||||
# Use asyncio.sleep instead of exit_signal.wait for better async compatibility
|
||||
await asyncio.sleep(ingestor.poll_interval)
|
||||
|
||||
except KeyboardInterrupt: # Handle Ctrl+C gracefully
|
||||
print("KeyboardInterrupt received. Setting exit_signal flag.")
|
||||
print('KeyboardInterrupt received. Setting exit_signal flag.')
|
||||
exit_signal.set()
|
||||
except Exception:
|
||||
print("Exception in main loop. Setting exit_signal flag.")
|
||||
print('Exception in main loop. Setting exit_signal flag.')
|
||||
traceback.print_exc()
|
||||
metrics.APP_ERRORS_TOTAL.labels(
|
||||
pod_id=POD_ID).inc() # Increment errors
|
||||
metrics.APP_ERRORS_TOTAL.labels(pod_id=POD_ID).inc() # Increment errors
|
||||
exit_signal.set()
|
||||
finally:
|
||||
# Record loop duration
|
||||
@@ -76,7 +73,7 @@ async def main():
|
||||
await ingestor.shutdown()
|
||||
metrics.APP_UP.labels(pod_id=POD_ID).set(0) # Mark app as DOWN
|
||||
|
||||
ingestor.logger.info("Main loop exit_signaled.")
|
||||
ingestor.logger.info('Main loop exit_signaled.')
|
||||
|
||||
# Give Prometheus a chance to scrape one last time before exiting (optional)
|
||||
await asyncio.sleep(5)
|
||||
@@ -96,7 +93,7 @@ def signal_handler(_signum, _frame):
|
||||
_signum: The signal number received
|
||||
_frame: The current stack frame (unused)
|
||||
"""
|
||||
print(f"Received signal {_signum}. Setting exit_signal flag.")
|
||||
print(f'Received signal {_signum}. Setting exit_signal flag.')
|
||||
exit_signal.set()
|
||||
|
||||
|
||||
@@ -115,12 +112,12 @@ def start_prometheus_server():
|
||||
Exception: If the server fails to start, the application will exit
|
||||
"""
|
||||
try:
|
||||
port = int(os.getenv("HTTP_METRICS_PORT", 9090))
|
||||
port = int(os.getenv('HTTP_METRICS_PORT', 9090))
|
||||
start_http_server(port)
|
||||
print(f"Prometheus server started on port {port}.")
|
||||
print(f'Prometheus server started on port {port}.')
|
||||
metrics.APP_UP.labels(pod_id=POD_ID).set(1) # Mark app as UP
|
||||
except Exception as e:
|
||||
print(f"Failed to start Prometheus server: {e}")
|
||||
print(f'Failed to start Prometheus server: {e}')
|
||||
os._exit(1)
|
||||
|
||||
|
||||
@@ -139,13 +136,13 @@ def run_async_main():
|
||||
asyncio.set_event_loop(loop)
|
||||
loop.run_until_complete(main())
|
||||
except KeyboardInterrupt:
|
||||
print("KeyboardInterrupt received in main thread.")
|
||||
print('KeyboardInterrupt received in main thread.')
|
||||
exit_signal.set()
|
||||
finally:
|
||||
loop.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if __name__ == '__main__':
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
signal.signal(signal.SIGTERM, signal_handler)
|
||||
signal.signal(signal.SIGHUP, signal_handler)
|
||||
|
||||
Reference in New Issue
Block a user