Refactor main server loop a bit, to add basic retry w/ error logging
hopefully this lets us get past a simple Postgres restart..
This commit is contained in:
		
							parent
							
								
									27adc5ed70
								
							
						
					
					
						commit
						c4b371cedd
					
				
					 2 changed files with 20 additions and 17 deletions
				
			
		| 
						 | 
				
			
			@ -61,22 +61,28 @@ class TempmonServerDaemon(Daemon):
 | 
			
		|||
        self.now = make_utc()
 | 
			
		||||
        session = TempmonSession()
 | 
			
		||||
 | 
			
		||||
        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()
 | 
			
		||||
 | 
			
		||||
        # TODO: not sure this is really necessary?
 | 
			
		||||
        self.set_last_checked()
 | 
			
		||||
        try:
 | 
			
		||||
            clients = session.query(tempmon.Client)\
 | 
			
		||||
                             .filter(tempmon.Client.enabled == True)
 | 
			
		||||
            for client in clients:
 | 
			
		||||
                self.check_readings_for_client(session, client)
 | 
			
		||||
        except:
 | 
			
		||||
            log.exception("Failed to check client probe readings (but will keep trying)")
 | 
			
		||||
            session.rollback()
 | 
			
		||||
        else:
 | 
			
		||||
            session.commit()
 | 
			
		||||
        finally:
 | 
			
		||||
            session.close()
 | 
			
		||||
 | 
			
		||||
    def check_readings_for_client(self, session, client):
 | 
			
		||||
        delay = client.delay or 60
 | 
			
		||||
        cutoff = self.now - datetime.timedelta(seconds=delay + 60)
 | 
			
		||||
        online = False
 | 
			
		||||
        for probe in client.enabled_probes():
 | 
			
		||||
            self.check_readings_for_probe(session, probe, cutoff)
 | 
			
		||||
            online = bool(self.check_readings_for_probe(session, probe, cutoff))
 | 
			
		||||
        if not online and client.online:
 | 
			
		||||
            log.info("marking client as OFFLINE: {}".format(client))
 | 
			
		||||
            client.online = False
 | 
			
		||||
 | 
			
		||||
    def check_readings_for_probe(self, session, probe, cutoff):
 | 
			
		||||
        readings = session.query(tempmon.Reading)\
 | 
			
		||||
| 
						 | 
				
			
			@ -103,6 +109,8 @@ class TempmonServerDaemon(Daemon):
 | 
			
		|||
        else: # no readings for probe
 | 
			
		||||
            self.update_status(probe, self.enum.TEMPMON_PROBE_STATUS_ERROR)
 | 
			
		||||
 | 
			
		||||
        return readings
 | 
			
		||||
 | 
			
		||||
    def update_status(self, probe, status, reading=None):
 | 
			
		||||
        data = {
 | 
			
		||||
            'probe': probe,
 | 
			
		||||
| 
						 | 
				
			
			@ -154,12 +162,6 @@ class TempmonServerDaemon(Daemon):
 | 
			
		|||
 | 
			
		||||
        probe.status_alert_sent = self.now
 | 
			
		||||
 | 
			
		||||
    def set_last_checked(self):
 | 
			
		||||
        session = Session()
 | 
			
		||||
        api.save_setting(session, 'tempmon.server.last_checked', self.now.strftime(self.timefmt))
 | 
			
		||||
        session.commit()
 | 
			
		||||
        session.close()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def make_daemon(config, pidfile=None):
 | 
			
		||||
    """
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue