-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.ts
More file actions
48 lines (34 loc) · 1.57 KB
/
Copy pathmain.ts
File metadata and controls
48 lines (34 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import * as Persistency from "./src/persist.ts";
import QueueManager from "./src/manager.ts";
import { createHandler } from "./src/handler.ts";
import { parseConfig } from "./src/config.ts";
const config = parseConfig(Deno.env.toObject(), Deno.args);
// Set up our persistency manager
const persist = config.persistEnabled
? new Persistency.FileStore
: new Persistency.MemoryStore;
persist.dir(config.persistDir);
// Set up the manager, which will handle our queues for us
const mgr = new QueueManager(persist, config.queueDepthLimit, config.queueCountLimit);
// Load up any existing queue data, if we're persisting
if (persist instanceof Persistency.FileStore) {
console.log("Loading in data from persist.dat...\n");
mgr.load();
}
const handler = createHandler(mgr, config.apiToken, config.rateLimitRequests);
// Start up the application
const server = Deno.serve({ hostname: config.host, port: config.port, onListen: ({ hostname, port }) => console.log(`Listening on ${hostname}:${port}`) }, handler);
const shutdown = async (signal: string) => {
console.log(`Received ${signal}, shutting down gracefully...`);
// Stop accepting new connections
await server.shutdown();
// If we're using file persistency, save all current state to persistant storage
if (persist instanceof Persistency.FileStore) {
console.log("Flushing data to persist.dat...\n");
mgr.save();
}
console.log("Goodbye!");
Deno.exit(0);
};
Deno.addSignalListener("SIGINT", () => shutdown("SIGINT"));
Deno.addSignalListener("SIGTERM", () => shutdown("SIGTERM"));