screenshot-service/request.js

94 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-05-05 21:54:34 +02:00
const { q, requests } = require("./memory");
const DOCKER_ARGS = require("./docker-args");
const { v4: uuid } = require("uuid");
const { spawn } = require("child_process");
module.exports = class ScreenshotRequest {
constructor(url, domains) {
this.url = url;
this.domains = domains;
this.id = uuid();
this.status = "waiting";
this.output = "";
this.images = [];
2022-05-25 19:38:30 +02:00
this.request_time = Date.now();
this.started_time = null;
this.finished_time = null;
this.processing_took = null;
this.waiting_took = null;
2022-05-05 21:54:34 +02:00
q.push(async () => {
return this.exec();
});
requests[this.id] = this;
}
async getJSON() {
return {
url: this.url,
domains: this.domains,
id: this.id,
status: this.status,
2022-05-25 19:38:30 +02:00
output: this.output,
2022-05-27 15:49:56 +02:00
images: this.images,
2022-05-25 19:38:30 +02:00
request_time: this.request_time,
started_time: this.started_time,
finished_time: this.finished_time,
processing_took: this.processing_took,
waiting_took: this.waiting_took,
elapsed_time_s: Math.round(
((this.status === "finished" ? this.finished_time : Date.now()) -
this.request_time) /
1000
),
2022-05-05 21:54:34 +02:00
};
}
2022-05-25 19:38:30 +02:00
setFinished() {
this.status = "finished";
this.finished_time = Date.now();
this.processing_took = this.finished_time - this.started_time;
this.waiting_took = this.started_time - this.request_time;
}
2022-05-05 21:54:34 +02:00
async exec() {
2022-05-25 19:38:30 +02:00
this.started_time = Date.now();
2022-05-05 21:54:34 +02:00
return new Promise((resolve, reject) => {
this.status = "running";
this.process = spawn(
"docker",
[
...DOCKER_ARGS,
JSON.stringify({
url: this.url,
third_party_domains: this.domains,
}),
this.id,
],
{ cwd: process.cwd() }
);
this.process.on("close", (exitCode) => {
2022-05-25 19:38:30 +02:00
this.setFinished();
2022-05-05 21:54:34 +02:00
if (exitCode === 0) {
resolve();
} else {
reject();
}
});
this.process.stdout.on("data", (d) => {
2022-05-27 15:49:56 +02:00
try {
const parsed = JSON.parse(d.toString());
if (parsed.new_file) {
this.images.push(parsed.new_file);
}
} catch (e) {}
2022-05-05 21:54:34 +02:00
this.output += d.toString();
/* console.log("DATA!", d.toString()); */
});
this.process.stderr.on("data", (d) => {
this.output += d.toString();
/* console.log("STDERR!", d.toString()); */
});
});
}
};