Skip to content

Commit d85817f

Browse files
Fix incredibly dumb debugging commit.
Replace `print` with actual logging.
1 parent 7e594a8 commit d85817f

1 file changed

Lines changed: 63 additions & 47 deletions

File tree

pwmon.py

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import time
1111
import enum
12+
import logging
1213
from datetime import datetime as dt
1314
from pprint import pprint as pp
1415

@@ -298,57 +299,72 @@ def make_gauge(name: str, value: int | float, m_type: str = 'gauge') -> dict:
298299
}
299300

300301

301-
def run_from_cli():
302+
def run_from_cli(data):
302303
"""Print data and exit. Useful when running the script from the CLI."""
303304
pp(data, compact=True)
304305
timestamp = data['common']['timestamp']
305-
print(f'timestamp:\t{timestamp:_}')
306+
logger.info('timestamp:\t%s', timestamp)
306307
sys.exit(0)
307308

309+
logging.basicConfig(format='%(asctime)s %(name)s.%(funcName)s %(levelname)s: %(message)s',
310+
datefmt='[%Y-%m-%d %H:%M:%S]', level=logging.INFO)
311+
logger: logging.Logger = logging.getLogger('pwmon')
308312

309313
if __name__ == "__main__":
310-
# If POLL_INTERVAL is a multiple of a minute, try to start at the beginning of the next minute
311-
if POLL_INTERVAL % 60 == 0 and AS_SERVICE:
312-
wait_time = 60 - time.localtime().tm_sec
313-
print('Found minute intervals, delaying first iteration',
314-
wait_time, 'seconds until the start of the next minute')
315-
print()
316-
time.sleep(wait_time)
317-
318-
while True:
319-
start = time.time()
320-
try:
321-
data = get_data()
322-
#ret = post_metrics(data)
323-
324-
print('Submitted at', dt.now())
325-
except APIError as apiEx:
326-
print(apiEx)
327-
# If this is an HTTP 429, back off immediately for at least 5 minutes
328-
if str(apiEx).find('429: Too Many Requests') > 0:
329-
FIVE_MINUTES = 5 * 60
330-
elapsed = time.time() - start
331-
# Back off for at least 3x POLL_INTERVAL, for a minimum of 5 minutes to allow things to cool down
332-
backoffInterval = POLL_INTERVAL * 3
333-
if backoffInterval < FIVE_MINUTES:
334-
backoffInterval = FIVE_MINUTES
335-
print('Backing off for', round(backoffInterval - elapsed, 0), 'seconds because of HTTP 429.')
336-
time.sleep(backoffInterval - elapsed)
337-
# Determine if we need to wait until the start of the minute again
338-
if POLL_INTERVAL % 60 == 0 and AS_SERVICE:
339-
wait_time = 60 - time.localtime().tm_sec
340-
time.sleep(wait_time)
341-
# Reset the start time to coincide with the top of the minute
342-
start = time.time()
343-
except Exception as ex:
344-
print('Failed to gather data:', ex)
345-
346-
if not AS_SERVICE:
347-
run_from_cli()
348-
349-
# Try to position each loop exactly POLL_INTERVAL seconds apart.
350-
# This is most useful when POLL_INTERVAL is an even division of a minute
351-
elapsed = time.time() - start
352-
if elapsed < 0 or elapsed > POLL_INTERVAL:
353-
elapsed = 0
354-
time.sleep(POLL_INTERVAL - elapsed)
314+
logger.info('Startup')
315+
try:
316+
# If POLL_INTERVAL is a multiple of a minute, try to start at the beginning of the next minute
317+
if POLL_INTERVAL % 60 == 0 and AS_SERVICE:
318+
wait_time = 60 - time.localtime().tm_sec
319+
logger.info('Found minute intervals, delaying first iteration %s seconds until the start of the next minute', wait_time)
320+
time.sleep(wait_time)
321+
322+
while True:
323+
start = time.time()
324+
try:
325+
data = get_data()
326+
ret = post_metrics(data)
327+
328+
logger.info('Submitted at %s', dt.now())
329+
except APIError as apiEx:
330+
logger.warning(apiEx)
331+
# If this is an HTTP 429, back off immediately for at least 5 minutes
332+
if str(apiEx).find('429: Too Many Requests') > 0:
333+
FIVE_MINUTES = 5 * 60
334+
elapsed = time.time() - start
335+
# Back off for at least 3x POLL_INTERVAL, for a minimum of 5 minutes to allow things to cool down
336+
backoffInterval = POLL_INTERVAL * 3
337+
if backoffInterval < FIVE_MINUTES:
338+
backoffInterval = FIVE_MINUTES
339+
logger.info('Backing off for %s seconds because of HTTP 429.', round(backoffInterval - elapsed, 0))
340+
time.sleep(backoffInterval - elapsed)
341+
# Determine if we need to wait until the start of the minute again
342+
if POLL_INTERVAL % 60 == 0 and AS_SERVICE:
343+
wait_time = 60 - time.localtime().tm_sec
344+
time.sleep(wait_time)
345+
# Reset the start time to coincide with the top of the minute
346+
start = time.time()
347+
except (SystemExit, KeyboardInterrupt) as ex:
348+
logger.info('%s received; shutting down...',
349+
ex.__class__.__name__)
350+
break
351+
except Exception as ex:
352+
logger.warning('Failed to gather data: %s', ex)
353+
logger.exception(ex)
354+
355+
if not AS_SERVICE:
356+
run_from_cli(data)
357+
358+
# Try to position each loop exactly POLL_INTERVAL seconds apart.
359+
# This is most useful when POLL_INTERVAL is an even division of a minute
360+
elapsed = time.time() - start
361+
if elapsed < 0 or elapsed > POLL_INTERVAL:
362+
elapsed = 0
363+
time.sleep(POLL_INTERVAL - elapsed)
364+
except (SystemExit, KeyboardInterrupt) as ex:
365+
logger.info('%s received; shutting down...',
366+
ex.__class__.__name__)
367+
except Exception as ex:
368+
logger.warning('Exception during main loop: %s', ex)
369+
logger.exception(ex)
370+
logger.info('Shutdown')

0 commit comments

Comments
 (0)