@getstrata/core 2.2.60 → 3.0.4
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 +26 -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
|
@@ -1,47 +1,9 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
// ../../src/core/queue/
|
|
3
|
-
import {
|
|
4
|
-
import { currentSqlDialect, sqlTimestamp } from "@getstrata/core/database/dialect";
|
|
5
|
-
|
|
6
|
-
// ../../src/core/queue/failedJobTable.ts
|
|
7
|
-
import { defineTable } from "@getstrata/core/database/table";
|
|
8
|
-
var failedJobTable = defineTable({
|
|
9
|
-
name: "failed_job",
|
|
10
|
-
primaryKey: "id",
|
|
11
|
-
columns: ["id", "job_name", "job_id", "payload", "exception", "failed_at"],
|
|
12
|
-
defaultOrderBy: { column: "failed_at", direction: "DESC" }
|
|
13
|
-
});
|
|
2
|
+
// ../../src/core/queue/createAppQueue.ts
|
|
3
|
+
import { FAILED_JOB_SERVICE_TOKEN } from "@getstrata/core/contracts/serviceTokens";
|
|
14
4
|
|
|
15
|
-
// ../../src/core/queue/
|
|
16
|
-
|
|
17
|
-
constructor() {
|
|
18
|
-
super(failedJobTable);
|
|
19
|
-
}
|
|
20
|
-
async create(values) {
|
|
21
|
-
const stored = {
|
|
22
|
-
...values,
|
|
23
|
-
...values.payload === undefined ? {} : {
|
|
24
|
-
payload: currentSqlDialect().driver === "pgsql" ? values.payload : JSON.stringify(values.payload)
|
|
25
|
-
},
|
|
26
|
-
...values.failed_at === undefined ? {} : { failed_at: sqlTimestamp(values.failed_at) }
|
|
27
|
-
};
|
|
28
|
-
return hydrateFailure(await super.create(stored));
|
|
29
|
-
}
|
|
30
|
-
async findAll(options = {}) {
|
|
31
|
-
const rows = await super.findAll(options);
|
|
32
|
-
return options.select?.length ? rows : rows.map(hydrateFailure);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
function hydrateFailure(row) {
|
|
36
|
-
const result = { ...row };
|
|
37
|
-
if (typeof row.payload === "string")
|
|
38
|
-
result.payload = JSON.parse(row.payload);
|
|
39
|
-
if ("failed_at" in row && !(row.failed_at instanceof Date)) {
|
|
40
|
-
result.failed_at = new Date(row.failed_at);
|
|
41
|
-
}
|
|
42
|
-
return result;
|
|
43
|
-
}
|
|
44
|
-
var failedJobRepository_default = FailedJobRepository;
|
|
5
|
+
// ../../src/core/queue/publicQueue.ts
|
|
6
|
+
import FailedJobRepository from "@getstrata/core/queue/failedJobRepository";
|
|
45
7
|
|
|
46
8
|
// ../../src/core/queue/failedJobService.ts
|
|
47
9
|
class FailedJobService {
|
|
@@ -53,6 +15,7 @@ class FailedJobService {
|
|
|
53
15
|
return await this.repository.create({
|
|
54
16
|
job_name: input.jobName,
|
|
55
17
|
job_id: input.jobId ?? null,
|
|
18
|
+
...input.handoff === undefined ? {} : { handoff: input.handoff },
|
|
56
19
|
payload: input.payload,
|
|
57
20
|
exception: input.exception,
|
|
58
21
|
failed_at: new Date
|
|
@@ -91,83 +54,133 @@ class FailedJobService {
|
|
|
91
54
|
}
|
|
92
55
|
var failedJobService_default = FailedJobService;
|
|
93
56
|
|
|
94
|
-
// ../../src/core/queue/
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
57
|
+
// ../../src/core/queue/jobRunner.ts
|
|
58
|
+
import { setTimeout as delay } from "timers/promises";
|
|
59
|
+
|
|
60
|
+
// ../../src/core/config/configSnapshot.ts
|
|
61
|
+
function copyApplicationConfig(value, ancestors = new Set) {
|
|
62
|
+
if (value === null || typeof value === "string" || typeof value === "boolean")
|
|
63
|
+
return value;
|
|
64
|
+
if (typeof value === "number" && Number.isFinite(value))
|
|
65
|
+
return value;
|
|
66
|
+
if (typeof value !== "object" || ancestors.has(value)) {
|
|
67
|
+
throw new TypeError("Application config must contain acyclic plain data.");
|
|
68
|
+
}
|
|
69
|
+
const array = Array.isArray(value);
|
|
70
|
+
const prototype = Object.getPrototypeOf(value);
|
|
71
|
+
if (prototype !== (array ? Array.prototype : Object.prototype) && !(prototype === null && !array)) {
|
|
72
|
+
throw new TypeError("Application config must contain only plain objects and arrays.");
|
|
73
|
+
}
|
|
74
|
+
if (Object.getOwnPropertySymbols(value).length > 0) {
|
|
75
|
+
throw new TypeError("Application config cannot contain symbol keys.");
|
|
76
|
+
}
|
|
77
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
78
|
+
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
79
|
+
if (!(array && key === "length") && (!descriptor.enumerable || !("value" in descriptor))) {
|
|
80
|
+
throw new TypeError("Application config cannot contain accessors or hidden properties.");
|
|
112
81
|
}
|
|
113
|
-
return factory();
|
|
114
|
-
}
|
|
115
|
-
names() {
|
|
116
|
-
return [...this.factories.keys()];
|
|
117
82
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
83
|
+
ancestors.add(value);
|
|
84
|
+
try {
|
|
85
|
+
if (array) {
|
|
86
|
+
const length = descriptors.length.value;
|
|
87
|
+
if (Object.keys(descriptors).length !== length + 1) {
|
|
88
|
+
throw new TypeError("Application config arrays must be dense and have no extra properties.");
|
|
89
|
+
}
|
|
90
|
+
const result = [];
|
|
91
|
+
for (let index = 0;index < length; index++) {
|
|
92
|
+
const descriptor = descriptors[String(index)];
|
|
93
|
+
if (!descriptor)
|
|
94
|
+
throw new TypeError("Application config arrays must be dense.");
|
|
95
|
+
result.push(copyApplicationConfig(descriptor.value, ancestors));
|
|
96
|
+
}
|
|
97
|
+
return Object.freeze(result);
|
|
98
|
+
}
|
|
99
|
+
const result = {};
|
|
100
|
+
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
101
|
+
Object.defineProperty(result, key, {
|
|
102
|
+
value: copyApplicationConfig(descriptor.value, ancestors),
|
|
103
|
+
enumerable: true
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return Object.freeze(result);
|
|
107
|
+
} finally {
|
|
108
|
+
ancestors.delete(value);
|
|
124
109
|
}
|
|
125
|
-
const registry = new JobRegistry;
|
|
126
|
-
globalThis[JOB_REGISTRY_KEY] = registry;
|
|
127
|
-
return registry;
|
|
128
110
|
}
|
|
129
|
-
var jobRegistry = readSharedJobRegistry();
|
|
130
|
-
|
|
131
|
-
// ../../src/core/queue/jobRunner.ts
|
|
132
|
-
import { setTimeout as delay } from "timers/promises";
|
|
133
111
|
|
|
134
112
|
// ../../src/core/queue/queueConfig.ts
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const maxAttempts
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
113
|
+
var DEFAULT_QUEUE_VISIBILITY_MS = 60000;
|
|
114
|
+
function snapshotQueueRetryPolicy(policy) {
|
|
115
|
+
const { maxAttempts, backoffMs } = policy;
|
|
116
|
+
if (!Number.isSafeInteger(maxAttempts) || maxAttempts < 1)
|
|
117
|
+
throw new TypeError("QUEUE_MAX_ATTEMPTS must be a positive safe integer.");
|
|
118
|
+
if (!Number.isSafeInteger(backoffMs) || backoffMs < 0 || backoffMs > 2147483647)
|
|
119
|
+
throw new TypeError("QUEUE_BACKOFF_MS must be a non-negative integer within the timer range.");
|
|
120
|
+
return Object.freeze({ maxAttempts, backoffMs });
|
|
121
|
+
}
|
|
122
|
+
function resolveQueueConfig(env = process.env) {
|
|
123
|
+
const driver = env.QUEUE_DRIVER ?? "sync";
|
|
124
|
+
if (!["sync", "async", "redis"].includes(driver))
|
|
125
|
+
throw new TypeError("Unknown queue driver.");
|
|
126
|
+
const policy = snapshotQueueRetryPolicy({
|
|
127
|
+
maxAttempts: Number(env.QUEUE_MAX_ATTEMPTS ?? "3"),
|
|
128
|
+
backoffMs: Number(env.QUEUE_BACKOFF_MS ?? "1000")
|
|
129
|
+
});
|
|
130
|
+
return Object.freeze({ driver, ...policy });
|
|
144
131
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
return resolveQueueConfig().driver;
|
|
148
|
-
},
|
|
149
|
-
get maxAttempts() {
|
|
150
|
-
return resolveQueueConfig().maxAttempts;
|
|
151
|
-
},
|
|
152
|
-
get backoffMs() {
|
|
153
|
-
return resolveQueueConfig().backoffMs;
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
function resolveRedisQueueTransport() {
|
|
157
|
-
const transport = process.env.QUEUE_REDIS_TRANSPORT ?? "lists";
|
|
132
|
+
function resolveRedisQueueTransport(env = process.env) {
|
|
133
|
+
const transport = env.QUEUE_REDIS_TRANSPORT ?? "lists";
|
|
158
134
|
if (transport !== "lists" && transport !== "streams")
|
|
159
135
|
throw new Error('QUEUE_REDIS_TRANSPORT must be "lists" or "streams".');
|
|
160
136
|
return transport;
|
|
161
137
|
}
|
|
162
|
-
function readQueueVisibilityMs() {
|
|
163
|
-
const value = Number(
|
|
164
|
-
if (!Number.isSafeInteger(value) || value < 30)
|
|
165
|
-
throw new Error("QUEUE_VISIBILITY_MS must be an integer
|
|
138
|
+
function readQueueVisibilityMs(env = process.env) {
|
|
139
|
+
const value = Number(env.QUEUE_VISIBILITY_MS ?? "60000");
|
|
140
|
+
if (!Number.isSafeInteger(value) || value < 30 || value > 2147483647)
|
|
141
|
+
throw new Error("QUEUE_VISIBILITY_MS must be an integer from 30 through 2147483647 milliseconds.");
|
|
166
142
|
return value;
|
|
167
143
|
}
|
|
144
|
+
function snapshotQueueRuntimeConfig(config) {
|
|
145
|
+
if (config.transport !== "lists" && config.transport !== "streams" || typeof config.visibilityMs !== "number")
|
|
146
|
+
throw new TypeError("Queue runtime requires explicit transport and numeric visibility.");
|
|
147
|
+
if (!Array.isArray(config.queueKeys) || config.queueKeys.length !== 3 || config.queueKeys.some((key) => typeof key !== "string" || !key.length) || new Set(config.queueKeys).size !== 3)
|
|
148
|
+
throw new TypeError("Queue runtime requires three distinct nonempty priority keys.");
|
|
149
|
+
return Object.freeze({
|
|
150
|
+
queueKeys: Object.freeze([...config.queueKeys]),
|
|
151
|
+
transport: resolveRedisQueueTransport({ QUEUE_REDIS_TRANSPORT: config.transport }),
|
|
152
|
+
visibilityMs: readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(config.visibilityMs) })
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
function resolveQueueRuntimeConfig(env = process.env) {
|
|
156
|
+
const prefix = env.APP_KEY_PREFIX?.trim() || "strata";
|
|
157
|
+
return snapshotQueueRuntimeConfig({
|
|
158
|
+
transport: resolveRedisQueueTransport(env),
|
|
159
|
+
visibilityMs: readQueueVisibilityMs(env),
|
|
160
|
+
queueKeys: [`${prefix}:queue:high`, `${prefix}:queue:default`, `${prefix}:queue:low`]
|
|
161
|
+
});
|
|
162
|
+
}
|
|
168
163
|
|
|
169
164
|
// ../../src/core/queue/jobRunner.ts
|
|
170
|
-
function
|
|
165
|
+
function snapshotQueueJobEnvelope(input) {
|
|
166
|
+
if (!input || typeof input !== "object" || Array.isArray(input) || ![Object.prototype, null].includes(Object.getPrototypeOf(input)))
|
|
167
|
+
throw new TypeError("Queue envelope must be plain data.");
|
|
168
|
+
const data = {};
|
|
169
|
+
for (const key of Reflect.ownKeys(input)) {
|
|
170
|
+
if (typeof key !== "string" || !["name", "payload", "attempts", "jobId", "deadlineAtMs", "cancellable", "handoff"].includes(key))
|
|
171
|
+
throw new TypeError("Unknown queue envelope field.");
|
|
172
|
+
const descriptor = Object.getOwnPropertyDescriptor(input, key);
|
|
173
|
+
if (!descriptor || !("value" in descriptor) || !descriptor.enumerable)
|
|
174
|
+
throw new TypeError("Queue envelope must contain own data fields.");
|
|
175
|
+
if (descriptor.value !== undefined)
|
|
176
|
+
Object.defineProperty(data, key, { value: descriptor.value, enumerable: true });
|
|
177
|
+
}
|
|
178
|
+
const captured = copyApplicationConfig(data);
|
|
179
|
+
if (typeof captured.name !== "string" || !captured.name || !captured.payload || typeof captured.payload !== "object" || Array.isArray(captured.payload))
|
|
180
|
+
throw new TypeError("Queue envelope requires a job name and payload record.");
|
|
181
|
+
return captured;
|
|
182
|
+
}
|
|
183
|
+
function parseQueueJobEnvelope(rawPayload, registry) {
|
|
171
184
|
let parsed;
|
|
172
185
|
try {
|
|
173
186
|
parsed = JSON.parse(rawPayload);
|
|
@@ -180,15 +193,27 @@ function parseQueueJobEnvelope(rawPayload) {
|
|
|
180
193
|
return null;
|
|
181
194
|
}
|
|
182
195
|
const envelope = parsed;
|
|
196
|
+
if (Object.keys(parsed).some((key) => ![
|
|
197
|
+
"name",
|
|
198
|
+
"payload",
|
|
199
|
+
"attempts",
|
|
200
|
+
"jobId",
|
|
201
|
+
"deadlineAtMs",
|
|
202
|
+
"cancellable",
|
|
203
|
+
"handoff"
|
|
204
|
+
].includes(key)))
|
|
205
|
+
return null;
|
|
206
|
+
if (envelope.handoff !== undefined && (typeof envelope.handoff !== "string" || !envelope.handoff.length || envelope.handoff.length > 32768))
|
|
207
|
+
return null;
|
|
183
208
|
if (typeof envelope.name !== "string" || envelope.name.length === 0) {
|
|
184
209
|
console.error("[QueueWorker] Ignoring queue payload without job name");
|
|
185
210
|
return null;
|
|
186
211
|
}
|
|
187
|
-
if (!
|
|
212
|
+
if (!registry.has(envelope.name)) {
|
|
188
213
|
console.error(`[QueueWorker] Ignoring unknown job name: ${envelope.name}`);
|
|
189
214
|
return null;
|
|
190
215
|
}
|
|
191
|
-
if (envelope.payload !== undefined && (typeof envelope.payload !== "object" || envelope.payload === null)) {
|
|
216
|
+
if (envelope.payload !== undefined && (typeof envelope.payload !== "object" || envelope.payload === null || Array.isArray(envelope.payload))) {
|
|
192
217
|
console.error("[QueueWorker] Ignoring queue payload with invalid payload object");
|
|
193
218
|
return null;
|
|
194
219
|
}
|
|
@@ -203,16 +228,18 @@ function parseQueueJobEnvelope(rawPayload) {
|
|
|
203
228
|
return {
|
|
204
229
|
...envelope.cancellable ? { cancellable: true } : {},
|
|
205
230
|
...envelope.deadlineAtMs === undefined ? {} : { deadlineAtMs: envelope.deadlineAtMs },
|
|
231
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
206
232
|
jobId: envelope.jobId,
|
|
207
233
|
name: envelope.name,
|
|
208
234
|
payload: envelope.payload ?? {},
|
|
209
235
|
attempts: typeof envelope.attempts === "number" ? envelope.attempts : 0
|
|
210
236
|
};
|
|
211
237
|
}
|
|
212
|
-
async function runQueueJob(envelope, failedJobs, options = {}) {
|
|
238
|
+
async function runQueueJob(envelope, failedJobs, registry, options = {}) {
|
|
239
|
+
envelope = snapshotQueueJobEnvelope(envelope);
|
|
240
|
+
const retryPolicy = snapshotQueueRetryPolicy(options.retryPolicy ?? resolveQueueConfig());
|
|
213
241
|
options.context?.signal.throwIfAborted();
|
|
214
|
-
|
|
215
|
-
if (!job) {
|
|
242
|
+
if (!registry.has(envelope.name)) {
|
|
216
243
|
throw new Error(`Unknown job "${envelope.name}".`);
|
|
217
244
|
}
|
|
218
245
|
const attempts = envelope.attempts ?? 0;
|
|
@@ -220,14 +247,23 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
|
|
|
220
247
|
jobId: envelope.jobId ?? crypto.randomUUID(),
|
|
221
248
|
signal: new AbortController().signal
|
|
222
249
|
};
|
|
250
|
+
let job;
|
|
223
251
|
try {
|
|
224
|
-
await
|
|
252
|
+
await failedJobs.runAttempt(envelope, { ...context, attempt: attempts + 1 }, (scoped) => {
|
|
253
|
+
scoped.signal.throwIfAborted();
|
|
254
|
+
job = registry.create(envelope.name);
|
|
255
|
+
if (!job)
|
|
256
|
+
throw new Error(`Unknown job "${envelope.name}".`);
|
|
257
|
+
return job.handle(envelope.payload, scoped);
|
|
258
|
+
});
|
|
225
259
|
} catch (error) {
|
|
226
260
|
options.context?.signal.throwIfAborted();
|
|
261
|
+
if (!job)
|
|
262
|
+
throw error;
|
|
227
263
|
const nextAttempt = attempts + 1;
|
|
228
|
-
const maxAttempts = job.maxAttempts ??
|
|
264
|
+
const maxAttempts = job.maxAttempts ?? retryPolicy.maxAttempts;
|
|
229
265
|
if (nextAttempt < maxAttempts) {
|
|
230
|
-
const backoffMs = job.backoffMs ??
|
|
266
|
+
const backoffMs = job.backoffMs ?? retryPolicy.backoffMs;
|
|
231
267
|
if (options.deferRetry) {
|
|
232
268
|
await options.deferRetry({ ...envelope, jobId: context.jobId, attempts: nextAttempt }, backoffMs * nextAttempt);
|
|
233
269
|
return;
|
|
@@ -236,11 +272,12 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
|
|
|
236
272
|
await runQueueJob({
|
|
237
273
|
...envelope,
|
|
238
274
|
attempts: nextAttempt
|
|
239
|
-
}, failedJobs, { ...options, context });
|
|
275
|
+
}, failedJobs, registry, { ...options, context, retryPolicy });
|
|
240
276
|
return;
|
|
241
277
|
}
|
|
242
278
|
await failedJobs.recordFailure({
|
|
243
279
|
jobName: envelope.name,
|
|
280
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
244
281
|
jobId: context.jobId,
|
|
245
282
|
payload: envelope.payload,
|
|
246
283
|
exception: error instanceof Error ? error.stack ?? error.message : String(error)
|
|
@@ -250,7 +287,85 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
|
|
|
250
287
|
}
|
|
251
288
|
}
|
|
252
289
|
|
|
290
|
+
// ../../src/core/queue/queueServices.ts
|
|
291
|
+
import { failedJobRepository } from "@getstrata/core/queue/failedJobRepository";
|
|
292
|
+
import {
|
|
293
|
+
QueueHandoffError
|
|
294
|
+
} from "@getstrata/core/queue/queueHandoff";
|
|
295
|
+
function createFailedJobService(database) {
|
|
296
|
+
return new FailedJobService(database.repository(failedJobRepository));
|
|
297
|
+
}
|
|
298
|
+
function createQueueServices(config) {
|
|
299
|
+
const { invoke, attemptEnvelope, recoveryEnvelope, validateReplay } = config;
|
|
300
|
+
const jobContext = Object.freeze({ ...config.jobContext });
|
|
301
|
+
const operate = (work, failure) => invoke(recoveryEnvelope(failure), ({ database }) => work(createFailedJobService(database)));
|
|
302
|
+
return Object.freeze({
|
|
303
|
+
recordFailure: (input) => operate((service) => service.recordFailure(input), input),
|
|
304
|
+
listRecent: (limit) => operate((service) => service.listRecent(limit)),
|
|
305
|
+
retry: (id, enqueue) => operate((service) => service.retry(id, async (record) => {
|
|
306
|
+
validateReplay?.(record);
|
|
307
|
+
await enqueue(record);
|
|
308
|
+
})),
|
|
309
|
+
delete: (id) => operate((service) => service.delete(id)),
|
|
310
|
+
flush: () => operate((service) => service.flush()),
|
|
311
|
+
runAttempt: (job, context, work) => {
|
|
312
|
+
const envelope = attemptEnvelope(job, context);
|
|
313
|
+
return invoke(envelope, ({ database, policy }) => work({
|
|
314
|
+
...context,
|
|
315
|
+
...jobContext,
|
|
316
|
+
database,
|
|
317
|
+
policy,
|
|
318
|
+
actor: envelope.actor,
|
|
319
|
+
tenant: envelope.tenant
|
|
320
|
+
}));
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
function createSystemQueueServices(invoke, jobContext = {}, handoff, systemJobs = []) {
|
|
325
|
+
if (!Array.isArray(systemJobs) || systemJobs.some((name) => typeof name !== "string" || !name.length))
|
|
326
|
+
throw new TypeError("System job admission requires explicit job names.");
|
|
327
|
+
const allowedSystemJobs = new Set(systemJobs);
|
|
328
|
+
const establishIdentity = (job) => {
|
|
329
|
+
if (job.handoff === undefined) {
|
|
330
|
+
if (handoff && !allowedSystemJobs.has(job.name))
|
|
331
|
+
throw new QueueHandoffError;
|
|
332
|
+
return { actor: { kind: "system", policy: "queue.job" } };
|
|
333
|
+
}
|
|
334
|
+
if (!handoff)
|
|
335
|
+
throw new QueueHandoffError;
|
|
336
|
+
return handoff.verify(job);
|
|
337
|
+
};
|
|
338
|
+
return createQueueServices({
|
|
339
|
+
invoke,
|
|
340
|
+
jobContext,
|
|
341
|
+
validateReplay: (record) => {
|
|
342
|
+
establishIdentity({
|
|
343
|
+
name: record.job_name,
|
|
344
|
+
payload: record.payload,
|
|
345
|
+
...record.handoff == null ? {} : { handoff: record.handoff }
|
|
346
|
+
});
|
|
347
|
+
},
|
|
348
|
+
attemptEnvelope: (job, context) => {
|
|
349
|
+
const identity = establishIdentity(job);
|
|
350
|
+
return {
|
|
351
|
+
kind: "job",
|
|
352
|
+
externalId: context.jobId,
|
|
353
|
+
signal: context.signal,
|
|
354
|
+
...context.deadlineAtMs === undefined ? {} : { deadlineAtMs: context.deadlineAtMs },
|
|
355
|
+
...identity,
|
|
356
|
+
metadata: { jobName: job.name, attempt: context.attempt ?? 1 }
|
|
357
|
+
};
|
|
358
|
+
},
|
|
359
|
+
recoveryEnvelope: (failure) => ({
|
|
360
|
+
kind: "command",
|
|
361
|
+
actor: { kind: "system", policy: "queue.recovery" },
|
|
362
|
+
metadata: { phase: "queue-recovery", ...failure ? { jobName: failure.jobName } : {} }
|
|
363
|
+
})
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
|
|
253
367
|
// ../../src/core/queue/redisQueue.ts
|
|
368
|
+
import { QueueHandoffError as QueueHandoffError3 } from "@getstrata/core/queue/queueHandoff";
|
|
254
369
|
var {RedisClient: RedisClient2 } = globalThis.Bun;
|
|
255
370
|
|
|
256
371
|
// ../../src/core/queue/redisListScripts.ts
|
|
@@ -411,14 +526,14 @@ function envFlagEnabled(value) {
|
|
|
411
526
|
}
|
|
412
527
|
|
|
413
528
|
// ../../src/core/runtime/appKeyPrefix.ts
|
|
414
|
-
function appKeyPrefix() {
|
|
415
|
-
return
|
|
529
|
+
function appKeyPrefix(env = process.env) {
|
|
530
|
+
return env.APP_KEY_PREFIX?.trim() || "strata";
|
|
416
531
|
}
|
|
417
|
-
function appCookieName(kind) {
|
|
418
|
-
return `${appKeyPrefix()}_${kind}`;
|
|
532
|
+
function appCookieName(kind, env = process.env) {
|
|
533
|
+
return `${appKeyPrefix(env)}_${kind}`;
|
|
419
534
|
}
|
|
420
|
-
function appDevSecret(kind) {
|
|
421
|
-
return `${appKeyPrefix()}-dev-${kind}`;
|
|
535
|
+
function appDevSecret(kind, env = process.env) {
|
|
536
|
+
return `${appKeyPrefix(env)}-dev-${kind}`;
|
|
422
537
|
}
|
|
423
538
|
function requireConfiguredSecret(names, devKind, env = process.env) {
|
|
424
539
|
for (const name of names) {
|
|
@@ -430,50 +545,18 @@ function requireConfiguredSecret(names, devKind, env = process.env) {
|
|
|
430
545
|
if (isProductionEnv(env)) {
|
|
431
546
|
throw new Error(`${names[0]} must be set outside development. Do not derive secrets from the app name.`);
|
|
432
547
|
}
|
|
433
|
-
return appDevSecret(devKind);
|
|
548
|
+
return appDevSecret(devKind, env);
|
|
434
549
|
}
|
|
435
550
|
function namespacedRedisKey(kind) {
|
|
436
551
|
return `${appKeyPrefix()}:${kind}`;
|
|
437
552
|
}
|
|
438
|
-
function smtpEhloHost() {
|
|
439
|
-
const raw =
|
|
553
|
+
function smtpEhloHost(env = process.env) {
|
|
554
|
+
const raw = env.MAIL_EHLO?.trim() || `${appKeyPrefix(env)}.local`;
|
|
440
555
|
const safe = raw.replace(/[^a-zA-Z0-9.-]/g, "");
|
|
441
556
|
return safe || "strata.local";
|
|
442
557
|
}
|
|
443
|
-
function
|
|
444
|
-
return
|
|
445
|
-
}
|
|
446
|
-
function appUserAgent() {
|
|
447
|
-
return process.env.APP_USER_AGENT?.trim() || appKeyPrefix();
|
|
448
|
-
}
|
|
449
|
-
function otelServiceName() {
|
|
450
|
-
return process.env.OTEL_SERVICE_NAME?.trim() || `${appKeyPrefix()}-api`;
|
|
451
|
-
}
|
|
452
|
-
function webhookSignatureHeader() {
|
|
453
|
-
return process.env.WEBHOOK_SIGNATURE_HEADER?.trim() || `x-${appKeyPrefix()}-signature`;
|
|
454
|
-
}
|
|
455
|
-
function appDisplayName() {
|
|
456
|
-
return process.env.APP_NAME?.trim() || "Strata";
|
|
457
|
-
}
|
|
458
|
-
function appEnv() {
|
|
459
|
-
return process.env.APP_ENV?.trim() || "local";
|
|
460
|
-
}
|
|
461
|
-
function appUrl() {
|
|
462
|
-
return (process.env.APP_URL?.trim() || "http://localhost:3000").replace(/\/$/, "");
|
|
463
|
-
}
|
|
464
|
-
function apiPrefix() {
|
|
465
|
-
const raw = process.env.API_PREFIX?.trim() || "/api/v1";
|
|
466
|
-
const withSlash = raw.startsWith("/") ? raw : `/${raw}`;
|
|
467
|
-
const trimmed = withSlash.replace(/\/+$/, "");
|
|
468
|
-
return trimmed || "/api/v1";
|
|
469
|
-
}
|
|
470
|
-
function sdkClientClassName() {
|
|
471
|
-
const override = process.env.APP_SDK_CLASS?.trim();
|
|
472
|
-
if (override && /^[A-Za-z_][A-Za-z0-9_]*$/.test(override)) {
|
|
473
|
-
return override;
|
|
474
|
-
}
|
|
475
|
-
const fromName = appDisplayName().replace(/[^A-Za-z0-9]/g, "");
|
|
476
|
-
return fromName ? `${fromName}Client` : "AppClient";
|
|
558
|
+
function otelServiceName(env = process.env) {
|
|
559
|
+
return env.OTEL_SERVICE_NAME?.trim() || `${appKeyPrefix(env)}-api`;
|
|
477
560
|
}
|
|
478
561
|
|
|
479
562
|
// ../../src/core/queue/redisQueueKeys.ts
|
|
@@ -486,25 +569,19 @@ function queueHighKey() {
|
|
|
486
569
|
function queueLowKey() {
|
|
487
570
|
return namespacedRedisKey("queue:low");
|
|
488
571
|
}
|
|
489
|
-
var QUEUE_LIST_KEY =
|
|
490
|
-
var QUEUE_HIGH_KEY =
|
|
491
|
-
var QUEUE_LOW_KEY =
|
|
572
|
+
var QUEUE_LIST_KEY = "strata:queue:default";
|
|
573
|
+
var QUEUE_HIGH_KEY = "strata:queue:high";
|
|
574
|
+
var QUEUE_LOW_KEY = "strata:queue:low";
|
|
492
575
|
function defaultQueueKeys() {
|
|
493
576
|
return [queueHighKey(), queueListKey(), queueLowKey()];
|
|
494
577
|
}
|
|
495
|
-
var QUEUE_KEYS =
|
|
496
|
-
function queueKeyForPriority(priority = "default") {
|
|
497
|
-
|
|
498
|
-
case "high":
|
|
499
|
-
return queueHighKey();
|
|
500
|
-
case "low":
|
|
501
|
-
return queueLowKey();
|
|
502
|
-
default:
|
|
503
|
-
return queueListKey();
|
|
504
|
-
}
|
|
578
|
+
var QUEUE_KEYS = Object.freeze([QUEUE_HIGH_KEY, QUEUE_LIST_KEY, QUEUE_LOW_KEY]);
|
|
579
|
+
function queueKeyForPriority(priority = "default", keys = defaultQueueKeys()) {
|
|
580
|
+
return priority === "high" ? keys[0] : priority === "low" ? keys[2] : keys[1];
|
|
505
581
|
}
|
|
506
582
|
|
|
507
583
|
// ../../src/core/queue/redisStreams.ts
|
|
584
|
+
import { QueueHandoffError as QueueHandoffError2 } from "@getstrata/core/queue/queueHandoff";
|
|
508
585
|
var {RedisClient } = globalThis.Bun;
|
|
509
586
|
|
|
510
587
|
// ../../src/core/queue/redisStreamsScripts.ts
|
|
@@ -717,7 +794,7 @@ function watchStreamDeadline(client, deadlineAtMs, remainingMs, expire, fail) {
|
|
|
717
794
|
clearTimeout(timer);
|
|
718
795
|
};
|
|
719
796
|
}
|
|
720
|
-
async function scheduleStreamRetry(client, key, reservation, envelope, delayMs) {
|
|
797
|
+
async function scheduleStreamRetry(client, key, reservation, envelope, delayMs, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
721
798
|
if (!Number.isSafeInteger(delayMs) || delayMs < 0 || delayMs > Number.MAX_SAFE_INTEGER / 2)
|
|
722
799
|
throw new Error("Invalid Streams retry delay");
|
|
723
800
|
const scheduled = await client.send("EVAL", [
|
|
@@ -729,7 +806,7 @@ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs)
|
|
|
729
806
|
STREAM_GROUP,
|
|
730
807
|
reservation.id,
|
|
731
808
|
reservation.owner,
|
|
732
|
-
String(
|
|
809
|
+
String(visibilityMs),
|
|
733
810
|
String(delayMs),
|
|
734
811
|
JSON.stringify(envelope),
|
|
735
812
|
envelope.deadlineAtMs === undefined ? "" : String(envelope.deadlineAtMs),
|
|
@@ -738,7 +815,7 @@ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs)
|
|
|
738
815
|
if (Number(scheduled) !== 1)
|
|
739
816
|
throw new Error("Queue lease lost while scheduling retry");
|
|
740
817
|
}
|
|
741
|
-
async function pollStreamPriorities(client, queueKeys, owner, cursors) {
|
|
818
|
+
async function pollStreamPriorities(client, queueKeys, owner, cursors, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
742
819
|
const keys = [];
|
|
743
820
|
for (const key of queueKeys)
|
|
744
821
|
keys.push(streamQueueKey(key), streamRetryKey(key));
|
|
@@ -748,7 +825,7 @@ async function pollStreamPriorities(client, queueKeys, owner, cursors) {
|
|
|
748
825
|
...keys,
|
|
749
826
|
STREAM_GROUP,
|
|
750
827
|
owner,
|
|
751
|
-
String(
|
|
828
|
+
String(visibilityMs),
|
|
752
829
|
...cursors
|
|
753
830
|
]);
|
|
754
831
|
if (!Array.isArray(raw))
|
|
@@ -799,7 +876,7 @@ async function ensureStreamGroup(client, key) {
|
|
|
799
876
|
throw error;
|
|
800
877
|
}
|
|
801
878
|
}
|
|
802
|
-
async function updateStreamReservation(client, key, reservation, action, controlKey) {
|
|
879
|
+
async function updateStreamReservation(client, key, reservation, action, controlKey, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
803
880
|
return Number(await client.send("EVAL", [
|
|
804
881
|
UPDATE_STREAM,
|
|
805
882
|
"3",
|
|
@@ -809,41 +886,46 @@ async function updateStreamReservation(client, key, reservation, action, control
|
|
|
809
886
|
STREAM_GROUP,
|
|
810
887
|
reservation.id,
|
|
811
888
|
reservation.owner,
|
|
812
|
-
String(
|
|
889
|
+
String(visibilityMs),
|
|
813
890
|
action,
|
|
814
891
|
reservation.payload,
|
|
815
892
|
controlKey ? "1" : "0"
|
|
816
893
|
])) === 1;
|
|
817
894
|
}
|
|
818
895
|
class RedisStreamsQueue {
|
|
896
|
+
registry;
|
|
897
|
+
queueKeys;
|
|
819
898
|
client;
|
|
820
|
-
constructor(redisUrl) {
|
|
899
|
+
constructor(redisUrl, registry, queueKeys = defaultQueueKeys()) {
|
|
900
|
+
this.registry = registry;
|
|
901
|
+
this.queueKeys = queueKeys;
|
|
902
|
+
this.queueKeys = Object.freeze([...queueKeys]);
|
|
821
903
|
this.client = new RedisClient(redisUrl);
|
|
822
904
|
}
|
|
823
905
|
close() {
|
|
824
906
|
this.client.close();
|
|
825
907
|
}
|
|
826
|
-
async dispatch(job, payload) {
|
|
827
|
-
await this.
|
|
908
|
+
async dispatch(job, payload, options) {
|
|
909
|
+
await this.admit(job, payload, {}, undefined, options?.handoff);
|
|
828
910
|
}
|
|
829
911
|
async enqueue(job, payload, options = {}) {
|
|
830
912
|
return this.admit(job, payload, options);
|
|
831
913
|
}
|
|
832
|
-
async replay(job, payload, jobId) {
|
|
833
|
-
const prefix =
|
|
914
|
+
async replay(job, payload, jobId, options) {
|
|
915
|
+
const prefix = this.queueKeys.find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
|
|
834
916
|
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)))
|
|
835
917
|
throw new Error("Replay identity does not belong to this Streams namespace");
|
|
836
|
-
await this.admit(job, payload, {}, jobId);
|
|
918
|
+
await this.admit(job, payload, {}, jobId, options?.handoff);
|
|
837
919
|
}
|
|
838
|
-
async admit(job, payload, options, replayId) {
|
|
920
|
+
async admit(job, payload, options, replayId, handoff) {
|
|
839
921
|
if (options.cancellable !== undefined && typeof options.cancellable !== "boolean")
|
|
840
922
|
throw new Error("cancellable must be a boolean");
|
|
841
923
|
if (options.timeoutMs !== undefined && (!Number.isSafeInteger(options.timeoutMs) || options.timeoutMs <= 0 || options.timeoutMs > MAX_TIMER_MS))
|
|
842
924
|
throw new Error("Streams timeoutMs must be an integer between 1 and 2147483647");
|
|
843
|
-
const name =
|
|
925
|
+
const name = this.registry.resolveName(job);
|
|
844
926
|
if (!name)
|
|
845
927
|
throw new Error("Job is not registered with the queue worker registry.");
|
|
846
|
-
const key = queueKeyForPriority(job.priority);
|
|
928
|
+
const key = queueKeyForPriority(job.priority, this.queueKeys);
|
|
847
929
|
await assertLegacyDrained(this.client, key);
|
|
848
930
|
const jobId = replayId ?? `${streamQueueKey(key)}/${crypto.randomUUID()}`;
|
|
849
931
|
const deadlineAtMs = options.timeoutMs === undefined ? undefined : await readStreamTime(this.client) + options.timeoutMs;
|
|
@@ -851,6 +933,7 @@ class RedisStreamsQueue {
|
|
|
851
933
|
name,
|
|
852
934
|
payload,
|
|
853
935
|
attempts: 0,
|
|
936
|
+
...handoff === undefined ? {} : { handoff },
|
|
854
937
|
jobId,
|
|
855
938
|
deadlineAtMs,
|
|
856
939
|
...options.cancellable ? { cancellable: true } : {}
|
|
@@ -868,7 +951,7 @@ class RedisStreamsQueue {
|
|
|
868
951
|
return jobId;
|
|
869
952
|
}
|
|
870
953
|
async cancel(jobId) {
|
|
871
|
-
const key =
|
|
954
|
+
const key = this.queueKeys.find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
|
|
872
955
|
if (!key)
|
|
873
956
|
throw new Error("Invalid cancellable Streams job ID");
|
|
874
957
|
return cancelStreamJob(this.client, key, jobId);
|
|
@@ -878,8 +961,11 @@ class RedisStreamsQueue {
|
|
|
878
961
|
class RedisStreamsWorker {
|
|
879
962
|
redisUrl;
|
|
880
963
|
failedJobs;
|
|
964
|
+
registry;
|
|
881
965
|
timeoutSeconds;
|
|
882
966
|
queueKeys;
|
|
967
|
+
visibilityMs;
|
|
968
|
+
retryPolicy;
|
|
883
969
|
client;
|
|
884
970
|
owner = crypto.randomUUID();
|
|
885
971
|
cursors = new Map;
|
|
@@ -888,12 +974,16 @@ class RedisStreamsWorker {
|
|
|
888
974
|
running = false;
|
|
889
975
|
stopping = false;
|
|
890
976
|
active;
|
|
891
|
-
constructor(redisUrl, failedJobs, timeoutSeconds = 5, queueKeys = defaultQueueKeys()) {
|
|
977
|
+
constructor(redisUrl, failedJobs, registry, timeoutSeconds = 5, queueKeys = defaultQueueKeys(), retryPolicy = resolveQueueConfig(), visibilityMs = readQueueVisibilityMs()) {
|
|
892
978
|
this.redisUrl = redisUrl;
|
|
893
979
|
this.failedJobs = failedJobs;
|
|
980
|
+
this.registry = registry;
|
|
894
981
|
this.timeoutSeconds = timeoutSeconds;
|
|
895
982
|
this.queueKeys = queueKeys;
|
|
896
|
-
|
|
983
|
+
this.visibilityMs = visibilityMs;
|
|
984
|
+
this.queueKeys = Object.freeze([...queueKeys]);
|
|
985
|
+
this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
|
|
986
|
+
readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(this.visibilityMs) });
|
|
897
987
|
this.client = new RedisClient(redisUrl);
|
|
898
988
|
}
|
|
899
989
|
requestStop() {
|
|
@@ -932,7 +1022,7 @@ class RedisStreamsWorker {
|
|
|
932
1022
|
do {
|
|
933
1023
|
if (this.stopping)
|
|
934
1024
|
return false;
|
|
935
|
-
const polled = await pollStreamPriorities(this.client, this.queueKeys, this.owner, this.queueKeys.map((key) => this.cursors.get(key) ?? "0-0"));
|
|
1025
|
+
const polled = await pollStreamPriorities(this.client, this.queueKeys, this.owner, this.queueKeys.map((key) => this.cursors.get(key) ?? "0-0"), this.visibilityMs);
|
|
936
1026
|
this.queueKeys.forEach((key, index) => {
|
|
937
1027
|
const cursor = polled.cursors[index];
|
|
938
1028
|
if (cursor)
|
|
@@ -952,9 +1042,15 @@ class RedisStreamsWorker {
|
|
|
952
1042
|
return false;
|
|
953
1043
|
}
|
|
954
1044
|
async handle(key, reservation) {
|
|
955
|
-
const envelope = parseQueueJobEnvelope(reservation.payload);
|
|
1045
|
+
const envelope = parseQueueJobEnvelope(reservation.payload, this.registry);
|
|
956
1046
|
if (!envelope) {
|
|
957
|
-
await
|
|
1047
|
+
await this.failedJobs.recordFailure({
|
|
1048
|
+
jobName: "queue.invalid-envelope",
|
|
1049
|
+
jobId: `${streamQueueKey(key)}/${reservation.id}`,
|
|
1050
|
+
payload: { raw: reservation.payload },
|
|
1051
|
+
exception: "Malformed or unknown queue envelope"
|
|
1052
|
+
});
|
|
1053
|
+
await updateStreamReservation(this.client, key, reservation, "quarantine", undefined, this.visibilityMs);
|
|
958
1054
|
return;
|
|
959
1055
|
}
|
|
960
1056
|
envelope.jobId ??= `${streamQueueKey(key)}/${reservation.id}`;
|
|
@@ -963,7 +1059,7 @@ class RedisStreamsWorker {
|
|
|
963
1059
|
try {
|
|
964
1060
|
controlKey = streamControlKey(key, envelope.jobId);
|
|
965
1061
|
} catch {
|
|
966
|
-
await updateStreamReservation(this.client, key, reservation, "quarantine");
|
|
1062
|
+
await updateStreamReservation(this.client, key, reservation, "quarantine", undefined, this.visibilityMs);
|
|
967
1063
|
return;
|
|
968
1064
|
}
|
|
969
1065
|
}
|
|
@@ -972,10 +1068,11 @@ class RedisStreamsWorker {
|
|
|
972
1068
|
await this.failedJobs.recordFailure({
|
|
973
1069
|
jobName: envelope.name,
|
|
974
1070
|
jobId: envelope.jobId,
|
|
1071
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
975
1072
|
payload: envelope.payload,
|
|
976
1073
|
exception: cancellationError.message
|
|
977
1074
|
});
|
|
978
|
-
await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
|
|
1075
|
+
await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
|
|
979
1076
|
return;
|
|
980
1077
|
}
|
|
981
1078
|
const remainingMs = envelope.deadlineAtMs === undefined ? undefined : envelope.deadlineAtMs - await readStreamTime(this.client);
|
|
@@ -984,15 +1081,17 @@ class RedisStreamsWorker {
|
|
|
984
1081
|
await this.failedJobs.recordFailure({
|
|
985
1082
|
jobName: envelope.name,
|
|
986
1083
|
jobId: envelope.jobId,
|
|
1084
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
987
1085
|
payload: envelope.payload,
|
|
988
1086
|
exception: deadlineError.message
|
|
989
1087
|
});
|
|
990
|
-
await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
|
|
1088
|
+
await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
|
|
991
1089
|
return;
|
|
992
1090
|
}
|
|
993
1091
|
const controller = new AbortController;
|
|
994
1092
|
this.active = controller;
|
|
995
1093
|
let acknowledge = false;
|
|
1094
|
+
let invalidHandoff = false;
|
|
996
1095
|
let released = false;
|
|
997
1096
|
let ownershipLost = false;
|
|
998
1097
|
let renewal;
|
|
@@ -1012,7 +1111,7 @@ class RedisStreamsWorker {
|
|
|
1012
1111
|
return;
|
|
1013
1112
|
renewal = (async () => {
|
|
1014
1113
|
try {
|
|
1015
|
-
if (!await updateStreamReservation(this.client, key, reservation, "renew", controlKey))
|
|
1114
|
+
if (!await updateStreamReservation(this.client, key, reservation, "renew", controlKey, this.visibilityMs))
|
|
1016
1115
|
loseOwnership(new Error("Queue lease lost"));
|
|
1017
1116
|
else if (controlKey && await readStreamCancellation(this.client, controlKey)) {
|
|
1018
1117
|
cancellationRequested = true;
|
|
@@ -1025,9 +1124,10 @@ class RedisStreamsWorker {
|
|
|
1025
1124
|
})().finally(() => {
|
|
1026
1125
|
renewal = undefined;
|
|
1027
1126
|
});
|
|
1028
|
-
}, Math.max(1, Math.floor(
|
|
1127
|
+
}, Math.max(1, Math.floor(this.visibilityMs / 3)));
|
|
1029
1128
|
try {
|
|
1030
|
-
await runQueueJob(envelope, this.failedJobs, {
|
|
1129
|
+
await runQueueJob(envelope, this.failedJobs, this.registry, {
|
|
1130
|
+
retryPolicy: this.retryPolicy,
|
|
1031
1131
|
context: {
|
|
1032
1132
|
jobId: envelope.jobId,
|
|
1033
1133
|
signal: controller.signal,
|
|
@@ -1035,7 +1135,7 @@ class RedisStreamsWorker {
|
|
|
1035
1135
|
},
|
|
1036
1136
|
deferRetry: async (next, delayMs) => {
|
|
1037
1137
|
controller.signal.throwIfAborted();
|
|
1038
|
-
await scheduleStreamRetry(this.client, key, reservation, next, delayMs);
|
|
1138
|
+
await scheduleStreamRetry(this.client, key, reservation, next, delayMs, this.visibilityMs);
|
|
1039
1139
|
released = true;
|
|
1040
1140
|
},
|
|
1041
1141
|
onFailureRecorded: () => {
|
|
@@ -1045,18 +1145,26 @@ class RedisStreamsWorker {
|
|
|
1045
1145
|
});
|
|
1046
1146
|
acknowledge = true;
|
|
1047
1147
|
} catch (error) {
|
|
1048
|
-
|
|
1148
|
+
if (error instanceof QueueHandoffError2)
|
|
1149
|
+
invalidHandoff = true;
|
|
1150
|
+
else
|
|
1151
|
+
console.error("[StreamsWorker] Job failed:", error);
|
|
1049
1152
|
} finally {
|
|
1050
1153
|
stopDeadlineWatch?.();
|
|
1051
1154
|
clearInterval(heartbeat);
|
|
1052
1155
|
await renewal;
|
|
1053
1156
|
this.active = undefined;
|
|
1054
1157
|
}
|
|
1158
|
+
if (invalidHandoff && !ownershipLost && !released) {
|
|
1159
|
+
await updateStreamReservation(this.client, key, reservation, "quarantine", controlKey, this.visibilityMs);
|
|
1160
|
+
return;
|
|
1161
|
+
}
|
|
1055
1162
|
if (!ownershipLost && !released && controlKey && (cancellationRequested || await readStreamCancellation(this.client, controlKey))) {
|
|
1056
1163
|
if (!failureRecorded)
|
|
1057
1164
|
await this.failedJobs.recordFailure({
|
|
1058
1165
|
jobName: envelope.name,
|
|
1059
1166
|
jobId: envelope.jobId,
|
|
1167
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
1060
1168
|
payload: envelope.payload,
|
|
1061
1169
|
exception: cancellationError.message
|
|
1062
1170
|
});
|
|
@@ -1068,13 +1176,14 @@ class RedisStreamsWorker {
|
|
|
1068
1176
|
await this.failedJobs.recordFailure({
|
|
1069
1177
|
jobName: envelope.name,
|
|
1070
1178
|
jobId: envelope.jobId,
|
|
1179
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
1071
1180
|
payload: envelope.payload,
|
|
1072
1181
|
exception: deadlineError.message
|
|
1073
1182
|
});
|
|
1074
1183
|
acknowledge = true;
|
|
1075
1184
|
}
|
|
1076
1185
|
if (acknowledge && !ownershipLost && !released)
|
|
1077
|
-
await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
|
|
1186
|
+
await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
|
|
1078
1187
|
}
|
|
1079
1188
|
async run() {
|
|
1080
1189
|
this.running = true;
|
|
@@ -1106,7 +1215,7 @@ async function migrateLegacyQueueToStreams(client, options) {
|
|
|
1106
1215
|
if (!Number.isSafeInteger(batchSize) || batchSize < 1 || batchSize > 1000)
|
|
1107
1216
|
throw new Error("Queue conversion batchSize must be between 1 and 1000.");
|
|
1108
1217
|
let converted = 0;
|
|
1109
|
-
for (const key of options.queueKeys ??
|
|
1218
|
+
for (const key of [...options.queueKeys ?? QUEUE_KEYS])
|
|
1110
1219
|
converted += Number(await client.send("EVAL", [
|
|
1111
1220
|
CONVERT_LIST,
|
|
1112
1221
|
"4",
|
|
@@ -1139,7 +1248,7 @@ function reservationExpired(lease, now, visibilityMs) {
|
|
|
1139
1248
|
function queueInvalidKey(queueKey) {
|
|
1140
1249
|
return `${queueKey}:invalid`;
|
|
1141
1250
|
}
|
|
1142
|
-
async function reserveQueueJob(client, queueKey, owner) {
|
|
1251
|
+
async function reserveQueueJob(client, queueKey, owner, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
1143
1252
|
const raw = await client.send("EVAL", [
|
|
1144
1253
|
RESERVE_JOB,
|
|
1145
1254
|
"3",
|
|
@@ -1148,14 +1257,14 @@ async function reserveQueueJob(client, queueKey, owner) {
|
|
|
1148
1257
|
queueProcessingLeaseKey(queueKey),
|
|
1149
1258
|
crypto.randomUUID(),
|
|
1150
1259
|
owner,
|
|
1151
|
-
String(
|
|
1260
|
+
String(visibilityMs)
|
|
1152
1261
|
]);
|
|
1153
1262
|
if (typeof raw !== "string")
|
|
1154
1263
|
return null;
|
|
1155
1264
|
const reservation = JSON.parse(raw);
|
|
1156
1265
|
return { ...reservation, record: raw };
|
|
1157
1266
|
}
|
|
1158
|
-
async function updateQueueReservation(client, queueKey, reservation, action) {
|
|
1267
|
+
async function updateQueueReservation(client, queueKey, reservation, action, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
1159
1268
|
return Number(await client.send("EVAL", [
|
|
1160
1269
|
RENEW_OR_ACK,
|
|
1161
1270
|
"3",
|
|
@@ -1166,11 +1275,11 @@ async function updateQueueReservation(client, queueKey, reservation, action) {
|
|
|
1166
1275
|
reservation.owner,
|
|
1167
1276
|
reservation.record,
|
|
1168
1277
|
action,
|
|
1169
|
-
String(
|
|
1278
|
+
String(visibilityMs),
|
|
1170
1279
|
reservation.payload
|
|
1171
1280
|
])) === 1;
|
|
1172
1281
|
}
|
|
1173
|
-
async function pollListPriorities(client, queueKeys, owner, reservationId, reclaim) {
|
|
1282
|
+
async function pollListPriorities(client, queueKeys, owner, reservationId, reclaim, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
1174
1283
|
const keys = [];
|
|
1175
1284
|
for (const key of queueKeys) {
|
|
1176
1285
|
keys.push(key, queueProcessingKey(key), queueProcessingLeaseKey(key));
|
|
@@ -1180,7 +1289,7 @@ async function pollListPriorities(client, queueKeys, owner, reservationId, recla
|
|
|
1180
1289
|
String(keys.length),
|
|
1181
1290
|
...keys,
|
|
1182
1291
|
owner,
|
|
1183
|
-
String(
|
|
1292
|
+
String(visibilityMs),
|
|
1184
1293
|
reservationId,
|
|
1185
1294
|
reclaim ? "1" : "0"
|
|
1186
1295
|
]);
|
|
@@ -1205,7 +1314,7 @@ async function pollListPriorities(client, queueKeys, owner, reservationId, recla
|
|
|
1205
1314
|
const reservation = JSON.parse(record);
|
|
1206
1315
|
return { queueKey, reservation: { ...reservation, record } };
|
|
1207
1316
|
}
|
|
1208
|
-
async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS) {
|
|
1317
|
+
async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
1209
1318
|
let reclaimed = 0;
|
|
1210
1319
|
for (const key of queueKeys) {
|
|
1211
1320
|
reclaimed += Number(await client.send("EVAL", [
|
|
@@ -1214,31 +1323,46 @@ async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS) {
|
|
|
1214
1323
|
key,
|
|
1215
1324
|
queueProcessingKey(key),
|
|
1216
1325
|
queueProcessingLeaseKey(key),
|
|
1217
|
-
String(
|
|
1326
|
+
String(visibilityMs)
|
|
1218
1327
|
]));
|
|
1219
1328
|
}
|
|
1220
1329
|
return reclaimed;
|
|
1221
1330
|
}
|
|
1222
1331
|
|
|
1223
1332
|
class RedisQueue {
|
|
1333
|
+
registry;
|
|
1334
|
+
queueKeys;
|
|
1224
1335
|
client;
|
|
1225
|
-
constructor(redisUrl) {
|
|
1336
|
+
constructor(redisUrl, registry, queueKeys = defaultQueueKeys()) {
|
|
1337
|
+
this.registry = registry;
|
|
1338
|
+
this.queueKeys = queueKeys;
|
|
1339
|
+
this.queueKeys = Object.freeze([...queueKeys]);
|
|
1226
1340
|
this.client = new RedisClient2(redisUrl);
|
|
1227
1341
|
}
|
|
1228
1342
|
close() {
|
|
1229
1343
|
this.client.close();
|
|
1230
1344
|
}
|
|
1231
|
-
async dispatch(job, payload) {
|
|
1232
|
-
|
|
1345
|
+
async dispatch(job, payload, options) {
|
|
1346
|
+
await this.admit(job, payload, options);
|
|
1347
|
+
}
|
|
1348
|
+
async replay(job, payload, jobId, options) {
|
|
1349
|
+
if (typeof jobId !== "string" || !jobId.length)
|
|
1350
|
+
throw new TypeError("Replay identity is required.");
|
|
1351
|
+
await this.admit(job, payload, options, jobId);
|
|
1352
|
+
}
|
|
1353
|
+
async admit(job, payload, options, jobId) {
|
|
1354
|
+
const name = this.registry.resolveName(job);
|
|
1233
1355
|
if (!name) {
|
|
1234
1356
|
throw new Error("Job is not registered with the queue worker registry.");
|
|
1235
1357
|
}
|
|
1236
1358
|
const envelope = {
|
|
1237
1359
|
name,
|
|
1360
|
+
jobId: jobId ?? crypto.randomUUID(),
|
|
1238
1361
|
payload,
|
|
1239
|
-
attempts: 0
|
|
1362
|
+
attempts: 0,
|
|
1363
|
+
...options?.handoff === undefined ? {} : { handoff: options.handoff }
|
|
1240
1364
|
};
|
|
1241
|
-
const queueKey = queueKeyForPriority(job.priority);
|
|
1365
|
+
const queueKey = queueKeyForPriority(job.priority, this.queueKeys);
|
|
1242
1366
|
await this.client.lpush(queueKey, JSON.stringify(envelope));
|
|
1243
1367
|
}
|
|
1244
1368
|
}
|
|
@@ -1246,17 +1370,25 @@ class RedisQueue {
|
|
|
1246
1370
|
class QueueWorker {
|
|
1247
1371
|
redisUrl;
|
|
1248
1372
|
failedJobs;
|
|
1373
|
+
registry;
|
|
1249
1374
|
timeoutSeconds;
|
|
1250
1375
|
queueKeys;
|
|
1376
|
+
visibilityMs;
|
|
1377
|
+
retryPolicy;
|
|
1251
1378
|
owner = crypto.randomUUID();
|
|
1252
1379
|
running = false;
|
|
1253
1380
|
stopping = false;
|
|
1254
1381
|
client;
|
|
1255
|
-
constructor(redisUrl, failedJobs, timeoutSeconds = 5, queueKeys =
|
|
1382
|
+
constructor(redisUrl, failedJobs, registry, timeoutSeconds = 5, queueKeys = defaultQueueKeys(), retryPolicy = resolveQueueConfig(), visibilityMs = readQueueVisibilityMs()) {
|
|
1256
1383
|
this.redisUrl = redisUrl;
|
|
1257
1384
|
this.failedJobs = failedJobs;
|
|
1385
|
+
this.registry = registry;
|
|
1258
1386
|
this.timeoutSeconds = timeoutSeconds;
|
|
1259
1387
|
this.queueKeys = queueKeys;
|
|
1388
|
+
this.visibilityMs = visibilityMs;
|
|
1389
|
+
this.queueKeys = Object.freeze([...queueKeys]);
|
|
1390
|
+
this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
|
|
1391
|
+
readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(this.visibilityMs) });
|
|
1260
1392
|
this.client = new RedisClient2(redisUrl);
|
|
1261
1393
|
}
|
|
1262
1394
|
requestStop() {
|
|
@@ -1271,7 +1403,7 @@ class QueueWorker {
|
|
|
1271
1403
|
do {
|
|
1272
1404
|
if (this.stopping)
|
|
1273
1405
|
return null;
|
|
1274
|
-
const reserved = await pollListPriorities(this.client, this.queueKeys, this.owner, crypto.randomUUID(), reclaim);
|
|
1406
|
+
const reserved = await pollListPriorities(this.client, this.queueKeys, this.owner, crypto.randomUUID(), reclaim, this.visibilityMs);
|
|
1275
1407
|
reclaim = false;
|
|
1276
1408
|
if (reserved)
|
|
1277
1409
|
return reserved;
|
|
@@ -1288,12 +1420,13 @@ class QueueWorker {
|
|
|
1288
1420
|
if (!reserved)
|
|
1289
1421
|
return false;
|
|
1290
1422
|
const { queueKey, reservation } = reserved;
|
|
1291
|
-
const envelope = parseQueueJobEnvelope(reservation.payload);
|
|
1423
|
+
const envelope = parseQueueJobEnvelope(reservation.payload, this.registry);
|
|
1292
1424
|
if (!envelope) {
|
|
1293
|
-
await updateQueueReservation(this.client, queueKey, reservation, "quarantine");
|
|
1425
|
+
await updateQueueReservation(this.client, queueKey, reservation, "quarantine", this.visibilityMs);
|
|
1294
1426
|
return true;
|
|
1295
1427
|
}
|
|
1296
1428
|
let acknowledge = false;
|
|
1429
|
+
let invalidHandoff = false;
|
|
1297
1430
|
let ownershipLost = false;
|
|
1298
1431
|
let renewal;
|
|
1299
1432
|
const heartbeat = setInterval(() => {
|
|
@@ -1301,7 +1434,7 @@ class QueueWorker {
|
|
|
1301
1434
|
return;
|
|
1302
1435
|
renewal = (async () => {
|
|
1303
1436
|
try {
|
|
1304
|
-
if (!await updateQueueReservation(this.client, queueKey, reservation, "renew")) {
|
|
1437
|
+
if (!await updateQueueReservation(this.client, queueKey, reservation, "renew", this.visibilityMs)) {
|
|
1305
1438
|
ownershipLost = true;
|
|
1306
1439
|
}
|
|
1307
1440
|
} catch (error) {
|
|
@@ -1311,22 +1444,30 @@ class QueueWorker {
|
|
|
1311
1444
|
})().finally(() => {
|
|
1312
1445
|
renewal = undefined;
|
|
1313
1446
|
});
|
|
1314
|
-
}, Math.max(1, Math.floor(
|
|
1447
|
+
}, Math.max(1, Math.floor(this.visibilityMs / 3)));
|
|
1315
1448
|
try {
|
|
1316
|
-
await runQueueJob(envelope, this.failedJobs, {
|
|
1449
|
+
await runQueueJob(envelope, this.failedJobs, this.registry, {
|
|
1450
|
+
retryPolicy: this.retryPolicy,
|
|
1317
1451
|
onFailureRecorded: () => {
|
|
1318
1452
|
acknowledge = true;
|
|
1319
1453
|
}
|
|
1320
1454
|
});
|
|
1321
1455
|
acknowledge = true;
|
|
1322
1456
|
} catch (error) {
|
|
1323
|
-
|
|
1457
|
+
if (error instanceof QueueHandoffError3)
|
|
1458
|
+
invalidHandoff = true;
|
|
1459
|
+
else
|
|
1460
|
+
console.error("[QueueWorker] Job failed:", error);
|
|
1324
1461
|
} finally {
|
|
1325
1462
|
clearInterval(heartbeat);
|
|
1326
1463
|
await renewal;
|
|
1327
1464
|
}
|
|
1465
|
+
if (invalidHandoff && !ownershipLost) {
|
|
1466
|
+
await updateQueueReservation(this.client, queueKey, reservation, "quarantine", this.visibilityMs);
|
|
1467
|
+
return true;
|
|
1468
|
+
}
|
|
1328
1469
|
if (acknowledge && !ownershipLost) {
|
|
1329
|
-
await updateQueueReservation(this.client, queueKey, reservation, "ack");
|
|
1470
|
+
await updateQueueReservation(this.client, queueKey, reservation, "ack", this.visibilityMs);
|
|
1330
1471
|
}
|
|
1331
1472
|
return true;
|
|
1332
1473
|
}
|
|
@@ -1355,12 +1496,13 @@ class QueueWorker {
|
|
|
1355
1496
|
this.client.close();
|
|
1356
1497
|
}
|
|
1357
1498
|
}
|
|
1358
|
-
async function countPendingQueueJobs(redisUrl) {
|
|
1499
|
+
async function countPendingQueueJobs(redisUrl, config = resolveQueueRuntimeConfig({})) {
|
|
1500
|
+
const { transport, queueKeys } = snapshotQueueRuntimeConfig(config);
|
|
1359
1501
|
const client = new RedisClient2(redisUrl);
|
|
1360
1502
|
try {
|
|
1361
1503
|
let total = 0;
|
|
1362
|
-
for (const queueKey of
|
|
1363
|
-
total +=
|
|
1504
|
+
for (const queueKey of queueKeys) {
|
|
1505
|
+
total += transport === "streams" ? await readStreamQueueDepth(client, queueKey) : await client.llen(queueKey);
|
|
1364
1506
|
}
|
|
1365
1507
|
return total;
|
|
1366
1508
|
} finally {
|
|
@@ -1374,16 +1516,12 @@ class Job {
|
|
|
1374
1516
|
maxAttempts;
|
|
1375
1517
|
backoffMs;
|
|
1376
1518
|
priority;
|
|
1377
|
-
constructor() {
|
|
1378
|
-
const jobName = this.constructor.jobName;
|
|
1379
|
-
if (jobName) {
|
|
1380
|
-
jobRegistry.track(jobName, this);
|
|
1381
|
-
}
|
|
1382
|
-
}
|
|
1383
1519
|
}
|
|
1384
1520
|
|
|
1385
1521
|
class SyncQueue {
|
|
1386
|
-
async dispatch(job, payload) {
|
|
1522
|
+
async dispatch(job, payload, options) {
|
|
1523
|
+
if (options?.handoff !== undefined)
|
|
1524
|
+
throw new Error("Identity handoff requires an application queue.");
|
|
1387
1525
|
await job.handle(payload);
|
|
1388
1526
|
}
|
|
1389
1527
|
}
|
|
@@ -1408,7 +1546,9 @@ class AsyncQueue {
|
|
|
1408
1546
|
this.active.delete(running);
|
|
1409
1547
|
});
|
|
1410
1548
|
}
|
|
1411
|
-
async dispatch(job, payload) {
|
|
1549
|
+
async dispatch(job, payload, options) {
|
|
1550
|
+
if (options?.handoff !== undefined)
|
|
1551
|
+
throw new Error("Identity handoff requires an application queue.");
|
|
1412
1552
|
this.enqueue(() => job.handle(payload), (error) => {
|
|
1413
1553
|
console.error("[AsyncQueue] Job failed:", error);
|
|
1414
1554
|
});
|
|
@@ -1429,69 +1569,89 @@ function createQueue(driver) {
|
|
|
1429
1569
|
// ../../src/core/queue/resilientQueue.ts
|
|
1430
1570
|
class ResilientQueue extends AsyncQueue {
|
|
1431
1571
|
failedJobs;
|
|
1572
|
+
registry;
|
|
1432
1573
|
asyncDispatch;
|
|
1433
|
-
|
|
1574
|
+
retryPolicy;
|
|
1575
|
+
constructor(failedJobs, registry, asyncDispatch = false, retryPolicy = resolveQueueConfig()) {
|
|
1434
1576
|
super();
|
|
1435
1577
|
this.failedJobs = failedJobs;
|
|
1578
|
+
this.registry = registry;
|
|
1436
1579
|
this.asyncDispatch = asyncDispatch;
|
|
1580
|
+
this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
|
|
1581
|
+
}
|
|
1582
|
+
async dispatch(job, payload, options) {
|
|
1583
|
+
await this.admit(job, payload, options);
|
|
1584
|
+
}
|
|
1585
|
+
async replay(job, payload, jobId, options) {
|
|
1586
|
+
if (typeof jobId !== "string" || !jobId.length)
|
|
1587
|
+
throw new TypeError("Replay identity is required.");
|
|
1588
|
+
await this.admit(job, payload, options, jobId);
|
|
1437
1589
|
}
|
|
1438
|
-
async
|
|
1590
|
+
async admit(job, payload, options, jobId) {
|
|
1439
1591
|
this.assertOpen();
|
|
1440
|
-
const name =
|
|
1592
|
+
const name = this.registry.resolveName(job);
|
|
1441
1593
|
if (!name) {
|
|
1442
1594
|
throw new Error("Job is not registered with the queue worker registry.");
|
|
1443
1595
|
}
|
|
1444
|
-
const envelope = {
|
|
1596
|
+
const envelope = snapshotQueueJobEnvelope({
|
|
1445
1597
|
name,
|
|
1598
|
+
jobId: jobId ?? crypto.randomUUID(),
|
|
1446
1599
|
payload,
|
|
1447
|
-
attempts: 0
|
|
1448
|
-
|
|
1600
|
+
attempts: 0,
|
|
1601
|
+
...options?.handoff === undefined ? {} : { handoff: options.handoff }
|
|
1602
|
+
});
|
|
1449
1603
|
if (this.asyncDispatch) {
|
|
1450
|
-
this.enqueue(() => runQueueJob(envelope, this.failedJobs), (error) => {
|
|
1604
|
+
this.enqueue(() => runQueueJob(envelope, this.failedJobs, this.registry, { retryPolicy: this.retryPolicy }), (error) => {
|
|
1451
1605
|
console.error("[ResilientQueue] Job failed:", error);
|
|
1452
1606
|
});
|
|
1453
1607
|
return;
|
|
1454
1608
|
}
|
|
1455
|
-
await runQueueJob(envelope, this.failedJobs);
|
|
1609
|
+
await runQueueJob(envelope, this.failedJobs, this.registry, { retryPolicy: this.retryPolicy });
|
|
1456
1610
|
}
|
|
1457
1611
|
}
|
|
1458
1612
|
|
|
1459
1613
|
// ../../src/core/queue/publicQueue.ts
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
function createTrackedJob(name, job) {
|
|
1464
|
-
return jobRegistry.track(name, job);
|
|
1614
|
+
import { createIdentityQueue, createQueueHandoff } from "@getstrata/core/queue/queueHandoff";
|
|
1615
|
+
function createTrackedJob(registry, name, job) {
|
|
1616
|
+
return registry.track(name, job);
|
|
1465
1617
|
}
|
|
1466
|
-
function createProductionQueue(driver, options
|
|
1467
|
-
options.
|
|
1468
|
-
const failedJobs = options.failedJobs ?? createFailedJobService();
|
|
1618
|
+
function createProductionQueue(driver, options) {
|
|
1619
|
+
const failedJobs = options.failedJobs;
|
|
1469
1620
|
if (driver === "redis") {
|
|
1470
1621
|
if (!options.redisUrl) {
|
|
1471
1622
|
throw new Error('QUEUE_DRIVER="redis" requires REDIS_URL to be set.');
|
|
1472
1623
|
}
|
|
1473
|
-
|
|
1624
|
+
const runtime = snapshotQueueRuntimeConfig(options.runtime ?? resolveQueueRuntimeConfig());
|
|
1625
|
+
return runtime.transport === "streams" ? new RedisStreamsQueue(options.redisUrl, options.registry, runtime.queueKeys) : new RedisQueue(options.redisUrl, options.registry, runtime.queueKeys);
|
|
1474
1626
|
}
|
|
1475
|
-
return new ResilientQueue(failedJobs, driver === "async");
|
|
1627
|
+
return new ResilientQueue(failedJobs, options.registry, driver === "async", snapshotQueueRetryPolicy(options.retryPolicy ?? resolveQueueConfig()));
|
|
1476
1628
|
}
|
|
1477
|
-
function createQueueWorker(redisUrl, failedJobs =
|
|
1478
|
-
|
|
1629
|
+
function createQueueWorker(redisUrl, failedJobs, registry, options = {}) {
|
|
1630
|
+
const runtime = snapshotQueueRuntimeConfig(options.runtime ?? resolveQueueRuntimeConfig());
|
|
1631
|
+
const retry = snapshotQueueRetryPolicy(options.retryPolicy ?? resolveQueueConfig());
|
|
1632
|
+
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);
|
|
1479
1633
|
}
|
|
1480
1634
|
|
|
1481
1635
|
// ../../src/core/queue/createAppQueue.ts
|
|
1482
|
-
|
|
1483
|
-
function createAppQueue(driver, redisUrl, failedJobs
|
|
1636
|
+
import { createIdentityQueue as createIdentityQueue2, createQueueHandoff as createQueueHandoff2 } from "@getstrata/core/queue/queueHandoff";
|
|
1637
|
+
function createAppQueue(driver, redisUrl, failedJobs, registry, retryPolicy, runtime) {
|
|
1484
1638
|
return createProductionQueue(driver, {
|
|
1485
1639
|
redisUrl,
|
|
1486
1640
|
failedJobs,
|
|
1487
|
-
|
|
1641
|
+
registry,
|
|
1642
|
+
retryPolicy,
|
|
1643
|
+
runtime
|
|
1488
1644
|
});
|
|
1489
1645
|
}
|
|
1490
1646
|
export {
|
|
1491
1647
|
FAILED_JOB_SERVICE_TOKEN,
|
|
1492
1648
|
createAppQueue,
|
|
1493
1649
|
createFailedJobService,
|
|
1650
|
+
createIdentityQueue2 as createIdentityQueue,
|
|
1651
|
+
createQueueHandoff2 as createQueueHandoff,
|
|
1652
|
+
createQueueServices,
|
|
1494
1653
|
createQueueWorker,
|
|
1654
|
+
createSystemQueueServices,
|
|
1495
1655
|
createTrackedJob,
|
|
1496
1656
|
resolveQueueConfig
|
|
1497
1657
|
};
|