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