@zintrust/workers 2.4.2 → 2.4.3
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/dist/dashboard/workers-api.js +128 -2
- package/dist/routes/workers.js +9 -10
- package/package.json +1 -1
- package/src/dashboard/workers-api.ts +186 -2
- package/src/routes/workers.ts +18 -10
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Env, queueConfig } from '@zintrust/core/config';
|
|
1
|
+
import { Cloudflare, Env, queueConfig } from '@zintrust/core/config';
|
|
2
2
|
import { ErrorFactory } from '@zintrust/core/errors';
|
|
3
3
|
import { Logger } from '@zintrust/core/logger';
|
|
4
4
|
import { WorkerFactory } from '../WorkerFactory.js';
|
|
@@ -7,6 +7,120 @@ import { WorkerRegistry } from '../WorkerRegistry.js';
|
|
|
7
7
|
import { maskInfrastructurePasswords } from '../helper/index.js';
|
|
8
8
|
const DEFAULT_PAGE_SIZE = 100;
|
|
9
9
|
const MAX_PAGE_SIZE = 200;
|
|
10
|
+
const shouldUseRedisRpcWorkerApi = () => Cloudflare.getWorkersEnv() !== null &&
|
|
11
|
+
Env.getBool('USE_REDIS_PROXY', false) &&
|
|
12
|
+
Env.get('REDIS_RPC_URL', '').trim().length > 0;
|
|
13
|
+
const createRequestId = () => {
|
|
14
|
+
const cryptoApi = globalThis.crypto;
|
|
15
|
+
if (typeof cryptoApi?.randomUUID === 'function')
|
|
16
|
+
return cryptoApi.randomUUID();
|
|
17
|
+
return `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
18
|
+
};
|
|
19
|
+
const callRedisRpc = async (service, method, payload = {}) => {
|
|
20
|
+
const baseUrl = Env.get('REDIS_RPC_URL', '').trim();
|
|
21
|
+
const endpoint = new URL('/rpc', baseUrl);
|
|
22
|
+
const secret = Env.get('REDIS_RPC_SECRET', Env.get('REDIS_PROXY_SECRET', Env.APP_KEY));
|
|
23
|
+
const response = await fetch(endpoint, {
|
|
24
|
+
method: 'POST',
|
|
25
|
+
headers: {
|
|
26
|
+
'content-type': 'application/json',
|
|
27
|
+
...(secret.trim() === '' ? {} : { 'x-redis-rpc-secret': secret }),
|
|
28
|
+
},
|
|
29
|
+
body: JSON.stringify({ requestId: createRequestId(), service, method, payload }),
|
|
30
|
+
});
|
|
31
|
+
const body = (await response.json().catch(() => ({})));
|
|
32
|
+
if (!response.ok || body.ok !== true) {
|
|
33
|
+
throw ErrorFactory.createTryCatchError(body.error?.message ?? `Redis RPC request failed with status ${response.status}`);
|
|
34
|
+
}
|
|
35
|
+
return body.result;
|
|
36
|
+
};
|
|
37
|
+
const getManifestEntries = () => {
|
|
38
|
+
const manifestGlobal = globalThis;
|
|
39
|
+
const manifest = manifestGlobal.__zintrustWorkersManifest ??
|
|
40
|
+
manifestGlobal.__zintrustWorkerManifest ??
|
|
41
|
+
manifestGlobal.__zintrustAppWorkerManifest ??
|
|
42
|
+
[];
|
|
43
|
+
return Array.isArray(manifest) ? [...manifest] : [];
|
|
44
|
+
};
|
|
45
|
+
const getManifestQueueNames = () => Array.from(new Set(getManifestEntries()
|
|
46
|
+
.map((entry) => entry.queueName)
|
|
47
|
+
.filter((queueName) => typeof queueName === 'string')
|
|
48
|
+
.map((queueName) => queueName.trim())
|
|
49
|
+
.filter(Boolean))).sort((left, right) => left.localeCompare(right));
|
|
50
|
+
const manifestWorkers = () => transformToWorkerData(getManifestEntries()
|
|
51
|
+
.map((entry) => {
|
|
52
|
+
const name = entry.name ?? entry.workerName;
|
|
53
|
+
if (typeof name !== 'string' || name.trim() === '')
|
|
54
|
+
return null;
|
|
55
|
+
return {
|
|
56
|
+
name: name.trim(),
|
|
57
|
+
queueName: typeof entry.queueName === 'string' ? entry.queueName : `${name}-queue`,
|
|
58
|
+
status: normalizeStatus(entry.status ?? 'stopped'),
|
|
59
|
+
autoStart: entry.autoStart ?? false,
|
|
60
|
+
activeStatus: entry.activeStatus ?? true,
|
|
61
|
+
};
|
|
62
|
+
})
|
|
63
|
+
.filter((entry) => entry !== null), 'redis');
|
|
64
|
+
const mergeManifestWorkers = (backendWorkers, manifest = manifestWorkers()) => {
|
|
65
|
+
const manifestQueues = new Set(manifest.map((worker) => worker.queueName));
|
|
66
|
+
const byName = new Map();
|
|
67
|
+
for (const worker of manifest) {
|
|
68
|
+
byName.set(worker.name, worker);
|
|
69
|
+
}
|
|
70
|
+
for (const worker of backendWorkers) {
|
|
71
|
+
const isPlaceholder = worker.name === `${worker.queueName}:redis-rpc` && manifestQueues.has(worker.queueName);
|
|
72
|
+
if (isPlaceholder)
|
|
73
|
+
continue;
|
|
74
|
+
byName.set(worker.name, { ...byName.get(worker.name), ...worker });
|
|
75
|
+
}
|
|
76
|
+
return Array.from(byName.values()).sort((left, right) => left.name.localeCompare(right.name));
|
|
77
|
+
};
|
|
78
|
+
const redisRpcPersistence = async (limit) => {
|
|
79
|
+
const manifest = manifestWorkers();
|
|
80
|
+
try {
|
|
81
|
+
const records = await callRedisRpc('worker', 'list');
|
|
82
|
+
const backendWorkers = transformToWorkerData(records
|
|
83
|
+
.map((record) => {
|
|
84
|
+
const name = record.workerName ?? record.name;
|
|
85
|
+
if (typeof name !== 'string' || name.trim() === '')
|
|
86
|
+
return null;
|
|
87
|
+
return {
|
|
88
|
+
name: name.trim(),
|
|
89
|
+
queueName: record.queueName,
|
|
90
|
+
status: normalizeStatus(record.status ?? 'stopped'),
|
|
91
|
+
autoStart: record.autoStart ?? false,
|
|
92
|
+
details: {
|
|
93
|
+
configuration: {
|
|
94
|
+
processorSpec: record.processorSpec ?? null,
|
|
95
|
+
concurrency: record.concurrency ?? null,
|
|
96
|
+
},
|
|
97
|
+
health: {},
|
|
98
|
+
metrics: {},
|
|
99
|
+
recentLogs: [],
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
})
|
|
103
|
+
.filter((record) => record !== null), 'redis');
|
|
104
|
+
const workers = mergeManifestWorkers(backendWorkers, manifest);
|
|
105
|
+
return {
|
|
106
|
+
workers,
|
|
107
|
+
total: workers.length,
|
|
108
|
+
drivers: ['redis'],
|
|
109
|
+
effectiveLimit: limit,
|
|
110
|
+
prePaginated: false,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
Logger.debug('[getWorkers] Redis RPC persistence unavailable; returning manifest result', error);
|
|
115
|
+
return {
|
|
116
|
+
workers: manifest,
|
|
117
|
+
total: manifest.length,
|
|
118
|
+
drivers: ['redis'],
|
|
119
|
+
effectiveLimit: limit,
|
|
120
|
+
prePaginated: false,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
};
|
|
10
124
|
// Helper for timeout handling
|
|
11
125
|
async function withTimeout(promise, timeoutMs, errorMsg) {
|
|
12
126
|
let timer;
|
|
@@ -27,6 +141,9 @@ async function withTimeout(promise, timeoutMs, errorMsg) {
|
|
|
27
141
|
}
|
|
28
142
|
}
|
|
29
143
|
async function fetchPersistenceWithTimeout(page, limit, query) {
|
|
144
|
+
if (shouldUseRedisRpcWorkerApi()) {
|
|
145
|
+
return redisRpcPersistence(limit);
|
|
146
|
+
}
|
|
30
147
|
const driver = Env.get('WORKER_PERSISTENCE_DRIVER', 'memory');
|
|
31
148
|
try {
|
|
32
149
|
const result = await withTimeout(getWorkersFromPersistence(page, limit, query.driver, query), 5000, 'Persistence timeout');
|
|
@@ -474,6 +591,10 @@ async function getRedisQueueData() {
|
|
|
474
591
|
}
|
|
475
592
|
const monitor = QueueMonitor.create({
|
|
476
593
|
knownQueues: async () => {
|
|
594
|
+
const manifestQueues = getManifestQueueNames();
|
|
595
|
+
if (shouldUseRedisRpcWorkerApi() && manifestQueues.length > 0) {
|
|
596
|
+
return manifestQueues;
|
|
597
|
+
}
|
|
477
598
|
const records = await WorkerFactory.listPersistedRecords();
|
|
478
599
|
return Array.from(new Set(records
|
|
479
600
|
.map((record) => record.queueName)
|
|
@@ -509,7 +630,12 @@ async function getRedisQueueData() {
|
|
|
509
630
|
};
|
|
510
631
|
}
|
|
511
632
|
catch (error) {
|
|
512
|
-
|
|
633
|
+
if (shouldUseRedisRpcWorkerApi()) {
|
|
634
|
+
Logger.debug('Error fetching Redis queue data:', error);
|
|
635
|
+
}
|
|
636
|
+
else {
|
|
637
|
+
Logger.error('Error fetching Redis queue data:', error);
|
|
638
|
+
}
|
|
513
639
|
return {
|
|
514
640
|
driver: 'redis',
|
|
515
641
|
totalQueues: 0,
|
package/dist/routes/workers.js
CHANGED
|
@@ -14,11 +14,12 @@ import { withCreateWorkerValidation, withWorkerOperationValidation, } from '../h
|
|
|
14
14
|
import { WorkerApiController } from '../http/WorkerApiController.js';
|
|
15
15
|
import { WorkerController } from '../http/WorkerController.js';
|
|
16
16
|
import { ResourceMonitor } from '../ResourceMonitor.js';
|
|
17
|
-
import { TelemetryDashboard } from '../telemetry/index.js';
|
|
18
17
|
import { registerStaticAssets } from '../ui/router/ui.js';
|
|
19
18
|
import { WorkerFactory } from '../WorkerFactory.js';
|
|
20
19
|
const controller = WorkerController.create();
|
|
21
20
|
const apiController = WorkerApiController.create();
|
|
21
|
+
const isRedisRpcRuntimeEnabled = () => Env.getBool('USE_REDIS_PROXY', false) && Env.get('REDIS_RPC_URL', '').trim().length > 0;
|
|
22
|
+
const shouldRegisterDashboardApis = () => Cloudflare.getWorkersEnv() === null || isRedisRpcRuntimeEnabled();
|
|
22
23
|
function registerCoreWorkerRoutes(r) {
|
|
23
24
|
// Core worker operations
|
|
24
25
|
Router.post(r, '/create', withCreateWorkerValidation(controller.create));
|
|
@@ -70,11 +71,11 @@ function registerUtilityRoutes(r) {
|
|
|
70
71
|
Router.get(r, '/queue-data', apiController.getQueueDataHandler);
|
|
71
72
|
Router.get(r, '/health', apiController.getHealthSummaryHandler);
|
|
72
73
|
}
|
|
73
|
-
function
|
|
74
|
-
Router.group(router,
|
|
74
|
+
function registerLifecycleGroup(router, basePath, middleware) {
|
|
75
|
+
Router.group(router, basePath, (r) => {
|
|
75
76
|
Logger.info('Registering Worker Management Routes');
|
|
76
77
|
registerCoreWorkerRoutes(r);
|
|
77
|
-
if (
|
|
78
|
+
if (shouldRegisterDashboardApis()) {
|
|
78
79
|
registerMonitoringRoutes(r); // ← Move FIRST - has /events
|
|
79
80
|
registerWorkerQueryRoutes(r);
|
|
80
81
|
registerVersioningRoutes(r);
|
|
@@ -82,6 +83,10 @@ function registerWorkerLifecycleRoutes(router, middleware) {
|
|
|
82
83
|
}
|
|
83
84
|
}, { middleware: middleware });
|
|
84
85
|
}
|
|
86
|
+
function registerWorkerLifecycleRoutes(router, middleware) {
|
|
87
|
+
registerLifecycleGroup(router, '/api/workers', middleware);
|
|
88
|
+
registerLifecycleGroup(router, '/workers/api', middleware);
|
|
89
|
+
}
|
|
85
90
|
function registerWorkerTelemetryRoutes(router, middleware) {
|
|
86
91
|
const options = middleware ? { middleware } : undefined;
|
|
87
92
|
Router.group(router, '/api', (r) => {
|
|
@@ -117,13 +122,7 @@ export function registerWorkerRoutes(router, _options, routeOptions) {
|
|
|
117
122
|
registerStaticAssets(router, routeOptions?.middleware ?? []);
|
|
118
123
|
registerWorkerLifecycleRoutes(router, routeOptions?.middleware);
|
|
119
124
|
registerWorkerTelemetryRoutes(router, routeOptions?.middleware);
|
|
120
|
-
// Register Telemetry Dashboard
|
|
121
|
-
const dashboard = TelemetryDashboard.create({
|
|
122
|
-
basePath: '/telemetry',
|
|
123
|
-
});
|
|
124
|
-
dashboard.registerRoutes(router);
|
|
125
125
|
const port = Env.get('PORT', '7777');
|
|
126
126
|
Logger.info(`Worker routes registered at http://127.0.0.1:${port}/workers`);
|
|
127
|
-
Logger.info(`Telemetry dashboard registered at http://127.0.0.1:${port}/telemetry`);
|
|
128
127
|
}
|
|
129
128
|
export default registerWorkerRoutes;
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Env, queueConfig } from '@zintrust/core/config';
|
|
1
|
+
import { Cloudflare, Env, queueConfig } from '@zintrust/core/config';
|
|
2
2
|
import { ErrorFactory } from '@zintrust/core/errors';
|
|
3
3
|
import { Logger } from '@zintrust/core/logger';
|
|
4
4
|
import { WorkerFactory } from '../WorkerFactory';
|
|
@@ -44,6 +44,177 @@ type PersistenceResult = {
|
|
|
44
44
|
prePaginated: boolean;
|
|
45
45
|
};
|
|
46
46
|
|
|
47
|
+
type RedisRpcWorkerRecord = {
|
|
48
|
+
workerName?: string;
|
|
49
|
+
name?: string;
|
|
50
|
+
queueName?: string;
|
|
51
|
+
status?: string;
|
|
52
|
+
autoStart?: boolean;
|
|
53
|
+
concurrency?: number;
|
|
54
|
+
processorSpec?: string | null;
|
|
55
|
+
source?: string;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
type WorkerManifestEntry = {
|
|
59
|
+
name?: string;
|
|
60
|
+
workerName?: string;
|
|
61
|
+
queueName?: string;
|
|
62
|
+
status?: string;
|
|
63
|
+
autoStart?: boolean;
|
|
64
|
+
activeStatus?: boolean;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
type WorkersManifestGlobal = typeof globalThis & {
|
|
68
|
+
__zintrustWorkersManifest?: ReadonlyArray<WorkerManifestEntry>;
|
|
69
|
+
__zintrustWorkerManifest?: ReadonlyArray<WorkerManifestEntry>;
|
|
70
|
+
__zintrustAppWorkerManifest?: ReadonlyArray<WorkerManifestEntry>;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const shouldUseRedisRpcWorkerApi = (): boolean =>
|
|
74
|
+
Cloudflare.getWorkersEnv() !== null &&
|
|
75
|
+
Env.getBool('USE_REDIS_PROXY', false) &&
|
|
76
|
+
Env.get('REDIS_RPC_URL', '').trim().length > 0;
|
|
77
|
+
|
|
78
|
+
const createRequestId = (): string => {
|
|
79
|
+
const cryptoApi = globalThis.crypto as { randomUUID?: () => string } | undefined;
|
|
80
|
+
if (typeof cryptoApi?.randomUUID === 'function') return cryptoApi.randomUUID();
|
|
81
|
+
return `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const callRedisRpc = async <T = unknown>(
|
|
85
|
+
service: string,
|
|
86
|
+
method: string,
|
|
87
|
+
payload: Record<string, unknown> = {}
|
|
88
|
+
): Promise<T> => {
|
|
89
|
+
const baseUrl = Env.get('REDIS_RPC_URL', '').trim();
|
|
90
|
+
const endpoint = new URL('/rpc', baseUrl);
|
|
91
|
+
const secret = Env.get('REDIS_RPC_SECRET', Env.get('REDIS_PROXY_SECRET', Env.APP_KEY));
|
|
92
|
+
const response = await fetch(endpoint, {
|
|
93
|
+
method: 'POST',
|
|
94
|
+
headers: {
|
|
95
|
+
'content-type': 'application/json',
|
|
96
|
+
...(secret.trim() === '' ? {} : { 'x-redis-rpc-secret': secret }),
|
|
97
|
+
},
|
|
98
|
+
body: JSON.stringify({ requestId: createRequestId(), service, method, payload }),
|
|
99
|
+
});
|
|
100
|
+
const body = (await response.json().catch(() => ({}))) as {
|
|
101
|
+
ok?: boolean;
|
|
102
|
+
result?: T;
|
|
103
|
+
error?: { message?: string };
|
|
104
|
+
};
|
|
105
|
+
if (!response.ok || body.ok !== true) {
|
|
106
|
+
throw ErrorFactory.createTryCatchError(
|
|
107
|
+
body.error?.message ?? `Redis RPC request failed with status ${response.status}`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
return body.result as T;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const getManifestEntries = (): WorkerManifestEntry[] => {
|
|
114
|
+
const manifestGlobal = globalThis as WorkersManifestGlobal;
|
|
115
|
+
const manifest =
|
|
116
|
+
manifestGlobal.__zintrustWorkersManifest ??
|
|
117
|
+
manifestGlobal.__zintrustWorkerManifest ??
|
|
118
|
+
manifestGlobal.__zintrustAppWorkerManifest ??
|
|
119
|
+
[];
|
|
120
|
+
return Array.isArray(manifest) ? [...manifest] : [];
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const getManifestQueueNames = (): string[] =>
|
|
124
|
+
Array.from(
|
|
125
|
+
new Set(
|
|
126
|
+
getManifestEntries()
|
|
127
|
+
.map((entry) => entry.queueName)
|
|
128
|
+
.filter((queueName): queueName is string => typeof queueName === 'string')
|
|
129
|
+
.map((queueName) => queueName.trim())
|
|
130
|
+
.filter(Boolean)
|
|
131
|
+
)
|
|
132
|
+
).sort((left, right) => left.localeCompare(right));
|
|
133
|
+
|
|
134
|
+
const manifestWorkers = (): WorkerData[] =>
|
|
135
|
+
transformToWorkerData(
|
|
136
|
+
getManifestEntries()
|
|
137
|
+
.map((entry): RawWorkerData | null => {
|
|
138
|
+
const name = entry.name ?? entry.workerName;
|
|
139
|
+
if (typeof name !== 'string' || name.trim() === '') return null;
|
|
140
|
+
return {
|
|
141
|
+
name: name.trim(),
|
|
142
|
+
queueName: typeof entry.queueName === 'string' ? entry.queueName : `${name}-queue`,
|
|
143
|
+
status: normalizeStatus(entry.status ?? 'stopped'),
|
|
144
|
+
autoStart: entry.autoStart ?? false,
|
|
145
|
+
activeStatus: entry.activeStatus ?? true,
|
|
146
|
+
};
|
|
147
|
+
})
|
|
148
|
+
.filter((entry): entry is RawWorkerData => entry !== null),
|
|
149
|
+
'redis'
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
const mergeManifestWorkers = (
|
|
153
|
+
backendWorkers: WorkerData[],
|
|
154
|
+
manifest: WorkerData[] = manifestWorkers()
|
|
155
|
+
): WorkerData[] => {
|
|
156
|
+
const manifestQueues = new Set(manifest.map((worker) => worker.queueName));
|
|
157
|
+
const byName = new Map<string, WorkerData>();
|
|
158
|
+
for (const worker of manifest) {
|
|
159
|
+
byName.set(worker.name, worker);
|
|
160
|
+
}
|
|
161
|
+
for (const worker of backendWorkers) {
|
|
162
|
+
const isPlaceholder =
|
|
163
|
+
worker.name === `${worker.queueName}:redis-rpc` && manifestQueues.has(worker.queueName);
|
|
164
|
+
if (isPlaceholder) continue;
|
|
165
|
+
byName.set(worker.name, { ...byName.get(worker.name), ...worker });
|
|
166
|
+
}
|
|
167
|
+
return Array.from(byName.values()).sort((left, right) => left.name.localeCompare(right.name));
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const redisRpcPersistence = async (limit: number): Promise<PersistenceResult> => {
|
|
171
|
+
const manifest = manifestWorkers();
|
|
172
|
+
try {
|
|
173
|
+
const records = await callRedisRpc<RedisRpcWorkerRecord[]>('worker', 'list');
|
|
174
|
+
const backendWorkers = transformToWorkerData(
|
|
175
|
+
records
|
|
176
|
+
.map((record): RawWorkerData | null => {
|
|
177
|
+
const name = record.workerName ?? record.name;
|
|
178
|
+
if (typeof name !== 'string' || name.trim() === '') return null;
|
|
179
|
+
return {
|
|
180
|
+
name: name.trim(),
|
|
181
|
+
queueName: record.queueName,
|
|
182
|
+
status: normalizeStatus(record.status ?? 'stopped'),
|
|
183
|
+
autoStart: record.autoStart ?? false,
|
|
184
|
+
details: {
|
|
185
|
+
configuration: {
|
|
186
|
+
processorSpec: record.processorSpec ?? null,
|
|
187
|
+
concurrency: record.concurrency ?? null,
|
|
188
|
+
},
|
|
189
|
+
health: {} as WorkerHealth,
|
|
190
|
+
metrics: {} as WorkerMetrics,
|
|
191
|
+
recentLogs: [],
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
})
|
|
195
|
+
.filter((record): record is RawWorkerData => record !== null),
|
|
196
|
+
'redis'
|
|
197
|
+
);
|
|
198
|
+
const workers = mergeManifestWorkers(backendWorkers, manifest);
|
|
199
|
+
return {
|
|
200
|
+
workers,
|
|
201
|
+
total: workers.length,
|
|
202
|
+
drivers: ['redis'],
|
|
203
|
+
effectiveLimit: limit,
|
|
204
|
+
prePaginated: false,
|
|
205
|
+
};
|
|
206
|
+
} catch (error) {
|
|
207
|
+
Logger.debug('[getWorkers] Redis RPC persistence unavailable; returning manifest result', error);
|
|
208
|
+
return {
|
|
209
|
+
workers: manifest,
|
|
210
|
+
total: manifest.length,
|
|
211
|
+
drivers: ['redis'],
|
|
212
|
+
effectiveLimit: limit,
|
|
213
|
+
prePaginated: false,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
47
218
|
// Helper for timeout handling
|
|
48
219
|
async function withTimeout<T>(
|
|
49
220
|
promise: Promise<T>,
|
|
@@ -72,6 +243,10 @@ async function fetchPersistenceWithTimeout(
|
|
|
72
243
|
limit: number,
|
|
73
244
|
query: GetWorkersQuery
|
|
74
245
|
): Promise<PersistenceResult> {
|
|
246
|
+
if (shouldUseRedisRpcWorkerApi()) {
|
|
247
|
+
return redisRpcPersistence(limit);
|
|
248
|
+
}
|
|
249
|
+
|
|
75
250
|
const driver = Env.get('WORKER_PERSISTENCE_DRIVER', 'memory');
|
|
76
251
|
try {
|
|
77
252
|
const result = await withTimeout(
|
|
@@ -625,6 +800,11 @@ async function getRedisQueueData(): Promise<QueueData> {
|
|
|
625
800
|
|
|
626
801
|
const monitor = QueueMonitor.create({
|
|
627
802
|
knownQueues: async () => {
|
|
803
|
+
const manifestQueues = getManifestQueueNames();
|
|
804
|
+
if (shouldUseRedisRpcWorkerApi() && manifestQueues.length > 0) {
|
|
805
|
+
return manifestQueues;
|
|
806
|
+
}
|
|
807
|
+
|
|
628
808
|
const records = await WorkerFactory.listPersistedRecords();
|
|
629
809
|
return Array.from(
|
|
630
810
|
new Set(
|
|
@@ -666,7 +846,11 @@ async function getRedisQueueData(): Promise<QueueData> {
|
|
|
666
846
|
failedJobs,
|
|
667
847
|
};
|
|
668
848
|
} catch (error) {
|
|
669
|
-
|
|
849
|
+
if (shouldUseRedisRpcWorkerApi()) {
|
|
850
|
+
Logger.debug('Error fetching Redis queue data:', error);
|
|
851
|
+
} else {
|
|
852
|
+
Logger.error('Error fetching Redis queue data:', error);
|
|
853
|
+
}
|
|
670
854
|
return {
|
|
671
855
|
driver: 'redis',
|
|
672
856
|
totalQueues: 0,
|
package/src/routes/workers.ts
CHANGED
|
@@ -20,7 +20,6 @@ import {
|
|
|
20
20
|
import { WorkerApiController } from '../http/WorkerApiController';
|
|
21
21
|
import { WorkerController } from '../http/WorkerController';
|
|
22
22
|
import { ResourceMonitor } from '../ResourceMonitor';
|
|
23
|
-
import { TelemetryDashboard } from '../telemetry';
|
|
24
23
|
import { registerStaticAssets } from '../ui/router/ui';
|
|
25
24
|
import { WorkerFactory } from '../WorkerFactory';
|
|
26
25
|
type WorkerUiOptions = WorkersDashboardUiOptions;
|
|
@@ -29,6 +28,12 @@ type RouteOptions = { middleware?: ReadonlyArray<string> } | undefined;
|
|
|
29
28
|
const controller = WorkerController.create();
|
|
30
29
|
const apiController = WorkerApiController.create();
|
|
31
30
|
|
|
31
|
+
const isRedisRpcRuntimeEnabled = (): boolean =>
|
|
32
|
+
Env.getBool('USE_REDIS_PROXY', false) && Env.get('REDIS_RPC_URL', '').trim().length > 0;
|
|
33
|
+
|
|
34
|
+
const shouldRegisterDashboardApis = (): boolean =>
|
|
35
|
+
Cloudflare.getWorkersEnv() === null || isRedisRpcRuntimeEnabled();
|
|
36
|
+
|
|
32
37
|
function registerCoreWorkerRoutes(r: IRouter): void {
|
|
33
38
|
// Core worker operations
|
|
34
39
|
Router.post(r, '/create', withCreateWorkerValidation(controller.create));
|
|
@@ -130,14 +135,18 @@ function registerUtilityRoutes(r: IRouter): void {
|
|
|
130
135
|
Router.get(r, '/health', apiController.getHealthSummaryHandler);
|
|
131
136
|
}
|
|
132
137
|
|
|
133
|
-
function
|
|
138
|
+
function registerLifecycleGroup(
|
|
139
|
+
router: IRouter,
|
|
140
|
+
basePath: string,
|
|
141
|
+
middleware?: ReadonlyArray<string>
|
|
142
|
+
): void {
|
|
134
143
|
Router.group(
|
|
135
144
|
router,
|
|
136
|
-
|
|
145
|
+
basePath,
|
|
137
146
|
(r: IRouter) => {
|
|
138
147
|
Logger.info('Registering Worker Management Routes');
|
|
139
148
|
registerCoreWorkerRoutes(r);
|
|
140
|
-
if (
|
|
149
|
+
if (shouldRegisterDashboardApis()) {
|
|
141
150
|
registerMonitoringRoutes(r); // ← Move FIRST - has /events
|
|
142
151
|
registerWorkerQueryRoutes(r);
|
|
143
152
|
registerVersioningRoutes(r);
|
|
@@ -148,6 +157,11 @@ function registerWorkerLifecycleRoutes(router: IRouter, middleware?: ReadonlyArr
|
|
|
148
157
|
);
|
|
149
158
|
}
|
|
150
159
|
|
|
160
|
+
function registerWorkerLifecycleRoutes(router: IRouter, middleware?: ReadonlyArray<string>): void {
|
|
161
|
+
registerLifecycleGroup(router, '/api/workers', middleware);
|
|
162
|
+
registerLifecycleGroup(router, '/workers/api', middleware);
|
|
163
|
+
}
|
|
164
|
+
|
|
151
165
|
function registerWorkerTelemetryRoutes(router: IRouter, middleware?: ReadonlyArray<string>): void {
|
|
152
166
|
const options = middleware ? { middleware } : undefined;
|
|
153
167
|
|
|
@@ -203,14 +217,8 @@ export function registerWorkerRoutes(
|
|
|
203
217
|
registerWorkerLifecycleRoutes(router, routeOptions?.middleware);
|
|
204
218
|
registerWorkerTelemetryRoutes(router, routeOptions?.middleware);
|
|
205
219
|
|
|
206
|
-
// Register Telemetry Dashboard
|
|
207
|
-
const dashboard = TelemetryDashboard.create({
|
|
208
|
-
basePath: '/telemetry',
|
|
209
|
-
});
|
|
210
|
-
dashboard.registerRoutes(router);
|
|
211
220
|
const port = Env.get('PORT', '7777');
|
|
212
221
|
Logger.info(`Worker routes registered at http://127.0.0.1:${port}/workers`);
|
|
213
|
-
Logger.info(`Telemetry dashboard registered at http://127.0.0.1:${port}/telemetry`);
|
|
214
222
|
}
|
|
215
223
|
|
|
216
224
|
export default registerWorkerRoutes;
|