Add configurable delay per client; improve try/catch
This commit is contained in:
parent
3f9adfa6c5
commit
4e11748b45
|
@ -66,37 +66,35 @@ class TempmonClient(Daemon):
|
||||||
session.close()
|
session.close()
|
||||||
raise ConfigurationError("No tempmon client configured for hostname: {}".format(hostname))
|
raise ConfigurationError("No tempmon client configured for hostname: {}".format(hostname))
|
||||||
client_uuid = client.uuid
|
client_uuid = client.uuid
|
||||||
|
self.delay = client.delay or 60
|
||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
# main loop: take readings, pause, repeat
|
# main loop: take readings, pause, repeat
|
||||||
while True:
|
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():
|
for probe in client.enabled_probes():
|
||||||
self.take_reading(session, probe)
|
self.take_reading(session, probe)
|
||||||
|
|
||||||
except:
|
except:
|
||||||
log.exception("Failed to read/record temperature data")
|
log.exception("Failed to read/record temperature data (but will keep trying)")
|
||||||
session.rollback()
|
session.rollback()
|
||||||
raise
|
|
||||||
|
|
||||||
else:
|
|
||||||
# make sure we show as being online
|
|
||||||
if not client.online:
|
|
||||||
client.online = True
|
|
||||||
session.commit()
|
|
||||||
|
|
||||||
finally:
|
|
||||||
session.close()
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
# make sure we show as being online
|
||||||
|
if not client.online:
|
||||||
|
client.online = True
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
finally:
|
||||||
session.close()
|
session.close()
|
||||||
|
|
||||||
# TODO: make this configurable
|
time.sleep(self.delay)
|
||||||
time.sleep(60)
|
|
||||||
|
|
||||||
def take_reading(self, session, probe):
|
def take_reading(self, session, probe):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -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')
|
|
@ -2,7 +2,7 @@
|
||||||
################################################################################
|
################################################################################
|
||||||
#
|
#
|
||||||
# Rattail -- Retail Software Framework
|
# Rattail -- Retail Software Framework
|
||||||
# Copyright © 2010-2016 Lance Edgar
|
# Copyright © 2010-2017 Lance Edgar
|
||||||
#
|
#
|
||||||
# This file is part of Rattail.
|
# This file is part of Rattail.
|
||||||
#
|
#
|
||||||
|
@ -28,6 +28,7 @@ from __future__ import unicode_literals, absolute_import
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
import six
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
from sqlalchemy import orm
|
from sqlalchemy import orm
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
@ -39,6 +40,7 @@ from rattail.db.model.core import ModelBase
|
||||||
Base = declarative_base(cls=ModelBase)
|
Base = declarative_base(cls=ModelBase)
|
||||||
|
|
||||||
|
|
||||||
|
@six.python_2_unicode_compatible
|
||||||
class Client(Base):
|
class Client(Base):
|
||||||
"""
|
"""
|
||||||
Represents a tempmon client.
|
Represents a tempmon client.
|
||||||
|
@ -52,16 +54,23 @@ class Client(Base):
|
||||||
config_key = sa.Column(sa.String(length=50), nullable=False)
|
config_key = sa.Column(sa.String(length=50), nullable=False)
|
||||||
hostname = sa.Column(sa.String(length=255), nullable=False)
|
hostname = sa.Column(sa.String(length=255), nullable=False)
|
||||||
location = sa.Column(sa.String(length=255), nullable=True)
|
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)
|
enabled = sa.Column(sa.Boolean(), nullable=False, default=False)
|
||||||
online = 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)
|
return '{} ({})'.format(self.config_key, self.hostname)
|
||||||
|
|
||||||
def enabled_probes(self):
|
def enabled_probes(self):
|
||||||
return [probe for probe in self.probes if probe.enabled]
|
return [probe for probe in self.probes if probe.enabled]
|
||||||
|
|
||||||
|
|
||||||
|
@six.python_2_unicode_compatible
|
||||||
class Probe(Base):
|
class Probe(Base):
|
||||||
"""
|
"""
|
||||||
Represents a probe connected to a tempmon client.
|
Represents a probe connected to a tempmon client.
|
||||||
|
@ -103,10 +112,11 @@ class Probe(Base):
|
||||||
status_changed = sa.Column(sa.DateTime(), nullable=True)
|
status_changed = sa.Column(sa.DateTime(), nullable=True)
|
||||||
status_alert_sent = sa.Column(sa.DateTime(), nullable=True)
|
status_alert_sent = sa.Column(sa.DateTime(), nullable=True)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __str__(self):
|
||||||
return unicode(self.description or '')
|
return self.description
|
||||||
|
|
||||||
|
|
||||||
|
@six.python_2_unicode_compatible
|
||||||
class Reading(Base):
|
class Reading(Base):
|
||||||
"""
|
"""
|
||||||
Represents a single tempurate reading from a tempmon probe.
|
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)
|
taken = sa.Column(sa.DateTime(), nullable=False, default=datetime.datetime.utcnow)
|
||||||
degrees_f = sa.Column(sa.Numeric(precision=7, scale=4), nullable=False)
|
degrees_f = sa.Column(sa.Numeric(precision=7, scale=4), nullable=False)
|
||||||
|
|
||||||
def __unicode__(self):
|
def __str__(self):
|
||||||
return unicode(self.degrees_f)
|
return str(self.degrees_f)
|
||||||
|
|
|
@ -60,23 +60,11 @@ class TempmonServerDaemon(Daemon):
|
||||||
def check_readings(self):
|
def check_readings(self):
|
||||||
self.now = make_utc()
|
self.now = make_utc()
|
||||||
session = TempmonSession()
|
session = TempmonSession()
|
||||||
probes = session.query(tempmon.Probe)\
|
|
||||||
.join(tempmon.Client)\
|
|
||||||
.filter(tempmon.Client.enabled == True)\
|
|
||||||
.filter(tempmon.Probe.enabled == True)\
|
|
||||||
.all()
|
|
||||||
|
|
||||||
if probes:
|
clients = session.query(tempmon.Client)\
|
||||||
cutoff = self.now - datetime.timedelta(seconds=120)
|
.filter(tempmon.Client.enabled == True)
|
||||||
uuids = [probe.uuid for probe in probes]
|
for client in clients:
|
||||||
readings = session.query(tempmon.Reading)\
|
self.check_readings_for_client(session, client)
|
||||||
.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!")
|
|
||||||
|
|
||||||
session.commit()
|
session.commit()
|
||||||
session.close()
|
session.close()
|
||||||
|
@ -84,27 +72,36 @@ class TempmonServerDaemon(Daemon):
|
||||||
# TODO: not sure this is really necessary?
|
# TODO: not sure this is really necessary?
|
||||||
self.set_last_checked()
|
self.set_last_checked()
|
||||||
|
|
||||||
def process_readings(self, probes, readings):
|
def check_readings_for_client(self, session, client):
|
||||||
for probe in probes:
|
delay = client.delay or 60
|
||||||
probe_readings = [r for r in readings if r.probe is probe]
|
cutoff = self.now - datetime.timedelta(seconds=delay + 60)
|
||||||
if probe_readings:
|
for probe in client.enabled_probes():
|
||||||
reading = sorted(probe_readings, key=lambda r: r.taken)[-1]
|
self.check_readings_for_probe(session, probe, cutoff)
|
||||||
|
|
||||||
if (reading.degrees_f <= probe.critical_temp_min or
|
def check_readings_for_probe(self, session, probe, cutoff):
|
||||||
reading.degrees_f >= probe.critical_temp_max):
|
readings = session.query(tempmon.Reading)\
|
||||||
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_CRITICAL_TEMP, 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:
|
if (reading.degrees_f <= probe.critical_temp_min or
|
||||||
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_LOW_TEMP, reading)
|
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:
|
elif reading.degrees_f < probe.good_temp_min:
|
||||||
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_HIGH_TEMP, reading)
|
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_LOW_TEMP, reading)
|
||||||
|
|
||||||
else: # temp is good
|
elif reading.degrees_f > probe.good_temp_max:
|
||||||
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_GOOD_TEMP, reading)
|
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_HIGH_TEMP, reading)
|
||||||
|
|
||||||
else: # no readings for probe
|
else: # temp is good
|
||||||
self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_ERROR)
|
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):
|
def update_status(self, probe, status, reading=None):
|
||||||
data = {
|
data = {
|
||||||
|
|
Loading…
Reference in a new issue