-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmemory.ts
More file actions
170 lines (127 loc) · 3.41 KB
/
memory.ts
File metadata and controls
170 lines (127 loc) · 3.41 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { Events, Identifiers } from "@mainsail/constants";
import { inject, injectable } from "@mainsail/container";
import type { Contracts } from "@mainsail/contracts";
import { EventEmitter } from "events";
import { performance } from "perf_hooks";
@injectable()
export class MemoryQueue extends EventEmitter implements Contracts.Kernel.Queue {
@inject(Identifiers.Services.EventDispatcher.Service)
private readonly events!: Contracts.Kernel.EventDispatcher;
@inject(Identifiers.Services.Log.Service)
private readonly logger!: Contracts.Kernel.Logger;
#jobs: Contracts.Kernel.QueueJob[] = [];
#running = false;
#started = false;
#onProcessedCallbacks: (() => void)[] = [];
public constructor() {
super();
this.setMaxListeners(0);
}
public async make(): Promise<Contracts.Kernel.Queue> {
return this;
}
public async start(): Promise<void> {
this.#started = true;
void this.#processJobs();
}
public async stop(): Promise<void> {
this.#started = false;
const promise = this.#waitUntilProcessed();
await this.clear();
return promise;
}
public async pause(): Promise<void> {
this.#started = false;
await this.#waitUntilProcessed();
}
public async resume(): Promise<void> {
await this.start();
}
public async clear(): Promise<void> {
this.#jobs = [];
}
public async drain(): Promise<void> {
while (this.#running || this.#jobs.length > 0) {
if (!this.#started) {
await this.start();
}
await this.#waitUntilProcessed();
}
this.#started = false;
}
public async push(job: Contracts.Kernel.QueueJob): Promise<void> {
this.#jobs.push(job);
void this.#processJobs();
}
public async later(delay: number, job: Contracts.Kernel.QueueJob): Promise<void> {
setTimeout(() => void this.push(job), delay);
}
public async bulk(jobs: Contracts.Kernel.QueueJob[]): Promise<void> {
for (const job of jobs) {
this.#jobs.push(job);
}
}
public size(): number {
return this.#jobs.length;
}
public isStarted(): boolean {
return this.#started;
}
public isRunning(): boolean {
return this.#running;
}
#waitUntilProcessed(): Promise<void> {
return new Promise((resolve) => {
if (this.#running) {
const onProcessed = () => {
resolve();
};
this.#onProcessedCallbacks.push(onProcessed);
} else {
resolve();
}
});
}
#resolveOnProcessed(): void {
while (this.#onProcessedCallbacks.length > 0) {
const onProcessed = this.#onProcessedCallbacks.shift()!;
onProcessed();
}
}
async #processJobs(): Promise<void> {
// Prevent entering if already processing
if (this.isRunning()) {
return;
}
while (this.#jobs.length > 0) {
if (!this.#started) {
break;
}
this.#running = true;
const job = this.#jobs.shift()!;
const start = performance.now();
try {
const data = await job.handle();
await this.events.dispatch(Events.QueueEvent.Finished, {
data: data,
driver: "memory",
executionTime: performance.now() - start,
});
this.emit("jobDone", job, data);
} catch (error) {
await this.events.dispatch(Events.QueueEvent.Failed, {
driver: "memory",
error: error,
executionTime: performance.now() - start,
});
this.logger.warn(`Queue error occured while handling job: ${error.message}`);
this.emit("jobError", job, error);
}
}
this.#running = false;
this.#resolveOnProcessed();
if (this.#jobs.length === 0) {
this.emit("drain");
}
}
}