This commit is contained in:
parent
a3c79aa9b9
commit
0a32ef9041
|
@ -18,6 +18,7 @@ class PrintServer {
|
||||||
if (fs.existsSync(localConfFile))
|
if (fs.existsSync(localConfFile))
|
||||||
conf = Object.assign({}, conf, yml(localConfFile));
|
conf = Object.assign({}, conf, yml(localConfFile));
|
||||||
this.conf = conf;
|
this.conf = conf;
|
||||||
|
this.jobs = [];
|
||||||
|
|
||||||
console.clear();
|
console.clear();
|
||||||
const decoration = '△▽'.repeat(10);
|
const decoration = '△▽'.repeat(10);
|
||||||
|
@ -33,7 +34,7 @@ class PrintServer {
|
||||||
process.on('unhandledRejection', this.rejectionHandler);
|
process.on('unhandledRejection', this.rejectionHandler);
|
||||||
|
|
||||||
this.serverLog('log', 'Ready to print'.green);
|
this.serverLog('log', 'Ready to print'.green);
|
||||||
setTimeout(() => this.poll());
|
await this.poll();
|
||||||
}
|
}
|
||||||
async stop() {
|
async stop() {
|
||||||
this.serverLog('log', 'Bye ( ◕ ‿ ◕ )っ'.green);
|
this.serverLog('log', 'Bye ( ◕ ‿ ◕ )っ'.green);
|
||||||
|
@ -102,8 +103,16 @@ class PrintServer {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
async poll() {
|
async poll() {
|
||||||
const conf = this.conf;
|
|
||||||
this.pollTimeout = null;
|
this.pollTimeout = null;
|
||||||
|
let delay = this.conf.refreshRate;
|
||||||
|
await this.getJobs();
|
||||||
|
if (this.dbDown) delay = this.conf.reconnectTimeout;
|
||||||
|
this.pollTimeout = setTimeout(() => this.poll(), delay);
|
||||||
|
}
|
||||||
|
async getJobs() {
|
||||||
|
if (this.polling) return;
|
||||||
|
this.polling = true;
|
||||||
|
const conf = this.conf;
|
||||||
|
|
||||||
if (this.dbDown) {
|
if (this.dbDown) {
|
||||||
try {
|
try {
|
||||||
|
@ -121,37 +130,27 @@ class PrintServer {
|
||||||
|
|
||||||
if (!this.dbDown) {
|
if (!this.dbDown) {
|
||||||
try {
|
try {
|
||||||
let jobs;
|
|
||||||
let nJobs = 0;
|
let nJobs = 0;
|
||||||
do {
|
while (this.jobs.length < conf.concurrency) {
|
||||||
jobs = [];
|
const jobId = await this.getJob();
|
||||||
for (let i = 0; i < conf.concurrency; i++) {
|
if (jobId) {
|
||||||
const jobId = await this.getJob();
|
nJobs++;
|
||||||
if (jobId) {
|
this.jobs.push(jobId);
|
||||||
const job = this.printJob(jobId);
|
const jobP = this.printJob(jobId);
|
||||||
// XXX: Workaround for Promise.all() unhandledRejection
|
// XXX: Workaround for Promise unhandledRejection
|
||||||
// https://stackoverflow.com/questions/67789309/why-do-i-get-an-unhandled-promise-rejection-with-await-promise-all
|
// https://stackoverflow.com/questions/67789309/why-do-i-get-an-unhandled-promise-rejection-with-await-promise-all
|
||||||
job.catch(() => {});
|
jobP.catch(err => this.errorHandler(err));
|
||||||
jobs.push(job);
|
} else
|
||||||
}
|
break;
|
||||||
else
|
}
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
nJobs += jobs.length;
|
|
||||||
await Promise.all(jobs);
|
|
||||||
} while (jobs.length);
|
|
||||||
|
|
||||||
if (nJobs > 0)
|
if (nJobs > 0)
|
||||||
this.serverLog('debug', `${nJobs} jobs printed`);
|
this.serverLog('debug', `${nJobs} jobs buffered`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.errorHandler(err);
|
this.errorHandler(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let delay = this.conf.refreshRate;
|
this.polling = false;
|
||||||
if (this.dbDown) delay = this.conf.reconnectTimeout;
|
|
||||||
this.pollTimeout = setTimeout(() => this.poll(), delay);
|
|
||||||
}
|
}
|
||||||
async getJob() {
|
async getJob() {
|
||||||
let jobId;
|
let jobId;
|
||||||
|
@ -281,17 +280,22 @@ class PrintServer {
|
||||||
throw jobErr;
|
throw jobErr;
|
||||||
} finally {
|
} finally {
|
||||||
conn.release();
|
conn.release();
|
||||||
|
|
||||||
|
if (!conf.keepFile) {
|
||||||
|
try {
|
||||||
|
const shouldDelete = tmpFileCreated
|
||||||
|
|| (tmpFilePath && await fs.pathExists(tmpFilePath));
|
||||||
|
if (shouldDelete) await fs.unlink(tmpFilePath);
|
||||||
|
} catch (err) {
|
||||||
|
this.jobLog(jobId, 'error', err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const index = this.jobs.indexOf(jobId);
|
||||||
|
if (index !== -1) this.jobs.splice(index, 1);
|
||||||
|
setTimeout(() => this.getJobs());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!conf.keepFile) {
|
|
||||||
try {
|
|
||||||
const shouldDelete = tmpFileCreated
|
|
||||||
|| (tmpFilePath && await fs.pathExists(tmpFilePath));
|
|
||||||
if (shouldDelete) await fs.unlink(tmpFilePath);
|
|
||||||
} catch (err) {
|
|
||||||
this.jobLog(jobId, 'error', err.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
jobLog(jobId, realm, message) {
|
jobLog(jobId, realm, message) {
|
||||||
this.log(`Job[${colors.yellow(jobId)}]`, realm, message);
|
this.log(`Job[${colors.yellow(jobId)}]`, realm, message);
|
||||||
|
|
Loading…
Reference in New Issue