-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.ts
More file actions
66 lines (55 loc) · 1.86 KB
/
test.ts
File metadata and controls
66 lines (55 loc) · 1.86 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import Pocketbase from "pocketbase";
import { createConnection, createQueue } from "./index";
// create this many tasks per second
const tasksPerSecond = 30;
export async function start() {
if (!process.env.POCKETBASE_EMAIL) {
throw new Error("POCKETBASE_EMAIL is required");
}
if (!process.env.POCKETBASE_PASSWORD) {
throw new Error("POCKETBASE_PASSWORD is required");
}
const pb = new Pocketbase(process.env.POCKETBASE_URL || "http://127.0.0.1:8090");
await pb.admins.authWithPassword(process.env.POCKETBASE_EMAIL, process.env.POCKETBASE_PASSWORD);
const connection = createConnection({ pb, verbose: false });
const greetingQueue = createQueue<{ message: string }>({
name: "greeting",
connection,
});
const currentTimeQueue = createQueue<{ message: string }>({
name: "current-time",
connection,
});
process.on("SIGINT", async () => {
greetingQueue.close();
currentTimeQueue.close();
await new Promise((resolve) => setImmediate(resolve));
console.log("exiting");
process.exit(0);
});
greetingQueue.process({ concurrency: 2 }, async ({ task, workerId }) => {
console.log(workerId, task.message);
});
currentTimeQueue.process({ concurrency: 4 }, async ({ task, workerId }) => {
const ms = Math.floor(Math.random() * 100);
await new Promise((resolve) => setTimeout(resolve, ms));
if (Math.random() < 0.5) {
throw new Error("random error");
}
console.log(workerId, task);
});
currentTimeQueue.on("stats", (stats) => {
console.log("stats", stats);
});
// eslint-disable-next-line no-constant-condition
while (true) {
await new Promise((resolve) => setTimeout(resolve, 1000 / tasksPerSecond));
currentTimeQueue.push({
task: { message: "current time is " + Date.now() },
});
greetingQueue.push({
task: { message: "hello world" },
});
}
}
start();