Skip to content

Commit 7143a22

Browse files
printing status (#140)
* virtual printer stuff * add dev printer flag; add hold to virtual prints * LF warning * get status from db * just return completed if not using virtual printer and in dev mode * i lied * fix unit test * fix unit tests 2 * error checking for status; some other misc changes * fix bad datetime module access * remove dev printer flag * remove dev printer flag again * fix tests * is_development_mode --------- Co-authored-by: evan <evanuxd@gmail.com>
1 parent 40dfe3c commit 7143a22

File tree

7 files changed

+93
-5
lines changed

7 files changed

+93
-5
lines changed

docker-compose.printer.dev.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: '2'
2+
services:
3+
sce-printer:
4+
container_name: sce-printer-dev
5+
build:
6+
context: .
7+
dockerfile: ./printer/Dockerfile.printer.dev
8+
ports:
9+
# we use port 14000 as that is what the website expects
10+
# the printing server to be running on
11+
- 14000:14000
12+
volumes:
13+
- ./config:/app/config
14+
- ./printer:/app/printer
15+
- ./tmp:/tmp
16+
command:
17+
- --development
18+
- --port=14000
19+
- --dont-delete-pdfs
20+
- --config-json-path=/app/config/config.json

printer/Dockerfile.printer.dev

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Base image from https://github.com/DrPsychick/docker-cups-airprint
2+
# Docker images are here https://hub.docker.com/r/drpsychick/airprint-bridge/tags
3+
FROM drpsychick/airprint-bridge:jammy
4+
5+
WORKDIR /app
6+
7+
RUN apt-get update
8+
9+
RUN apt install -y python3 python3-pip python3-venv
10+
11+
# Create the virtual environment with Python
12+
RUN python3 -m venv /opt/venv
13+
14+
# Set the virtual environment as the default Python environment
15+
ENV PATH="/opt/venv/bin:$PATH"
16+
17+
COPY ./printer/requirements.txt /app/printer/requirements.txt
18+
19+
RUN /opt/venv/bin/pip install -r /app/printer/requirements.txt
20+
21+
EXPOSE 9000
22+
23+
ENTRYPOINT ["./printer/what_dev.sh"]

printer/modules/gerard.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import logging
66
import shlex
77
import subprocess
8-
8+
import datetime
99

1010
LP_COMMAND = """
1111
lp \
12+
-H {hold_time} \
1213
-n {num_copies} {maybe_page_range} \
1314
-o sides={sides} \
1415
-o media=na_letter_8.5x11in \
@@ -36,16 +37,23 @@ def __next__(self):
3637

3738
print_job_suffix = IDIterator()
3839

39-
4040
def create_print_job(
4141
num_copies,
4242
maybe_page_range,
4343
sides,
4444
printer_name,
4545
file_path,
4646
is_development_mode=False,
47+
# no_dev_printer=False
4748
):
49+
hold_time = "immediate"
50+
if is_development_mode:
51+
future_datetime = datetime.datetime.fromtimestamp(datetime.datetime.utcnow().timestamp() + 5)
52+
hold_time = f"{future_datetime.hour}:{future_datetime.minute}:{future_datetime.second}"
53+
54+
4855
command = LP_COMMAND.format(
56+
hold_time=hold_time,
4957
num_copies=num_copies,
5058
maybe_page_range=maybe_page_range,
5159
sides=sides,
@@ -59,6 +67,7 @@ def create_print_job(
5967
)
6068
job_id = f"HP_LaserJet_p2015dn_Right-{next(print_job_suffix)}"
6169
return job_id
70+
6271

6372
args_list = shlex.split(command.strip())
6473
logging.info(f"running command {command}")

printer/server.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import threading
77
import time
88
import uuid
9+
import sqlite3
910

1011
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
1112
from fastapi.middleware.cors import CORSMiddleware
@@ -63,6 +64,7 @@ def get_args() -> argparse.Namespace:
6364
default=False,
6465
help="specify if server should run in development. this means requests won't get sent to a printer but logger instead",
6566
)
67+
6668
parser.add_argument(
6769
"--dont-delete-pdfs",
6870
action="store_true",
@@ -119,6 +121,10 @@ def send_file_to_printer(
119121

120122
# only the right printer works right now, so we default to it
121123
PRINTER_NAME = os.environ.get("RIGHT_PRINTER_NAME")
124+
125+
if (args.development):
126+
PRINTER_NAME = "HP_LaserJet_p2015dn_Right"
127+
122128
metrics_handler.print_jobs_recieved.inc()
123129

124130
job_id = gerard.create_print_job(
@@ -148,6 +154,17 @@ def api():
148154
def metrics():
149155
return prometheus_client.generate_latest()
150156

157+
@app.get("/status/")
158+
async def status(id: str = ''):
159+
try:
160+
db = sqlite3.connect(args.database_file_path)
161+
cursor = db.cursor()
162+
cursor.execute(f"SELECT status FROM logs WHERE job_id = ?", (id,))
163+
status = cursor.fetchone()[0]
164+
return {"status": status}
165+
except Exception:
166+
logging.exception("failed to get status of job with id: " + id)
167+
return {"status": "failed"}
151168

152169
@app.post("/print")
153170
async def read_item(

printer/test/test_gerard.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def test_create_print_job(self, mock_popen):
4141
mock_popen.call_args_list[0],
4242
mock.call(
4343
shlex.split(gerard.LP_COMMAND.format(
44+
hold_time="immediate",
4445
num_copies=1,
4546
maybe_page_range="1",
4647
sides="one-side",
@@ -76,6 +77,7 @@ def test_create_print_job_nonzero_returncode(self, mock_popen):
7677
mock_popen.call_args_list[0],
7778
mock.call(
7879
shlex.split(gerard.LP_COMMAND.format(
80+
hold_time="immediate",
7981
num_copies=1,
8082
maybe_page_range="",
8183
sides="dark-side",
@@ -113,6 +115,7 @@ def test_create_print_job_cant_parse_stdout(self, mock_popen):
113115
mock_popen.call_args_list[0],
114116
mock.call(
115117
shlex.split(gerard.LP_COMMAND.format(
118+
hold_time="immediate",
116119
num_copies=1,
117120
maybe_page_range="1",
118121
sides="one-side",
@@ -129,19 +132,19 @@ def test_create_print_job_cant_parse_development_mode(self):
129132

130133
self.assertEqual(
131134
gerard.create_print_job(
132-
1, "1", "one-side", "HP_P2015_DN", "/tmp/test-id", True
135+
1, "1", "one-side", "HP_P2015_DN", "/tmp/test-id", True,
133136
),
134137
"HP_LaserJet_p2015dn_Right-0",
135138
)
136139
self.assertEqual(
137140
gerard.create_print_job(
138-
1, "1", "one-side", "HP_P2015_DN", "/tmp/test-id", True
141+
1, "1", "one-side", "HP_P2015_DN", "/tmp/test-id", True,
139142
),
140143
"HP_LaserJet_p2015dn_Right-1",
141144
)
142145
self.assertEqual(
143146
gerard.create_print_job(
144-
1, "1", "one-side", "HP_P2015_DN", "/tmp/test-id", True
147+
1, "1", "one-side", "HP_P2015_DN", "/tmp/test-id", True,
145148
),
146149
"HP_LaserJet_p2015dn_Right-2",
147150
)

printer/test/test_server.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def test_print_endpoint(self, mock_pathlib_unlink, mock_open_func, mock_popen, _
7878
mock_popen.call_args_list[0],
7979
mock.call(
8080
shlex.split(gerard.LP_COMMAND.format(
81+
hold_time="immediate",
8182
num_copies=1,
8283
maybe_page_range="",
8384
sides="one-sided",
@@ -137,6 +138,7 @@ def test_print_endpoint_dont_delete_pdf(
137138
mock_popen.call_args_list[0],
138139
mock.call(
139140
shlex.split(gerard.LP_COMMAND.format(
141+
hold_time="immediate",
140142
num_copies=1,
141143
maybe_page_range="",
142144
sides="one-sided",
@@ -221,6 +223,7 @@ def test_print_endpoint_nonzero_returncode(
221223
mock_popen.call_args_list[0],
222224
mock.call(
223225
shlex.split(gerard.LP_COMMAND.format(
226+
hold_time="immediate",
224227
num_copies=1,
225228
maybe_page_range="",
226229
sides="dark-side",
@@ -275,6 +278,7 @@ def test_junk_print_id(self, mock_pathlib_unlink, mock_open_func, mock_popen, _)
275278
mock_popen.call_args_list[0],
276279
mock.call(
277280
shlex.split(gerard.LP_COMMAND.format(
281+
hold_time="immediate",
278282
num_copies=1,
279283
maybe_page_range="",
280284
sides="one-sided",

printer/what_dev.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
# MAKE SURE THIS FILE IS USING LF FOR ITS EOL SEQUENCE!!!!!!!!!!!
4+
# OTHERWISE DOCKER WILL COMPLAIN THAT THIS FILE DOESNT EXIST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5+
6+
/root/start-cups.sh > /dev/null 2>&1 &
7+
8+
sleep 10
9+
10+
lpadmin -p HP_LaserJet_p2015dn_Right -E -v file:///dev/null
11+
12+
python3 /app/printer/server.py $@

0 commit comments

Comments
 (0)