Add configurable delay per client; improve try/catch

This commit is contained in:
Lance Edgar 2017-02-07 14:47:57 -06:00
parent 3f9adfa6c5
commit 4e11748b45
5 changed files with 97 additions and 58 deletions

View file

@ -66,37 +66,35 @@ class TempmonClient(Daemon):
session.close()
raise ConfigurationError("No tempmon client configured for hostname: {}".format(hostname))
client_uuid = client.uuid
self.delay = client.delay or 60
session.close()
# main loop: take readings, pause, repeat
while True:
session = Session()
client = session.query(tempmon.Client).get(client_uuid)
if client.enabled:
try:
session = Session()
try:
client = session.query(tempmon.Client).get(client_uuid)
self.delay = client.delay or 60
if client.enabled:
for probe in client.enabled_probes():
self.take_reading(session, probe)
except:
log.exception("Failed to read/record temperature data")
session.rollback()
raise
else:
# make sure we show as being online
if not client.online:
client.online = True
session.commit()
finally:
session.close()
except:
log.exception("Failed to read/record temperature data (but will keep trying)")
session.rollback()
else:
# make sure we show as being online
if not client.online:
client.online = True
session.commit()
finally:
session.close()
# TODO: make this configurable
time.sleep(60)
time.sleep(self.delay)
def take_reading(self, session, probe):
"""

View file

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
"""add client delay
Revision ID: 76d52bb064b8
Revises: 7c7d205787b0
Create Date: 2017-02-07 14:46:11.268920
"""
from __future__ import unicode_literals, absolute_import
# revision identifiers, used by Alembic.
revision = '76d52bb064b8'
down_revision = u'7c7d205787b0'
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
import rattail.db.types
def upgrade():
# client
op.add_column('client', sa.Column('delay', sa.Integer(), nullable=True))
def downgrade():
# client
op.drop_column('client', 'delay')

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2016 Lance Edgar
# Copyright © 2010-2017 Lance Edgar
#
# This file is part of Rattail.
#
@ -28,6 +28,7 @@ from __future__ import unicode_literals, absolute_import
import datetime
import six
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.declarative import declarative_base
@ -39,6 +40,7 @@ from rattail.db.model.core import ModelBase
Base = declarative_base(cls=ModelBase)
@six.python_2_unicode_compatible
class Client(Base):
"""
Represents a tempmon client.
@ -52,16 +54,23 @@ class Client(Base):
config_key = sa.Column(sa.String(length=50), nullable=False)
hostname = sa.Column(sa.String(length=255), nullable=False)
location = sa.Column(sa.String(length=255), nullable=True)
delay = sa.Column(sa.Integer(), nullable=True, doc="""
Number of seconds to delay between reading / recording temperatures. If
not set, a default of 60 seconds will be assumed.
""")
enabled = sa.Column(sa.Boolean(), nullable=False, default=False)
online = sa.Column(sa.Boolean(), nullable=False, default=False)
def __unicode__(self):
def __str__(self):
return '{} ({})'.format(self.config_key, self.hostname)
def enabled_probes(self):
return [probe for probe in self.probes if probe.enabled]
@six.python_2_unicode_compatible
class Probe(Base):
"""
Represents a probe connected to a tempmon client.
@ -103,10 +112,11 @@ class Probe(Base):
status_changed = sa.Column(sa.DateTime(), nullable=True)
status_alert_sent = sa.Column(sa.DateTime(), nullable=True)
def __unicode__(self):
return unicode(self.description or '')
def __str__(self):
return self.description
@six.python_2_unicode_compatible
class Reading(Base):
"""
Represents a single tempurate reading from a tempmon probe.
@ -136,5 +146,5 @@ class Reading(Base):
taken = sa.Column(sa.DateTime(), nullable=False, default=datetime.datetime.utcnow)
degrees_f = sa.Column(sa.Numeric(precision=7, scale=4), nullable=False)
def __unicode__(self):
return unicode(self.degrees_f)
def __str__(self):
return str(self.degrees_f)

View file

@ -60,23 +60,11 @@ class TempmonServerDaemon(Daemon):
def check_readings(self):
self.now = make_utc()
session = TempmonSession()
probes = session.query(tempmon.Probe)\
.join(tempmon.Client)\
.filter(tempmon.Client.enabled == True)\
.filter(tempmon.Probe.enabled == True)\
.all()
if probes:
cutoff = self.now - datetime.timedelta(seconds=120)
uuids = [probe.uuid for probe in probes]
readings = session.query(tempmon.Reading)\
.filter(tempmon.Reading.probe_uuid.in_(uuids))\
.filter(tempmon.Reading.taken >= cutoff)\
.all()
self.process_readings(probes, readings)
else:
log.warning("found no enabled probes!")
clients = session.query(tempmon.Client)\
.filter(tempmon.Client.enabled == True)
for client in clients:
self.check_readings_for_client(session, client)
session.commit()
session.close()
@ -84,27 +72,36 @@ class TempmonServerDaemon(Daemon):
# TODO: not sure this is really necessary?
self.set_last_checked()
def process_readings(self, probes, readings):
for probe in probes:
probe_readings = [r for r in readings if r.probe is probe]
if probe_readings:
reading = sorted(probe_readings, key=lambda r: r.taken)[-1]
def check_readings_for_client(self, session, client):
delay = client.delay or 60
cutoff = self.now - datetime.timedelta(seconds=delay + 60)
for probe in client.enabled_probes():
self.check_readings_for_probe(session, probe, cutoff)
if (reading.degrees_f <= probe.critical_temp_min or
reading.degrees_f >= probe.critical_temp_max):
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_CRITICAL_TEMP, reading)
def check_readings_for_probe(self, session, probe, cutoff):
readings = session.query(tempmon.Reading)\
.filter(tempmon.Reading.probe == probe)\
.filter(tempmon.Reading.taken >= cutoff)\
.all()
if readings:
# we really only care about the latest reading
reading = sorted(readings, key=lambda r: r.taken)[-1]
elif reading.degrees_f < probe.good_temp_min:
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_LOW_TEMP, reading)
if (reading.degrees_f <= probe.critical_temp_min or
reading.degrees_f >= probe.critical_temp_max):
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_CRITICAL_TEMP, reading)
elif reading.degrees_f > probe.good_temp_max:
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_HIGH_TEMP, reading)
elif reading.degrees_f < probe.good_temp_min:
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_LOW_TEMP, reading)
else: # temp is good
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_GOOD_TEMP, reading)
else: # no readings for probe
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_ERROR)
elif reading.degrees_f > probe.good_temp_max:
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_HIGH_TEMP, reading)
else: # temp is good
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_GOOD_TEMP, reading)
else: # no readings for probe
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_ERROR)
def update_status(self, probe, status, reading=None):
data = {

View file

@ -65,6 +65,7 @@ requires = [
# package # low high
'rattail[db]', # 0.7.46
'six', # 1.10.0
]