@getstrata/core 2.2.60 → 3.0.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/CHANGELOG.md +22 -0
- package/dist/core/audit/exportAuditLogs.d.ts +12 -3
- package/dist/core/audit/exportStore.d.ts +6 -2
- package/dist/core/audit/siemFormatter.d.ts +2 -2
- package/dist/core/auth/abilityCatalog.d.ts +1 -9
- package/dist/core/auth/authContext.d.ts +2 -0
- package/dist/core/auth/authOutcome.d.ts +32 -0
- package/dist/core/auth/authReader.d.ts +22 -0
- package/dist/core/auth/basicAuthGuard.d.ts +19 -3
- package/dist/core/auth/emailVerification.d.ts +1 -1
- package/dist/core/auth/guard.d.ts +27 -6
- package/dist/core/auth/intendedUrlCookie.d.ts +13 -7
- package/dist/core/auth/jwt.d.ts +5 -4
- package/dist/core/auth/jwtGuard.d.ts +7 -6
- package/dist/core/auth/membershipContext.d.ts +10 -7
- package/dist/core/auth/membershipContextMiddleware.d.ts +3 -3
- package/dist/core/auth/membershipMiddleware.d.ts +1 -2
- package/dist/core/auth/membershipServiceImplementation.d.ts +1 -1
- package/dist/core/auth/oauth/oidcIdToken.d.ts +25 -16
- package/dist/core/auth/oauth/oidcProvider.d.ts +6 -4
- package/dist/core/auth/oauth/providers.d.ts +3 -1
- package/dist/core/auth/oneTimeToken.d.ts +2 -3
- package/dist/core/auth/passwordConfirmCookie.d.ts +15 -6
- package/dist/core/auth/passwordLogin.d.ts +5 -5
- package/dist/core/auth/pendingMfaCookie.d.ts +2 -2
- package/dist/core/auth/policy.d.ts +23 -10
- package/dist/core/auth/requireAuthCapability.d.ts +3 -0
- package/dist/core/auth/resolveMembershipService.d.ts +3 -2
- package/dist/core/auth/saml/samlAssertionReplay.d.ts +4 -4
- package/dist/core/auth/saml/samlServiceProvider.d.ts +11 -7
- package/dist/core/auth/scimAuthMiddleware.d.ts +10 -3
- package/dist/core/auth/sessionCookie.d.ts +20 -9
- package/dist/core/auth/sessionGuard.d.ts +14 -3
- package/dist/core/cache/createCacheStore.d.ts +6 -3
- package/dist/core/cache/redisCacheStore.d.ts +5 -1
- package/dist/core/cache/repository.d.ts +5 -1
- package/dist/core/cache/simpleCache.d.ts +2 -1
- package/dist/core/cache/tokens.d.ts +8 -0
- package/dist/core/config/applicationConfig.d.ts +21 -0
- package/dist/core/config/configSnapshot.d.ts +3 -0
- package/dist/core/config/envSchema.d.ts +3 -2
- package/dist/core/contracts/application.d.ts +153 -0
- package/dist/core/contracts/authUserDirectory.d.ts +24 -7
- package/dist/core/contracts/container.d.ts +20 -23
- package/dist/core/contracts/di.d.ts +4 -2
- package/dist/core/contracts/executionContext.d.ts +5 -0
- package/dist/core/contracts/executionOperations.d.ts +10 -0
- package/dist/core/contracts/executionOwnership.d.ts +38 -0
- package/dist/core/contracts/persistence.d.ts +20 -0
- package/dist/core/contracts/serviceTokens.d.ts +32 -18
- package/dist/core/contracts/services.d.ts +63 -0
- package/dist/core/contracts/token.d.ts +19 -0
- package/dist/core/crypto/fieldEncryption.d.ts +19 -7
- package/dist/core/crypto/fieldEncryptionRotation.d.ts +21 -5
- package/dist/core/crypto/mfaSecret.d.ts +10 -3
- package/dist/core/database/baseRepository.d.ts +14 -10
- package/dist/core/database/bindings.d.ts +6 -0
- package/dist/core/database/bunSql.d.ts +3 -12
- package/dist/core/database/definitions.d.ts +209 -0
- package/dist/core/database/dialect.d.ts +5 -24
- package/dist/core/database/driverPorts.d.ts +116 -0
- package/dist/core/database/errors.d.ts +47 -7
- package/dist/core/database/factory.d.ts +51 -31
- package/dist/core/database/index.d.ts +9 -10
- package/dist/core/database/keyset.d.ts +2 -1
- package/dist/core/database/legacyNativeTransaction.d.ts +5 -0
- package/dist/core/database/manager.d.ts +12 -0
- package/dist/core/database/model.d.ts +91 -97
- package/dist/core/database/modelQuery.d.ts +9 -5
- package/dist/core/database/modelRegistry.d.ts +52 -33
- package/dist/core/database/mysqlConnection.d.ts +1 -19
- package/dist/core/database/mysqlNative.d.ts +38 -0
- package/dist/core/database/nativeTransactions.d.ts +16 -0
- package/dist/core/database/ormInternal.d.ts +85 -0
- package/dist/core/database/ormRuntime.d.ts +4 -0
- package/dist/core/database/ownedReadOnlyObservation.d.ts +16 -0
- package/dist/core/database/postgresNative.d.ts +11 -0
- package/dist/core/database/query.d.ts +24 -23
- package/dist/core/database/queryPlanSnapshot.d.ts +7 -0
- package/dist/core/database/rawMysql.d.ts +7 -0
- package/dist/core/database/rawPostgres.d.ts +23 -0
- package/dist/core/database/rawSqlite.d.ts +6 -0
- package/dist/core/database/relationQueries.d.ts +3 -2
- package/dist/core/database/relationQuery.d.ts +24 -6
- package/dist/core/database/repositoryQuery.d.ts +6 -1
- package/dist/core/database/schema/driver.d.ts +1 -1
- package/dist/core/database/seeders/types.d.ts +1 -1
- package/dist/core/database/session.d.ts +13 -0
- package/dist/core/database/sessionFactory.d.ts +9 -0
- package/dist/core/database/sqlDialects.d.ts +22 -0
- package/dist/core/database/tokens.d.ts +7 -0
- package/dist/core/database/transactionSession.d.ts +43 -0
- package/dist/core/errors/http.d.ts +4 -1
- package/dist/core/events/{eventBus.d.ts → eventBusClass.d.ts} +1 -3
- package/dist/core/events/index.d.ts +2 -3
- package/dist/core/events/outbox/index.d.ts +44 -13
- package/dist/core/events/outbox/metrics.d.ts +4 -4
- package/dist/core/events/outbox/workerTransport.d.ts +17 -0
- package/dist/core/http/authMiddleware.d.ts +2 -2
- package/dist/core/http/authorizeMiddleware.d.ts +2 -2
- package/dist/core/http/bodySizeLimitMiddleware.d.ts +1 -1
- package/dist/core/http/conditionalResponse.d.ts +7 -2
- package/dist/core/http/contentSecurityPolicy.d.ts +4 -3
- package/dist/core/http/corsMiddleware.d.ts +2 -1
- package/dist/core/http/csrfMiddleware.d.ts +6 -1
- package/dist/core/http/csrfToken.d.ts +17 -7
- package/dist/core/http/databaseError.d.ts +4 -0
- package/dist/core/http/etag.d.ts +5 -2
- package/dist/core/http/flashMiddleware.d.ts +1 -1
- package/dist/core/http/flashSession.d.ts +13 -7
- package/dist/core/http/index.d.ts +1 -1
- package/dist/core/http/loginThrottleMiddleware.d.ts +3 -3
- package/dist/core/http/memoryThrottleMiddleware.d.ts +2 -2
- package/dist/core/http/parseMultipartUpload.d.ts +3 -2
- package/dist/core/http/requestMetaContext.d.ts +5 -0
- package/dist/core/http/requireAbilityMiddleware.d.ts +1 -1
- package/dist/core/http/requireAuthMiddleware.d.ts +2 -2
- package/dist/core/http/requirePasswordConfirmMiddleware.d.ts +2 -1
- package/dist/core/http/requireVerifiedMiddleware.d.ts +2 -2
- package/dist/core/http/requireWebAuthMiddleware.d.ts +2 -2
- package/dist/core/http/response.d.ts +4 -3
- package/dist/core/http/scimThrottleMiddleware.d.ts +4 -3
- package/dist/core/http/securedRouteModelBinding.d.ts +10 -3
- package/dist/core/http/securityHeadersMiddleware.d.ts +1 -2
- package/dist/core/http/signedUrl.d.ts +8 -6
- package/dist/core/http/throttleMiddleware.d.ts +9 -5
- package/dist/core/http/uploads.d.ts +9 -3
- package/dist/core/http/webErrorResponse.d.ts +8 -2
- package/dist/core/jobs/exportAuditLogsJob.d.ts +5 -2
- package/dist/core/jobs/invalidateCacheTagsJob.d.ts +5 -4
- package/dist/core/lifecycle/gracefulShutdown.d.ts +2 -5
- package/dist/core/logging/requestLoggingMiddleware.d.ts +1 -1
- package/dist/core/mail/mailer.d.ts +7 -5
- package/dist/core/openapi/generator.d.ts +12 -4
- package/dist/core/openapi/registeredRoute.d.ts +4 -4
- package/dist/core/queue/createAppQueue.d.ts +9 -4
- package/dist/core/queue/failedJobMetrics.d.ts +11 -2
- package/dist/core/queue/failedJobRepository.d.ts +6 -3
- package/dist/core/queue/failedJobService.d.ts +1 -0
- package/dist/core/queue/index.d.ts +16 -6
- package/dist/core/queue/jobRegistry.d.ts +3 -2
- package/dist/core/queue/jobRunner.d.ts +10 -4
- package/dist/core/queue/publicQueue.d.ts +18 -9
- package/dist/core/queue/queueConfig.d.ts +20 -10
- package/dist/core/queue/queueHandoff.d.ts +31 -0
- package/dist/core/queue/queueMetrics.d.ts +10 -3
- package/dist/core/queue/queueServices.d.ts +28 -0
- package/dist/core/queue/queueWorkerTransport.d.ts +28 -0
- package/dist/core/queue/redisQueue.d.ts +19 -10
- package/dist/core/queue/redisQueueKeys.d.ts +5 -5
- package/dist/core/queue/redisStreams.d.ts +17 -10
- package/dist/core/queue/resilientQueue.d.ts +10 -4
- package/dist/core/queue/types.d.ts +1 -0
- package/dist/core/runtime/appKeyPrefix.d.ts +5 -5
- package/dist/core/runtime/application.d.ts +7 -0
- package/dist/core/runtime/executionContext.d.ts +9 -0
- package/dist/core/runtime/executionTasks.d.ts +49 -0
- package/dist/core/runtime/resourceJournal.d.ts +67 -0
- package/dist/core/runtime/serviceGraph.d.ts +14 -0
- package/dist/core/security/oauthState.d.ts +13 -9
- package/dist/core/security/publicReads.d.ts +2 -2
- package/dist/core/security/safeFetch.d.ts +1 -0
- package/dist/core/security/safeUrl.d.ts +3 -0
- package/dist/core/security/scimTenantTokens.d.ts +1 -1
- package/dist/core/security/tokenExpiry.d.ts +1 -1
- package/dist/core/security/webhookSignature.d.ts +1 -1
- package/dist/core/storage/storage.d.ts +24 -16
- package/dist/core/tenant/databaseTenantContext.d.ts +5 -7
- package/dist/core/tenant/resolveTenant.d.ts +2 -2
- package/dist/core/tenant/scopedDatabasePolicy.d.ts +8 -0
- package/dist/core/tenant/tenantDatabaseScope.d.ts +3 -7
- package/dist/core/tenant/tenantMiddleware.d.ts +15 -4
- package/dist/core/tracing/otel.d.ts +4 -6
- package/dist/core/tracing/tracingMiddleware.d.ts +3 -3
- package/dist/core/view/etaViewEngine.d.ts +1 -1
- package/dist/core/view/index.d.ts +5 -4
- package/dist/core/view/tokens.d.ts +8 -0
- package/dist/core/view/viewRenderer.d.ts +2 -2
- package/dist/core/view/webErrorView.d.ts +8 -7
- package/dist/core/view/webLayoutData.d.ts +6 -4
- package/dist/entries/audit/exportAuditLogs.js +87 -1024
- package/dist/entries/audit/siemFormatter.js +4 -90
- package/dist/entries/auth/abilityCatalog.js +28 -0
- package/dist/entries/auth/accessControl.js +5 -16
- package/dist/entries/auth/accountStateMigration.js +216 -236
- package/dist/entries/auth/authOutcome.js +80 -0
- package/dist/entries/auth/authReader.js +68 -0
- package/dist/entries/auth/basicAuthGuard.js +144 -1249
- package/dist/entries/auth/emailVerification.js +2 -2
- package/dist/entries/auth/intendedUrlCookie.js +35 -59
- package/dist/entries/auth/jwt.js +22 -47
- package/dist/entries/auth/jwtGuard.js +99 -231
- package/dist/entries/auth/membershipMiddleware.js +71 -10
- package/dist/entries/auth/membershipScope.js +16 -22
- package/dist/entries/auth/membershipService.js +11 -28
- package/dist/entries/auth/oauth/oidcProvider.js +59 -64
- package/dist/entries/auth/oauth/providers.js +12 -95
- package/dist/entries/auth/oauth/samlProvider.js +53 -1080
- package/dist/entries/auth/oneTimeToken.js +27 -289
- package/dist/entries/auth/passwordConfirmCookie.js +40 -63
- package/dist/entries/auth/passwordLogin.js +94 -399
- package/dist/entries/auth/pendingMfaCookie.js +6 -6
- package/dist/entries/auth/policy.js +22 -56
- package/dist/entries/auth/saml/samlServiceProvider.js +55 -1084
- package/dist/entries/auth/scimAuthMiddleware.js +16 -3358
- package/dist/entries/auth/sessionCookie.js +58 -73
- package/dist/entries/auth/sessionGuard.js +95 -248
- package/dist/entries/auth/tokenHash.js +11 -43
- package/dist/entries/cache/createCacheStore.js +126 -117
- package/dist/entries/cache/repository.js +25 -0
- package/dist/entries/cache/simpleCache.js +12 -7
- package/dist/entries/cache/tokens.js +9 -0
- package/dist/entries/config/applicationConfig.js +163 -0
- package/dist/entries/config/envSchema.js +21 -4
- package/dist/entries/contracts/application.js +1 -0
- package/dist/entries/contracts/authUserDirectory.js +10 -0
- package/dist/entries/contracts/container.js +99 -26
- package/dist/entries/contracts/di.js +3 -0
- package/dist/entries/contracts/executionContext.js +7 -0
- package/dist/entries/contracts/executionOperations.js +7 -0
- package/dist/entries/contracts/executionOwnership.js +7 -0
- package/dist/entries/contracts/membership.js +1 -0
- package/dist/entries/contracts/persistence.js +1 -0
- package/dist/entries/contracts/serviceTokens.js +26 -116
- package/dist/entries/contracts/services.js +98 -0
- package/dist/entries/contracts/token.js +41 -0
- package/dist/entries/crypto/fieldEncryption.js +49 -50
- package/dist/entries/crypto/mfaSecret.js +86 -82
- package/dist/entries/database/acquisitionMetrics.js +107 -0
- package/dist/entries/database/baseRepository.d.ts +2 -0
- package/dist/entries/database/bindings.js +2082 -0
- package/dist/entries/database/definitions.js +175 -0
- package/dist/entries/database/errors.js +114 -89
- package/dist/entries/database/factory.js +67 -28
- package/dist/entries/database/migrations.js +172 -224
- package/dist/entries/database/model.d.ts +1 -0
- package/dist/entries/database/ownedReadOnlyObservation.js +1282 -0
- package/dist/entries/database/query.js +159 -161
- package/dist/entries/database/repositoryQuery.d.ts +1 -0
- package/dist/entries/database/repositoryQuery.js +1 -1373
- package/dist/entries/database/schema.js +216 -236
- package/dist/entries/database/session.js +1 -0
- package/dist/entries/database/sqliteConnection.js +525 -83
- package/dist/entries/database/tokens.js +9 -0
- package/dist/entries/errors/http.js +118 -1
- package/dist/entries/http/authMiddleware.js +8 -5
- package/dist/entries/http/authorizeMiddleware.js +1 -1
- package/dist/entries/http/bodySizeLimitMiddleware.js +2 -2
- package/dist/entries/http/clientIp.js +2 -2
- package/dist/entries/http/conditionalResponse.js +19 -7
- package/dist/entries/http/corsMiddleware.js +6 -101
- package/dist/entries/http/csrfMiddleware.js +92 -106
- package/dist/entries/http/csrfToken.js +58 -94
- package/dist/entries/http/etag.js +7 -4
- package/dist/entries/http/flashMiddleware.js +57 -99
- package/dist/entries/http/flashSession.js +41 -68
- package/dist/entries/http/memoryThrottleMiddleware.js +43 -186
- package/dist/entries/http/metricsMiddleware.js +2 -16
- package/dist/entries/http/parseMultipartUpload.js +15 -29
- package/dist/entries/http/requireAbilityMiddleware.js +2 -2
- package/dist/entries/http/requirePasswordConfirmMiddleware.js +19 -31
- package/dist/entries/http/requireVerifiedMiddleware.js +4 -7
- package/dist/entries/http/requireWebAuthMiddleware.js +2 -2
- package/dist/entries/http/response.js +67 -233
- package/dist/entries/http/routeMiddleware.js +0 -14
- package/dist/entries/http/scimThrottleMiddleware.js +43 -237
- package/dist/entries/http/securedRouteModelBinding.js +37 -25
- package/dist/entries/http/securityHeadersMiddleware.js +8 -25
- package/dist/entries/http/signedUrl.js +20 -110
- package/dist/entries/http/throttleMiddleware.js +41 -185
- package/dist/entries/http/uploads.js +9 -24
- package/dist/entries/jobs/exportAuditLogsJob.js +17 -1498
- package/dist/entries/jobs/invalidateCacheTagsJob.js +12 -47
- package/dist/entries/logging/requestLoggingMiddleware.js +4 -4
- package/dist/entries/mail/mailer.js +68 -78
- package/dist/entries/openapi/generator.js +33 -100
- package/dist/entries/queue/createAppQueue.js +416 -256
- package/dist/entries/queue/failedJobRepository.js +17 -8
- package/dist/entries/queue/failedJobService.js +1 -0
- package/dist/entries/queue/jobRunner.js +141 -66
- package/dist/entries/queue/publicQueue.js +411 -257
- package/dist/entries/queue/queueConfig.js +61 -0
- package/dist/entries/queue/queueHandoff.js +318 -0
- package/dist/entries/queue/queueMetrics.js +989 -1386
- package/dist/entries/queue/queueServices.js +133 -0
- package/dist/entries/queue/queueWorkerTransport.js +1703 -0
- package/dist/entries/queue/redisQueue.js +282 -180
- package/dist/entries/queue.js +6 -45
- package/dist/entries/runtime/application.js +1585 -0
- package/dist/entries/runtime/asyncContextStore.js +0 -16
- package/dist/entries/security/oauthState.js +35 -67
- package/dist/entries/security/publicReads.js +4 -4
- package/dist/entries/security/safeFetch.js +1 -0
- package/dist/entries/security/scimTenantTokens.js +3 -3
- package/dist/entries/security/tokenExpiry.js +2 -2
- package/dist/entries/security/totp.js +1 -87
- package/dist/entries/security/webhookSignature.js +2 -90
- package/dist/entries/storage/storage.js +120 -45
- package/dist/entries/tenant/databaseTenantContext.js +6 -907
- package/dist/entries/tenant/publicTenantHost.js +78 -0
- package/dist/entries/tenant/resolveTenant.js +14 -0
- package/dist/entries/tenant/scopedDatabasePolicy.js +75 -0
- package/dist/entries/tenant/tenancyConfig.js +1 -19
- package/dist/entries/tenant/tenantDatabaseScope.js +3 -892
- package/dist/entries/tracing/tracingMiddleware.js +104 -173
- package/dist/entries/view/tokens.js +11 -0
- package/dist/framework/public-api.d.ts +62 -44
- package/dist/index.js +10435 -11351
- package/dist/internal/ormInternal.js +117 -0
- package/dist/internal/ormRuntime.js +5000 -0
- package/package.json +158 -46
- package/dist/core/database/bindConnection.d.ts +0 -1
- package/dist/core/database/boundConnection.d.ts +0 -5
- package/dist/core/database/defaultConnection.d.ts +0 -7
- package/dist/core/database/namedConnections.d.ts +0 -15
- package/dist/core/database/readOnlyCollector.d.ts +0 -7
- package/dist/core/database/repositoryConnection.d.ts +0 -4
- package/dist/core/database/transaction.d.ts +0 -23
- package/dist/core/database/transactionControl.d.ts +0 -6
- package/dist/core/events/deferredModelEvents.d.ts +0 -7
- package/dist/core/facades/index.d.ts +0 -10
- package/dist/core/runtime/applicationRegistry.d.ts +0 -18
- package/dist/entries/database/bindConnection.js +0 -1
- package/dist/entries/database/boundConnection.js +0 -1
- package/dist/entries/database/defaultConnection.js +0 -1
- package/dist/entries/database/namedConnections.js +0 -1
- package/dist/entries/database/repositoryConnection.js +0 -1
- package/dist/entries/database/transaction.js +0 -1
- package/dist/entries/facades.js +0 -711
- package/dist/entries/runtime/applicationRegistry.js +0 -1
|
@@ -0,0 +1,1703 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/queue/publicQueue.ts
|
|
3
|
+
import FailedJobRepository from "@getstrata/core/queue/failedJobRepository";
|
|
4
|
+
|
|
5
|
+
// ../../src/core/queue/failedJobService.ts
|
|
6
|
+
class FailedJobService {
|
|
7
|
+
repository;
|
|
8
|
+
constructor(repository) {
|
|
9
|
+
this.repository = repository;
|
|
10
|
+
}
|
|
11
|
+
async recordFailure(input) {
|
|
12
|
+
return await this.repository.create({
|
|
13
|
+
job_name: input.jobName,
|
|
14
|
+
job_id: input.jobId ?? null,
|
|
15
|
+
...input.handoff === undefined ? {} : { handoff: input.handoff },
|
|
16
|
+
payload: input.payload,
|
|
17
|
+
exception: input.exception,
|
|
18
|
+
failed_at: new Date
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
listRecent(limit = 50) {
|
|
22
|
+
return this.repository.findAll({
|
|
23
|
+
limit,
|
|
24
|
+
orderBy: { column: "failed_at", direction: "DESC" }
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
async retry(id, enqueue) {
|
|
28
|
+
if (typeof enqueue !== "function")
|
|
29
|
+
throw new Error("Failed-job retry requires an admission callback.");
|
|
30
|
+
const failedJob = await this.repository.findByIdOrThrow(id, (jobId) => new Error(`Failed job ${jobId} not found.`));
|
|
31
|
+
await enqueue(failedJob);
|
|
32
|
+
await this.repository.deleteById(id);
|
|
33
|
+
return failedJob;
|
|
34
|
+
}
|
|
35
|
+
async delete(id) {
|
|
36
|
+
const deleted = await this.repository.deleteById(id);
|
|
37
|
+
if (!deleted) {
|
|
38
|
+
throw new Error(`Failed job ${id} not found.`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async flush() {
|
|
42
|
+
const jobs = await this.repository.findAll();
|
|
43
|
+
let deleted = 0;
|
|
44
|
+
for (const job of jobs) {
|
|
45
|
+
if (await this.repository.deleteById(job.id)) {
|
|
46
|
+
deleted += 1;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return deleted;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
var failedJobService_default = FailedJobService;
|
|
53
|
+
|
|
54
|
+
// ../../src/core/queue/jobRunner.ts
|
|
55
|
+
import { setTimeout as delay } from "timers/promises";
|
|
56
|
+
|
|
57
|
+
// ../../src/core/config/configSnapshot.ts
|
|
58
|
+
function copyApplicationConfig(value, ancestors = new Set) {
|
|
59
|
+
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
60
|
+
return value;
|
|
61
|
+
if (typeof value === "number" && Number.isFinite(value))
|
|
62
|
+
return value;
|
|
63
|
+
if (typeof value !== "object" || ancestors.has(value)) {
|
|
64
|
+
throw new TypeError("Application config must contain acyclic plain data.");
|
|
65
|
+
}
|
|
66
|
+
const array = Array.isArray(value);
|
|
67
|
+
const prototype = Object.getPrototypeOf(value);
|
|
68
|
+
if (prototype !== (array ? Array.prototype : Object.prototype) && !(prototype === null && !array)) {
|
|
69
|
+
throw new TypeError("Application config must contain only plain objects and arrays.");
|
|
70
|
+
}
|
|
71
|
+
if (Object.getOwnPropertySymbols(value).length > 0) {
|
|
72
|
+
throw new TypeError("Application config cannot contain symbol keys.");
|
|
73
|
+
}
|
|
74
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
75
|
+
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
76
|
+
if (!(array && key === "length") && (!descriptor.enumerable || !("value" in descriptor))) {
|
|
77
|
+
throw new TypeError("Application config cannot contain accessors or hidden properties.");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
ancestors.add(value);
|
|
81
|
+
try {
|
|
82
|
+
if (array) {
|
|
83
|
+
const length = descriptors.length.value;
|
|
84
|
+
if (Object.keys(descriptors).length !== length + 1) {
|
|
85
|
+
throw new TypeError("Application config arrays must be dense and have no extra properties.");
|
|
86
|
+
}
|
|
87
|
+
const result = [];
|
|
88
|
+
for (let index = 0;index < length; index++) {
|
|
89
|
+
const descriptor = descriptors[String(index)];
|
|
90
|
+
if (!descriptor)
|
|
91
|
+
throw new TypeError("Application config arrays must be dense.");
|
|
92
|
+
result.push(copyApplicationConfig(descriptor.value, ancestors));
|
|
93
|
+
}
|
|
94
|
+
return Object.freeze(result);
|
|
95
|
+
}
|
|
96
|
+
const result = {};
|
|
97
|
+
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
98
|
+
Object.defineProperty(result, key, {
|
|
99
|
+
value: copyApplicationConfig(descriptor.value, ancestors),
|
|
100
|
+
enumerable: true
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return Object.freeze(result);
|
|
104
|
+
} finally {
|
|
105
|
+
ancestors.delete(value);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// ../../src/core/queue/queueConfig.ts
|
|
110
|
+
var DEFAULT_QUEUE_VISIBILITY_MS = 60000;
|
|
111
|
+
function snapshotQueueRetryPolicy(policy) {
|
|
112
|
+
const { maxAttempts, backoffMs } = policy;
|
|
113
|
+
if (!Number.isSafeInteger(maxAttempts) || maxAttempts < 1)
|
|
114
|
+
throw new TypeError("QUEUE_MAX_ATTEMPTS must be a positive safe integer.");
|
|
115
|
+
if (!Number.isSafeInteger(backoffMs) || backoffMs < 0 || backoffMs > 2147483647)
|
|
116
|
+
throw new TypeError("QUEUE_BACKOFF_MS must be a non-negative integer within the timer range.");
|
|
117
|
+
return Object.freeze({ maxAttempts, backoffMs });
|
|
118
|
+
}
|
|
119
|
+
function resolveQueueConfig(env = process.env) {
|
|
120
|
+
const driver = env.QUEUE_DRIVER ?? "sync";
|
|
121
|
+
if (!["sync", "async", "redis"].includes(driver))
|
|
122
|
+
throw new TypeError("Unknown queue driver.");
|
|
123
|
+
const policy = snapshotQueueRetryPolicy({
|
|
124
|
+
maxAttempts: Number(env.QUEUE_MAX_ATTEMPTS ?? "3"),
|
|
125
|
+
backoffMs: Number(env.QUEUE_BACKOFF_MS ?? "1000")
|
|
126
|
+
});
|
|
127
|
+
return Object.freeze({ driver, ...policy });
|
|
128
|
+
}
|
|
129
|
+
function resolveRedisQueueTransport(env = process.env) {
|
|
130
|
+
const transport = env.QUEUE_REDIS_TRANSPORT ?? "lists";
|
|
131
|
+
if (transport !== "lists" && transport !== "streams")
|
|
132
|
+
throw new Error('QUEUE_REDIS_TRANSPORT must be "lists" or "streams".');
|
|
133
|
+
return transport;
|
|
134
|
+
}
|
|
135
|
+
function readQueueVisibilityMs(env = process.env) {
|
|
136
|
+
const value = Number(env.QUEUE_VISIBILITY_MS ?? "60000");
|
|
137
|
+
if (!Number.isSafeInteger(value) || value < 30 || value > 2147483647)
|
|
138
|
+
throw new Error("QUEUE_VISIBILITY_MS must be an integer from 30 through 2147483647 milliseconds.");
|
|
139
|
+
return value;
|
|
140
|
+
}
|
|
141
|
+
function snapshotQueueRuntimeConfig(config) {
|
|
142
|
+
if (config.transport !== "lists" && config.transport !== "streams" || typeof config.visibilityMs !== "number")
|
|
143
|
+
throw new TypeError("Queue runtime requires explicit transport and numeric visibility.");
|
|
144
|
+
if (!Array.isArray(config.queueKeys) || config.queueKeys.length !== 3 || config.queueKeys.some((key) => typeof key !== "string" || !key.length) || new Set(config.queueKeys).size !== 3)
|
|
145
|
+
throw new TypeError("Queue runtime requires three distinct nonempty priority keys.");
|
|
146
|
+
return Object.freeze({
|
|
147
|
+
queueKeys: Object.freeze([...config.queueKeys]),
|
|
148
|
+
transport: resolveRedisQueueTransport({ QUEUE_REDIS_TRANSPORT: config.transport }),
|
|
149
|
+
visibilityMs: readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(config.visibilityMs) })
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
function resolveQueueRuntimeConfig(env = process.env) {
|
|
153
|
+
const prefix = env.APP_KEY_PREFIX?.trim() || "strata";
|
|
154
|
+
return snapshotQueueRuntimeConfig({
|
|
155
|
+
transport: resolveRedisQueueTransport(env),
|
|
156
|
+
visibilityMs: readQueueVisibilityMs(env),
|
|
157
|
+
queueKeys: [`${prefix}:queue:high`, `${prefix}:queue:default`, `${prefix}:queue:low`]
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// ../../src/core/queue/jobRunner.ts
|
|
162
|
+
function snapshotQueueJobEnvelope(input) {
|
|
163
|
+
if (!input || typeof input !== "object" || Array.isArray(input) || ![Object.prototype, null].includes(Object.getPrototypeOf(input)))
|
|
164
|
+
throw new TypeError("Queue envelope must be plain data.");
|
|
165
|
+
const data = {};
|
|
166
|
+
for (const key of Reflect.ownKeys(input)) {
|
|
167
|
+
if (typeof key !== "string" || !["name", "payload", "attempts", "jobId", "deadlineAtMs", "cancellable", "handoff"].includes(key))
|
|
168
|
+
throw new TypeError("Unknown queue envelope field.");
|
|
169
|
+
const descriptor = Object.getOwnPropertyDescriptor(input, key);
|
|
170
|
+
if (!descriptor || !("value" in descriptor) || !descriptor.enumerable)
|
|
171
|
+
throw new TypeError("Queue envelope must contain own data fields.");
|
|
172
|
+
if (descriptor.value !== undefined)
|
|
173
|
+
Object.defineProperty(data, key, { value: descriptor.value, enumerable: true });
|
|
174
|
+
}
|
|
175
|
+
const captured = copyApplicationConfig(data);
|
|
176
|
+
if (typeof captured.name !== "string" || !captured.name || !captured.payload || typeof captured.payload !== "object" || Array.isArray(captured.payload))
|
|
177
|
+
throw new TypeError("Queue envelope requires a job name and payload record.");
|
|
178
|
+
return captured;
|
|
179
|
+
}
|
|
180
|
+
function parseQueueJobEnvelope(rawPayload, registry) {
|
|
181
|
+
let parsed;
|
|
182
|
+
try {
|
|
183
|
+
parsed = JSON.parse(rawPayload);
|
|
184
|
+
} catch {
|
|
185
|
+
console.error("[QueueWorker] Ignoring malformed queue payload");
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
if (!parsed || typeof parsed !== "object") {
|
|
189
|
+
console.error("[QueueWorker] Ignoring non-object queue payload");
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
const envelope = parsed;
|
|
193
|
+
if (Object.keys(parsed).some((key) => ![
|
|
194
|
+
"name",
|
|
195
|
+
"payload",
|
|
196
|
+
"attempts",
|
|
197
|
+
"jobId",
|
|
198
|
+
"deadlineAtMs",
|
|
199
|
+
"cancellable",
|
|
200
|
+
"handoff"
|
|
201
|
+
].includes(key)))
|
|
202
|
+
return null;
|
|
203
|
+
if (envelope.handoff !== undefined && (typeof envelope.handoff !== "string" || !envelope.handoff.length || envelope.handoff.length > 32768))
|
|
204
|
+
return null;
|
|
205
|
+
if (typeof envelope.name !== "string" || envelope.name.length === 0) {
|
|
206
|
+
console.error("[QueueWorker] Ignoring queue payload without job name");
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
if (!registry.has(envelope.name)) {
|
|
210
|
+
console.error(`[QueueWorker] Ignoring unknown job name: ${envelope.name}`);
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
if (envelope.payload !== undefined && (typeof envelope.payload !== "object" || envelope.payload === null || Array.isArray(envelope.payload))) {
|
|
214
|
+
console.error("[QueueWorker] Ignoring queue payload with invalid payload object");
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
if (envelope.jobId !== undefined && (typeof envelope.jobId !== "string" || !envelope.jobId.length))
|
|
218
|
+
return null;
|
|
219
|
+
if (envelope.attempts !== undefined && (!Number.isSafeInteger(envelope.attempts) || envelope.attempts < 0))
|
|
220
|
+
return null;
|
|
221
|
+
if (envelope.deadlineAtMs !== undefined && (!Number.isSafeInteger(envelope.deadlineAtMs) || envelope.deadlineAtMs <= 0))
|
|
222
|
+
return null;
|
|
223
|
+
if (envelope.cancellable !== undefined && (typeof envelope.cancellable !== "boolean" || envelope.cancellable && !envelope.jobId))
|
|
224
|
+
return null;
|
|
225
|
+
return {
|
|
226
|
+
...envelope.cancellable ? { cancellable: true } : {},
|
|
227
|
+
...envelope.deadlineAtMs === undefined ? {} : { deadlineAtMs: envelope.deadlineAtMs },
|
|
228
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
229
|
+
jobId: envelope.jobId,
|
|
230
|
+
name: envelope.name,
|
|
231
|
+
payload: envelope.payload ?? {},
|
|
232
|
+
attempts: typeof envelope.attempts === "number" ? envelope.attempts : 0
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
async function runQueueJob(envelope, failedJobs, registry, options = {}) {
|
|
236
|
+
envelope = snapshotQueueJobEnvelope(envelope);
|
|
237
|
+
const retryPolicy = snapshotQueueRetryPolicy(options.retryPolicy ?? resolveQueueConfig());
|
|
238
|
+
options.context?.signal.throwIfAborted();
|
|
239
|
+
if (!registry.has(envelope.name)) {
|
|
240
|
+
throw new Error(`Unknown job "${envelope.name}".`);
|
|
241
|
+
}
|
|
242
|
+
const attempts = envelope.attempts ?? 0;
|
|
243
|
+
const context = options.context ?? {
|
|
244
|
+
jobId: envelope.jobId ?? crypto.randomUUID(),
|
|
245
|
+
signal: new AbortController().signal
|
|
246
|
+
};
|
|
247
|
+
let job;
|
|
248
|
+
try {
|
|
249
|
+
await failedJobs.runAttempt(envelope, { ...context, attempt: attempts + 1 }, (scoped) => {
|
|
250
|
+
scoped.signal.throwIfAborted();
|
|
251
|
+
job = registry.create(envelope.name);
|
|
252
|
+
if (!job)
|
|
253
|
+
throw new Error(`Unknown job "${envelope.name}".`);
|
|
254
|
+
return job.handle(envelope.payload, scoped);
|
|
255
|
+
});
|
|
256
|
+
} catch (error) {
|
|
257
|
+
options.context?.signal.throwIfAborted();
|
|
258
|
+
if (!job)
|
|
259
|
+
throw error;
|
|
260
|
+
const nextAttempt = attempts + 1;
|
|
261
|
+
const maxAttempts = job.maxAttempts ?? retryPolicy.maxAttempts;
|
|
262
|
+
if (nextAttempt < maxAttempts) {
|
|
263
|
+
const backoffMs = job.backoffMs ?? retryPolicy.backoffMs;
|
|
264
|
+
if (options.deferRetry) {
|
|
265
|
+
await options.deferRetry({ ...envelope, jobId: context.jobId, attempts: nextAttempt }, backoffMs * nextAttempt);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
await delay(backoffMs * nextAttempt, undefined, { signal: options.context?.signal });
|
|
269
|
+
await runQueueJob({
|
|
270
|
+
...envelope,
|
|
271
|
+
attempts: nextAttempt
|
|
272
|
+
}, failedJobs, registry, { ...options, context, retryPolicy });
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
await failedJobs.recordFailure({
|
|
276
|
+
jobName: envelope.name,
|
|
277
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
278
|
+
jobId: context.jobId,
|
|
279
|
+
payload: envelope.payload,
|
|
280
|
+
exception: error instanceof Error ? error.stack ?? error.message : String(error)
|
|
281
|
+
});
|
|
282
|
+
options.onFailureRecorded?.();
|
|
283
|
+
throw error;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// ../../src/core/queue/queueServices.ts
|
|
288
|
+
import { failedJobRepository } from "@getstrata/core/queue/failedJobRepository";
|
|
289
|
+
import {
|
|
290
|
+
QueueHandoffError
|
|
291
|
+
} from "@getstrata/core/queue/queueHandoff";
|
|
292
|
+
function createFailedJobService(database) {
|
|
293
|
+
return new FailedJobService(database.repository(failedJobRepository));
|
|
294
|
+
}
|
|
295
|
+
function createQueueServices(config) {
|
|
296
|
+
const { invoke, attemptEnvelope, recoveryEnvelope, validateReplay } = config;
|
|
297
|
+
const jobContext = Object.freeze({ ...config.jobContext });
|
|
298
|
+
const operate = (work, failure) => invoke(recoveryEnvelope(failure), ({ database }) => work(createFailedJobService(database)));
|
|
299
|
+
return Object.freeze({
|
|
300
|
+
recordFailure: (input) => operate((service) => service.recordFailure(input), input),
|
|
301
|
+
listRecent: (limit) => operate((service) => service.listRecent(limit)),
|
|
302
|
+
retry: (id, enqueue) => operate((service) => service.retry(id, async (record) => {
|
|
303
|
+
validateReplay?.(record);
|
|
304
|
+
await enqueue(record);
|
|
305
|
+
})),
|
|
306
|
+
delete: (id) => operate((service) => service.delete(id)),
|
|
307
|
+
flush: () => operate((service) => service.flush()),
|
|
308
|
+
runAttempt: (job, context, work) => {
|
|
309
|
+
const envelope = attemptEnvelope(job, context);
|
|
310
|
+
return invoke(envelope, ({ database, policy }) => work({
|
|
311
|
+
...context,
|
|
312
|
+
...jobContext,
|
|
313
|
+
database,
|
|
314
|
+
policy,
|
|
315
|
+
actor: envelope.actor,
|
|
316
|
+
tenant: envelope.tenant
|
|
317
|
+
}));
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
function createSystemQueueServices(invoke, jobContext = {}, handoff, systemJobs = []) {
|
|
322
|
+
if (!Array.isArray(systemJobs) || systemJobs.some((name) => typeof name !== "string" || !name.length))
|
|
323
|
+
throw new TypeError("System job admission requires explicit job names.");
|
|
324
|
+
const allowedSystemJobs = new Set(systemJobs);
|
|
325
|
+
const establishIdentity = (job) => {
|
|
326
|
+
if (job.handoff === undefined) {
|
|
327
|
+
if (handoff && !allowedSystemJobs.has(job.name))
|
|
328
|
+
throw new QueueHandoffError;
|
|
329
|
+
return { actor: { kind: "system", policy: "queue.job" } };
|
|
330
|
+
}
|
|
331
|
+
if (!handoff)
|
|
332
|
+
throw new QueueHandoffError;
|
|
333
|
+
return handoff.verify(job);
|
|
334
|
+
};
|
|
335
|
+
return createQueueServices({
|
|
336
|
+
invoke,
|
|
337
|
+
jobContext,
|
|
338
|
+
validateReplay: (record) => {
|
|
339
|
+
establishIdentity({
|
|
340
|
+
name: record.job_name,
|
|
341
|
+
payload: record.payload,
|
|
342
|
+
...record.handoff == null ? {} : { handoff: record.handoff }
|
|
343
|
+
});
|
|
344
|
+
},
|
|
345
|
+
attemptEnvelope: (job, context) => {
|
|
346
|
+
const identity = establishIdentity(job);
|
|
347
|
+
return {
|
|
348
|
+
kind: "job",
|
|
349
|
+
externalId: context.jobId,
|
|
350
|
+
signal: context.signal,
|
|
351
|
+
...context.deadlineAtMs === undefined ? {} : { deadlineAtMs: context.deadlineAtMs },
|
|
352
|
+
...identity,
|
|
353
|
+
metadata: { jobName: job.name, attempt: context.attempt ?? 1 }
|
|
354
|
+
};
|
|
355
|
+
},
|
|
356
|
+
recoveryEnvelope: (failure) => ({
|
|
357
|
+
kind: "command",
|
|
358
|
+
actor: { kind: "system", policy: "queue.recovery" },
|
|
359
|
+
metadata: { phase: "queue-recovery", ...failure ? { jobName: failure.jobName } : {} }
|
|
360
|
+
})
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// ../../src/core/queue/redisQueue.ts
|
|
365
|
+
import { QueueHandoffError as QueueHandoffError3 } from "@getstrata/core/queue/queueHandoff";
|
|
366
|
+
var {RedisClient: RedisClient2 } = globalThis.Bun;
|
|
367
|
+
|
|
368
|
+
// ../../src/core/queue/redisListScripts.ts
|
|
369
|
+
var SCRIPT_HEADER = `
|
|
370
|
+
local function checkType(key, expected)
|
|
371
|
+
local actual = redis.call('TYPE', key).ok
|
|
372
|
+
if actual ~= 'none' and actual ~= expected then
|
|
373
|
+
return redis.error_reply('Unexpected queue key type: ' .. key)
|
|
374
|
+
end
|
|
375
|
+
end
|
|
376
|
+
local function nowMs()
|
|
377
|
+
local time = redis.call('TIME')
|
|
378
|
+
return tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
|
|
379
|
+
end
|
|
380
|
+
`;
|
|
381
|
+
var RESERVE_JOB = `${SCRIPT_HEADER}
|
|
382
|
+
local err = checkType(KEYS[1], 'list') or checkType(KEYS[2], 'list') or checkType(KEYS[3], 'hash')
|
|
383
|
+
if err then return err end
|
|
384
|
+
local payload = redis.call('LINDEX', KEYS[1], -1)
|
|
385
|
+
if not payload then return nil end
|
|
386
|
+
local record = cjson.encode({strataReservation=1, id=ARGV[1], owner=ARGV[2], payload=payload})
|
|
387
|
+
local lease = cjson.encode({owner=ARGV[2], expiresAt=nowMs()+tonumber(ARGV[3])})
|
|
388
|
+
redis.call('LPUSH', KEYS[2], record)
|
|
389
|
+
redis.call('HSET', KEYS[3], ARGV[1], lease)
|
|
390
|
+
redis.call('RPOP', KEYS[1])
|
|
391
|
+
return record
|
|
392
|
+
`;
|
|
393
|
+
var RENEW_OR_ACK = `${SCRIPT_HEADER}
|
|
394
|
+
local err = checkType(KEYS[1], 'list') or checkType(KEYS[2], 'hash') or checkType(KEYS[3], 'list')
|
|
395
|
+
if err then return err end
|
|
396
|
+
local raw = redis.call('HGET', KEYS[2], ARGV[1])
|
|
397
|
+
if not raw then return 0 end
|
|
398
|
+
local ok, lease = pcall(cjson.decode, raw)
|
|
399
|
+
local now = nowMs()
|
|
400
|
+
if not ok or type(lease) ~= 'table' or lease.owner ~= ARGV[2] or not tonumber(lease.expiresAt) or tonumber(lease.expiresAt) <= now then
|
|
401
|
+
return 0
|
|
402
|
+
end
|
|
403
|
+
if ARGV[4] == 'renew' then
|
|
404
|
+
redis.call('HSET', KEYS[2], ARGV[1], cjson.encode({owner=ARGV[2], expiresAt=now+tonumber(ARGV[5])}))
|
|
405
|
+
return 1
|
|
406
|
+
end
|
|
407
|
+
if ARGV[4] == 'quarantine' then redis.call('LPUSH', KEYS[3], ARGV[6]) end
|
|
408
|
+
local removed = redis.call('LREM', KEYS[1], 1, ARGV[3])
|
|
409
|
+
redis.call('HDEL', KEYS[2], ARGV[1])
|
|
410
|
+
return removed
|
|
411
|
+
`;
|
|
412
|
+
var RECLAIM_JOBS = `${SCRIPT_HEADER}
|
|
413
|
+
local err = checkType(KEYS[1], 'list') or checkType(KEYS[2], 'list') or checkType(KEYS[3], 'hash')
|
|
414
|
+
if err then return err end
|
|
415
|
+
local now = nowMs()
|
|
416
|
+
local reclaimed = 0
|
|
417
|
+
for _, record in ipairs(redis.call('LRANGE', KEYS[2], 0, -1)) do
|
|
418
|
+
local payload, field = record, record
|
|
419
|
+
local modern = false
|
|
420
|
+
local ok, reservation = pcall(cjson.decode, record)
|
|
421
|
+
if ok and type(reservation) == 'table' and reservation.strataReservation == 1 then
|
|
422
|
+
payload, field, modern = reservation.payload, reservation.id, true
|
|
423
|
+
end
|
|
424
|
+
local raw = redis.call('HGET', KEYS[3], field)
|
|
425
|
+
local expired = true
|
|
426
|
+
if raw then
|
|
427
|
+
if modern then
|
|
428
|
+
local leaseOk, lease = pcall(cjson.decode, raw)
|
|
429
|
+
expired = not leaseOk or type(lease) ~= 'table' or not tonumber(lease.expiresAt) or tonumber(lease.expiresAt) <= now
|
|
430
|
+
else
|
|
431
|
+
local reservedAt = tonumber(raw)
|
|
432
|
+
expired = not reservedAt or now - reservedAt >= tonumber(ARGV[1])
|
|
433
|
+
end
|
|
434
|
+
end
|
|
435
|
+
if expired then
|
|
436
|
+
redis.call('LPUSH', KEYS[1], payload)
|
|
437
|
+
redis.call('LREM', KEYS[2], 1, record)
|
|
438
|
+
redis.call('HDEL', KEYS[3], field)
|
|
439
|
+
reclaimed = reclaimed + 1
|
|
440
|
+
end
|
|
441
|
+
end
|
|
442
|
+
return reclaimed
|
|
443
|
+
`;
|
|
444
|
+
var POLL_LIST_PRIORITIES = `${SCRIPT_HEADER}
|
|
445
|
+
local owner = ARGV[1]
|
|
446
|
+
local visibility = ARGV[2]
|
|
447
|
+
local reservationId = ARGV[3]
|
|
448
|
+
local reclaim = ARGV[4] == '1'
|
|
449
|
+
local function reclaimQueue(listKey, processingKey, leaseKey)
|
|
450
|
+
local err = checkType(listKey, 'list') or checkType(processingKey, 'list') or checkType(leaseKey, 'hash')
|
|
451
|
+
if err then return err end
|
|
452
|
+
local now = nowMs()
|
|
453
|
+
for _, record in ipairs(redis.call('LRANGE', processingKey, 0, -1)) do
|
|
454
|
+
local payload, field = record, record
|
|
455
|
+
local modern = false
|
|
456
|
+
local ok, reservation = pcall(cjson.decode, record)
|
|
457
|
+
if ok and type(reservation) == 'table' and reservation.strataReservation == 1 then
|
|
458
|
+
payload, field, modern = reservation.payload, reservation.id, true
|
|
459
|
+
end
|
|
460
|
+
local raw = redis.call('HGET', leaseKey, field)
|
|
461
|
+
local expired = true
|
|
462
|
+
if raw then
|
|
463
|
+
if modern then
|
|
464
|
+
local leaseOk, lease = pcall(cjson.decode, raw)
|
|
465
|
+
expired = not leaseOk or type(lease) ~= 'table' or not tonumber(lease.expiresAt) or tonumber(lease.expiresAt) <= now
|
|
466
|
+
else
|
|
467
|
+
local reservedAt = tonumber(raw)
|
|
468
|
+
expired = not reservedAt or now - reservedAt >= tonumber(visibility)
|
|
469
|
+
end
|
|
470
|
+
end
|
|
471
|
+
if expired then
|
|
472
|
+
redis.call('LPUSH', listKey, payload)
|
|
473
|
+
redis.call('LREM', processingKey, 1, record)
|
|
474
|
+
redis.call('HDEL', leaseKey, field)
|
|
475
|
+
end
|
|
476
|
+
end
|
|
477
|
+
end
|
|
478
|
+
local function reserveQueue(listKey, processingKey, leaseKey)
|
|
479
|
+
local err = checkType(listKey, 'list') or checkType(processingKey, 'list') or checkType(leaseKey, 'hash')
|
|
480
|
+
if err then return err end
|
|
481
|
+
local payload = redis.call('LINDEX', listKey, -1)
|
|
482
|
+
if not payload then return nil end
|
|
483
|
+
local record = cjson.encode({strataReservation=1, id=reservationId, owner=owner, payload=payload})
|
|
484
|
+
local lease = cjson.encode({owner=owner, expiresAt=nowMs()+tonumber(visibility)})
|
|
485
|
+
redis.call('LPUSH', processingKey, record)
|
|
486
|
+
redis.call('HSET', leaseKey, reservationId, lease)
|
|
487
|
+
redis.call('RPOP', listKey)
|
|
488
|
+
return record
|
|
489
|
+
end
|
|
490
|
+
local count = #KEYS / 3
|
|
491
|
+
if reclaim then
|
|
492
|
+
for i = 1, count do
|
|
493
|
+
local err = reclaimQueue(KEYS[(i - 1) * 3 + 1], KEYS[(i - 1) * 3 + 2], KEYS[(i - 1) * 3 + 3])
|
|
494
|
+
if err then return err end
|
|
495
|
+
end
|
|
496
|
+
end
|
|
497
|
+
for i = 1, count do
|
|
498
|
+
local record = reserveQueue(KEYS[(i - 1) * 3 + 1], KEYS[(i - 1) * 3 + 2], KEYS[(i - 1) * 3 + 3])
|
|
499
|
+
if type(record) == 'string' then return {'1', tostring(i), record} end
|
|
500
|
+
if record then return record end
|
|
501
|
+
end
|
|
502
|
+
return {'0'}
|
|
503
|
+
`;
|
|
504
|
+
|
|
505
|
+
// ../../src/core/runtime/appEnv.ts
|
|
506
|
+
var NON_PRODUCTION_APP_ENVS = new Set(["local", "development", "dev", "test", "testing", "ci"]);
|
|
507
|
+
function normalizeEnvValue(value) {
|
|
508
|
+
return (value ?? "").trim().toLowerCase();
|
|
509
|
+
}
|
|
510
|
+
function isProductionEnv(env = process.env) {
|
|
511
|
+
const appEnv = normalizeEnvValue(env.APP_ENV);
|
|
512
|
+
const nodeEnv = normalizeEnvValue(env.NODE_ENV);
|
|
513
|
+
if (appEnv === "production" || nodeEnv === "production") {
|
|
514
|
+
return true;
|
|
515
|
+
}
|
|
516
|
+
if (appEnv === "") {
|
|
517
|
+
return false;
|
|
518
|
+
}
|
|
519
|
+
return !NON_PRODUCTION_APP_ENVS.has(appEnv);
|
|
520
|
+
}
|
|
521
|
+
function envFlagEnabled(value) {
|
|
522
|
+
return value === "true";
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// ../../src/core/runtime/appKeyPrefix.ts
|
|
526
|
+
function appKeyPrefix(env = process.env) {
|
|
527
|
+
return env.APP_KEY_PREFIX?.trim() || "strata";
|
|
528
|
+
}
|
|
529
|
+
function appCookieName(kind, env = process.env) {
|
|
530
|
+
return `${appKeyPrefix(env)}_${kind}`;
|
|
531
|
+
}
|
|
532
|
+
function appDevSecret(kind, env = process.env) {
|
|
533
|
+
return `${appKeyPrefix(env)}-dev-${kind}`;
|
|
534
|
+
}
|
|
535
|
+
function requireConfiguredSecret(names, devKind, env = process.env) {
|
|
536
|
+
for (const name of names) {
|
|
537
|
+
const value = env[name]?.trim();
|
|
538
|
+
if (value) {
|
|
539
|
+
return value;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
if (isProductionEnv(env)) {
|
|
543
|
+
throw new Error(`${names[0]} must be set outside development. Do not derive secrets from the app name.`);
|
|
544
|
+
}
|
|
545
|
+
return appDevSecret(devKind, env);
|
|
546
|
+
}
|
|
547
|
+
function namespacedRedisKey(kind) {
|
|
548
|
+
return `${appKeyPrefix()}:${kind}`;
|
|
549
|
+
}
|
|
550
|
+
function smtpEhloHost(env = process.env) {
|
|
551
|
+
const raw = env.MAIL_EHLO?.trim() || `${appKeyPrefix(env)}.local`;
|
|
552
|
+
const safe = raw.replace(/[^a-zA-Z0-9.-]/g, "");
|
|
553
|
+
return safe || "strata.local";
|
|
554
|
+
}
|
|
555
|
+
function otelServiceName(env = process.env) {
|
|
556
|
+
return env.OTEL_SERVICE_NAME?.trim() || `${appKeyPrefix(env)}-api`;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// ../../src/core/queue/redisQueueKeys.ts
|
|
560
|
+
function queueListKey() {
|
|
561
|
+
return namespacedRedisKey("queue:default");
|
|
562
|
+
}
|
|
563
|
+
function queueHighKey() {
|
|
564
|
+
return namespacedRedisKey("queue:high");
|
|
565
|
+
}
|
|
566
|
+
function queueLowKey() {
|
|
567
|
+
return namespacedRedisKey("queue:low");
|
|
568
|
+
}
|
|
569
|
+
var QUEUE_LIST_KEY = "strata:queue:default";
|
|
570
|
+
var QUEUE_HIGH_KEY = "strata:queue:high";
|
|
571
|
+
var QUEUE_LOW_KEY = "strata:queue:low";
|
|
572
|
+
function defaultQueueKeys() {
|
|
573
|
+
return [queueHighKey(), queueListKey(), queueLowKey()];
|
|
574
|
+
}
|
|
575
|
+
var QUEUE_KEYS = Object.freeze([QUEUE_HIGH_KEY, QUEUE_LIST_KEY, QUEUE_LOW_KEY]);
|
|
576
|
+
function queueKeyForPriority(priority = "default", keys = defaultQueueKeys()) {
|
|
577
|
+
return priority === "high" ? keys[0] : priority === "low" ? keys[2] : keys[1];
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// ../../src/core/queue/redisStreams.ts
|
|
581
|
+
import { QueueHandoffError as QueueHandoffError2 } from "@getstrata/core/queue/queueHandoff";
|
|
582
|
+
var {RedisClient } = globalThis.Bun;
|
|
583
|
+
|
|
584
|
+
// ../../src/core/queue/redisStreamsScripts.ts
|
|
585
|
+
var ENQUEUE_CONTROLLED = `
|
|
586
|
+
local kind = redis.call('TYPE', KEYS[1]).ok
|
|
587
|
+
if kind ~= 'none' and kind ~= 'stream' then return redis.error_reply('Invalid stream admission key type') end
|
|
588
|
+
if redis.call('EXISTS', KEYS[2]) ~= 0 then return redis.error_reply('Streams control identity already exists') end
|
|
589
|
+
redis.call('XADD', KEYS[1], '*', 'payload', ARGV[1])
|
|
590
|
+
redis.call('HSET', KEYS[2], 'state', 'active')
|
|
591
|
+
return 1
|
|
592
|
+
`;
|
|
593
|
+
var CANCEL_STREAM = `
|
|
594
|
+
local state = redis.call('HGET', KEYS[1], 'state')
|
|
595
|
+
if not state then return 0 end
|
|
596
|
+
local kind = redis.call('TYPE', KEYS[2]).ok
|
|
597
|
+
if kind ~= 'none' and kind ~= 'zset' then return redis.error_reply('Invalid stream retry key type') end
|
|
598
|
+
local payload = redis.call('HGET', KEYS[1], 'retryPayload')
|
|
599
|
+
if payload and redis.call('ZSCORE', KEYS[2], payload) then redis.call('ZADD', KEYS[2], 0, payload) end
|
|
600
|
+
redis.call('HSET', KEYS[1], 'state', 'cancelled')
|
|
601
|
+
return 1
|
|
602
|
+
`;
|
|
603
|
+
var SCHEDULE_RETRY = `
|
|
604
|
+
local pending = redis.call('XPENDING', KEYS[1], ARGV[1], ARGV[2], ARGV[2], 1)
|
|
605
|
+
if #pending == 0 or pending[1][2] ~= ARGV[3] or tonumber(pending[1][3]) >= tonumber(ARGV[4]) then return 0 end
|
|
606
|
+
local kind = redis.call('TYPE', KEYS[2]).ok
|
|
607
|
+
if kind ~= 'none' and kind ~= 'zset' then return redis.error_reply('Invalid stream retry key type') end
|
|
608
|
+
if ARGV[8] == '1' then
|
|
609
|
+
local state = redis.call('HGET', KEYS[3], 'state')
|
|
610
|
+
if state == 'cancelled' then return redis.error_reply('Streams job cancelled') end
|
|
611
|
+
if state ~= 'active' then return redis.error_reply('Missing or invalid Streams control state') end
|
|
612
|
+
end
|
|
613
|
+
local now = redis.call('TIME')
|
|
614
|
+
local due = tonumber(now[1])*1000 + math.floor(tonumber(now[2])/1000) + tonumber(ARGV[5])
|
|
615
|
+
if ARGV[7] ~= '' then due = math.min(due, tonumber(ARGV[7])) end
|
|
616
|
+
redis.call('ZADD', KEYS[2], due, ARGV[6])
|
|
617
|
+
if ARGV[8] == '1' then redis.call('HSET', KEYS[3], 'retryPayload', ARGV[6]) end
|
|
618
|
+
redis.call('XACK', KEYS[1], ARGV[1], ARGV[2])
|
|
619
|
+
redis.call('XDEL', KEYS[1], ARGV[2])
|
|
620
|
+
if #redis.call('XPENDING', KEYS[1], ARGV[1], '-', '+', 1, ARGV[3]) == 0 then redis.call('XGROUP', 'DELCONSUMER', KEYS[1], ARGV[1], ARGV[3]) end
|
|
621
|
+
return 1
|
|
622
|
+
`;
|
|
623
|
+
var POLL_STREAM_PRIORITIES = `
|
|
624
|
+
local group = ARGV[1]
|
|
625
|
+
local owner = ARGV[2]
|
|
626
|
+
local visibility = ARGV[3]
|
|
627
|
+
local function payloadOf(fields)
|
|
628
|
+
for index = 1, #fields, 2 do
|
|
629
|
+
if fields[index] == 'payload' and type(fields[index + 1]) == 'string' then return fields[index + 1] end
|
|
630
|
+
end
|
|
631
|
+
return ''
|
|
632
|
+
end
|
|
633
|
+
local cursors = {}
|
|
634
|
+
for i = 1, #KEYS / 2 do cursors[i] = ARGV[3 + i] end
|
|
635
|
+
for i = 1, #KEYS / 2 do
|
|
636
|
+
local stream = KEYS[(i - 1) * 2 + 1]
|
|
637
|
+
local retries = KEYS[(i - 1) * 2 + 2]
|
|
638
|
+
local cursor = ARGV[3 + i]
|
|
639
|
+
local streamKind = redis.call('TYPE', stream).ok
|
|
640
|
+
local retryKind = redis.call('TYPE', retries).ok
|
|
641
|
+
if streamKind ~= 'none' and streamKind ~= 'stream' then return redis.error_reply('Invalid stream retry promotion key type') end
|
|
642
|
+
if retryKind ~= 'none' and retryKind ~= 'zset' then return redis.error_reply('Invalid stream retry promotion key type') end
|
|
643
|
+
if streamKind == 'stream' then
|
|
644
|
+
local now = redis.call('TIME')
|
|
645
|
+
local due = tonumber(now[1]) * 1000 + math.floor(tonumber(now[2]) / 1000)
|
|
646
|
+
local ready = redis.call('ZRANGE', retries, '-inf', due, 'BYSCORE', 'LIMIT', 0, 100)
|
|
647
|
+
for _, payload in ipairs(ready) do
|
|
648
|
+
redis.call('XADD', stream, '*', 'payload', payload)
|
|
649
|
+
redis.call('ZREM', retries, payload)
|
|
650
|
+
end
|
|
651
|
+
local window = redis.call('XPENDING', stream, group, cursor, '+', 10)
|
|
652
|
+
local claimed = redis.call('XAUTOCLAIM', stream, group, owner, visibility, cursor, 'COUNT', 1)
|
|
653
|
+
for _, entry in ipairs(window) do
|
|
654
|
+
if entry[2] ~= owner and #redis.call('XPENDING', stream, group, '-', '+', 1, entry[2]) == 0 then
|
|
655
|
+
redis.call('XGROUP', 'DELCONSUMER', stream, group, entry[2])
|
|
656
|
+
end
|
|
657
|
+
end
|
|
658
|
+
if #redis.call('XPENDING', stream, group, '-', '+', 1, owner) == 0 then
|
|
659
|
+
redis.call('XGROUP', 'DELCONSUMER', stream, group, owner)
|
|
660
|
+
end
|
|
661
|
+
cursors[i] = claimed[1]
|
|
662
|
+
if #claimed[2] > 0 then
|
|
663
|
+
return {'1', tostring(i), claimed[2][1][1], payloadOf(claimed[2][1][2]), unpack(cursors)}
|
|
664
|
+
end
|
|
665
|
+
local fresh = redis.call('XREADGROUP', 'GROUP', group, owner, 'COUNT', 1, 'STREAMS', stream, '>')
|
|
666
|
+
if not fresh and #redis.call('XPENDING', stream, group, '-', '+', 1, owner) == 0 then
|
|
667
|
+
redis.call('XGROUP', 'DELCONSUMER', stream, group, owner)
|
|
668
|
+
end
|
|
669
|
+
if fresh and #fresh > 0 and #fresh[1][2] > 0 then
|
|
670
|
+
return {'1', tostring(i), fresh[1][2][1][1], payloadOf(fresh[1][2][1][2]), unpack(cursors)}
|
|
671
|
+
end
|
|
672
|
+
end
|
|
673
|
+
end
|
|
674
|
+
return {'0', unpack(cursors)}
|
|
675
|
+
`;
|
|
676
|
+
var ASSERT_DRAINED = `
|
|
677
|
+
if redis.call('LLEN', KEYS[1]) > 0 or redis.call('LLEN', KEYS[2]) > 0 or redis.call('HLEN', KEYS[3]) > 0 then
|
|
678
|
+
return redis.error_reply('Legacy queue state requires maintenance conversion before Streams activation')
|
|
679
|
+
end
|
|
680
|
+
return 1
|
|
681
|
+
`;
|
|
682
|
+
var UPDATE_STREAM = `
|
|
683
|
+
local pending = redis.call('XPENDING', KEYS[1], ARGV[1], ARGV[2], ARGV[2], 1)
|
|
684
|
+
if #pending == 0 or pending[1][2] ~= ARGV[3] or tonumber(pending[1][3]) >= tonumber(ARGV[4]) then return 0 end
|
|
685
|
+
if ARGV[7] == '1' then
|
|
686
|
+
local kind = redis.call('TYPE', KEYS[3]).ok
|
|
687
|
+
if kind ~= 'none' and kind ~= 'hash' then return redis.error_reply('Invalid stream control key type') end
|
|
688
|
+
end
|
|
689
|
+
if ARGV[5] == 'renew' then
|
|
690
|
+
local claimed = redis.call('XCLAIM', KEYS[1], ARGV[1], ARGV[3], 0, ARGV[2], 'JUSTID')
|
|
691
|
+
return #claimed
|
|
692
|
+
end
|
|
693
|
+
if ARGV[5] == 'quarantine' then
|
|
694
|
+
local kind = redis.call('TYPE', KEYS[2]).ok
|
|
695
|
+
if kind ~= 'none' and kind ~= 'stream' then return redis.error_reply('Invalid stream quarantine key type') end
|
|
696
|
+
redis.call('XADD', KEYS[2], '*', 'sourceId', ARGV[2], 'payload', ARGV[6])
|
|
697
|
+
end
|
|
698
|
+
local acknowledged = redis.call('XACK', KEYS[1], ARGV[1], ARGV[2])
|
|
699
|
+
if acknowledged == 1 then
|
|
700
|
+
redis.call('XDEL', KEYS[1], ARGV[2])
|
|
701
|
+
if ARGV[7] == '1' then redis.call('DEL', KEYS[3]) end
|
|
702
|
+
end
|
|
703
|
+
if #redis.call('XPENDING', KEYS[1], ARGV[1], '-', '+', 1, ARGV[3]) == 0 then redis.call('XGROUP', 'DELCONSUMER', KEYS[1], ARGV[1], ARGV[3]) end
|
|
704
|
+
return acknowledged
|
|
705
|
+
`;
|
|
706
|
+
var CONVERT_LIST = `
|
|
707
|
+
local kinds = {'list','list','hash','stream'}
|
|
708
|
+
for i=1,4 do
|
|
709
|
+
local kind = redis.call('TYPE', KEYS[i]).ok
|
|
710
|
+
if kind ~= 'none' and kind ~= kinds[i] then return redis.error_reply('Invalid queue conversion key type') end
|
|
711
|
+
end
|
|
712
|
+
if redis.call('LLEN', KEYS[2]) > 0 or redis.call('HLEN', KEYS[3]) > 0 then return redis.error_reply('Drain or recover processing reservations before queue conversion') end
|
|
713
|
+
local converted = 0
|
|
714
|
+
for i=1,tonumber(ARGV[1]) do
|
|
715
|
+
local payload = redis.call('LINDEX', KEYS[1], -1)
|
|
716
|
+
if not payload then break end
|
|
717
|
+
redis.call('XADD', KEYS[4], '*', 'payload', payload)
|
|
718
|
+
redis.call('RPOP', KEYS[1])
|
|
719
|
+
converted = converted + 1
|
|
720
|
+
end
|
|
721
|
+
return converted
|
|
722
|
+
`;
|
|
723
|
+
|
|
724
|
+
// ../../src/core/queue/redisStreams.ts
|
|
725
|
+
var STREAM_GROUP = "strata-workers-v1";
|
|
726
|
+
function streamQueueKey(queueKey) {
|
|
727
|
+
return `${queueKey}:stream`;
|
|
728
|
+
}
|
|
729
|
+
function streamDeadLetterKey(queueKey) {
|
|
730
|
+
return `${streamQueueKey(queueKey)}:invalid`;
|
|
731
|
+
}
|
|
732
|
+
function streamRetryKey(queueKey) {
|
|
733
|
+
return `${streamQueueKey(queueKey)}:retries`;
|
|
734
|
+
}
|
|
735
|
+
function streamControlKey(key, jobId) {
|
|
736
|
+
const prefix = `${streamQueueKey(key)}/`;
|
|
737
|
+
if (!jobId.startsWith(prefix) || !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(jobId.slice(prefix.length)))
|
|
738
|
+
throw new Error("Invalid cancellable Streams job ID");
|
|
739
|
+
return `${streamQueueKey(key)}:control:${jobId.slice(prefix.length)}`;
|
|
740
|
+
}
|
|
741
|
+
async function cancelStreamJob(client, key, jobId) {
|
|
742
|
+
return Number(await client.send("EVAL", [
|
|
743
|
+
CANCEL_STREAM,
|
|
744
|
+
"2",
|
|
745
|
+
streamControlKey(key, jobId),
|
|
746
|
+
streamRetryKey(key)
|
|
747
|
+
])) === 1;
|
|
748
|
+
}
|
|
749
|
+
async function readStreamCancellation(client, controlKey) {
|
|
750
|
+
const state = await client.send("HGET", [controlKey, "state"]);
|
|
751
|
+
if (state !== "active" && state !== "cancelled")
|
|
752
|
+
throw new Error("Missing or invalid Streams control state");
|
|
753
|
+
return state === "cancelled";
|
|
754
|
+
}
|
|
755
|
+
var MAX_TIMER_MS = 2147483647;
|
|
756
|
+
async function readStreamTime(client) {
|
|
757
|
+
const value = await client.send("TIME", []);
|
|
758
|
+
if (!Array.isArray(value) || value.length !== 2 || typeof value[0] !== "string" || typeof value[1] !== "string" || !/^\d+$/.test(value[0]) || !/^\d+$/.test(value[1]))
|
|
759
|
+
throw new Error("Invalid Redis TIME response");
|
|
760
|
+
const seconds = Number(value[0]);
|
|
761
|
+
const micros = Number(value[1]);
|
|
762
|
+
const now = seconds * 1000 + Math.floor(micros / 1000);
|
|
763
|
+
if (!Number.isSafeInteger(seconds) || seconds < 0 || !Number.isSafeInteger(micros) || micros < 0 || micros >= 1e6 || !Number.isSafeInteger(now))
|
|
764
|
+
throw new Error("Invalid Redis TIME response");
|
|
765
|
+
return now;
|
|
766
|
+
}
|
|
767
|
+
function watchStreamDeadline(client, deadlineAtMs, remainingMs, expire, fail) {
|
|
768
|
+
let stopped = false;
|
|
769
|
+
let timer;
|
|
770
|
+
const schedule = (remaining) => {
|
|
771
|
+
timer = setTimeout(() => {
|
|
772
|
+
(async () => {
|
|
773
|
+
try {
|
|
774
|
+
const remaining = deadlineAtMs - await readStreamTime(client);
|
|
775
|
+
if (stopped)
|
|
776
|
+
return;
|
|
777
|
+
if (remaining <= 0)
|
|
778
|
+
expire();
|
|
779
|
+
else
|
|
780
|
+
schedule(remaining);
|
|
781
|
+
} catch (error) {
|
|
782
|
+
if (!stopped)
|
|
783
|
+
fail(error);
|
|
784
|
+
}
|
|
785
|
+
})();
|
|
786
|
+
}, Math.max(1, Math.min(MAX_TIMER_MS, remaining)));
|
|
787
|
+
};
|
|
788
|
+
schedule(remainingMs);
|
|
789
|
+
return () => {
|
|
790
|
+
stopped = true;
|
|
791
|
+
clearTimeout(timer);
|
|
792
|
+
};
|
|
793
|
+
}
|
|
794
|
+
async function scheduleStreamRetry(client, key, reservation, envelope, delayMs, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
795
|
+
if (!Number.isSafeInteger(delayMs) || delayMs < 0 || delayMs > Number.MAX_SAFE_INTEGER / 2)
|
|
796
|
+
throw new Error("Invalid Streams retry delay");
|
|
797
|
+
const scheduled = await client.send("EVAL", [
|
|
798
|
+
SCHEDULE_RETRY,
|
|
799
|
+
"3",
|
|
800
|
+
streamQueueKey(key),
|
|
801
|
+
streamRetryKey(key),
|
|
802
|
+
envelope.cancellable ? streamControlKey(key, envelope.jobId ?? "") : streamQueueKey(key),
|
|
803
|
+
STREAM_GROUP,
|
|
804
|
+
reservation.id,
|
|
805
|
+
reservation.owner,
|
|
806
|
+
String(visibilityMs),
|
|
807
|
+
String(delayMs),
|
|
808
|
+
JSON.stringify(envelope),
|
|
809
|
+
envelope.deadlineAtMs === undefined ? "" : String(envelope.deadlineAtMs),
|
|
810
|
+
envelope.cancellable ? "1" : "0"
|
|
811
|
+
]);
|
|
812
|
+
if (Number(scheduled) !== 1)
|
|
813
|
+
throw new Error("Queue lease lost while scheduling retry");
|
|
814
|
+
}
|
|
815
|
+
async function pollStreamPriorities(client, queueKeys, owner, cursors, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
816
|
+
const keys = [];
|
|
817
|
+
for (const key of queueKeys)
|
|
818
|
+
keys.push(streamQueueKey(key), streamRetryKey(key));
|
|
819
|
+
const raw = await client.send("EVAL", [
|
|
820
|
+
POLL_STREAM_PRIORITIES,
|
|
821
|
+
String(keys.length),
|
|
822
|
+
...keys,
|
|
823
|
+
STREAM_GROUP,
|
|
824
|
+
owner,
|
|
825
|
+
String(visibilityMs),
|
|
826
|
+
...cursors
|
|
827
|
+
]);
|
|
828
|
+
if (!Array.isArray(raw))
|
|
829
|
+
throw new Error("Invalid Redis stream poll response");
|
|
830
|
+
const flag = String(raw[0]);
|
|
831
|
+
const cursorCount = queueKeys.length;
|
|
832
|
+
if (flag === "0") {
|
|
833
|
+
if (raw.length !== cursorCount + 1)
|
|
834
|
+
throw new Error("Invalid Redis stream poll response");
|
|
835
|
+
return { cursors: raw.slice(1).map(String), hit: null };
|
|
836
|
+
}
|
|
837
|
+
if (flag !== "1" || raw.length !== cursorCount + 4)
|
|
838
|
+
throw new Error("Invalid Redis stream poll response");
|
|
839
|
+
const index = Number(raw[1]);
|
|
840
|
+
if (!Number.isInteger(index) || index < 1 || index > queueKeys.length)
|
|
841
|
+
throw new Error("Invalid Redis stream poll response");
|
|
842
|
+
return {
|
|
843
|
+
cursors: raw.slice(4).map(String),
|
|
844
|
+
hit: {
|
|
845
|
+
index: index - 1,
|
|
846
|
+
reservation: { id: String(raw[2]), owner, payload: String(raw[3]) }
|
|
847
|
+
}
|
|
848
|
+
};
|
|
849
|
+
}
|
|
850
|
+
async function readStreamQueueDepth(client, key) {
|
|
851
|
+
return Number(await client.send("EVAL", [
|
|
852
|
+
"return redis.call('XLEN', KEYS[1]) + redis.call('ZCARD', KEYS[2])",
|
|
853
|
+
"2",
|
|
854
|
+
streamQueueKey(key),
|
|
855
|
+
streamRetryKey(key)
|
|
856
|
+
]));
|
|
857
|
+
}
|
|
858
|
+
async function assertLegacyDrained(client, key) {
|
|
859
|
+
await client.send("EVAL", [
|
|
860
|
+
ASSERT_DRAINED,
|
|
861
|
+
"3",
|
|
862
|
+
key,
|
|
863
|
+
`${key}:processing`,
|
|
864
|
+
`${key}:processing:leases`
|
|
865
|
+
]);
|
|
866
|
+
}
|
|
867
|
+
async function ensureStreamGroup(client, key) {
|
|
868
|
+
await assertLegacyDrained(client, key);
|
|
869
|
+
try {
|
|
870
|
+
await client.send("XGROUP", ["CREATE", streamQueueKey(key), STREAM_GROUP, "0", "MKSTREAM"]);
|
|
871
|
+
} catch (error) {
|
|
872
|
+
if (!(error instanceof Error) || !error.message.includes("BUSYGROUP"))
|
|
873
|
+
throw error;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
async function updateStreamReservation(client, key, reservation, action, controlKey, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
877
|
+
return Number(await client.send("EVAL", [
|
|
878
|
+
UPDATE_STREAM,
|
|
879
|
+
"3",
|
|
880
|
+
streamQueueKey(key),
|
|
881
|
+
streamDeadLetterKey(key),
|
|
882
|
+
controlKey ?? streamQueueKey(key),
|
|
883
|
+
STREAM_GROUP,
|
|
884
|
+
reservation.id,
|
|
885
|
+
reservation.owner,
|
|
886
|
+
String(visibilityMs),
|
|
887
|
+
action,
|
|
888
|
+
reservation.payload,
|
|
889
|
+
controlKey ? "1" : "0"
|
|
890
|
+
])) === 1;
|
|
891
|
+
}
|
|
892
|
+
class RedisStreamsQueue {
|
|
893
|
+
registry;
|
|
894
|
+
queueKeys;
|
|
895
|
+
client;
|
|
896
|
+
constructor(redisUrl, registry, queueKeys = defaultQueueKeys()) {
|
|
897
|
+
this.registry = registry;
|
|
898
|
+
this.queueKeys = queueKeys;
|
|
899
|
+
this.queueKeys = Object.freeze([...queueKeys]);
|
|
900
|
+
this.client = new RedisClient(redisUrl);
|
|
901
|
+
}
|
|
902
|
+
close() {
|
|
903
|
+
this.client.close();
|
|
904
|
+
}
|
|
905
|
+
async dispatch(job, payload, options) {
|
|
906
|
+
await this.admit(job, payload, {}, undefined, options?.handoff);
|
|
907
|
+
}
|
|
908
|
+
async enqueue(job, payload, options = {}) {
|
|
909
|
+
return this.admit(job, payload, options);
|
|
910
|
+
}
|
|
911
|
+
async replay(job, payload, jobId, options) {
|
|
912
|
+
const prefix = this.queueKeys.find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
|
|
913
|
+
if (!prefix || !/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|[0-9]+-[0-9]+)$/.test(jobId.slice(streamQueueKey(prefix).length + 1)))
|
|
914
|
+
throw new Error("Replay identity does not belong to this Streams namespace");
|
|
915
|
+
await this.admit(job, payload, {}, jobId, options?.handoff);
|
|
916
|
+
}
|
|
917
|
+
async admit(job, payload, options, replayId, handoff) {
|
|
918
|
+
if (options.cancellable !== undefined && typeof options.cancellable !== "boolean")
|
|
919
|
+
throw new Error("cancellable must be a boolean");
|
|
920
|
+
if (options.timeoutMs !== undefined && (!Number.isSafeInteger(options.timeoutMs) || options.timeoutMs <= 0 || options.timeoutMs > MAX_TIMER_MS))
|
|
921
|
+
throw new Error("Streams timeoutMs must be an integer between 1 and 2147483647");
|
|
922
|
+
const name = this.registry.resolveName(job);
|
|
923
|
+
if (!name)
|
|
924
|
+
throw new Error("Job is not registered with the queue worker registry.");
|
|
925
|
+
const key = queueKeyForPriority(job.priority, this.queueKeys);
|
|
926
|
+
await assertLegacyDrained(this.client, key);
|
|
927
|
+
const jobId = replayId ?? `${streamQueueKey(key)}/${crypto.randomUUID()}`;
|
|
928
|
+
const deadlineAtMs = options.timeoutMs === undefined ? undefined : await readStreamTime(this.client) + options.timeoutMs;
|
|
929
|
+
const envelope = JSON.stringify({
|
|
930
|
+
name,
|
|
931
|
+
payload,
|
|
932
|
+
attempts: 0,
|
|
933
|
+
...handoff === undefined ? {} : { handoff },
|
|
934
|
+
jobId,
|
|
935
|
+
deadlineAtMs,
|
|
936
|
+
...options.cancellable ? { cancellable: true } : {}
|
|
937
|
+
});
|
|
938
|
+
if (options.cancellable)
|
|
939
|
+
await this.client.send("EVAL", [
|
|
940
|
+
ENQUEUE_CONTROLLED,
|
|
941
|
+
"2",
|
|
942
|
+
streamQueueKey(key),
|
|
943
|
+
streamControlKey(key, jobId),
|
|
944
|
+
envelope
|
|
945
|
+
]);
|
|
946
|
+
else
|
|
947
|
+
await this.client.send("XADD", [streamQueueKey(key), "*", "payload", envelope]);
|
|
948
|
+
return jobId;
|
|
949
|
+
}
|
|
950
|
+
async cancel(jobId) {
|
|
951
|
+
const key = this.queueKeys.find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
|
|
952
|
+
if (!key)
|
|
953
|
+
throw new Error("Invalid cancellable Streams job ID");
|
|
954
|
+
return cancelStreamJob(this.client, key, jobId);
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
class RedisStreamsWorker {
|
|
959
|
+
redisUrl;
|
|
960
|
+
failedJobs;
|
|
961
|
+
registry;
|
|
962
|
+
timeoutSeconds;
|
|
963
|
+
queueKeys;
|
|
964
|
+
visibilityMs;
|
|
965
|
+
retryPolicy;
|
|
966
|
+
client;
|
|
967
|
+
owner = crypto.randomUUID();
|
|
968
|
+
cursors = new Map;
|
|
969
|
+
processing = false;
|
|
970
|
+
initialized = false;
|
|
971
|
+
running = false;
|
|
972
|
+
stopping = false;
|
|
973
|
+
active;
|
|
974
|
+
constructor(redisUrl, failedJobs, registry, timeoutSeconds = 5, queueKeys = defaultQueueKeys(), retryPolicy = resolveQueueConfig(), visibilityMs = readQueueVisibilityMs()) {
|
|
975
|
+
this.redisUrl = redisUrl;
|
|
976
|
+
this.failedJobs = failedJobs;
|
|
977
|
+
this.registry = registry;
|
|
978
|
+
this.timeoutSeconds = timeoutSeconds;
|
|
979
|
+
this.queueKeys = queueKeys;
|
|
980
|
+
this.visibilityMs = visibilityMs;
|
|
981
|
+
this.queueKeys = Object.freeze([...queueKeys]);
|
|
982
|
+
this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
|
|
983
|
+
readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(this.visibilityMs) });
|
|
984
|
+
this.client = new RedisClient(redisUrl);
|
|
985
|
+
}
|
|
986
|
+
requestStop() {
|
|
987
|
+
this.stopping = true;
|
|
988
|
+
}
|
|
989
|
+
isRunning() {
|
|
990
|
+
return this.running;
|
|
991
|
+
}
|
|
992
|
+
close() {
|
|
993
|
+
this.stopping = true;
|
|
994
|
+
this.active?.abort(new Error("Queue worker closed"));
|
|
995
|
+
this.client.close();
|
|
996
|
+
}
|
|
997
|
+
async initialize() {
|
|
998
|
+
if (this.initialized)
|
|
999
|
+
return;
|
|
1000
|
+
for (const key of this.queueKeys)
|
|
1001
|
+
await ensureStreamGroup(this.client, key);
|
|
1002
|
+
this.initialized = true;
|
|
1003
|
+
}
|
|
1004
|
+
async processNext() {
|
|
1005
|
+
if (this.processing)
|
|
1006
|
+
throw new Error("Queue worker already processing a job.");
|
|
1007
|
+
this.processing = true;
|
|
1008
|
+
try {
|
|
1009
|
+
return await this.processOne();
|
|
1010
|
+
} finally {
|
|
1011
|
+
this.processing = false;
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
async processOne() {
|
|
1015
|
+
if (this.stopping)
|
|
1016
|
+
return false;
|
|
1017
|
+
await this.initialize();
|
|
1018
|
+
const deadline = performance.now() + this.timeoutSeconds * 1000;
|
|
1019
|
+
do {
|
|
1020
|
+
if (this.stopping)
|
|
1021
|
+
return false;
|
|
1022
|
+
const polled = await pollStreamPriorities(this.client, this.queueKeys, this.owner, this.queueKeys.map((key) => this.cursors.get(key) ?? "0-0"), this.visibilityMs);
|
|
1023
|
+
this.queueKeys.forEach((key, index) => {
|
|
1024
|
+
const cursor = polled.cursors[index];
|
|
1025
|
+
if (cursor)
|
|
1026
|
+
this.cursors.set(key, cursor);
|
|
1027
|
+
});
|
|
1028
|
+
if (polled.hit) {
|
|
1029
|
+
const key = this.queueKeys[polled.hit.index];
|
|
1030
|
+
if (!key)
|
|
1031
|
+
throw new Error("Invalid Redis stream poll response");
|
|
1032
|
+
await this.handle(key, polled.hit.reservation);
|
|
1033
|
+
return true;
|
|
1034
|
+
}
|
|
1035
|
+
if (performance.now() >= deadline || this.stopping)
|
|
1036
|
+
return false;
|
|
1037
|
+
await Bun.sleep(Math.min(100, Math.max(1, deadline - performance.now())));
|
|
1038
|
+
} while (!this.stopping);
|
|
1039
|
+
return false;
|
|
1040
|
+
}
|
|
1041
|
+
async handle(key, reservation) {
|
|
1042
|
+
const envelope = parseQueueJobEnvelope(reservation.payload, this.registry);
|
|
1043
|
+
if (!envelope) {
|
|
1044
|
+
await this.failedJobs.recordFailure({
|
|
1045
|
+
jobName: "queue.invalid-envelope",
|
|
1046
|
+
jobId: `${streamQueueKey(key)}/${reservation.id}`,
|
|
1047
|
+
payload: { raw: reservation.payload },
|
|
1048
|
+
exception: "Malformed or unknown queue envelope"
|
|
1049
|
+
});
|
|
1050
|
+
await updateStreamReservation(this.client, key, reservation, "quarantine", undefined, this.visibilityMs);
|
|
1051
|
+
return;
|
|
1052
|
+
}
|
|
1053
|
+
envelope.jobId ??= `${streamQueueKey(key)}/${reservation.id}`;
|
|
1054
|
+
let controlKey;
|
|
1055
|
+
if (envelope.cancellable) {
|
|
1056
|
+
try {
|
|
1057
|
+
controlKey = streamControlKey(key, envelope.jobId);
|
|
1058
|
+
} catch {
|
|
1059
|
+
await updateStreamReservation(this.client, key, reservation, "quarantine", undefined, this.visibilityMs);
|
|
1060
|
+
return;
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
const cancellationError = new Error("Streams job cancelled");
|
|
1064
|
+
if (controlKey && await readStreamCancellation(this.client, controlKey)) {
|
|
1065
|
+
await this.failedJobs.recordFailure({
|
|
1066
|
+
jobName: envelope.name,
|
|
1067
|
+
jobId: envelope.jobId,
|
|
1068
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
1069
|
+
payload: envelope.payload,
|
|
1070
|
+
exception: cancellationError.message
|
|
1071
|
+
});
|
|
1072
|
+
await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
|
|
1073
|
+
return;
|
|
1074
|
+
}
|
|
1075
|
+
const remainingMs = envelope.deadlineAtMs === undefined ? undefined : envelope.deadlineAtMs - await readStreamTime(this.client);
|
|
1076
|
+
const deadlineError = new Error("Streams job deadline exceeded");
|
|
1077
|
+
if (remainingMs !== undefined && remainingMs <= 0) {
|
|
1078
|
+
await this.failedJobs.recordFailure({
|
|
1079
|
+
jobName: envelope.name,
|
|
1080
|
+
jobId: envelope.jobId,
|
|
1081
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
1082
|
+
payload: envelope.payload,
|
|
1083
|
+
exception: deadlineError.message
|
|
1084
|
+
});
|
|
1085
|
+
await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
|
|
1086
|
+
return;
|
|
1087
|
+
}
|
|
1088
|
+
const controller = new AbortController;
|
|
1089
|
+
this.active = controller;
|
|
1090
|
+
let acknowledge = false;
|
|
1091
|
+
let invalidHandoff = false;
|
|
1092
|
+
let released = false;
|
|
1093
|
+
let ownershipLost = false;
|
|
1094
|
+
let renewal;
|
|
1095
|
+
let deadlineExpired = false;
|
|
1096
|
+
let cancellationRequested = false;
|
|
1097
|
+
let failureRecorded = false;
|
|
1098
|
+
const loseOwnership = (error) => {
|
|
1099
|
+
ownershipLost = true;
|
|
1100
|
+
controller.abort(error);
|
|
1101
|
+
};
|
|
1102
|
+
const stopDeadlineWatch = remainingMs === undefined || envelope.deadlineAtMs === undefined ? undefined : watchStreamDeadline(this.client, envelope.deadlineAtMs, remainingMs, () => {
|
|
1103
|
+
deadlineExpired = true;
|
|
1104
|
+
controller.abort(deadlineError);
|
|
1105
|
+
}, loseOwnership);
|
|
1106
|
+
const heartbeat = setInterval(() => {
|
|
1107
|
+
if (renewal || ownershipLost || released)
|
|
1108
|
+
return;
|
|
1109
|
+
renewal = (async () => {
|
|
1110
|
+
try {
|
|
1111
|
+
if (!await updateStreamReservation(this.client, key, reservation, "renew", controlKey, this.visibilityMs))
|
|
1112
|
+
loseOwnership(new Error("Queue lease lost"));
|
|
1113
|
+
else if (controlKey && await readStreamCancellation(this.client, controlKey)) {
|
|
1114
|
+
cancellationRequested = true;
|
|
1115
|
+
controller.abort(cancellationError);
|
|
1116
|
+
}
|
|
1117
|
+
} catch (error) {
|
|
1118
|
+
loseOwnership(error);
|
|
1119
|
+
console.error("[StreamsWorker] Lease renewal failed:", error);
|
|
1120
|
+
}
|
|
1121
|
+
})().finally(() => {
|
|
1122
|
+
renewal = undefined;
|
|
1123
|
+
});
|
|
1124
|
+
}, Math.max(1, Math.floor(this.visibilityMs / 3)));
|
|
1125
|
+
try {
|
|
1126
|
+
await runQueueJob(envelope, this.failedJobs, this.registry, {
|
|
1127
|
+
retryPolicy: this.retryPolicy,
|
|
1128
|
+
context: {
|
|
1129
|
+
jobId: envelope.jobId,
|
|
1130
|
+
signal: controller.signal,
|
|
1131
|
+
deadlineAtMs: envelope.deadlineAtMs
|
|
1132
|
+
},
|
|
1133
|
+
deferRetry: async (next, delayMs) => {
|
|
1134
|
+
controller.signal.throwIfAborted();
|
|
1135
|
+
await scheduleStreamRetry(this.client, key, reservation, next, delayMs, this.visibilityMs);
|
|
1136
|
+
released = true;
|
|
1137
|
+
},
|
|
1138
|
+
onFailureRecorded: () => {
|
|
1139
|
+
acknowledge = true;
|
|
1140
|
+
failureRecorded = true;
|
|
1141
|
+
}
|
|
1142
|
+
});
|
|
1143
|
+
acknowledge = true;
|
|
1144
|
+
} catch (error) {
|
|
1145
|
+
if (error instanceof QueueHandoffError2)
|
|
1146
|
+
invalidHandoff = true;
|
|
1147
|
+
else
|
|
1148
|
+
console.error("[StreamsWorker] Job failed:", error);
|
|
1149
|
+
} finally {
|
|
1150
|
+
stopDeadlineWatch?.();
|
|
1151
|
+
clearInterval(heartbeat);
|
|
1152
|
+
await renewal;
|
|
1153
|
+
this.active = undefined;
|
|
1154
|
+
}
|
|
1155
|
+
if (invalidHandoff && !ownershipLost && !released) {
|
|
1156
|
+
await updateStreamReservation(this.client, key, reservation, "quarantine", controlKey, this.visibilityMs);
|
|
1157
|
+
return;
|
|
1158
|
+
}
|
|
1159
|
+
if (!ownershipLost && !released && controlKey && (cancellationRequested || await readStreamCancellation(this.client, controlKey))) {
|
|
1160
|
+
if (!failureRecorded)
|
|
1161
|
+
await this.failedJobs.recordFailure({
|
|
1162
|
+
jobName: envelope.name,
|
|
1163
|
+
jobId: envelope.jobId,
|
|
1164
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
1165
|
+
payload: envelope.payload,
|
|
1166
|
+
exception: cancellationError.message
|
|
1167
|
+
});
|
|
1168
|
+
failureRecorded = true;
|
|
1169
|
+
acknowledge = true;
|
|
1170
|
+
}
|
|
1171
|
+
if (!ownershipLost && !released && envelope.deadlineAtMs !== undefined && (deadlineExpired || envelope.deadlineAtMs <= await readStreamTime(this.client))) {
|
|
1172
|
+
if (!failureRecorded)
|
|
1173
|
+
await this.failedJobs.recordFailure({
|
|
1174
|
+
jobName: envelope.name,
|
|
1175
|
+
jobId: envelope.jobId,
|
|
1176
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
1177
|
+
payload: envelope.payload,
|
|
1178
|
+
exception: deadlineError.message
|
|
1179
|
+
});
|
|
1180
|
+
acknowledge = true;
|
|
1181
|
+
}
|
|
1182
|
+
if (acknowledge && !ownershipLost && !released)
|
|
1183
|
+
await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
|
|
1184
|
+
}
|
|
1185
|
+
async run() {
|
|
1186
|
+
this.running = true;
|
|
1187
|
+
try {
|
|
1188
|
+
while (!this.stopping) {
|
|
1189
|
+
try {
|
|
1190
|
+
await this.processNext();
|
|
1191
|
+
} catch (error) {
|
|
1192
|
+
if (!(error && typeof error === "object" && ("code" in error) && error.code === "ERR_REDIS_CONNECTION_CLOSED"))
|
|
1193
|
+
throw error;
|
|
1194
|
+
this.client.close();
|
|
1195
|
+
await Bun.sleep(250);
|
|
1196
|
+
if (this.stopping)
|
|
1197
|
+
return;
|
|
1198
|
+
this.client = new RedisClient(this.redisUrl);
|
|
1199
|
+
this.initialized = false;
|
|
1200
|
+
this.cursors.clear();
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
} finally {
|
|
1204
|
+
this.running = false;
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
async function migrateLegacyQueueToStreams(client, options) {
|
|
1209
|
+
if (options.maintenance !== true)
|
|
1210
|
+
throw new Error("Queue conversion requires maintenance mode: stop all producers and workers first.");
|
|
1211
|
+
const batchSize = options.batchSize ?? 100;
|
|
1212
|
+
if (!Number.isSafeInteger(batchSize) || batchSize < 1 || batchSize > 1000)
|
|
1213
|
+
throw new Error("Queue conversion batchSize must be between 1 and 1000.");
|
|
1214
|
+
let converted = 0;
|
|
1215
|
+
for (const key of [...options.queueKeys ?? QUEUE_KEYS])
|
|
1216
|
+
converted += Number(await client.send("EVAL", [
|
|
1217
|
+
CONVERT_LIST,
|
|
1218
|
+
"4",
|
|
1219
|
+
key,
|
|
1220
|
+
`${key}:processing`,
|
|
1221
|
+
`${key}:processing:leases`,
|
|
1222
|
+
streamQueueKey(key),
|
|
1223
|
+
String(batchSize)
|
|
1224
|
+
]));
|
|
1225
|
+
return converted;
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
// ../../src/core/queue/redisQueue.ts
|
|
1229
|
+
function queueProcessingKey(queueKey) {
|
|
1230
|
+
return `${queueKey}:processing`;
|
|
1231
|
+
}
|
|
1232
|
+
function queueProcessingLeaseKey(queueKey) {
|
|
1233
|
+
return `${queueKey}:processing:leases`;
|
|
1234
|
+
}
|
|
1235
|
+
function reservationExpired(lease, now, visibilityMs) {
|
|
1236
|
+
if (lease === null || lease.length === 0) {
|
|
1237
|
+
return true;
|
|
1238
|
+
}
|
|
1239
|
+
const reservedAt = Number(lease);
|
|
1240
|
+
if (!Number.isFinite(reservedAt)) {
|
|
1241
|
+
return true;
|
|
1242
|
+
}
|
|
1243
|
+
return now - reservedAt >= visibilityMs;
|
|
1244
|
+
}
|
|
1245
|
+
function queueInvalidKey(queueKey) {
|
|
1246
|
+
return `${queueKey}:invalid`;
|
|
1247
|
+
}
|
|
1248
|
+
async function reserveQueueJob(client, queueKey, owner, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
1249
|
+
const raw = await client.send("EVAL", [
|
|
1250
|
+
RESERVE_JOB,
|
|
1251
|
+
"3",
|
|
1252
|
+
queueKey,
|
|
1253
|
+
queueProcessingKey(queueKey),
|
|
1254
|
+
queueProcessingLeaseKey(queueKey),
|
|
1255
|
+
crypto.randomUUID(),
|
|
1256
|
+
owner,
|
|
1257
|
+
String(visibilityMs)
|
|
1258
|
+
]);
|
|
1259
|
+
if (typeof raw !== "string")
|
|
1260
|
+
return null;
|
|
1261
|
+
const reservation = JSON.parse(raw);
|
|
1262
|
+
return { ...reservation, record: raw };
|
|
1263
|
+
}
|
|
1264
|
+
async function updateQueueReservation(client, queueKey, reservation, action, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
1265
|
+
return Number(await client.send("EVAL", [
|
|
1266
|
+
RENEW_OR_ACK,
|
|
1267
|
+
"3",
|
|
1268
|
+
queueProcessingKey(queueKey),
|
|
1269
|
+
queueProcessingLeaseKey(queueKey),
|
|
1270
|
+
queueInvalidKey(queueKey),
|
|
1271
|
+
reservation.id,
|
|
1272
|
+
reservation.owner,
|
|
1273
|
+
reservation.record,
|
|
1274
|
+
action,
|
|
1275
|
+
String(visibilityMs),
|
|
1276
|
+
reservation.payload
|
|
1277
|
+
])) === 1;
|
|
1278
|
+
}
|
|
1279
|
+
async function pollListPriorities(client, queueKeys, owner, reservationId, reclaim, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
1280
|
+
const keys = [];
|
|
1281
|
+
for (const key of queueKeys) {
|
|
1282
|
+
keys.push(key, queueProcessingKey(key), queueProcessingLeaseKey(key));
|
|
1283
|
+
}
|
|
1284
|
+
const raw = await client.send("EVAL", [
|
|
1285
|
+
POLL_LIST_PRIORITIES,
|
|
1286
|
+
String(keys.length),
|
|
1287
|
+
...keys,
|
|
1288
|
+
owner,
|
|
1289
|
+
String(visibilityMs),
|
|
1290
|
+
reservationId,
|
|
1291
|
+
reclaim ? "1" : "0"
|
|
1292
|
+
]);
|
|
1293
|
+
if (!Array.isArray(raw))
|
|
1294
|
+
throw new Error("Invalid Redis list poll response");
|
|
1295
|
+
const flag = String(raw[0]);
|
|
1296
|
+
if (flag === "0") {
|
|
1297
|
+
if (raw.length !== 1)
|
|
1298
|
+
throw new Error("Invalid Redis list poll response");
|
|
1299
|
+
return null;
|
|
1300
|
+
}
|
|
1301
|
+
if (flag !== "1" || raw.length !== 3)
|
|
1302
|
+
throw new Error("Invalid Redis list poll response");
|
|
1303
|
+
const index = Number(raw[1]);
|
|
1304
|
+
const record = raw[2];
|
|
1305
|
+
if (!Number.isInteger(index) || index < 1 || index > queueKeys.length || typeof record !== "string") {
|
|
1306
|
+
throw new Error("Invalid Redis list poll response");
|
|
1307
|
+
}
|
|
1308
|
+
const queueKey = queueKeys[index - 1];
|
|
1309
|
+
if (!queueKey)
|
|
1310
|
+
throw new Error("Invalid Redis list poll response");
|
|
1311
|
+
const reservation = JSON.parse(record);
|
|
1312
|
+
return { queueKey, reservation: { ...reservation, record } };
|
|
1313
|
+
}
|
|
1314
|
+
async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
1315
|
+
let reclaimed = 0;
|
|
1316
|
+
for (const key of queueKeys) {
|
|
1317
|
+
reclaimed += Number(await client.send("EVAL", [
|
|
1318
|
+
RECLAIM_JOBS,
|
|
1319
|
+
"3",
|
|
1320
|
+
key,
|
|
1321
|
+
queueProcessingKey(key),
|
|
1322
|
+
queueProcessingLeaseKey(key),
|
|
1323
|
+
String(visibilityMs)
|
|
1324
|
+
]));
|
|
1325
|
+
}
|
|
1326
|
+
return reclaimed;
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
class RedisQueue {
|
|
1330
|
+
registry;
|
|
1331
|
+
queueKeys;
|
|
1332
|
+
client;
|
|
1333
|
+
constructor(redisUrl, registry, queueKeys = defaultQueueKeys()) {
|
|
1334
|
+
this.registry = registry;
|
|
1335
|
+
this.queueKeys = queueKeys;
|
|
1336
|
+
this.queueKeys = Object.freeze([...queueKeys]);
|
|
1337
|
+
this.client = new RedisClient2(redisUrl);
|
|
1338
|
+
}
|
|
1339
|
+
close() {
|
|
1340
|
+
this.client.close();
|
|
1341
|
+
}
|
|
1342
|
+
async dispatch(job, payload, options) {
|
|
1343
|
+
await this.admit(job, payload, options);
|
|
1344
|
+
}
|
|
1345
|
+
async replay(job, payload, jobId, options) {
|
|
1346
|
+
if (typeof jobId !== "string" || !jobId.length)
|
|
1347
|
+
throw new TypeError("Replay identity is required.");
|
|
1348
|
+
await this.admit(job, payload, options, jobId);
|
|
1349
|
+
}
|
|
1350
|
+
async admit(job, payload, options, jobId) {
|
|
1351
|
+
const name = this.registry.resolveName(job);
|
|
1352
|
+
if (!name) {
|
|
1353
|
+
throw new Error("Job is not registered with the queue worker registry.");
|
|
1354
|
+
}
|
|
1355
|
+
const envelope = {
|
|
1356
|
+
name,
|
|
1357
|
+
jobId: jobId ?? crypto.randomUUID(),
|
|
1358
|
+
payload,
|
|
1359
|
+
attempts: 0,
|
|
1360
|
+
...options?.handoff === undefined ? {} : { handoff: options.handoff }
|
|
1361
|
+
};
|
|
1362
|
+
const queueKey = queueKeyForPriority(job.priority, this.queueKeys);
|
|
1363
|
+
await this.client.lpush(queueKey, JSON.stringify(envelope));
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
class QueueWorker {
|
|
1368
|
+
redisUrl;
|
|
1369
|
+
failedJobs;
|
|
1370
|
+
registry;
|
|
1371
|
+
timeoutSeconds;
|
|
1372
|
+
queueKeys;
|
|
1373
|
+
visibilityMs;
|
|
1374
|
+
retryPolicy;
|
|
1375
|
+
owner = crypto.randomUUID();
|
|
1376
|
+
running = false;
|
|
1377
|
+
stopping = false;
|
|
1378
|
+
client;
|
|
1379
|
+
constructor(redisUrl, failedJobs, registry, timeoutSeconds = 5, queueKeys = defaultQueueKeys(), retryPolicy = resolveQueueConfig(), visibilityMs = readQueueVisibilityMs()) {
|
|
1380
|
+
this.redisUrl = redisUrl;
|
|
1381
|
+
this.failedJobs = failedJobs;
|
|
1382
|
+
this.registry = registry;
|
|
1383
|
+
this.timeoutSeconds = timeoutSeconds;
|
|
1384
|
+
this.queueKeys = queueKeys;
|
|
1385
|
+
this.visibilityMs = visibilityMs;
|
|
1386
|
+
this.queueKeys = Object.freeze([...queueKeys]);
|
|
1387
|
+
this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
|
|
1388
|
+
readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(this.visibilityMs) });
|
|
1389
|
+
this.client = new RedisClient2(redisUrl);
|
|
1390
|
+
}
|
|
1391
|
+
requestStop() {
|
|
1392
|
+
this.stopping = true;
|
|
1393
|
+
}
|
|
1394
|
+
isRunning() {
|
|
1395
|
+
return this.running;
|
|
1396
|
+
}
|
|
1397
|
+
async reserve() {
|
|
1398
|
+
const deadline = performance.now() + this.timeoutSeconds * 1000;
|
|
1399
|
+
let reclaim = true;
|
|
1400
|
+
do {
|
|
1401
|
+
if (this.stopping)
|
|
1402
|
+
return null;
|
|
1403
|
+
const reserved = await pollListPriorities(this.client, this.queueKeys, this.owner, crypto.randomUUID(), reclaim, this.visibilityMs);
|
|
1404
|
+
reclaim = false;
|
|
1405
|
+
if (reserved)
|
|
1406
|
+
return reserved;
|
|
1407
|
+
if (this.stopping || performance.now() >= deadline)
|
|
1408
|
+
return null;
|
|
1409
|
+
await Bun.sleep(Math.min(100, Math.max(1, deadline - performance.now())));
|
|
1410
|
+
} while (!this.stopping);
|
|
1411
|
+
return null;
|
|
1412
|
+
}
|
|
1413
|
+
async processNext() {
|
|
1414
|
+
if (this.stopping)
|
|
1415
|
+
return false;
|
|
1416
|
+
const reserved = await this.reserve();
|
|
1417
|
+
if (!reserved)
|
|
1418
|
+
return false;
|
|
1419
|
+
const { queueKey, reservation } = reserved;
|
|
1420
|
+
const envelope = parseQueueJobEnvelope(reservation.payload, this.registry);
|
|
1421
|
+
if (!envelope) {
|
|
1422
|
+
await updateQueueReservation(this.client, queueKey, reservation, "quarantine", this.visibilityMs);
|
|
1423
|
+
return true;
|
|
1424
|
+
}
|
|
1425
|
+
let acknowledge = false;
|
|
1426
|
+
let invalidHandoff = false;
|
|
1427
|
+
let ownershipLost = false;
|
|
1428
|
+
let renewal;
|
|
1429
|
+
const heartbeat = setInterval(() => {
|
|
1430
|
+
if (renewal || ownershipLost)
|
|
1431
|
+
return;
|
|
1432
|
+
renewal = (async () => {
|
|
1433
|
+
try {
|
|
1434
|
+
if (!await updateQueueReservation(this.client, queueKey, reservation, "renew", this.visibilityMs)) {
|
|
1435
|
+
ownershipLost = true;
|
|
1436
|
+
}
|
|
1437
|
+
} catch (error) {
|
|
1438
|
+
ownershipLost = true;
|
|
1439
|
+
console.error("[QueueWorker] Lease renewal failed:", error);
|
|
1440
|
+
}
|
|
1441
|
+
})().finally(() => {
|
|
1442
|
+
renewal = undefined;
|
|
1443
|
+
});
|
|
1444
|
+
}, Math.max(1, Math.floor(this.visibilityMs / 3)));
|
|
1445
|
+
try {
|
|
1446
|
+
await runQueueJob(envelope, this.failedJobs, this.registry, {
|
|
1447
|
+
retryPolicy: this.retryPolicy,
|
|
1448
|
+
onFailureRecorded: () => {
|
|
1449
|
+
acknowledge = true;
|
|
1450
|
+
}
|
|
1451
|
+
});
|
|
1452
|
+
acknowledge = true;
|
|
1453
|
+
} catch (error) {
|
|
1454
|
+
if (error instanceof QueueHandoffError3)
|
|
1455
|
+
invalidHandoff = true;
|
|
1456
|
+
else
|
|
1457
|
+
console.error("[QueueWorker] Job failed:", error);
|
|
1458
|
+
} finally {
|
|
1459
|
+
clearInterval(heartbeat);
|
|
1460
|
+
await renewal;
|
|
1461
|
+
}
|
|
1462
|
+
if (invalidHandoff && !ownershipLost) {
|
|
1463
|
+
await updateQueueReservation(this.client, queueKey, reservation, "quarantine", this.visibilityMs);
|
|
1464
|
+
return true;
|
|
1465
|
+
}
|
|
1466
|
+
if (acknowledge && !ownershipLost) {
|
|
1467
|
+
await updateQueueReservation(this.client, queueKey, reservation, "ack", this.visibilityMs);
|
|
1468
|
+
}
|
|
1469
|
+
return true;
|
|
1470
|
+
}
|
|
1471
|
+
async run() {
|
|
1472
|
+
this.running = true;
|
|
1473
|
+
try {
|
|
1474
|
+
while (!this.stopping) {
|
|
1475
|
+
try {
|
|
1476
|
+
await this.processNext();
|
|
1477
|
+
} catch (error) {
|
|
1478
|
+
if (!(error && typeof error === "object" && ("code" in error) && error.code === "ERR_REDIS_CONNECTION_CLOSED"))
|
|
1479
|
+
throw error;
|
|
1480
|
+
this.client.close();
|
|
1481
|
+
await Bun.sleep(250);
|
|
1482
|
+
if (this.stopping)
|
|
1483
|
+
return;
|
|
1484
|
+
this.client = new RedisClient2(this.redisUrl);
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
} finally {
|
|
1488
|
+
this.running = false;
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
close() {
|
|
1492
|
+
this.stopping = true;
|
|
1493
|
+
this.client.close();
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
async function countPendingQueueJobs(redisUrl, config = resolveQueueRuntimeConfig({})) {
|
|
1497
|
+
const { transport, queueKeys } = snapshotQueueRuntimeConfig(config);
|
|
1498
|
+
const client = new RedisClient2(redisUrl);
|
|
1499
|
+
try {
|
|
1500
|
+
let total = 0;
|
|
1501
|
+
for (const queueKey of queueKeys) {
|
|
1502
|
+
total += transport === "streams" ? await readStreamQueueDepth(client, queueKey) : await client.llen(queueKey);
|
|
1503
|
+
}
|
|
1504
|
+
return total;
|
|
1505
|
+
} finally {
|
|
1506
|
+
client.close();
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
// ../../src/core/queue/index.ts
|
|
1511
|
+
class Job {
|
|
1512
|
+
static jobName;
|
|
1513
|
+
maxAttempts;
|
|
1514
|
+
backoffMs;
|
|
1515
|
+
priority;
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
class SyncQueue {
|
|
1519
|
+
async dispatch(job, payload, options) {
|
|
1520
|
+
if (options?.handoff !== undefined)
|
|
1521
|
+
throw new Error("Identity handoff requires an application queue.");
|
|
1522
|
+
await job.handle(payload);
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
class AsyncQueue {
|
|
1527
|
+
active = new Set;
|
|
1528
|
+
closing;
|
|
1529
|
+
closed = false;
|
|
1530
|
+
assertOpen() {
|
|
1531
|
+
if (this.closed)
|
|
1532
|
+
throw new Error("Async queue is closed.");
|
|
1533
|
+
}
|
|
1534
|
+
enqueue(task, onError) {
|
|
1535
|
+
this.assertOpen();
|
|
1536
|
+
const running = new Promise((resolve) => {
|
|
1537
|
+
setTimeout(() => {
|
|
1538
|
+
Promise.resolve().then(task).catch(onError).finally(resolve);
|
|
1539
|
+
}, 0);
|
|
1540
|
+
});
|
|
1541
|
+
this.active.add(running);
|
|
1542
|
+
running.then(() => {
|
|
1543
|
+
this.active.delete(running);
|
|
1544
|
+
});
|
|
1545
|
+
}
|
|
1546
|
+
async dispatch(job, payload, options) {
|
|
1547
|
+
if (options?.handoff !== undefined)
|
|
1548
|
+
throw new Error("Identity handoff requires an application queue.");
|
|
1549
|
+
this.enqueue(() => job.handle(payload), (error) => {
|
|
1550
|
+
console.error("[AsyncQueue] Job failed:", error);
|
|
1551
|
+
});
|
|
1552
|
+
}
|
|
1553
|
+
close() {
|
|
1554
|
+
this.closing ??= (async () => {
|
|
1555
|
+
while (this.active.size)
|
|
1556
|
+
await Promise.all([...this.active]);
|
|
1557
|
+
this.closed = true;
|
|
1558
|
+
})();
|
|
1559
|
+
return this.closing;
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1562
|
+
function createQueue(driver) {
|
|
1563
|
+
return driver === "async" ? new AsyncQueue : new SyncQueue;
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
// ../../src/core/queue/resilientQueue.ts
|
|
1567
|
+
class ResilientQueue extends AsyncQueue {
|
|
1568
|
+
failedJobs;
|
|
1569
|
+
registry;
|
|
1570
|
+
asyncDispatch;
|
|
1571
|
+
retryPolicy;
|
|
1572
|
+
constructor(failedJobs, registry, asyncDispatch = false, retryPolicy = resolveQueueConfig()) {
|
|
1573
|
+
super();
|
|
1574
|
+
this.failedJobs = failedJobs;
|
|
1575
|
+
this.registry = registry;
|
|
1576
|
+
this.asyncDispatch = asyncDispatch;
|
|
1577
|
+
this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
|
|
1578
|
+
}
|
|
1579
|
+
async dispatch(job, payload, options) {
|
|
1580
|
+
await this.admit(job, payload, options);
|
|
1581
|
+
}
|
|
1582
|
+
async replay(job, payload, jobId, options) {
|
|
1583
|
+
if (typeof jobId !== "string" || !jobId.length)
|
|
1584
|
+
throw new TypeError("Replay identity is required.");
|
|
1585
|
+
await this.admit(job, payload, options, jobId);
|
|
1586
|
+
}
|
|
1587
|
+
async admit(job, payload, options, jobId) {
|
|
1588
|
+
this.assertOpen();
|
|
1589
|
+
const name = this.registry.resolveName(job);
|
|
1590
|
+
if (!name) {
|
|
1591
|
+
throw new Error("Job is not registered with the queue worker registry.");
|
|
1592
|
+
}
|
|
1593
|
+
const envelope = snapshotQueueJobEnvelope({
|
|
1594
|
+
name,
|
|
1595
|
+
jobId: jobId ?? crypto.randomUUID(),
|
|
1596
|
+
payload,
|
|
1597
|
+
attempts: 0,
|
|
1598
|
+
...options?.handoff === undefined ? {} : { handoff: options.handoff }
|
|
1599
|
+
});
|
|
1600
|
+
if (this.asyncDispatch) {
|
|
1601
|
+
this.enqueue(() => runQueueJob(envelope, this.failedJobs, this.registry, { retryPolicy: this.retryPolicy }), (error) => {
|
|
1602
|
+
console.error("[ResilientQueue] Job failed:", error);
|
|
1603
|
+
});
|
|
1604
|
+
return;
|
|
1605
|
+
}
|
|
1606
|
+
await runQueueJob(envelope, this.failedJobs, this.registry, { retryPolicy: this.retryPolicy });
|
|
1607
|
+
}
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
// ../../src/core/queue/publicQueue.ts
|
|
1611
|
+
import { createIdentityQueue, createQueueHandoff } from "@getstrata/core/queue/queueHandoff";
|
|
1612
|
+
function createTrackedJob(registry, name, job) {
|
|
1613
|
+
return registry.track(name, job);
|
|
1614
|
+
}
|
|
1615
|
+
function createProductionQueue(driver, options) {
|
|
1616
|
+
const failedJobs = options.failedJobs;
|
|
1617
|
+
if (driver === "redis") {
|
|
1618
|
+
if (!options.redisUrl) {
|
|
1619
|
+
throw new Error('QUEUE_DRIVER="redis" requires REDIS_URL to be set.');
|
|
1620
|
+
}
|
|
1621
|
+
const runtime = snapshotQueueRuntimeConfig(options.runtime ?? resolveQueueRuntimeConfig());
|
|
1622
|
+
return runtime.transport === "streams" ? new RedisStreamsQueue(options.redisUrl, options.registry, runtime.queueKeys) : new RedisQueue(options.redisUrl, options.registry, runtime.queueKeys);
|
|
1623
|
+
}
|
|
1624
|
+
return new ResilientQueue(failedJobs, options.registry, driver === "async", snapshotQueueRetryPolicy(options.retryPolicy ?? resolveQueueConfig()));
|
|
1625
|
+
}
|
|
1626
|
+
function createQueueWorker(redisUrl, failedJobs, registry, options = {}) {
|
|
1627
|
+
const runtime = snapshotQueueRuntimeConfig(options.runtime ?? resolveQueueRuntimeConfig());
|
|
1628
|
+
const retry = snapshotQueueRetryPolicy(options.retryPolicy ?? resolveQueueConfig());
|
|
1629
|
+
return runtime.transport === "streams" ? new RedisStreamsWorker(redisUrl, failedJobs, registry, 5, runtime.queueKeys, retry, runtime.visibilityMs) : new QueueWorker(redisUrl, failedJobs, registry, 5, runtime.queueKeys, retry, runtime.visibilityMs);
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
// ../../src/core/queue/queueWorkerTransport.ts
|
|
1633
|
+
function createQueueWorkerTransport(options) {
|
|
1634
|
+
const runtime = snapshotQueueRuntimeConfig(options.runtime ?? resolveQueueRuntimeConfig());
|
|
1635
|
+
const retryPolicy = snapshotQueueRetryPolicy(options.retryPolicy ?? resolveQueueConfig());
|
|
1636
|
+
const { redisUrl, queueServices } = options;
|
|
1637
|
+
const registrySource = options.registry;
|
|
1638
|
+
const createWorker = options.createWorker ?? ((url, services, registry) => createQueueWorker(url, services, registry, { runtime, retryPolicy }));
|
|
1639
|
+
const name = options.name ?? "queue.worker";
|
|
1640
|
+
let worker;
|
|
1641
|
+
let running;
|
|
1642
|
+
let stopping = false;
|
|
1643
|
+
let acquired = false;
|
|
1644
|
+
const active = new Set;
|
|
1645
|
+
function track(promise) {
|
|
1646
|
+
active.add(promise);
|
|
1647
|
+
promise.finally(() => active.delete(promise)).catch(() => {});
|
|
1648
|
+
return promise;
|
|
1649
|
+
}
|
|
1650
|
+
return Object.freeze({
|
|
1651
|
+
transport: {
|
|
1652
|
+
name,
|
|
1653
|
+
async setup(scope) {
|
|
1654
|
+
await scope.acquire({
|
|
1655
|
+
name,
|
|
1656
|
+
open: () => {
|
|
1657
|
+
try {
|
|
1658
|
+
worker = createWorker(redisUrl, queueServices, typeof registrySource === "function" ? registrySource() : registrySource);
|
|
1659
|
+
acquired = true;
|
|
1660
|
+
return { ok: true, value: worker };
|
|
1661
|
+
} catch (error) {
|
|
1662
|
+
return { ok: false, error, disposition: "unknown" };
|
|
1663
|
+
}
|
|
1664
|
+
},
|
|
1665
|
+
stop: (value) => {
|
|
1666
|
+
stopping = true;
|
|
1667
|
+
value.requestStop();
|
|
1668
|
+
},
|
|
1669
|
+
drain: async () => {
|
|
1670
|
+
while (active.size)
|
|
1671
|
+
await Promise.allSettled([...active]);
|
|
1672
|
+
},
|
|
1673
|
+
close: async (value) => {
|
|
1674
|
+
await value.close();
|
|
1675
|
+
acquired = false;
|
|
1676
|
+
}
|
|
1677
|
+
});
|
|
1678
|
+
}
|
|
1679
|
+
},
|
|
1680
|
+
start() {
|
|
1681
|
+
if (running)
|
|
1682
|
+
return running;
|
|
1683
|
+
if (!worker || !acquired || stopping || active.size)
|
|
1684
|
+
return Promise.reject(new Error("Queue worker is not available for admission."));
|
|
1685
|
+
const selected = worker;
|
|
1686
|
+
running = track(Promise.resolve().then(() => selected.run()));
|
|
1687
|
+
running.catch(() => {});
|
|
1688
|
+
return running;
|
|
1689
|
+
},
|
|
1690
|
+
processNext() {
|
|
1691
|
+
if (!worker || !acquired || stopping || running || active.size)
|
|
1692
|
+
return Promise.reject(new Error("Queue worker is not available for bounded admission."));
|
|
1693
|
+
const selected = worker;
|
|
1694
|
+
if (!selected.processNext)
|
|
1695
|
+
return Promise.reject(new Error("Queue worker does not support bounded polling."));
|
|
1696
|
+
const poll = selected.processNext.bind(selected);
|
|
1697
|
+
return track(Promise.resolve().then(() => poll()));
|
|
1698
|
+
}
|
|
1699
|
+
});
|
|
1700
|
+
}
|
|
1701
|
+
export {
|
|
1702
|
+
createQueueWorkerTransport
|
|
1703
|
+
};
|