Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/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")