fix context.destroy in proxy, get recent more efficiently #20
|
@ -28,7 +28,7 @@ def dealer(dealer_addr, router_addr):
|
||||||
|
|
||||||
dealer.close()
|
dealer.close()
|
||||||
router.close()
|
router.close()
|
||||||
context.close()
|
context.destroy()
|
||||||
|
|
||||||
|
|
||||||
def proxy_buffering(frontend_addr, backend_addr, capture_addr=None,
|
def proxy_buffering(frontend_addr, backend_addr, capture_addr=None,
|
||||||
|
@ -200,7 +200,7 @@ def proxy_buffering(frontend_addr, backend_addr, capture_addr=None,
|
||||||
# we never used to get here
|
# we never used to get here
|
||||||
frontend.close()
|
frontend.close()
|
||||||
backend.close()
|
backend.close()
|
||||||
context.close()
|
context.destroy()
|
||||||
|
|
||||||
|
|
||||||
def proxy_forwarder(frontend_addr, backend_addr, capture_addr):
|
def proxy_forwarder(frontend_addr, backend_addr, capture_addr):
|
||||||
|
@ -236,7 +236,7 @@ def proxy_forwarder(frontend_addr, backend_addr, capture_addr):
|
||||||
backend.close()
|
backend.close()
|
||||||
if capture:
|
if capture:
|
||||||
capture.close()
|
capture.close()
|
||||||
context.close()
|
context.destroy()
|
||||||
|
|
||||||
|
|
||||||
def capture(capture_addr):
|
def capture(capture_addr):
|
||||||
|
|
|
@ -70,8 +70,11 @@ class ScreenPublisher(Publisher):
|
||||||
return padding + msg
|
return padding + msg
|
||||||
|
|
||||||
def make_weather(self):
|
def make_weather(self):
|
||||||
|
try:
|
||||||
current = Weather.get_recent(self.weather, 30*60)
|
current = Weather.get_recent(self.weather, 30*60)
|
||||||
return self.align_center(current.desc)
|
return self.align_center(current.desc)
|
||||||
|
except Weather.DoesNotExist:
|
||||||
|
return "no weather"
|
||||||
|
|
||||||
def make_rain(self, weather):
|
def make_rain(self, weather):
|
||||||
return "~?~"
|
return "~?~"
|
||||||
|
@ -95,18 +98,18 @@ class ScreenPublisher(Publisher):
|
||||||
# .replace does not mutate original string
|
# .replace does not mutate original string
|
||||||
shortname = a.replace('room', 'r')
|
shortname = a.replace('room', 'r')
|
||||||
|
|
||||||
try:
|
|
||||||
t0 = time.time()
|
t0 = time.time()
|
||||||
result = Temperatures.get_recent(a, secs=30*60)
|
try:
|
||||||
|
result = Temperatures.get_recent(a, 15*60)
|
||||||
|
result_str = str(round(result.temp, 1)).rjust(4)
|
||||||
|
except Temperatures.DoesNotExist:
|
||||||
|
logger.warning(f"No recent temp found for '{a}'")
|
||||||
|
result_str = "NONE"
|
||||||
|
|
||||||
t1 = time.time() - t0
|
t1 = time.time() - t0
|
||||||
logger.debug(f"query for: {t1:.3f}s, name='{a}'")
|
t_total = round(t1, 3)
|
||||||
tempstr = f"{result.temp:.1f}"
|
logger.debug(f"query for: {t_total}s, name='{a}'")
|
||||||
if result.temp < 10.0:
|
temps.append(f"{shortname}: {result_str} C")
|
||||||
tempstr = " " + tempstr
|
|
||||||
temps.append(f"{shortname}: {tempstr} C")
|
|
||||||
except KeyError:
|
|
||||||
logger.trace(f"no recent temp for '{a}'")
|
|
||||||
temps.append(f"{shortname}: -- C")
|
|
||||||
|
|
||||||
fill = max([len(a) for a in temps])
|
fill = max([len(a) for a in temps])
|
||||||
chunks = chunk([a.rjust(fill) for a in temps], 2)
|
chunks = chunk([a.rjust(fill) for a in temps], 2)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
import json
|
import json
|
||||||
from datetime import datetime, timezone, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
import peewee
|
import peewee
|
||||||
from peewee import DateTimeField, TextField, DecimalField, CharField, BooleanField
|
from peewee import DateTimeField, TextField, DecimalField, CharField, BooleanField
|
||||||
|
@ -34,15 +34,14 @@ def dbconnect(**mysqlconf):
|
||||||
|
|
||||||
|
|
||||||
def seconds(secs):
|
def seconds(secs):
|
||||||
return datetime.now(timezone.utc)-timedelta(seconds=secs)
|
return datetime.now()-timedelta(seconds=secs)
|
||||||
|
|
||||||
|
|
||||||
class BaseModel(peewee.Model):
|
class BaseModel(peewee.Model):
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_last(cls, name):
|
def get_last(cls, name):
|
||||||
# http://docs.peewee-orm.com/en/latest/peewee/querying.html
|
# http://docs.peewee-orm.com/en/latest/peewee/querying.html
|
||||||
return cls.select().where(
|
return cls.select().where(cls.name == name).order_by(-cls.id).get()
|
||||||
cls.name == name).order_by(-cls.id).get()
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_last_many(cls, names):
|
def get_last_many(cls, names):
|
||||||
|
@ -50,9 +49,13 @@ class BaseModel(peewee.Model):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_recent(cls, name, secs):
|
def get_recent(cls, name, secs):
|
||||||
return cls.select().where(
|
last = cls.get_last(name)
|
||||||
cls.time > seconds(secs) and cls.name == name).order_by(
|
last_age = datetime.utcnow() - last.time
|
||||||
cls.time.desc()).get()
|
if last_age.total_seconds() <= float(secs):
|
||||||
|
return last
|
||||||
|
else:
|
||||||
|
logger.info(f"Last value in {cls.__name__} is older than {secs}s, age: {last_age}")
|
||||||
|
raise cls.DoesNotExist
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def retry_create(cls, *args, **kwargs):
|
def retry_create(cls, *args, **kwargs):
|
||||||
|
|
Loading…
Reference in New Issue