#!/usr/bin/env python3 # -*- coding: utf-8 -*- # System modules import json from time import sleep import subprocess # Third-party modules from astropy.time import Time from flask import Response, Blueprint # Custom modules from utils.url_stuff import api_base_url from web.server.scheduler import scheduler web = Blueprint('stream', __name__) ######## # STREAM ######## @web.route('/logfile') def stream_logfile(): def inner(today): proc = subprocess.Popen( [f"tail -f -n30 ./data/log/OARPAF.{today}.log"], shell=True, stdout=subprocess.PIPE ) try: for line in iter(proc.stdout.readline, ''): line = line.rstrip().decode() if not line: sleep(0.05) yield "event: logfile\ndata: \n\n" continue sleep(0.05) yield f"event: logfile\ndata: {line}\n\n" except GeneratorExit: print("qui") print("Stop streaming log") print("Closing subprocess") proc.terminate() proc.wait() print("Subprocess closed") print("Streaming template log...") today = Time.now().iso.split()[0] return Response(inner(today), mimetype="text/event-stream")