1
0
Fork 0

Fix healthcheck in python3

Subprocess returns stdout as bytes instead of str nowadays, so I
addressed it in a quick and dirty way.

The extra return in healthcheck function is added there to stop the app
from complaining about it and also because I discovered that the
solution for tracking printer status here is essentially hardcoded for
English, while my local test environment outputs lpstat logs
in Ukrainian.
python3
vibe 2023-11-12 21:44:59 +01:00
parent db76e8a64f
commit 92a1286055
1 changed files with 11 additions and 11 deletions

View File

@ -7,6 +7,7 @@ import time
import cairocffi as cairo
import flask
from flask import json
import pangocffi as pango
import pangocairocffi as pangocairo
@ -59,8 +60,7 @@ class Renderer(object):
layout.apply_markup('<span font_desc="%s">%s</span>' % (fontname, text))
else:
font = pango.FontDescription()
font.family = "Sans"
font.size = 65
font.family = fontname
layout.font_description = font
layout.text = text
@ -81,10 +81,10 @@ def healthcheck():
last_checked, last_status, last_details = app.health
if time.time() - last_checked < 1:
return last_status, last_details
output = subprocess.check_output(['lpstat', '-p', '-d'])
output = subprocess.run(['lpstat', '-p', '-d'], capture_output=True).stdout
mark = False
for line in output.split('\n'):
line = line.strip()
for line in output.split(b'\n'):
line = line.strip().decode('utf-8')
if line.startswith('printer DYMO_LabelWriter_450'):
if 'is idle.' in line:
return True, 'Idle'
@ -98,13 +98,13 @@ def healthcheck():
app.health = (time.time(), False, line)
return False, line
mark = False
return False, 'Printer is down or there\'s something wrong with lpstat output.'
# TODO: Uncomment and fix this
# @app.route('/health')
# def health():
# ok, details = healthcheck()
# return json.dumps({'ok': ok, 'details': details})
@app.route('/health')
def health():
ok, details = healthcheck()
return json.dumps({'ok': ok, 'details': details})
@app.route('/stuff/preview/<int:size>/')
def stuff_preview(size):
@ -124,7 +124,7 @@ def stuff_print(size):
if not healthcheck()[0]:
return 'Printer is down.'
last = app.last
print(last, time.time() - last)
print((last, time.time() - last))
if time.time() - last < DELAY:
return 'Please wait {} more seconds before next print.'.format(int(DELAY - (time.time() - last)))
text = flask.request.args.get('text')