Fixed tag parameter stuff, added tag to Rinit, added format checking
This commit is contained in:
parent
19638966bb
commit
871979105c
5 changed files with 30 additions and 26 deletions
|
@ -2,7 +2,7 @@ import fetch from "node-fetch";
|
|||
import fs from "fs";
|
||||
import fileType from "file-type";
|
||||
|
||||
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif", "video/mp4", "video/webm", "video/mov"];
|
||||
const formats = ["image/jpeg", "image/png", "image/webp", "image/gif", "video/mp4", "video/webm", "video/quicktime"];
|
||||
|
||||
export const jobs = {};
|
||||
|
||||
|
|
|
@ -59,18 +59,18 @@ class ImageConnection {
|
|||
onMessage(msg) {
|
||||
const op = msg.readUint8(0);
|
||||
if (op === Rinit) {
|
||||
this.max = msg.readUint16LE(1);
|
||||
this.formats = JSON.parse(msg.toString("utf8", 3));
|
||||
this.max = msg.readUint16LE(3);
|
||||
this.formats = JSON.parse(msg.toString("utf8", 5));
|
||||
return;
|
||||
}
|
||||
const tag = msg.readUint32LE(1);
|
||||
const tag = msg.readUint16LE(1);
|
||||
const promise = this.requests.get(tag);
|
||||
this.requests.delete(tag);
|
||||
if (op === Rqueue) this.njobs++;
|
||||
if (op === Rcancel || op === Rwait) this.njobs--;
|
||||
if (op === Rerror) {
|
||||
this.njobs--;
|
||||
promise.reject(new Error(msg.slice(5, msg.length).toString()));
|
||||
promise.reject(new Error(msg.slice(3, msg.length).toString()));
|
||||
return;
|
||||
}
|
||||
promise.resolve();
|
||||
|
@ -107,10 +107,9 @@ class ImageConnection {
|
|||
|
||||
queue(jobid, jobobj) {
|
||||
const str = JSON.stringify(jobobj);
|
||||
const buf = Buffer.alloc(4 + str.length);
|
||||
const buf = Buffer.alloc(4);
|
||||
buf.writeUint32LE(jobid);
|
||||
buf.write(str, 4);
|
||||
return this.do(Tqueue, buf);
|
||||
return this.do(Tqueue, Buffer.concat([buf, Buffer.from(str)]));
|
||||
}
|
||||
|
||||
wait(jobid) {
|
||||
|
@ -151,10 +150,10 @@ class ImageConnection {
|
|||
}
|
||||
|
||||
async do(op, data) {
|
||||
const buf = Buffer.alloc(1 + 4);
|
||||
const buf = Buffer.alloc(1 + 2);
|
||||
const tag = this.tag++;
|
||||
buf.writeUint8(op);
|
||||
buf.writeUint32LE(tag, 1);
|
||||
buf.writeUint16LE(tag, 1);
|
||||
this.conn.send(Buffer.concat([buf, data]));
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
this.requests.set(tag, { resolve, reject });
|
||||
|
|
|
@ -65,12 +65,13 @@ class ImageWorker extends BaseServiceWorker {
|
|||
return sorted[0];
|
||||
}
|
||||
|
||||
async getIdeal() {
|
||||
async getIdeal(object) {
|
||||
const idealServers = [];
|
||||
for (const [address, connection] of this.connections) {
|
||||
if (connection.conn.readyState !== 0 && connection.conn.readyState !== 1) {
|
||||
continue;
|
||||
}
|
||||
if (!connection.formats[object.cmd].includes(object.type)) continue;
|
||||
idealServers.push({
|
||||
addr: address,
|
||||
load: connection.njobs / connection.max
|
||||
|
@ -108,7 +109,7 @@ class ImageWorker extends BaseServiceWorker {
|
|||
async run(object) {
|
||||
if (process.env.API === "true") {
|
||||
const num = this.nextID++;
|
||||
const currentServer = await this.getIdeal();
|
||||
const currentServer = await this.getIdeal(object);
|
||||
await currentServer.queue(num, object);
|
||||
await currentServer.wait(num);
|
||||
const output = await currentServer.getOutput(num);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue