Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion printer/modules/gerard.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def create_print_job(
printer_name,
file_path,
is_development_mode=False,
# no_dev_printer=False
):
hold_time = "immediate"
if is_development_mode:
Expand Down
26 changes: 11 additions & 15 deletions printer/modules/lpstat_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

from modules import sqlite_helpers

LPSTAT_CMD = "lpstat -o HP_LaserJet_p2015dn_Right"
SLEEP_TIME = 2

jobs_seen_last = set()
LPSTAT_CMD = "lpstat -o -W completed HP_LaserJet_p2015dn_Right"
POLL_LPSTAT_INTERVAL_SECONDS = 2

logging.basicConfig(
# in mondo we trust
Expand All @@ -18,8 +17,14 @@


def query_lpstat():
global jobs_seen_last
global current_jobs
"""
the output of this command looks like
ben@ben:/app# lpstat -W completed -o HP_LaserJet_p2015dn_Right
HP_LaserJet_p2015dn_Right-3 ben 5120 Mon Dec 22 21:43:54 2025
HP_LaserJet_p2015dn_Right-2 ben 8192 Mon Dec 22 21:43:23 2025
HP_LaserJet_p2015dn_Right-1 ben 8192 Mon Dec 22 21:41:08 2025
"""
p = subprocess.Popen(
LPSTAT_CMD,
shell=True,
Expand Down Expand Up @@ -52,22 +57,13 @@ def query_lpstat():


def poll_lpstat(sqlite_file):
global jobs_seen_last
while True:
try:
current_jobs = set(query_lpstat())
completed_jobs = jobs_seen_last - current_jobs

completed_jobs = set(query_lpstat())
sqlite_helpers.mark_jobs_completed(
sqlite_file, [job for job in completed_jobs]
)
sqlite_helpers.mark_jobs_acknowledged(
sqlite_file, [job for job in current_jobs]
)

jobs_seen_last.clear()
jobs_seen_last.update(current_jobs)

except Exception:
logging.exception("what happened to query_lpstat?")
time.sleep(SLEEP_TIME)
time.sleep(POLL_LPSTAT_INTERVAL_SECONDS)
2 changes: 1 addition & 1 deletion printer/modules/sqlite_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def mark_jobs_with_status(sqlite_file, jobs, status):
job_ids = [(job_id,) for job_id in jobs]
if not job_ids:
return
logging.info(f"marking {job_ids} as {status} in sqlite")
logging.debug(f"marking {job_ids} as {status} in sqlite")

sql_update = (
f"UPDATE logs SET status = '{status}' WHERE job_id = ?"
Expand Down
21 changes: 1 addition & 20 deletions printer/test/test_lpstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ class TestLpStatSqlite(unittest.TestCase):
"HP_LaserJet_p2015dn_Right-53 root 5120 Sat May 31 18:19:38 2025"
)

def setUp(self):
lpstat_helpers.jobs_seen_last.clear()

# returns a single line result
@mock.patch("modules.lpstat_helpers.subprocess.Popen")
def test_query_lpstat(self, mock_popen):
Expand Down Expand Up @@ -77,9 +74,6 @@ def test_query_lpstat_nonzero(self, mock_popen):
def test_poll_lpstat(
self, mock_sleep, mock_query_lpstat, mock_mark_acknowledged, mock_mark_completed
):
# Set initial seen job
lpstat_helpers.jobs_seen_last = {"HP_LaserJet_p2015dn_Right-52"}

# Simulate current jobs reported by lpstat
mock_query_lpstat.return_value = [
"HP_LaserJet_p2015dn_Right-53",
Expand All @@ -94,20 +88,7 @@ def test_poll_lpstat(

# Check correct jobs marked as completed and acknowledged
mock_mark_completed.assert_called_once_with(
"dummy.db", ["HP_LaserJet_p2015dn_Right-52"]
)
mock_mark_acknowledged.assert_called_once()
database_name, acknowledged_jobs = mock_mark_acknowledged.call_args_list[0].args
self.assertEqual(database_name, "dummy.db")
self.assertCountEqual(
acknowledged_jobs,
["HP_LaserJet_p2015dn_Right-53", "HP_LaserJet_p2015dn_Right-54"],
)

# Ensure state was cleared and updated
self.assertEqual(
lpstat_helpers.jobs_seen_last,
{"HP_LaserJet_p2015dn_Right-53", "HP_LaserJet_p2015dn_Right-54"},
"dummy.db", ["HP_LaserJet_p2015dn_Right-54", "HP_LaserJet_p2015dn_Right-53"],
)


Expand Down