fix: capacity check ignores physically-ended slots; feat: ha_poll_interval setting (default 60s)
Deploy via Portainer / deploy (push) Successful in 0s

This commit is contained in:
Jo
2026-07-07 12:46:37 +02:00
parent 221a0d7b8c
commit b13d718d92
7 changed files with 227 additions and 20 deletions
+17 -4
View File
@@ -226,6 +226,7 @@ def init_db():
('ical_token',''),
('ha_url',''),
('ha_token',''),
('ha_poll_interval','60'),
]
conn.executemany('INSERT OR IGNORE INTO settings VALUES (?,?)', defaults)
@@ -1303,8 +1304,11 @@ def job_detail(id):
return redirect(url_for('jobs'))
import math as _math
total_plates_needed = _math.ceil((job['order_qty'] or 1) / max(1, job['pieces_per_plate'] or 1))
_s = get_settings()
ha_poll_interval = int(_s.get('ha_poll_interval', 60))
return render_template('job_detail.html', job=job, slots=slots,
total_plates_needed=total_plates_needed)
total_plates_needed=total_plates_needed,
ha_poll_interval=ha_poll_interval)
@app.route('/jobs/<int:id>/delete', methods=['POST'])
def delete_job(id):
@@ -1716,7 +1720,8 @@ def _check_capacity(conn, start_dt, end_dt, max_printers, exclude_id=None):
"""
q = """SELECT planned_start, planned_end FROM print_slots
WHERE status NOT IN ('cancelled','failed','done')
AND planned_start < ? AND planned_end > ?"""
AND planned_start < ? AND planned_end > ?
AND planned_end > datetime('now')"""
params = [end_dt.isoformat(), start_dt.isoformat()]
if exclude_id:
q += ' AND id != ?'
@@ -1752,7 +1757,8 @@ def _check_overlap(conn, printer_id, start_dt, end_dt, exclude_id=None):
FROM print_slots ps JOIN jobs j ON ps.job_id=j.id
WHERE ps.printer_id=?
AND ps.status NOT IN ('cancelled','failed','done')
AND ps.planned_start < ? AND ps.planned_end > ?"""
AND ps.planned_start < ? AND ps.planned_end > ?
AND ps.planned_end > datetime('now')"""
params = [printer_id, end_dt.isoformat(), start_dt.isoformat()]
if exclude_id:
q += ' AND ps.id != ?'
@@ -2063,7 +2069,14 @@ def _start_ha_poll():
time.sleep(10)
while True:
_ha_sync_once()
time.sleep(120)
try:
_c = sqlite3.connect(DATABASE, timeout=5)
_c.row_factory = sqlite3.Row
_interval = int(_c.execute("SELECT value FROM settings WHERE key='ha_poll_interval'").fetchone()['value'])
_c.close()
except Exception:
_interval = 60
time.sleep(max(10, _interval))
t = threading.Thread(target=_loop, daemon=True, name='ha-poll')
t.start()