Skip to content

Commit

Permalink
Replace eris-fleet with a pm2-based cluster system, overhaul image ha…
Browse files Browse the repository at this point in the history
…ndling, removed azure image api
  • Loading branch information
TheEssem committed Sep 21, 2022
1 parent 5a33647 commit db0decf
Show file tree
Hide file tree
Showing 45 changed files with 1,774 additions and 854 deletions.
5 changes: 0 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ METRICS=
# The image API type to be used
# Set this to `none` to process all images locally
# Set this to `ws` if you want to use the external image API script, located in api/index.js
# Set this to `azure` to use the Azure Functions API
API_TYPE=none
# If API_TYPE is `azure`, set this to your Azure webhook URL
AZURE_URL=
# If API_TYPE is `azure`, set an optional password for webhook responses
AZURE_PASS=

# Put ID of server to limit owner-only commands to
ADMIN_SERVER=
13 changes: 1 addition & 12 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,4 @@ libvips/

# Databases
data/
*.sqlite

# Azure Functions artifacts
bin
obj
appsettings.json
local.settings.json

# Azurite artifacts
__blobstorage__
__queuestorage__
__azurite_db*__.json
*.sqlite
12 changes: 8 additions & 4 deletions api/IMPLEMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The esmBot image API is a combined HTTP and WebSocket API. The default port to a
### GET `/image/?id=<job id>`
Get image data after job is finished running. The Content-Type header is properly set.

### GET `/count`
Get the current amount of running jobs. Response is a plaintext number value.

## WebSockets
A client sends *requests* (T-messages) to a server, which subsequently *replies* (R-messages) to the client.
### Message IDs
Expand All @@ -24,11 +27,11 @@ A client sends *requests* (T-messages) to a server, which subsequently *replies*
[j] means JSON data that goes until the end of the message.
`tag` is used to identify a request/response pair, like `lock` in the original API. `jid` is used to identify a job. `job` is a job object.
- Rerror tag[2] error[s]
- Tqueue tag[2] jid[4] job[j]
- Tqueue tag[2] jid[8] job[j]
- Rqueue tag[2]
- Tcancel tag[2] jid[4]
- Tcancel tag[2] jid[8]
- Rcancel tag[2]
- Twait tag[2] jid[4]
- Twait tag[2] jid[8]
- Rwait tag[2]
- Rinit tag[2] max_jobs[2] running_jobs[2] formats[j]

Expand All @@ -42,6 +45,7 @@ The job object is formatted like this:
"params": { // content varies depending on the command, some common parameters are listed here
"type": string, // mime type of output, should usually be the same as input
...
}
},
"name": string // filename of the image, without extension
}
```
13 changes: 9 additions & 4 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ wss.on("connection", (ws, request) => {
const tag = msg.slice(1, 3);
const req = msg.toString().slice(3);
if (opcode == Tqueue) {
const id = msg.readUInt32LE(3);
const obj = msg.slice(7);
const id = msg.readBigInt64LE(3);
const obj = msg.slice(11);
const job = { msg: obj, num: jobAmount, verifyEvent: new EventEmitter() };
jobs.set(id, job);
queue.push(id);
Expand All @@ -128,7 +128,7 @@ wss.on("connection", (ws, request) => {
const cancelResponse = Buffer.concat([Buffer.from([Rcancel]), tag]);
ws.send(cancelResponse);
} else if (opcode == Twait) {
const id = msg.readUInt32LE(3);
const id = msg.readBigUInt64LE(3);
const job = jobs.get(id);
if (!job) {
const errorResponse = Buffer.concat([Buffer.from([Rerror]), tag, Buffer.from("Invalid job ID")]);
Expand Down Expand Up @@ -178,7 +178,7 @@ httpServer.on("request", async (req, res) => {
res.statusCode = 400;
return res.end("400 Bad Request");
}
const id = parseInt(reqUrl.searchParams.get("id"));
const id = BigInt(reqUrl.searchParams.get("id"));
if (!jobs.has(id)) {
res.statusCode = 410;
return res.end("410 Gone");
Expand Down Expand Up @@ -208,6 +208,11 @@ httpServer.on("request", async (req, res) => {
return res.end(data, (err) => {
if (err) error(err);
});
} else if (reqUrl.pathname === "/count" && req.method === "GET") {
log(`Sending job count to ${req.socket.remoteAddress}:${req.socket.remotePort} via HTTP`);
return res.end(jobAmount.toString(), (err) => {
if (err) error(err);
});
} else {
res.statusCode = 404;
return res.end("404 Not Found");
Expand Down
Loading

0 comments on commit db0decf

Please sign in to comment.