Watchdog added, managing multiple browser instances

This commit is contained in:
Bla 2023-10-26 13:57:20 +00:00
parent 704bf29892
commit 993d542610

View file

@ -1,30 +1,30 @@
from seleniumwire import webdriver # Import from seleniumwire from seleniumwire import webdriver # Import from seleniumwire
from seleniumwire.utils import decode from seleniumwire.utils import decode
from time import sleep from time import sleep
from collections import deque
from flask import Flask as f from flask import Flask as f
from flask import Response from flask import Response
from dateutil import parser from dateutil import parser
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
import json import json
import re import re
import asyncio
import logging
import threading
import os
#log = logging.getLogger('werkzeug')
#log.setLevel(logging.ERROR)
#log.disabled = True
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument("--incognito")
options.add_argument("--nogpu")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1280,1280")
options.add_argument("--no-sandbox")
options.add_argument("--enable-javascript")
options.add_argument('--disable-dev-shm-usage')
# Create a new instance of the Chrome driver # Create a new instance of the Chrome driver
driver = webdriver.Chrome(options=options)
q = deque()
valuestore = {} valuestore = {}
datestore = {} datestore = {}
lastUpdate = datetime.now(timezone.utc)
app = f("web") app = f("web")
#app.logger.disabled = True
h2 = timedelta(hours=2) h2 = timedelta(hours=2)
def interceptor(request, response): # A response interceptor takes two args def interceptor(request, response): # A response interceptor takes two args
@ -37,12 +37,30 @@ def interceptor(request, response): # A response interceptor takes two args
handleJson(j) handleJson(j)
except ValueError: except ValueError:
if i.find("online-level") > 0: if i.find("online-level") > 0:
print ("Fixed: ", i) #print ("Fixed: ", i)
j = json.loads(i[:i.rfind(']') + 1]) j = json.loads(i[:i.rfind(']') + 1])
handleJson(j) handleJson(j)
else: #else:
print ("Err: ", i) #print ("Err: ", i)
# StackOverflow
def killChildren(driver):
print('Quitting session: ', driver.session_id)
driver.quit()
try:
pid = True
while pid:
pid = os.waitpid(-1, os.WNOHANG)
print("Reaped child: ", str(pid))
try:
if pid[0] == 0:
pid = False
except:
pass
except ChildProcessError:
pass
def idIsRecent(id): def idIsRecent(id):
return id in valuestore and datetime.now(timezone.utc) - parser.parse(datestore[id]) < timedelta(seconds=30) return id in valuestore and datetime.now(timezone.utc) - parser.parse(datestore[id]) < timedelta(seconds=30)
@ -60,7 +78,7 @@ def handleJson(jsonObj):
# Gibt den letzten bekannten Wert fuer die NMTID zurueck # Gibt den letzten bekannten Wert fuer die NMTID zurueck
@app.route('/nmt/<int:id>') @app.route('/nmt/<int:id>')
def nmtid(id): def nmtid(id):
if id in valuestore and datetime.now(timezone.utc) - parser.parse(datestore[id]) < timedelta(seconds=30): if idIsRecent(id):
return format( valuestore[id], '.1f') return format( valuestore[id], '.1f')
else: else:
return "E_NA" # Entweder out of date oder nicht existent return "E_NA" # Entweder out of date oder nicht existent
@ -71,9 +89,58 @@ def ids():
l = filter(idIsRecent,list(valuestore.keys())) l = filter(idIsRecent,list(valuestore.keys()))
return ','.join(map(str,l)) return ','.join(map(str,l))
if __name__ == '__main__': def createDriver():
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument("--incognito")
options.add_argument("--nogpu")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1280,1280")
options.add_argument("--no-sandbox")
options.add_argument("--enable-javascript")
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options)
driver.response_interceptor = interceptor driver.response_interceptor = interceptor
return driver
if __name__ == '__main__':
# start flask server
threading.Thread(target=lambda: app.run(host='0.0.0.0', port=80, debug=False, use_reloader=False)).start()
# wait time to restart will be dynamically adapted,
# if unexpected things happen in chrome or the real world
restartWait = timedelta(hours=4)
# setup webdriver
d0 = createDriver()
# start initial driver
d0.get('https://dus-travis.dus.com/')
lastaccess = datetime.now(timezone.utc)
# loop and restart with restartWait Interval
while True:
sleep(10)
if datetime.now(timezone.utc) > lastaccess + restartWait:
d1 = createDriver()
d1.get('https://dus-travis.dus.com/')
print ("New Failover Driver started, waiting 10 seconds ...")
sleep(10)
killChildren(d0)
del d0
print ("Original driver deleted")
d0 = createDriver()
d0.get('https://dus-travis.dus.com/')
print ("Recreating original driver and waiting 10 seconds ...")
sleep(10)
killChildren(d1)
del d1
print ("Failover Driver deleted")
lastaccess = datetime.now(timezone.utc)
l = filter(idIsRecent,list(valuestore.keys()))
if len(l) < 1:
restartWait = timedelta(seconds=60)
driver.get('https://dus-travis.dus.com/')
app.run(host='0.0.0.0', port=80, debug=False)