Skip to content

Commit 35a9cf6

Browse files
committed
check printer idle
1 parent 825775c commit 35a9cf6

5 files changed

Lines changed: 69 additions & 78 deletions

File tree

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
"devDependencies": {
1818
"@expo-google-fonts/noto-sans-sc": "^0.4.2",
1919
"@hydrooj/eslint-config": "^2.0.3",
20-
"@hydrooj/register": "^1.0.3",
21-
"@hydrooj/utils": "^1.4.37",
20+
"@hydrooj/register": "^1.0.4",
21+
"@hydrooj/utils": "^1.5.1",
2222
"@neutralinojs/neu": "^11.6.0",
23-
"@types/lodash": "^4.17.20",
24-
"@types/node": "^20.19.24",
23+
"@types/lodash": "^4.17.21",
24+
"@types/node": "^20.19.25",
2525
"@yao-pkg/pkg": "^6.10.1",
2626
"chardet": "^2.1.1",
2727
"dejavu-fonts-ttf": "^2.37.3",
@@ -34,6 +34,7 @@
3434
},
3535
"resolutions": {
3636
"formidable": "patch:formidable@npm%3A2.1.2#~/.yarn/patches/formidable-npm-2.1.2-40ba18d67f.patch",
37-
"http-proxy@npm:^1.18.1": "patch:http-proxy@npm%3A1.18.1#~/.yarn/patches/http-proxy-npm-1.18.1-a313c479c5.patch"
37+
"http-proxy@npm:^1.18.1": "patch:http-proxy@npm%3A1.18.1#~/.yarn/patches/http-proxy-npm-1.18.1-a313c479c5.patch",
38+
"koa@npm:^3.0.3": "patch:koa@npm%3A3.0.3#~/.yarn/patches/koa-npm-3.0.3-69f0f9bdfc.patch"
3839
}
3940
}

packages/machine-setup/frontend/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
"vue": "^3.5.24"
1414
},
1515
"devDependencies": {
16-
"@vitejs/plugin-vue": "^6.0.1",
17-
"naive-ui": "^2.43.1",
16+
"@vitejs/plugin-vue": "^6.0.2",
17+
"naive-ui": "^2.43.2",
1818
"typescript": "5.8.3",
19-
"vite": "^7.2.2",
19+
"vite": "^7.2.4",
2020
"vue-tsc": "^2.2.12"
2121
}
2222
}

packages/server/client/printer.ts

Lines changed: 46 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -50,68 +50,58 @@ export async function ConvertCodeToPDF(code: Buffer, lang, filename, team, locat
5050
const typst = generateTypst(team, location, fakeFilename, filename, lang, codeColor);
5151
compiler.addSource('/main.typst', typst);
5252
compiler.addSource(`/${fakeFilename}`, toUtf8(code));
53-
const docs = await compiler.compile({
54-
format: 'pdf',
55-
mainFilePath: '/main.typst',
56-
});
57-
compiler.addSource(`/${fakeFilename}`, '');
5853
logger.info(`Convert ${filename} to PDF`);
59-
return docs;
54+
try {
55+
return await compiler.compile({
56+
format: 'pdf',
57+
mainFilePath: '/main.typst',
58+
});
59+
} catch (e) {
60+
logger.error(e);
61+
compiler = await createTypstCompiler();
62+
throw e;
63+
} finally {
64+
compiler.addSource(`/${fakeFilename}`, '');
65+
}
6066
}
6167

6268
export async function printFile(docs) {
63-
try {
64-
let finalFile = null;
65-
const files = [];
66-
for (const doc of docs) {
67-
const {
68-
_id, tid, code, lang, filename, team, location,
69-
} = doc;
70-
const pdf = await ConvertCodeToPDF(
71-
code ? Buffer.from(code, 'base64') : Buffer.from('empty file'),
72-
lang,
73-
filename,
74-
team,
75-
location,
76-
config.printColor,
77-
);
78-
fs.writeFileSync(path.resolve(process.cwd(), `data${path.sep}${tid}#${_id}.pdf`), pdf);
79-
files.push(path.resolve(process.cwd(), `data${path.sep}${tid}#${_id}.pdf`));
80-
}
81-
if (files.length === 1) {
82-
finalFile = files[0];
83-
} else {
84-
finalFile = path.resolve(process.cwd(), `data${path.sep}${new Date().getTime()}-merged.pdf`);
85-
await mergePDFs(files, finalFile);
86-
}
87-
if (config.printers.length) {
88-
while (true) {
89-
const printersInfo: any[] = await getPrinters();
90-
const printers = printersInfo.filter((p) => config.printers.includes(p.printer));
91-
const randomP = printers[Math.floor(Math.random() * printers.length)];
92-
if (randomP.status === 'idle') {
93-
logger.info(`Printing ${finalFile} on ${randomP.printer}`);
94-
await print(finalFile, randomP.printer, 1, files.length > 1 ? undefined : config.printPageMax);
95-
return randomP.printer;
96-
}
97-
for (const printer of printers.filter((p) => p.printer !== randomP.printer)) {
98-
logger.info(`Checking ${printer.printer} ${printer.status}`);
99-
if (printer.status === 'idle') {
100-
logger.info(`Printing ${finalFile} on ${printer.printer}`);
101-
await print(finalFile, printer.printer, 1, files.length > 1 ? undefined : config.printPageMax);
102-
return printer.printer;
103-
}
104-
}
105-
logger.info('No Printer can found to print, sleeping...');
106-
await sleep(3000);
107-
}
69+
let finalFile = null;
70+
const files = [];
71+
for (const doc of docs) {
72+
const {
73+
_id, tid, code, lang, filename, team, location,
74+
} = doc;
75+
const pdf = await ConvertCodeToPDF(
76+
code ? Buffer.from(code, 'base64') : Buffer.from('empty file'),
77+
lang,
78+
filename,
79+
team,
80+
location,
81+
config.printColor,
82+
);
83+
fs.writeFileSync(path.resolve(process.cwd(), `data${path.sep}${tid}#${_id}.pdf`), pdf);
84+
files.push(path.resolve(process.cwd(), `data${path.sep}${tid}#${_id}.pdf`));
85+
}
86+
if (files.length === 1) {
87+
finalFile = files[0];
88+
} else {
89+
finalFile = path.resolve(process.cwd(), `data${path.sep}${new Date().getTime()}-merged.pdf`);
90+
await mergePDFs(files, finalFile);
91+
}
92+
while (config.printers.length) {
93+
const printersInfo: any[] = await getPrinters();
94+
const printers = printersInfo.filter((p) => config.printers.includes(p.printer) && p.status === 'idle');
95+
if (printers.length) {
96+
const randomP = printers[Math.floor(Math.random() * printers.length)];
97+
logger.info(`Printing ${finalFile} on ${randomP.printer}`);
98+
await print(finalFile, randomP.printer, 1, files.length > 1 ? undefined : config.printPageMax);
99+
return randomP.printer;
108100
}
109-
logger.error('No Printer Configured');
110-
return null;
111-
} catch (e) {
112-
logger.error(e);
113-
return null;
101+
logger.info('No idle printer can found to print, sleeping...');
102+
await sleep(3000);
114103
}
104+
throw new Error('No Printer Configured');
115105
}
116106

117107
async function fetchTask(c) {

packages/server/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
"@cordisjs/plugin-logger": "^1.0.1",
1919
"@cordisjs/plugin-timer": "^1.1.1",
2020
"@freedom_sky/esc-pos-encoder": "^5.9.0",
21-
"@hydrooj/framework": "0.2.4",
22-
"@myriaddreamin/typst-ts-web-compiler": "0.6.1-rc2",
21+
"@hydrooj/framework": "0.2.6",
22+
"@myriaddreamin/typst-ts-web-compiler": "0.6.1-rc5",
2323
"@myteril/node-win-printer": "patch:@myteril/node-win-printer@npm%3A1.1.5#~/.yarn/patches/@myteril-node-win-printer-npm-1.1.5-c62910fcfd.patch",
2424
"base16384": "^1.0.0",
2525
"cordis": "4.0.0-beta.5",
2626
"koa-proxies": "^0.12.4",
2727
"nedb-promises": "^6.2.3",
2828
"pdf-lib": "^1.17.1",
2929
"prom-client": "^15.1.3",
30-
"schemastery": "^3.17.1",
30+
"schemastery": "^3.17.2",
3131
"superagent": "^10.2.3",
3232
"unix-print": "^1.3.2",
3333
"ws": "^8.18.3"

packages/ui/package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@
1010
"preferUnplugged": true,
1111
"devDependencies": {
1212
"@hydrooj/xcpc-tools": "workspace:*",
13-
"@mantine/core": "^8.3.6",
14-
"@mantine/form": "^8.3.6",
15-
"@mantine/hooks": "^8.3.6",
16-
"@mantine/modals": "^8.3.6",
17-
"@mantine/notifications": "^8.3.6",
18-
"@mantine/nprogress": "^8.3.6",
13+
"@mantine/core": "^8.3.9",
14+
"@mantine/form": "^8.3.9",
15+
"@mantine/hooks": "^8.3.9",
16+
"@mantine/modals": "^8.3.9",
17+
"@mantine/notifications": "^8.3.9",
18+
"@mantine/nprogress": "^8.3.9",
1919
"@tabler/icons-react": "^3.35.0",
20-
"@tanstack/react-query": "^5.90.7",
21-
"@types/react": "^18.3.26",
20+
"@tanstack/react-query": "^5.90.10",
21+
"@types/react": "^18.3.27",
2222
"@types/react-dom": "^18.3.7",
2323
"css-loader": "^6.11.0",
2424
"esbuild-loader": "^4.4.0",
25-
"mantine-datatable": "^8.3.6",
25+
"mantine-datatable": "^8.3.8",
2626
"mpegts.js": "^1.8.0",
2727
"postcss": "^8.5.6",
2828
"postcss-loader": "^7.3.4",
2929
"postcss-preset-mantine": "^1.18.0",
3030
"postcss-simple-vars": "^7.0.1",
3131
"react": "^18.3.1",
3232
"react-dom": "^18.3.1",
33-
"react-router-dom": "^6.30.1",
33+
"react-router-dom": "^6.30.2",
3434
"style-loader": "^3.3.4",
35-
"webpack": "^5.102.1",
35+
"webpack": "^5.103.0",
3636
"webpack-dev-server": "^5.2.2"
3737
}
3838
}

0 commit comments

Comments
 (0)