def _exec_usage(self): with db_session() as session: # Next check if it's time to send bandwidth usage notifications delta = datetime.timedelta(minutes=self.usage_freq) exp = timeutils.utcnow() - delta start, = session.query( Billing.last_update ).filter(Billing.name == "usage").\ first() if start and start > exp: # Not time yet LOG.info('Not time to send usage statistics yet {0}'. format(exp)) session.rollback() return # Check the connection before sending the notifications if not test_mnb_connection(): # Abort the exists notifications LOG.info("Aborting usage notifications. Could not connect") session.rollback() return # Calculate the stopping point by rounding backward to the nearest # N minutes. i.e. if N = 60, this will round us back to HH:00:00, # or if N = 15, it will round us back to HH:15:00, HH:30:00, # HH:45:00, or HH:00:00, whichever is closest. N = cfg.CONF['admin_api'].usage_freq now = timeutils.utcnow() stop = now - datetime.timedelta(minutes=now.minute % N, seconds=now.second, microseconds=now.microsecond) # Release the lock session.query(Billing).\ filter(Billing.name == "usage").\ update({"last_update": stop}, synchronize_session='fetch') session.commit() # Send the usage notifications. Pass the timestamps to save # queries. update_mnb('lbaas.bandwidth.usage', start, stop)
def _exec_exists(self): with db_session() as session: # Check if it's time to send exists notifications delta = datetime.timedelta(minutes=self.exists_freq) exp = timeutils.utcnow() - delta exp_time = exp.strftime('%Y-%m-%d %H:%M:%S') updated = session.query( Billing.last_update ).filter(Billing.name == "exists").\ filter(Billing.last_update > exp_time).\ first() if updated is not None: # Not time yet LOG.info('Not time to send exists notifications yet {0}'. format(exp_time)) session.rollback() return # Check the connection before sending the notifications if not test_mnb_connection(): # Abort the exists notifications LOG.info("Aborting exists notifications. Could not connect") session.rollback() return #Update the exists timestamp now session.query(Billing).\ filter(Billing.name == "exists").\ update({"last_update": func.now()}, synchronize_session='fetch') session.commit() # Send the notifications update_mnb('lbaas.instance.exists', None, None)