raiton 6.6.0-beta.14 → 6.8.1-beta.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/README.md +16 -9
- package/build/bin/index.mjs +9150 -3911
- package/deno.json +4 -1
- package/docs/contributors/README.md +1 -0
- package/docs/contributors/core/README.md +8 -0
- package/docs/contributors/core/scheduler.md +113 -0
- package/docs/contributors/framework/README.md +2 -0
- package/docs/contributors/framework/cache/README.md +55 -0
- package/docs/contributors/framework/cache/cache.md +86 -0
- package/docs/contributors/framework/cache/etag.md +82 -0
- package/docs/contributors/framework/decorators/README.md +5 -0
- package/docs/contributors/framework/decorators/scheduler.md +83 -0
- package/docs/contributors/framework/runtime.md +13 -3
- package/docs/user-guide/README.md +9 -5
- package/docs/user-guide/cache.md +79 -0
- package/docs/user-guide/cli.md +35 -3
- package/docs/user-guide/decorators/README.md +3 -0
- package/docs/user-guide/decorators/cache.md +79 -0
- package/docs/user-guide/decorators/mcp.md +2 -2
- package/docs/user-guide/decorators/scheduler.md +75 -0
- package/docs/user-guide/mcp.md +2 -2
- package/docs/user-guide/runtime.md +2 -1
- package/docs/user-guide/scheduler.md +229 -0
- package/package.json +9 -4
- package/source/bin/bootstrapper.ts +4 -4
- package/source/bin/cli-tools.ts +27 -3
- package/source/bin/cli.ts +2 -2
- package/source/bin/commands.registry.ts +17 -0
- package/source/bin/constants.ts +1 -0
- package/source/bin/index.ts +2 -2
- package/source/commands/artifact.command.ts +2 -2
- package/source/commands/build.command.ts +2 -2
- package/source/commands/develop.command.ts +4 -5
- package/source/commands/grafts.command.ts +1 -1
- package/source/commands/start.command.ts +2 -2
- package/source/core/application.ts +10 -10
- package/source/core/artifacts/artifact.ts +1 -1
- package/source/core/artifacts/index.ts +1 -1
- package/source/core/builder.ts +81 -15
- package/source/core/bytes.util.ts +1 -1
- package/source/core/commands.ts +24 -14
- package/source/core/config/config.ts +2 -2
- package/source/core/config/define.ts +2 -2
- package/source/core/config/index.ts +2 -2
- package/source/core/context.ts +1 -1
- package/source/core/controller/builder.ts +16 -10
- package/source/core/controller/compiler.ts +6 -6
- package/source/core/controller/index.ts +4 -4
- package/source/core/controller/metadata.ts +2 -2
- package/source/core/controller/tracker.ts +1 -1
- package/source/core/directories.ts +3 -2
- package/source/core/guards.ts +1 -1
- package/source/core/helpers/index.ts +1 -1
- package/source/core/hooks.ts +1 -1
- package/source/core/index.ts +17 -16
- package/source/core/injection/index.ts +1 -1
- package/source/core/injection/injection.ts +7 -4
- package/source/core/mcp/builder.ts +8 -8
- package/source/core/mcp/index.ts +3 -3
- package/source/core/mcp/metadata.ts +2 -2
- package/source/core/mcp/tracker.ts +1 -1
- package/source/core/middleware/compose.ts +2 -2
- package/source/core/middleware/index.ts +2 -2
- package/source/core/middleware/pipeline.ts +2 -2
- package/source/core/plugins/index.ts +2 -2
- package/source/core/plugins/plugin.ts +1 -1
- package/source/core/plugins/scope.ts +4 -4
- package/source/core/raiton.ts +1 -1
- package/source/core/router/handler.ts +30 -6
- package/source/core/router/handler.utils.ts +3 -3
- package/source/core/router/index.ts +4 -4
- package/source/core/router/matcher.ts +1 -1
- package/source/core/router/route.ts +1 -1
- package/source/core/router/router.ts +4 -4
- package/source/core/scheduler/builder.ts +25 -0
- package/source/core/scheduler/cron.ts +400 -0
- package/source/core/scheduler/duration.ts +42 -0
- package/source/core/scheduler/index.ts +6 -0
- package/source/core/scheduler/manager.ts +542 -0
- package/source/core/scheduler/metadata.ts +53 -0
- package/source/core/scheduler/queue/backend.ts +142 -0
- package/source/core/scheduler/queue/index.ts +3 -0
- package/source/core/scheduler/queue/rabbitmq.backend.ts +124 -0
- package/source/core/scheduler/queue/redis.backend.ts +144 -0
- package/source/core/server.ts +1 -1
- package/source/core/socket/builder.ts +2 -2
- package/source/core/socket/index.ts +2 -2
- package/source/core/socket/metadata.ts +2 -2
- package/source/core/thread.ts +55 -22
- package/source/framework/artifacts.ts +32 -5
- package/source/framework/cache/cache.decorator.ts +35 -0
- package/source/framework/cache/cache.ts +197 -0
- package/source/framework/cache/etag.ts +174 -0
- package/source/framework/cache/index.ts +4 -0
- package/source/framework/cache/types.ts +111 -0
- package/source/framework/constants/decorators.constant.ts +4 -0
- package/source/framework/constants/index.ts +2 -2
- package/source/framework/decorators/access-guard.decorator.ts +2 -2
- package/source/framework/decorators/api-response.decorator.ts +1 -1
- package/source/framework/decorators/api.decorator.ts +1 -1
- package/source/framework/decorators/controllable.decorator.ts +3 -3
- package/source/framework/decorators/csrf-guard.decorator.ts +4 -4
- package/source/framework/decorators/csrf-token.decorator.ts +1 -1
- package/source/framework/decorators/guard.decorator.ts +4 -4
- package/source/framework/decorators/health-check.decorator.ts +3 -3
- package/source/framework/decorators/index.ts +15 -14
- package/source/framework/decorators/injection.decorator.ts +3 -3
- package/source/framework/decorators/mcp.decorator.ts +4 -4
- package/source/framework/decorators/middleware.decorator.ts +2 -2
- package/source/framework/decorators/parametrable.decorator.ts +2 -2
- package/source/framework/decorators/routable.decorator.ts +3 -3
- package/source/framework/decorators/route-interaction.decorator.ts +4 -4
- package/source/framework/decorators/scheduler.decorator.ts +139 -0
- package/source/framework/decorators/socket.decorator.ts +3 -3
- package/source/framework/encryption.ts +2 -2
- package/source/framework/enums/index.ts +9 -8
- package/source/framework/enums/runtime.enum.ts +1 -2
- package/source/framework/enums/scheduler.enum.ts +23 -0
- package/source/framework/env.ts +2 -2
- package/source/framework/exceptions/http-exception.ts +3 -3
- package/source/framework/exceptions/index.ts +2 -2
- package/source/framework/health-check.ts +4 -4
- package/source/framework/index.ts +18 -17
- package/source/framework/mcp/handler.ts +3 -3
- package/source/framework/mcp/index.ts +3 -3
- package/source/framework/mcp/protocol.ts +1 -1
- package/source/framework/mcp/streamable-http.ts +3 -3
- package/source/framework/parameter-bag.ts +2 -2
- package/source/framework/plugins/body-parser.plugin.ts +3 -3
- package/source/framework/plugins/index.ts +4 -4
- package/source/framework/plugins/mcp.plugin.ts +5 -5
- package/source/framework/plugins/openapi.plugin.ts +4 -4
- package/source/framework/plugins/security/body-limit.ts +3 -3
- package/source/framework/plugins/security/cors.ts +2 -2
- package/source/framework/plugins/security/csrf.ts +6 -6
- package/source/framework/plugins/security/headers.ts +2 -2
- package/source/framework/plugins/security/index.ts +6 -6
- package/source/framework/plugins/security/method-guard.ts +3 -3
- package/source/framework/plugins/security/rate-limit.ts +3 -3
- package/source/framework/repositories.ts +1 -1
- package/source/framework/responses/error.ts +2 -2
- package/source/framework/responses/helpers.ts +3 -3
- package/source/framework/responses/http-throwable.ts +3 -3
- package/source/framework/responses/http.ts +3 -3
- package/source/framework/responses/index.ts +4 -4
- package/source/framework/runtime/bun/server.ts +5 -5
- package/source/framework/runtime/deno/server.ts +156 -96
- package/source/framework/runtime/index.ts +11 -17
- package/source/framework/runtime/node/reply.ts +1 -1
- package/source/framework/runtime/node/request.ts +2 -2
- package/source/framework/runtime/node/server.ts +3 -3
- package/source/framework/runtime/web/server.ts +2 -2
- package/source/framework/services.ts +1 -1
- package/source/framework/utilities/artifact.util.ts +4 -0
- package/source/framework/utilities/csrf.util.ts +1 -1
- package/source/framework/utilities/index.ts +10 -10
- package/source/framework/utilities/openapi.utils.ts +7 -7
- package/source/framework/utilities/utilities.util.ts +1 -1
- package/source/types/application.ts +2 -2
- package/source/types/builder.ts +1 -1
- package/source/types/config.ts +1 -1
- package/source/types/controller.ts +3 -3
- package/source/types/core.ts +1 -1
- package/source/types/csrf.ts +1 -1
- package/source/types/guard.ts +1 -1
- package/source/types/http-responses.ts +1 -1
- package/source/types/index.ts +27 -26
- package/source/types/injection.ts +1 -1
- package/source/types/middleware.ts +1 -1
- package/source/types/plugin.ts +1 -1
- package/source/types/raiton.ts +2 -1
- package/source/types/responses.ts +2 -2
- package/source/types/router.ts +2 -2
- package/source/types/runtime.ts +3 -5
- package/source/types/scheduler.ts +180 -0
- package/source/types/thread.ts +4 -4
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type {QueueBackendInterface, QueueDequeueOptionsInterface, QueueMessageInterface} from "../../../types/index.ts";
|
|
2
|
+
|
|
3
|
+
export abstract class BaseQueueBackend implements QueueBackendInterface {
|
|
4
|
+
abstract readonly name: string;
|
|
5
|
+
protected connected = false;
|
|
6
|
+
|
|
7
|
+
get isConnected(): boolean {
|
|
8
|
+
return this.connected;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
abstract connect(): Promise<void>;
|
|
12
|
+
|
|
13
|
+
abstract disconnect(): Promise<void>;
|
|
14
|
+
|
|
15
|
+
abstract enqueue(queue: string, message: QueueMessageInterface): Promise<string>;
|
|
16
|
+
|
|
17
|
+
abstract dequeue(queue: string, options?: QueueDequeueOptionsInterface): Promise<QueueMessageInterface[]>;
|
|
18
|
+
|
|
19
|
+
abstract acknowledge(queue: string, receipt: any): Promise<void>;
|
|
20
|
+
|
|
21
|
+
abstract reject(queue: string, receipt: any, requeue?: boolean): Promise<void>;
|
|
22
|
+
|
|
23
|
+
abstract length(queue: string): Promise<number>;
|
|
24
|
+
|
|
25
|
+
abstract purge(queue: string): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function wait(milliseconds: number): Promise<void> {
|
|
29
|
+
return new Promise(resolve => setTimeout(resolve, milliseconds));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface MemoryEntry {
|
|
33
|
+
message: QueueMessageInterface;
|
|
34
|
+
receipt: MemoryReceipt;
|
|
35
|
+
availableAt: number;
|
|
36
|
+
locked: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface MemoryReceipt {
|
|
40
|
+
queue: string;
|
|
41
|
+
id: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* In-process queue backend used by default when no external broker is
|
|
46
|
+
* configured. Supports blocking dequeue, visibility timeout and ack/nack.
|
|
47
|
+
*/
|
|
48
|
+
export class MemoryQueueBackend extends BaseQueueBackend {
|
|
49
|
+
readonly name = 'memory';
|
|
50
|
+
private readonly queues = new Map<string, MemoryEntry[]>();
|
|
51
|
+
private readonly visibilityMs: number;
|
|
52
|
+
|
|
53
|
+
constructor(options: {visibilityMs?: number} = {}) {
|
|
54
|
+
super();
|
|
55
|
+
this.visibilityMs = options.visibilityMs ?? 30_000;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async connect(): Promise<void> {
|
|
59
|
+
this.connected = true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async disconnect(): Promise<void> {
|
|
63
|
+
this.connected = false;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async enqueue(queue: string, message: QueueMessageInterface): Promise<string> {
|
|
67
|
+
const entry: MemoryEntry = {
|
|
68
|
+
message,
|
|
69
|
+
receipt: {queue, id: message.id},
|
|
70
|
+
availableAt: message.availableAt ?? Date.now(),
|
|
71
|
+
locked: false,
|
|
72
|
+
};
|
|
73
|
+
this.list(queue).push(entry);
|
|
74
|
+
return message.id;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async dequeue(queue: string, options: QueueDequeueOptionsInterface = {}): Promise<QueueMessageInterface[]> {
|
|
78
|
+
const blockMs = options.blockMs ?? 0;
|
|
79
|
+
const count = options.count ?? 1;
|
|
80
|
+
const deadline = Date.now() + blockMs;
|
|
81
|
+
|
|
82
|
+
while (true) {
|
|
83
|
+
const messages = this.tryDequeue(queue, count);
|
|
84
|
+
if (messages.length > 0) return messages;
|
|
85
|
+
if (Date.now() >= deadline) return [];
|
|
86
|
+
await wait(Math.min(25, Math.max(0, deadline - Date.now())));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async acknowledge(_queue: string, receipt: MemoryReceipt): Promise<void> {
|
|
91
|
+
const list = this.list(receipt.queue);
|
|
92
|
+
const index = list.findIndex(e => e.receipt.id === receipt.id);
|
|
93
|
+
if (index !== -1) list.splice(index, 1);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async reject(queue: string, receipt: MemoryReceipt, requeue = true): Promise<void> {
|
|
97
|
+
const list = this.list(queue);
|
|
98
|
+
const index = list.findIndex(e => e.receipt.id === receipt.id);
|
|
99
|
+
if (index === -1) return;
|
|
100
|
+
const entry = list[index];
|
|
101
|
+
if (requeue) {
|
|
102
|
+
entry.locked = false;
|
|
103
|
+
entry.availableAt = Date.now();
|
|
104
|
+
entry.message.attempts += 1;
|
|
105
|
+
} else {
|
|
106
|
+
list.splice(index, 1);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async length(queue: string): Promise<number> {
|
|
111
|
+
return this.list(queue).length;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async purge(queue: string): Promise<void> {
|
|
115
|
+
this.list(queue).length = 0;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private list(queue: string): MemoryEntry[] {
|
|
119
|
+
let list = this.queues.get(queue);
|
|
120
|
+
if (!list) {
|
|
121
|
+
list = [];
|
|
122
|
+
this.queues.set(queue, list);
|
|
123
|
+
}
|
|
124
|
+
return list;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private tryDequeue(queue: string, count: number): QueueMessageInterface[] {
|
|
128
|
+
const now = Date.now();
|
|
129
|
+
const list = this.list(queue);
|
|
130
|
+
const result: QueueMessageInterface[] = [];
|
|
131
|
+
|
|
132
|
+
for (const entry of list) {
|
|
133
|
+
if (result.length >= count) break;
|
|
134
|
+
if (entry.locked || entry.availableAt > now) continue;
|
|
135
|
+
entry.locked = true;
|
|
136
|
+
entry.availableAt = now + this.visibilityMs;
|
|
137
|
+
result.push({...entry.message, receipt: entry.receipt});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import {BaseQueueBackend, wait} from "./backend.ts";
|
|
2
|
+
import type {QueueDequeueOptionsInterface, QueueMessageInterface} from "../../../types/index.ts";
|
|
3
|
+
|
|
4
|
+
export interface RabbitMqQueueBackendOptions {
|
|
5
|
+
url?: string;
|
|
6
|
+
connection?: any;
|
|
7
|
+
channel?: any;
|
|
8
|
+
exchange?: string;
|
|
9
|
+
persistent?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* RabbitMQ-backed queue using a single AMQP channel (`sendToQueue` to publish,
|
|
14
|
+
* `get` + manual ack/nack to consume).
|
|
15
|
+
*
|
|
16
|
+
* The `amqplib` package is imported lazily so the framework never requires it
|
|
17
|
+
* unless this backend is actually used:
|
|
18
|
+
*
|
|
19
|
+
* ```ts
|
|
20
|
+
* import {SchedulerManager, RabbitMqQueueBackend} from 'raiton/core';
|
|
21
|
+
* SchedulerManager.current.registerBackend('default', new RabbitMqQueueBackend({url: 'amqp://localhost'}))
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export class RabbitMqQueueBackend extends BaseQueueBackend {
|
|
25
|
+
readonly name = 'rabbitmq';
|
|
26
|
+
private connection: any;
|
|
27
|
+
private channel: any;
|
|
28
|
+
private readonly options: RabbitMqQueueBackendOptions;
|
|
29
|
+
|
|
30
|
+
constructor(options: RabbitMqQueueBackendOptions = {}) {
|
|
31
|
+
super();
|
|
32
|
+
this.options = options;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async connect(): Promise<void> {
|
|
36
|
+
if (this.connected) return;
|
|
37
|
+
if (this.options.channel) {
|
|
38
|
+
this.channel = this.options.channel;
|
|
39
|
+
} else if (this.options.connection) {
|
|
40
|
+
this.connection = this.options.connection;
|
|
41
|
+
this.channel = await this.connection.createChannel();
|
|
42
|
+
} else {
|
|
43
|
+
const amqp = await importAmqplib();
|
|
44
|
+
this.connection = await amqp.connect(this.options.url ?? 'amqp://localhost');
|
|
45
|
+
this.channel = await this.connection.createChannel();
|
|
46
|
+
}
|
|
47
|
+
this.connected = true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async disconnect(): Promise<void> {
|
|
51
|
+
if (!this.connected) return;
|
|
52
|
+
if (typeof this.channel.close === 'function') await this.channel.close();
|
|
53
|
+
if (this.connection && typeof this.connection.close === 'function') await this.connection.close();
|
|
54
|
+
this.connected = false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async enqueue(queue: string, message: QueueMessageInterface): Promise<string> {
|
|
58
|
+
await this.ensureConnected();
|
|
59
|
+
await this.channel.assertQueue(queue, {durable: true});
|
|
60
|
+
this.channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)), {
|
|
61
|
+
persistent: this.options.persistent ?? true,
|
|
62
|
+
});
|
|
63
|
+
return message.id;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async dequeue(queue: string, options: QueueDequeueOptionsInterface = {}): Promise<QueueMessageInterface[]> {
|
|
67
|
+
await this.ensureConnected();
|
|
68
|
+
await this.channel.assertQueue(queue, {durable: true});
|
|
69
|
+
const blockMs = options.blockMs ?? 0;
|
|
70
|
+
const count = options.count ?? 1;
|
|
71
|
+
const deadline = Date.now() + blockMs;
|
|
72
|
+
const results: QueueMessageInterface[] = [];
|
|
73
|
+
|
|
74
|
+
while (results.length < count) {
|
|
75
|
+
const message = await this.channel.get(queue, {noAck: false});
|
|
76
|
+
if (message) {
|
|
77
|
+
const parsed = JSON.parse(message.content.toString()) as QueueMessageInterface;
|
|
78
|
+
parsed.receipt = {channel: this.channel, tag: message.fields.deliveryTag};
|
|
79
|
+
results.push(parsed);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (Date.now() >= deadline) break;
|
|
83
|
+
await wait(Math.min(25, Math.max(0, deadline - Date.now())));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return results;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async acknowledge(_queue: string, receipt: any): Promise<void> {
|
|
90
|
+
await this.ensureConnected();
|
|
91
|
+
receipt.channel.ack(receipt.tag);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async reject(_queue: string, receipt: any, requeue = true): Promise<void> {
|
|
95
|
+
await this.ensureConnected();
|
|
96
|
+
receipt.channel.nack(receipt.tag, false, requeue);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async length(queue: string): Promise<number> {
|
|
100
|
+
await this.ensureConnected();
|
|
101
|
+
const info = await this.channel.checkQueue(queue);
|
|
102
|
+
return info.messageCount;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async purge(queue: string): Promise<void> {
|
|
106
|
+
await this.ensureConnected();
|
|
107
|
+
await this.channel.purgeQueue(queue);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private async ensureConnected(): Promise<void> {
|
|
111
|
+
if (!this.connected) await this.connect();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function importAmqplib(): Promise<any> {
|
|
116
|
+
try {
|
|
117
|
+
// @ts-ignore - optional peer dependency, imported lazily
|
|
118
|
+
return await import('amqplib');
|
|
119
|
+
} catch (e: any) {
|
|
120
|
+
throw new Error(
|
|
121
|
+
'RabbitMqQueueBackend requires the "amqplib" package. Install it with `bun add amqplib` or `npm install amqplib`.',
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import {BaseQueueBackend, wait} from "./backend.ts";
|
|
2
|
+
import type {QueueDequeueOptionsInterface, QueueMessageInterface} from "../../../types/index.ts";
|
|
3
|
+
|
|
4
|
+
export interface RedisQueueBackendOptions {
|
|
5
|
+
url?: string;
|
|
6
|
+
client?: any;
|
|
7
|
+
prefix?: string;
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Redis-backed queue using a reliable list pattern (enqueue with `RPUSH`,
|
|
13
|
+
* dequeue atomically with `LMOVE` to a processing list, ack with `LREM`).
|
|
14
|
+
*
|
|
15
|
+
* The `redis` package is imported lazily so the framework never requires it
|
|
16
|
+
* unless this backend is actually used:
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* import {SchedulerManager, RedisQueueBackend} from 'raiton/core';
|
|
20
|
+
* SchedulerManager.current.registerBackend('default', new RedisQueueBackend({url: 'redis://localhost:6379'}))
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export class RedisQueueBackend extends BaseQueueBackend {
|
|
24
|
+
readonly name = 'redis';
|
|
25
|
+
private client: any;
|
|
26
|
+
private readonly options: RedisQueueBackendOptions;
|
|
27
|
+
private readonly prefix: string;
|
|
28
|
+
|
|
29
|
+
constructor(options: RedisQueueBackendOptions = {}) {
|
|
30
|
+
super();
|
|
31
|
+
this.options = options;
|
|
32
|
+
this.prefix = options.prefix ?? 'raiton:queue:';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async connect(): Promise<void> {
|
|
36
|
+
if (this.connected) return;
|
|
37
|
+
if (this.options.client) {
|
|
38
|
+
this.client = this.options.client;
|
|
39
|
+
} else {
|
|
40
|
+
const redis = await importRedis();
|
|
41
|
+
this.client = redis.createClient({url: this.options.url, ...this.options});
|
|
42
|
+
this.client.on?.('error', (err: any) => {
|
|
43
|
+
// Keep the process alive on transient redis errors.
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (typeof this.client.connect === 'function' && !this.client.isOpen) {
|
|
47
|
+
await this.client.connect();
|
|
48
|
+
}
|
|
49
|
+
this.connected = true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async disconnect(): Promise<void> {
|
|
53
|
+
if (!this.connected) return;
|
|
54
|
+
if (typeof this.client.disconnect === 'function') {
|
|
55
|
+
await this.client.disconnect();
|
|
56
|
+
} else if (typeof this.client.quit === 'function') {
|
|
57
|
+
await this.client.quit();
|
|
58
|
+
}
|
|
59
|
+
this.connected = false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async enqueue(queue: string, message: QueueMessageInterface): Promise<string> {
|
|
63
|
+
await this.ensureConnected();
|
|
64
|
+
const raw = JSON.stringify(message);
|
|
65
|
+
await this.client.rPush(this.key(queue), raw);
|
|
66
|
+
return message.id;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async dequeue(queue: string, options: QueueDequeueOptionsInterface = {}): Promise<QueueMessageInterface[]> {
|
|
70
|
+
await this.ensureConnected();
|
|
71
|
+
const blockMs = options.blockMs ?? 0;
|
|
72
|
+
const count = options.count ?? 1;
|
|
73
|
+
const deadline = Date.now() + blockMs;
|
|
74
|
+
const results: QueueMessageInterface[] = [];
|
|
75
|
+
|
|
76
|
+
while (results.length < count) {
|
|
77
|
+
const raw = await this.client.lMove(
|
|
78
|
+
this.key(queue),
|
|
79
|
+
this.processingKey(queue),
|
|
80
|
+
'RIGHT',
|
|
81
|
+
'LEFT',
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
if (raw !== null && raw !== undefined) {
|
|
85
|
+
const message = JSON.parse(raw) as QueueMessageInterface;
|
|
86
|
+
message.receipt = raw;
|
|
87
|
+
results.push(message);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (Date.now() >= deadline) break;
|
|
92
|
+
await wait(Math.min(25, Math.max(0, deadline - Date.now())));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return results;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async acknowledge(queue: string, receipt: any): Promise<void> {
|
|
99
|
+
await this.ensureConnected();
|
|
100
|
+
await this.client.lRem(this.processingKey(queue), 1, receipt);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async reject(queue: string, receipt: any, requeue = true): Promise<void> {
|
|
104
|
+
await this.ensureConnected();
|
|
105
|
+
if (requeue) {
|
|
106
|
+
await this.client.rPush(this.key(queue), receipt);
|
|
107
|
+
}
|
|
108
|
+
await this.client.lRem(this.processingKey(queue), 1, receipt);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async length(queue: string): Promise<number> {
|
|
112
|
+
await this.ensureConnected();
|
|
113
|
+
return Number(await this.client.lLen(this.key(queue)));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async purge(queue: string): Promise<void> {
|
|
117
|
+
await this.ensureConnected();
|
|
118
|
+
await this.client.del(this.key(queue));
|
|
119
|
+
await this.client.del(this.processingKey(queue));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private key(queue: string): string {
|
|
123
|
+
return `${this.prefix}${queue}`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private processingKey(queue: string): string {
|
|
127
|
+
return `${this.prefix}${queue}:processing`;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private async ensureConnected(): Promise<void> {
|
|
131
|
+
if (!this.connected) await this.connect();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async function importRedis(): Promise<any> {
|
|
136
|
+
try {
|
|
137
|
+
// @ts-ignore - optional peer dependency, imported lazily
|
|
138
|
+
return await import('redis');
|
|
139
|
+
} catch (e: any) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
'RedisQueueBackend requires the "redis" package. Install it with `bun add redis` or `npm install redis`.',
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
}
|
package/source/core/server.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {registerSocket, unregisterSocket, getSocketMetadata} from "./metadata";
|
|
2
|
-
import {Injection} from "../injection";
|
|
1
|
+
import {registerSocket, unregisterSocket, getSocketMetadata} from "./metadata.ts";
|
|
2
|
+
import {Injection} from "../injection/index.ts";
|
|
3
3
|
|
|
4
4
|
export function compileSocket(SocketClass: any) {
|
|
5
5
|
const metadata = getSocketMetadata(SocketClass.prototype || SocketClass)
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./metadata"
|
|
2
|
-
export * from "./builder"
|
|
1
|
+
export * from "./metadata.ts"
|
|
2
|
+
export * from "./builder.ts"
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {ConstructorType, SocketMetaInterface} from "../../types";
|
|
2
|
-
import {METADATA_KEYS} from "../../framework";
|
|
1
|
+
import {ConstructorType, SocketMetaInterface} from "../../types/index.ts";
|
|
2
|
+
import {METADATA_KEYS} from "../../framework/index.ts";
|
|
3
3
|
import "reflect-metadata";
|
|
4
4
|
|
|
5
5
|
export interface SocketRegistryEntryInterface {
|
package/source/core/thread.ts
CHANGED
|
@@ -7,18 +7,20 @@ import type {
|
|
|
7
7
|
ThreadOptionsInterface,
|
|
8
8
|
ThreadSetupOptionsInterface,
|
|
9
9
|
ThreadWaitCallable,
|
|
10
|
-
} from "../types";
|
|
11
|
-
import {EventMessageEnum, RuntimeType} from "../framework/enums";
|
|
10
|
+
} from "../types/index.ts";
|
|
11
|
+
import {EventMessageEnum, RuntimeType} from "../framework/enums/index.ts";
|
|
12
12
|
import {ProcessUtility} from "@protorians/core";
|
|
13
|
-
import {until} from "./process.util";
|
|
14
|
-
import {ApplicationInterface} from "../types/application";
|
|
15
|
-
import {Runtime} from "../framework/runtime";
|
|
13
|
+
import {until} from "./process.util.ts";
|
|
14
|
+
import {ApplicationInterface} from "../types/application.ts";
|
|
15
|
+
import {Runtime} from "../framework/runtime/index.ts";
|
|
16
16
|
import {LBadge, Logger} from "@protorians/logger";
|
|
17
|
-
import {ControllerBuilder} from "./controller";
|
|
18
|
-
import {registerDefaultHealthCheck} from "../framework/health-check";
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
17
|
+
import {ControllerBuilder} from "./controller/index.ts";
|
|
18
|
+
import {registerDefaultHealthCheck} from "../framework/health-check.ts";
|
|
19
|
+
import {SchedulerManager} from "./scheduler/index.ts";
|
|
20
|
+
import {bodyParserPlugin} from "../framework/plugins/body-parser.plugin.ts";
|
|
21
|
+
import {Injection} from "./injection/injection.ts";
|
|
22
|
+
import {Throwable} from "../framework/exceptions/index.ts";
|
|
23
|
+
import {isBunUsed, isDenoUsed, isNodeUsed} from "../bin/constants.ts";
|
|
22
24
|
import os from "os";
|
|
23
25
|
|
|
24
26
|
|
|
@@ -41,16 +43,31 @@ export class RaitonThread implements ThreadInterface {
|
|
|
41
43
|
public readonly builder: BuilderInterface,
|
|
42
44
|
protected _options: ThreadOptionsInterface = {}
|
|
43
45
|
) {
|
|
44
|
-
this.appDir = process.cwd();
|
|
46
|
+
this.appDir = isDenoUsed ? (globalThis as any).Deno.cwd() : process.cwd();
|
|
45
47
|
RaitonThread.instance = this;
|
|
46
48
|
}
|
|
47
49
|
|
|
48
50
|
public restart(): void {
|
|
49
|
-
|
|
51
|
+
if (isDenoUsed) {
|
|
52
|
+
(globalThis as any).Deno.emit?.(EventMessageEnum.RESTART)
|
|
53
|
+
return
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
if (typeof process.send === 'function' && process.connected) {
|
|
57
|
+
process.send(EventMessageEnum.RESTART)
|
|
58
|
+
}
|
|
59
|
+
} catch (e: any) {
|
|
60
|
+
Logger.debug('Restart IPC unavailable', e?.message ?? e)
|
|
61
|
+
}
|
|
50
62
|
}
|
|
51
63
|
|
|
52
64
|
public async stop(): Promise<void> {
|
|
65
|
+
await SchedulerManager.current.stop();
|
|
53
66
|
await Injection.shutdown();
|
|
67
|
+
if (isDenoUsed) {
|
|
68
|
+
(globalThis as any).Deno.exit(0)
|
|
69
|
+
return
|
|
70
|
+
}
|
|
54
71
|
process.exit(0)
|
|
55
72
|
}
|
|
56
73
|
|
|
@@ -75,7 +92,9 @@ export class RaitonThread implements ThreadInterface {
|
|
|
75
92
|
}
|
|
76
93
|
|
|
77
94
|
public setup({application, runtime}: ThreadSetupOptionsInterface): this {
|
|
78
|
-
const defaultRuntime =
|
|
95
|
+
const defaultRuntime = isBunUsed
|
|
96
|
+
? RuntimeType.Bun
|
|
97
|
+
: (isDenoUsed ? RuntimeType.Deno : RuntimeType.Node);
|
|
79
98
|
this.runtime = new Runtime(runtime || defaultRuntime);
|
|
80
99
|
this.application = application;
|
|
81
100
|
this.application.use(bodyParserPlugin())
|
|
@@ -89,19 +108,33 @@ export class RaitonThread implements ThreadInterface {
|
|
|
89
108
|
if (!this.runtime)
|
|
90
109
|
throw new Throwable('Runtime not defined');
|
|
91
110
|
|
|
92
|
-
|
|
93
|
-
|
|
111
|
+
const scanRoot = isNodeUsed
|
|
112
|
+
? (this.builder.out || this.builder.source)
|
|
113
|
+
: this.builder.source;
|
|
114
|
+
|
|
115
|
+
if (scanRoot) {
|
|
116
|
+
await ControllerBuilder.scan(scanRoot)
|
|
94
117
|
registerDefaultHealthCheck(this.application)
|
|
95
118
|
}
|
|
96
119
|
|
|
97
|
-
|
|
98
|
-
process.on('SIGINT', async () => {
|
|
99
|
-
await this.stop();
|
|
100
|
-
});
|
|
120
|
+
await SchedulerManager.current.start();
|
|
101
121
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
122
|
+
if (this._options.serve) {
|
|
123
|
+
if (isDenoUsed) {
|
|
124
|
+
const addSignalListener = (globalThis as any).Deno.addSignalListener;
|
|
125
|
+
if (typeof addSignalListener === 'function') {
|
|
126
|
+
addSignalListener('SIGINT', async () => { await this.stop(); });
|
|
127
|
+
addSignalListener('SIGTERM', async () => { await this.stop(); });
|
|
128
|
+
}
|
|
129
|
+
} else {
|
|
130
|
+
process.on('SIGINT', async () => {
|
|
131
|
+
await this.stop();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
process.on('SIGTERM', async () => {
|
|
135
|
+
await this.stop();
|
|
136
|
+
});
|
|
137
|
+
}
|
|
105
138
|
|
|
106
139
|
const port = this.application.config.port || 5712;
|
|
107
140
|
const hostname = this.application.config.hostname || '0.0.0.0';
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {Injection} from "../core/injection";
|
|
1
|
+
import {Injection} from "../core/injection/index.ts";
|
|
2
2
|
import {Logger} from "@protorians/logger";
|
|
3
|
-
import type {ConstructorType} from "../types";
|
|
4
|
-
import {Raiton} from "../core/raiton";
|
|
5
|
-
import {isArtifact} from "./utilities";
|
|
6
|
-
import {compileMcp} from "../core/mcp/builder";
|
|
3
|
+
import type {ConstructorType} from "../types/index.ts";
|
|
4
|
+
import {Raiton} from "../core/raiton.ts";
|
|
5
|
+
import {isArtifact} from "./utilities/index.ts";
|
|
6
|
+
import {compileMcp} from "../core/mcp/builder.ts";
|
|
7
|
+
import {compileScheduler} from "../core/scheduler/builder.ts";
|
|
7
8
|
|
|
8
9
|
export type HmrChannel =
|
|
9
10
|
| 'hmr:di'
|
|
@@ -13,6 +14,7 @@ export type HmrChannel =
|
|
|
13
14
|
| 'hmr:hook'
|
|
14
15
|
| 'hmr:mcp'
|
|
15
16
|
| 'hmr:health-check'
|
|
17
|
+
| 'hmr:scheduler'
|
|
16
18
|
|
|
17
19
|
export interface ArtifactClassification {
|
|
18
20
|
channel: HmrChannel
|
|
@@ -48,6 +50,7 @@ export const HMR_CHANNELS: Record<HmrChannel, readonly string[]> = {
|
|
|
48
50
|
'hmr:hook': ['hook', 'event', 'listener'],
|
|
49
51
|
'hmr:mcp': ['mcp'],
|
|
50
52
|
'hmr:health-check': ['health-check'],
|
|
53
|
+
'hmr:scheduler': ['scheduler', 'task', 'cron', 'job'],
|
|
51
54
|
}
|
|
52
55
|
|
|
53
56
|
export class Artifacts {
|
|
@@ -84,6 +87,10 @@ export class Artifacts {
|
|
|
84
87
|
'socket',
|
|
85
88
|
'mcp',
|
|
86
89
|
'health-check',
|
|
90
|
+
'scheduler',
|
|
91
|
+
'task',
|
|
92
|
+
'cron',
|
|
93
|
+
'job',
|
|
87
94
|
]
|
|
88
95
|
|
|
89
96
|
static register(type: string) {
|
|
@@ -237,4 +244,24 @@ export class Artifacts {
|
|
|
237
244
|
}
|
|
238
245
|
}
|
|
239
246
|
}
|
|
247
|
+
|
|
248
|
+
static reloadScheduler(modulo: any, filename?: string) {
|
|
249
|
+
|
|
250
|
+
if (Raiton.thread?.builder.options.serve === false)
|
|
251
|
+
return Logger.warn(
|
|
252
|
+
'Artifact reload is only available in development mode'
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
for (const mod of Object.values(modulo)) {
|
|
256
|
+
const name = (mod && typeof mod === 'object' && 'name' in mod) ? mod.name : (
|
|
257
|
+
typeof mod === 'function' ? mod.name ?? mod.constructor.name : undefined
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
if (typeof name === 'string' && typeof mod === 'function') {
|
|
261
|
+
Injection.invalidateCascade(name)
|
|
262
|
+
if (filename) Injection.registerArtifactPath(name, filename)
|
|
263
|
+
compileScheduler(mod)
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
240
267
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import {METADATA_KEYS} from "../constants/decorators.constant.ts";
|
|
3
|
+
import type {CacheOptions} from "./types.ts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Décorateur de route : met en cache la réponse du handler pendant `ttl`.
|
|
7
|
+
*
|
|
8
|
+
* Contrairement aux ETag (validation), le cache évite complètement l'exécution
|
|
9
|
+
* du handler tant que l'entrée est fraîche.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* @Controllable("/products")
|
|
13
|
+
* class ProductController {
|
|
14
|
+
* @Get("/featured")
|
|
15
|
+
* @Cache({ ttl: 60_000, vary: ["accept-language"] })
|
|
16
|
+
* featured() {
|
|
17
|
+
* return this.products.findFeatured()
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
*/
|
|
21
|
+
export function Cache(options: CacheOptions = {}) {
|
|
22
|
+
return (target: any, propertyKey?: string) => {
|
|
23
|
+
if (propertyKey) {
|
|
24
|
+
Reflect.defineMetadata(METADATA_KEYS.CACHE, options, target, propertyKey);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function getCacheMetadata(target: any, propertyKey: string): CacheOptions | undefined {
|
|
30
|
+
return Reflect.getMetadata(METADATA_KEYS.CACHE, target, propertyKey);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function isCached(target: any, propertyKey: string): boolean {
|
|
34
|
+
return Reflect.hasMetadata(METADATA_KEYS.CACHE, target, propertyKey);
|
|
35
|
+
}
|