@getstrata/core 2.2.59 → 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 +27 -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 -254
- package/dist/entries/queue/failedJobRepository.js +19 -8
- package/dist/entries/queue/failedJobService.js +1 -0
- package/dist/entries/queue/jobRunner.js +141 -66
- package/dist/entries/queue/publicQueue.js +411 -255
- package/dist/entries/queue/queueConfig.js +61 -0
- package/dist/entries/queue/queueHandoff.js +318 -0
- package/dist/entries/queue/queueMetrics.js +989 -1384
- 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 +10433 -11347
- 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,45 +1,6 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
// ../../src/core/queue/
|
|
3
|
-
import
|
|
4
|
-
import { 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
|
-
});
|
|
14
|
-
|
|
15
|
-
// ../../src/core/queue/failedJobRepository.ts
|
|
16
|
-
class FailedJobRepository extends BaseRepository {
|
|
17
|
-
constructor() {
|
|
18
|
-
super(failedJobTable);
|
|
19
|
-
}
|
|
20
|
-
async create(values) {
|
|
21
|
-
const stored = {
|
|
22
|
-
...values,
|
|
23
|
-
...values.payload === undefined ? {} : { payload: JSON.stringify(values.payload) },
|
|
24
|
-
...values.failed_at === undefined ? {} : { failed_at: sqlTimestamp(values.failed_at) }
|
|
25
|
-
};
|
|
26
|
-
return hydrateFailure(await super.create(stored));
|
|
27
|
-
}
|
|
28
|
-
async findAll(options = {}) {
|
|
29
|
-
const rows = await super.findAll(options);
|
|
30
|
-
return options.select?.length ? rows : rows.map(hydrateFailure);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
function hydrateFailure(row) {
|
|
34
|
-
const result = { ...row };
|
|
35
|
-
if (typeof row.payload === "string")
|
|
36
|
-
result.payload = JSON.parse(row.payload);
|
|
37
|
-
if ("failed_at" in row && !(row.failed_at instanceof Date)) {
|
|
38
|
-
result.failed_at = new Date(row.failed_at);
|
|
39
|
-
}
|
|
40
|
-
return result;
|
|
41
|
-
}
|
|
42
|
-
var failedJobRepository_default = FailedJobRepository;
|
|
2
|
+
// ../../src/core/queue/publicQueue.ts
|
|
3
|
+
import FailedJobRepository from "@getstrata/core/queue/failedJobRepository";
|
|
43
4
|
|
|
44
5
|
// ../../src/core/queue/failedJobService.ts
|
|
45
6
|
class FailedJobService {
|
|
@@ -51,6 +12,7 @@ class FailedJobService {
|
|
|
51
12
|
return await this.repository.create({
|
|
52
13
|
job_name: input.jobName,
|
|
53
14
|
job_id: input.jobId ?? null,
|
|
15
|
+
...input.handoff === undefined ? {} : { handoff: input.handoff },
|
|
54
16
|
payload: input.payload,
|
|
55
17
|
exception: input.exception,
|
|
56
18
|
failed_at: new Date
|
|
@@ -89,83 +51,133 @@ class FailedJobService {
|
|
|
89
51
|
}
|
|
90
52
|
var failedJobService_default = FailedJobService;
|
|
91
53
|
|
|
92
|
-
// ../../src/core/queue/
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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.");
|
|
110
78
|
}
|
|
111
|
-
return factory();
|
|
112
79
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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);
|
|
122
106
|
}
|
|
123
|
-
const registry = new JobRegistry;
|
|
124
|
-
globalThis[JOB_REGISTRY_KEY] = registry;
|
|
125
|
-
return registry;
|
|
126
107
|
}
|
|
127
|
-
var jobRegistry = readSharedJobRegistry();
|
|
128
|
-
|
|
129
|
-
// ../../src/core/queue/jobRunner.ts
|
|
130
|
-
import { setTimeout as delay } from "timers/promises";
|
|
131
108
|
|
|
132
109
|
// ../../src/core/queue/queueConfig.ts
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const maxAttempts
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
function resolveRedisQueueTransport() {
|
|
155
|
-
const transport = process.env.QUEUE_REDIS_TRANSPORT ?? "lists";
|
|
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";
|
|
156
131
|
if (transport !== "lists" && transport !== "streams")
|
|
157
132
|
throw new Error('QUEUE_REDIS_TRANSPORT must be "lists" or "streams".');
|
|
158
133
|
return transport;
|
|
159
134
|
}
|
|
160
|
-
function readQueueVisibilityMs() {
|
|
161
|
-
const value = Number(
|
|
162
|
-
if (!Number.isSafeInteger(value) || value < 30)
|
|
163
|
-
throw new Error("QUEUE_VISIBILITY_MS must be an integer
|
|
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.");
|
|
164
139
|
return value;
|
|
165
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
|
+
}
|
|
166
160
|
|
|
167
161
|
// ../../src/core/queue/jobRunner.ts
|
|
168
|
-
function
|
|
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) {
|
|
169
181
|
let parsed;
|
|
170
182
|
try {
|
|
171
183
|
parsed = JSON.parse(rawPayload);
|
|
@@ -178,15 +190,27 @@ function parseQueueJobEnvelope(rawPayload) {
|
|
|
178
190
|
return null;
|
|
179
191
|
}
|
|
180
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;
|
|
181
205
|
if (typeof envelope.name !== "string" || envelope.name.length === 0) {
|
|
182
206
|
console.error("[QueueWorker] Ignoring queue payload without job name");
|
|
183
207
|
return null;
|
|
184
208
|
}
|
|
185
|
-
if (!
|
|
209
|
+
if (!registry.has(envelope.name)) {
|
|
186
210
|
console.error(`[QueueWorker] Ignoring unknown job name: ${envelope.name}`);
|
|
187
211
|
return null;
|
|
188
212
|
}
|
|
189
|
-
if (envelope.payload !== undefined && (typeof envelope.payload !== "object" || envelope.payload === null)) {
|
|
213
|
+
if (envelope.payload !== undefined && (typeof envelope.payload !== "object" || envelope.payload === null || Array.isArray(envelope.payload))) {
|
|
190
214
|
console.error("[QueueWorker] Ignoring queue payload with invalid payload object");
|
|
191
215
|
return null;
|
|
192
216
|
}
|
|
@@ -201,16 +225,18 @@ function parseQueueJobEnvelope(rawPayload) {
|
|
|
201
225
|
return {
|
|
202
226
|
...envelope.cancellable ? { cancellable: true } : {},
|
|
203
227
|
...envelope.deadlineAtMs === undefined ? {} : { deadlineAtMs: envelope.deadlineAtMs },
|
|
228
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
204
229
|
jobId: envelope.jobId,
|
|
205
230
|
name: envelope.name,
|
|
206
231
|
payload: envelope.payload ?? {},
|
|
207
232
|
attempts: typeof envelope.attempts === "number" ? envelope.attempts : 0
|
|
208
233
|
};
|
|
209
234
|
}
|
|
210
|
-
async function runQueueJob(envelope, failedJobs, options = {}) {
|
|
235
|
+
async function runQueueJob(envelope, failedJobs, registry, options = {}) {
|
|
236
|
+
envelope = snapshotQueueJobEnvelope(envelope);
|
|
237
|
+
const retryPolicy = snapshotQueueRetryPolicy(options.retryPolicy ?? resolveQueueConfig());
|
|
211
238
|
options.context?.signal.throwIfAborted();
|
|
212
|
-
|
|
213
|
-
if (!job) {
|
|
239
|
+
if (!registry.has(envelope.name)) {
|
|
214
240
|
throw new Error(`Unknown job "${envelope.name}".`);
|
|
215
241
|
}
|
|
216
242
|
const attempts = envelope.attempts ?? 0;
|
|
@@ -218,14 +244,23 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
|
|
|
218
244
|
jobId: envelope.jobId ?? crypto.randomUUID(),
|
|
219
245
|
signal: new AbortController().signal
|
|
220
246
|
};
|
|
247
|
+
let job;
|
|
221
248
|
try {
|
|
222
|
-
await
|
|
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
|
+
});
|
|
223
256
|
} catch (error) {
|
|
224
257
|
options.context?.signal.throwIfAborted();
|
|
258
|
+
if (!job)
|
|
259
|
+
throw error;
|
|
225
260
|
const nextAttempt = attempts + 1;
|
|
226
|
-
const maxAttempts = job.maxAttempts ??
|
|
261
|
+
const maxAttempts = job.maxAttempts ?? retryPolicy.maxAttempts;
|
|
227
262
|
if (nextAttempt < maxAttempts) {
|
|
228
|
-
const backoffMs = job.backoffMs ??
|
|
263
|
+
const backoffMs = job.backoffMs ?? retryPolicy.backoffMs;
|
|
229
264
|
if (options.deferRetry) {
|
|
230
265
|
await options.deferRetry({ ...envelope, jobId: context.jobId, attempts: nextAttempt }, backoffMs * nextAttempt);
|
|
231
266
|
return;
|
|
@@ -234,11 +269,12 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
|
|
|
234
269
|
await runQueueJob({
|
|
235
270
|
...envelope,
|
|
236
271
|
attempts: nextAttempt
|
|
237
|
-
}, failedJobs, { ...options, context });
|
|
272
|
+
}, failedJobs, registry, { ...options, context, retryPolicy });
|
|
238
273
|
return;
|
|
239
274
|
}
|
|
240
275
|
await failedJobs.recordFailure({
|
|
241
276
|
jobName: envelope.name,
|
|
277
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
242
278
|
jobId: context.jobId,
|
|
243
279
|
payload: envelope.payload,
|
|
244
280
|
exception: error instanceof Error ? error.stack ?? error.message : String(error)
|
|
@@ -248,7 +284,85 @@ async function runQueueJob(envelope, failedJobs, options = {}) {
|
|
|
248
284
|
}
|
|
249
285
|
}
|
|
250
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
|
+
|
|
251
364
|
// ../../src/core/queue/redisQueue.ts
|
|
365
|
+
import { QueueHandoffError as QueueHandoffError3 } from "@getstrata/core/queue/queueHandoff";
|
|
252
366
|
var {RedisClient: RedisClient2 } = globalThis.Bun;
|
|
253
367
|
|
|
254
368
|
// ../../src/core/queue/redisListScripts.ts
|
|
@@ -409,14 +523,14 @@ function envFlagEnabled(value) {
|
|
|
409
523
|
}
|
|
410
524
|
|
|
411
525
|
// ../../src/core/runtime/appKeyPrefix.ts
|
|
412
|
-
function appKeyPrefix() {
|
|
413
|
-
return
|
|
526
|
+
function appKeyPrefix(env = process.env) {
|
|
527
|
+
return env.APP_KEY_PREFIX?.trim() || "strata";
|
|
414
528
|
}
|
|
415
|
-
function appCookieName(kind) {
|
|
416
|
-
return `${appKeyPrefix()}_${kind}`;
|
|
529
|
+
function appCookieName(kind, env = process.env) {
|
|
530
|
+
return `${appKeyPrefix(env)}_${kind}`;
|
|
417
531
|
}
|
|
418
|
-
function appDevSecret(kind) {
|
|
419
|
-
return `${appKeyPrefix()}-dev-${kind}`;
|
|
532
|
+
function appDevSecret(kind, env = process.env) {
|
|
533
|
+
return `${appKeyPrefix(env)}-dev-${kind}`;
|
|
420
534
|
}
|
|
421
535
|
function requireConfiguredSecret(names, devKind, env = process.env) {
|
|
422
536
|
for (const name of names) {
|
|
@@ -428,50 +542,18 @@ function requireConfiguredSecret(names, devKind, env = process.env) {
|
|
|
428
542
|
if (isProductionEnv(env)) {
|
|
429
543
|
throw new Error(`${names[0]} must be set outside development. Do not derive secrets from the app name.`);
|
|
430
544
|
}
|
|
431
|
-
return appDevSecret(devKind);
|
|
545
|
+
return appDevSecret(devKind, env);
|
|
432
546
|
}
|
|
433
547
|
function namespacedRedisKey(kind) {
|
|
434
548
|
return `${appKeyPrefix()}:${kind}`;
|
|
435
549
|
}
|
|
436
|
-
function smtpEhloHost() {
|
|
437
|
-
const raw =
|
|
550
|
+
function smtpEhloHost(env = process.env) {
|
|
551
|
+
const raw = env.MAIL_EHLO?.trim() || `${appKeyPrefix(env)}.local`;
|
|
438
552
|
const safe = raw.replace(/[^a-zA-Z0-9.-]/g, "");
|
|
439
553
|
return safe || "strata.local";
|
|
440
554
|
}
|
|
441
|
-
function
|
|
442
|
-
return
|
|
443
|
-
}
|
|
444
|
-
function appUserAgent() {
|
|
445
|
-
return process.env.APP_USER_AGENT?.trim() || appKeyPrefix();
|
|
446
|
-
}
|
|
447
|
-
function otelServiceName() {
|
|
448
|
-
return process.env.OTEL_SERVICE_NAME?.trim() || `${appKeyPrefix()}-api`;
|
|
449
|
-
}
|
|
450
|
-
function webhookSignatureHeader() {
|
|
451
|
-
return process.env.WEBHOOK_SIGNATURE_HEADER?.trim() || `x-${appKeyPrefix()}-signature`;
|
|
452
|
-
}
|
|
453
|
-
function appDisplayName() {
|
|
454
|
-
return process.env.APP_NAME?.trim() || "Strata";
|
|
455
|
-
}
|
|
456
|
-
function appEnv() {
|
|
457
|
-
return process.env.APP_ENV?.trim() || "local";
|
|
458
|
-
}
|
|
459
|
-
function appUrl() {
|
|
460
|
-
return (process.env.APP_URL?.trim() || "http://localhost:3000").replace(/\/$/, "");
|
|
461
|
-
}
|
|
462
|
-
function apiPrefix() {
|
|
463
|
-
const raw = process.env.API_PREFIX?.trim() || "/api/v1";
|
|
464
|
-
const withSlash = raw.startsWith("/") ? raw : `/${raw}`;
|
|
465
|
-
const trimmed = withSlash.replace(/\/+$/, "");
|
|
466
|
-
return trimmed || "/api/v1";
|
|
467
|
-
}
|
|
468
|
-
function sdkClientClassName() {
|
|
469
|
-
const override = process.env.APP_SDK_CLASS?.trim();
|
|
470
|
-
if (override && /^[A-Za-z_][A-Za-z0-9_]*$/.test(override)) {
|
|
471
|
-
return override;
|
|
472
|
-
}
|
|
473
|
-
const fromName = appDisplayName().replace(/[^A-Za-z0-9]/g, "");
|
|
474
|
-
return fromName ? `${fromName}Client` : "AppClient";
|
|
555
|
+
function otelServiceName(env = process.env) {
|
|
556
|
+
return env.OTEL_SERVICE_NAME?.trim() || `${appKeyPrefix(env)}-api`;
|
|
475
557
|
}
|
|
476
558
|
|
|
477
559
|
// ../../src/core/queue/redisQueueKeys.ts
|
|
@@ -484,25 +566,19 @@ function queueHighKey() {
|
|
|
484
566
|
function queueLowKey() {
|
|
485
567
|
return namespacedRedisKey("queue:low");
|
|
486
568
|
}
|
|
487
|
-
var QUEUE_LIST_KEY =
|
|
488
|
-
var QUEUE_HIGH_KEY =
|
|
489
|
-
var QUEUE_LOW_KEY =
|
|
569
|
+
var QUEUE_LIST_KEY = "strata:queue:default";
|
|
570
|
+
var QUEUE_HIGH_KEY = "strata:queue:high";
|
|
571
|
+
var QUEUE_LOW_KEY = "strata:queue:low";
|
|
490
572
|
function defaultQueueKeys() {
|
|
491
573
|
return [queueHighKey(), queueListKey(), queueLowKey()];
|
|
492
574
|
}
|
|
493
|
-
var QUEUE_KEYS =
|
|
494
|
-
function queueKeyForPriority(priority = "default") {
|
|
495
|
-
|
|
496
|
-
case "high":
|
|
497
|
-
return queueHighKey();
|
|
498
|
-
case "low":
|
|
499
|
-
return queueLowKey();
|
|
500
|
-
default:
|
|
501
|
-
return queueListKey();
|
|
502
|
-
}
|
|
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];
|
|
503
578
|
}
|
|
504
579
|
|
|
505
580
|
// ../../src/core/queue/redisStreams.ts
|
|
581
|
+
import { QueueHandoffError as QueueHandoffError2 } from "@getstrata/core/queue/queueHandoff";
|
|
506
582
|
var {RedisClient } = globalThis.Bun;
|
|
507
583
|
|
|
508
584
|
// ../../src/core/queue/redisStreamsScripts.ts
|
|
@@ -715,7 +791,7 @@ function watchStreamDeadline(client, deadlineAtMs, remainingMs, expire, fail) {
|
|
|
715
791
|
clearTimeout(timer);
|
|
716
792
|
};
|
|
717
793
|
}
|
|
718
|
-
async function scheduleStreamRetry(client, key, reservation, envelope, delayMs) {
|
|
794
|
+
async function scheduleStreamRetry(client, key, reservation, envelope, delayMs, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
719
795
|
if (!Number.isSafeInteger(delayMs) || delayMs < 0 || delayMs > Number.MAX_SAFE_INTEGER / 2)
|
|
720
796
|
throw new Error("Invalid Streams retry delay");
|
|
721
797
|
const scheduled = await client.send("EVAL", [
|
|
@@ -727,7 +803,7 @@ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs)
|
|
|
727
803
|
STREAM_GROUP,
|
|
728
804
|
reservation.id,
|
|
729
805
|
reservation.owner,
|
|
730
|
-
String(
|
|
806
|
+
String(visibilityMs),
|
|
731
807
|
String(delayMs),
|
|
732
808
|
JSON.stringify(envelope),
|
|
733
809
|
envelope.deadlineAtMs === undefined ? "" : String(envelope.deadlineAtMs),
|
|
@@ -736,7 +812,7 @@ async function scheduleStreamRetry(client, key, reservation, envelope, delayMs)
|
|
|
736
812
|
if (Number(scheduled) !== 1)
|
|
737
813
|
throw new Error("Queue lease lost while scheduling retry");
|
|
738
814
|
}
|
|
739
|
-
async function pollStreamPriorities(client, queueKeys, owner, cursors) {
|
|
815
|
+
async function pollStreamPriorities(client, queueKeys, owner, cursors, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
740
816
|
const keys = [];
|
|
741
817
|
for (const key of queueKeys)
|
|
742
818
|
keys.push(streamQueueKey(key), streamRetryKey(key));
|
|
@@ -746,7 +822,7 @@ async function pollStreamPriorities(client, queueKeys, owner, cursors) {
|
|
|
746
822
|
...keys,
|
|
747
823
|
STREAM_GROUP,
|
|
748
824
|
owner,
|
|
749
|
-
String(
|
|
825
|
+
String(visibilityMs),
|
|
750
826
|
...cursors
|
|
751
827
|
]);
|
|
752
828
|
if (!Array.isArray(raw))
|
|
@@ -797,7 +873,7 @@ async function ensureStreamGroup(client, key) {
|
|
|
797
873
|
throw error;
|
|
798
874
|
}
|
|
799
875
|
}
|
|
800
|
-
async function updateStreamReservation(client, key, reservation, action, controlKey) {
|
|
876
|
+
async function updateStreamReservation(client, key, reservation, action, controlKey, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
801
877
|
return Number(await client.send("EVAL", [
|
|
802
878
|
UPDATE_STREAM,
|
|
803
879
|
"3",
|
|
@@ -807,41 +883,46 @@ async function updateStreamReservation(client, key, reservation, action, control
|
|
|
807
883
|
STREAM_GROUP,
|
|
808
884
|
reservation.id,
|
|
809
885
|
reservation.owner,
|
|
810
|
-
String(
|
|
886
|
+
String(visibilityMs),
|
|
811
887
|
action,
|
|
812
888
|
reservation.payload,
|
|
813
889
|
controlKey ? "1" : "0"
|
|
814
890
|
])) === 1;
|
|
815
891
|
}
|
|
816
892
|
class RedisStreamsQueue {
|
|
893
|
+
registry;
|
|
894
|
+
queueKeys;
|
|
817
895
|
client;
|
|
818
|
-
constructor(redisUrl) {
|
|
896
|
+
constructor(redisUrl, registry, queueKeys = defaultQueueKeys()) {
|
|
897
|
+
this.registry = registry;
|
|
898
|
+
this.queueKeys = queueKeys;
|
|
899
|
+
this.queueKeys = Object.freeze([...queueKeys]);
|
|
819
900
|
this.client = new RedisClient(redisUrl);
|
|
820
901
|
}
|
|
821
902
|
close() {
|
|
822
903
|
this.client.close();
|
|
823
904
|
}
|
|
824
|
-
async dispatch(job, payload) {
|
|
825
|
-
await this.
|
|
905
|
+
async dispatch(job, payload, options) {
|
|
906
|
+
await this.admit(job, payload, {}, undefined, options?.handoff);
|
|
826
907
|
}
|
|
827
908
|
async enqueue(job, payload, options = {}) {
|
|
828
909
|
return this.admit(job, payload, options);
|
|
829
910
|
}
|
|
830
|
-
async replay(job, payload, jobId) {
|
|
831
|
-
const prefix =
|
|
911
|
+
async replay(job, payload, jobId, options) {
|
|
912
|
+
const prefix = this.queueKeys.find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
|
|
832
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)))
|
|
833
914
|
throw new Error("Replay identity does not belong to this Streams namespace");
|
|
834
|
-
await this.admit(job, payload, {}, jobId);
|
|
915
|
+
await this.admit(job, payload, {}, jobId, options?.handoff);
|
|
835
916
|
}
|
|
836
|
-
async admit(job, payload, options, replayId) {
|
|
917
|
+
async admit(job, payload, options, replayId, handoff) {
|
|
837
918
|
if (options.cancellable !== undefined && typeof options.cancellable !== "boolean")
|
|
838
919
|
throw new Error("cancellable must be a boolean");
|
|
839
920
|
if (options.timeoutMs !== undefined && (!Number.isSafeInteger(options.timeoutMs) || options.timeoutMs <= 0 || options.timeoutMs > MAX_TIMER_MS))
|
|
840
921
|
throw new Error("Streams timeoutMs must be an integer between 1 and 2147483647");
|
|
841
|
-
const name =
|
|
922
|
+
const name = this.registry.resolveName(job);
|
|
842
923
|
if (!name)
|
|
843
924
|
throw new Error("Job is not registered with the queue worker registry.");
|
|
844
|
-
const key = queueKeyForPriority(job.priority);
|
|
925
|
+
const key = queueKeyForPriority(job.priority, this.queueKeys);
|
|
845
926
|
await assertLegacyDrained(this.client, key);
|
|
846
927
|
const jobId = replayId ?? `${streamQueueKey(key)}/${crypto.randomUUID()}`;
|
|
847
928
|
const deadlineAtMs = options.timeoutMs === undefined ? undefined : await readStreamTime(this.client) + options.timeoutMs;
|
|
@@ -849,6 +930,7 @@ class RedisStreamsQueue {
|
|
|
849
930
|
name,
|
|
850
931
|
payload,
|
|
851
932
|
attempts: 0,
|
|
933
|
+
...handoff === undefined ? {} : { handoff },
|
|
852
934
|
jobId,
|
|
853
935
|
deadlineAtMs,
|
|
854
936
|
...options.cancellable ? { cancellable: true } : {}
|
|
@@ -866,7 +948,7 @@ class RedisStreamsQueue {
|
|
|
866
948
|
return jobId;
|
|
867
949
|
}
|
|
868
950
|
async cancel(jobId) {
|
|
869
|
-
const key =
|
|
951
|
+
const key = this.queueKeys.find((key) => jobId.startsWith(`${streamQueueKey(key)}/`));
|
|
870
952
|
if (!key)
|
|
871
953
|
throw new Error("Invalid cancellable Streams job ID");
|
|
872
954
|
return cancelStreamJob(this.client, key, jobId);
|
|
@@ -876,8 +958,11 @@ class RedisStreamsQueue {
|
|
|
876
958
|
class RedisStreamsWorker {
|
|
877
959
|
redisUrl;
|
|
878
960
|
failedJobs;
|
|
961
|
+
registry;
|
|
879
962
|
timeoutSeconds;
|
|
880
963
|
queueKeys;
|
|
964
|
+
visibilityMs;
|
|
965
|
+
retryPolicy;
|
|
881
966
|
client;
|
|
882
967
|
owner = crypto.randomUUID();
|
|
883
968
|
cursors = new Map;
|
|
@@ -886,12 +971,16 @@ class RedisStreamsWorker {
|
|
|
886
971
|
running = false;
|
|
887
972
|
stopping = false;
|
|
888
973
|
active;
|
|
889
|
-
constructor(redisUrl, failedJobs, timeoutSeconds = 5, queueKeys = defaultQueueKeys()) {
|
|
974
|
+
constructor(redisUrl, failedJobs, registry, timeoutSeconds = 5, queueKeys = defaultQueueKeys(), retryPolicy = resolveQueueConfig(), visibilityMs = readQueueVisibilityMs()) {
|
|
890
975
|
this.redisUrl = redisUrl;
|
|
891
976
|
this.failedJobs = failedJobs;
|
|
977
|
+
this.registry = registry;
|
|
892
978
|
this.timeoutSeconds = timeoutSeconds;
|
|
893
979
|
this.queueKeys = queueKeys;
|
|
894
|
-
|
|
980
|
+
this.visibilityMs = visibilityMs;
|
|
981
|
+
this.queueKeys = Object.freeze([...queueKeys]);
|
|
982
|
+
this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
|
|
983
|
+
readQueueVisibilityMs({ QUEUE_VISIBILITY_MS: String(this.visibilityMs) });
|
|
895
984
|
this.client = new RedisClient(redisUrl);
|
|
896
985
|
}
|
|
897
986
|
requestStop() {
|
|
@@ -930,7 +1019,7 @@ class RedisStreamsWorker {
|
|
|
930
1019
|
do {
|
|
931
1020
|
if (this.stopping)
|
|
932
1021
|
return false;
|
|
933
|
-
const polled = await pollStreamPriorities(this.client, this.queueKeys, this.owner, this.queueKeys.map((key) => this.cursors.get(key) ?? "0-0"));
|
|
1022
|
+
const polled = await pollStreamPriorities(this.client, this.queueKeys, this.owner, this.queueKeys.map((key) => this.cursors.get(key) ?? "0-0"), this.visibilityMs);
|
|
934
1023
|
this.queueKeys.forEach((key, index) => {
|
|
935
1024
|
const cursor = polled.cursors[index];
|
|
936
1025
|
if (cursor)
|
|
@@ -950,9 +1039,15 @@ class RedisStreamsWorker {
|
|
|
950
1039
|
return false;
|
|
951
1040
|
}
|
|
952
1041
|
async handle(key, reservation) {
|
|
953
|
-
const envelope = parseQueueJobEnvelope(reservation.payload);
|
|
1042
|
+
const envelope = parseQueueJobEnvelope(reservation.payload, this.registry);
|
|
954
1043
|
if (!envelope) {
|
|
955
|
-
await
|
|
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);
|
|
956
1051
|
return;
|
|
957
1052
|
}
|
|
958
1053
|
envelope.jobId ??= `${streamQueueKey(key)}/${reservation.id}`;
|
|
@@ -961,7 +1056,7 @@ class RedisStreamsWorker {
|
|
|
961
1056
|
try {
|
|
962
1057
|
controlKey = streamControlKey(key, envelope.jobId);
|
|
963
1058
|
} catch {
|
|
964
|
-
await updateStreamReservation(this.client, key, reservation, "quarantine");
|
|
1059
|
+
await updateStreamReservation(this.client, key, reservation, "quarantine", undefined, this.visibilityMs);
|
|
965
1060
|
return;
|
|
966
1061
|
}
|
|
967
1062
|
}
|
|
@@ -970,10 +1065,11 @@ class RedisStreamsWorker {
|
|
|
970
1065
|
await this.failedJobs.recordFailure({
|
|
971
1066
|
jobName: envelope.name,
|
|
972
1067
|
jobId: envelope.jobId,
|
|
1068
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
973
1069
|
payload: envelope.payload,
|
|
974
1070
|
exception: cancellationError.message
|
|
975
1071
|
});
|
|
976
|
-
await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
|
|
1072
|
+
await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
|
|
977
1073
|
return;
|
|
978
1074
|
}
|
|
979
1075
|
const remainingMs = envelope.deadlineAtMs === undefined ? undefined : envelope.deadlineAtMs - await readStreamTime(this.client);
|
|
@@ -982,15 +1078,17 @@ class RedisStreamsWorker {
|
|
|
982
1078
|
await this.failedJobs.recordFailure({
|
|
983
1079
|
jobName: envelope.name,
|
|
984
1080
|
jobId: envelope.jobId,
|
|
1081
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
985
1082
|
payload: envelope.payload,
|
|
986
1083
|
exception: deadlineError.message
|
|
987
1084
|
});
|
|
988
|
-
await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
|
|
1085
|
+
await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
|
|
989
1086
|
return;
|
|
990
1087
|
}
|
|
991
1088
|
const controller = new AbortController;
|
|
992
1089
|
this.active = controller;
|
|
993
1090
|
let acknowledge = false;
|
|
1091
|
+
let invalidHandoff = false;
|
|
994
1092
|
let released = false;
|
|
995
1093
|
let ownershipLost = false;
|
|
996
1094
|
let renewal;
|
|
@@ -1010,7 +1108,7 @@ class RedisStreamsWorker {
|
|
|
1010
1108
|
return;
|
|
1011
1109
|
renewal = (async () => {
|
|
1012
1110
|
try {
|
|
1013
|
-
if (!await updateStreamReservation(this.client, key, reservation, "renew", controlKey))
|
|
1111
|
+
if (!await updateStreamReservation(this.client, key, reservation, "renew", controlKey, this.visibilityMs))
|
|
1014
1112
|
loseOwnership(new Error("Queue lease lost"));
|
|
1015
1113
|
else if (controlKey && await readStreamCancellation(this.client, controlKey)) {
|
|
1016
1114
|
cancellationRequested = true;
|
|
@@ -1023,9 +1121,10 @@ class RedisStreamsWorker {
|
|
|
1023
1121
|
})().finally(() => {
|
|
1024
1122
|
renewal = undefined;
|
|
1025
1123
|
});
|
|
1026
|
-
}, Math.max(1, Math.floor(
|
|
1124
|
+
}, Math.max(1, Math.floor(this.visibilityMs / 3)));
|
|
1027
1125
|
try {
|
|
1028
|
-
await runQueueJob(envelope, this.failedJobs, {
|
|
1126
|
+
await runQueueJob(envelope, this.failedJobs, this.registry, {
|
|
1127
|
+
retryPolicy: this.retryPolicy,
|
|
1029
1128
|
context: {
|
|
1030
1129
|
jobId: envelope.jobId,
|
|
1031
1130
|
signal: controller.signal,
|
|
@@ -1033,7 +1132,7 @@ class RedisStreamsWorker {
|
|
|
1033
1132
|
},
|
|
1034
1133
|
deferRetry: async (next, delayMs) => {
|
|
1035
1134
|
controller.signal.throwIfAborted();
|
|
1036
|
-
await scheduleStreamRetry(this.client, key, reservation, next, delayMs);
|
|
1135
|
+
await scheduleStreamRetry(this.client, key, reservation, next, delayMs, this.visibilityMs);
|
|
1037
1136
|
released = true;
|
|
1038
1137
|
},
|
|
1039
1138
|
onFailureRecorded: () => {
|
|
@@ -1043,18 +1142,26 @@ class RedisStreamsWorker {
|
|
|
1043
1142
|
});
|
|
1044
1143
|
acknowledge = true;
|
|
1045
1144
|
} catch (error) {
|
|
1046
|
-
|
|
1145
|
+
if (error instanceof QueueHandoffError2)
|
|
1146
|
+
invalidHandoff = true;
|
|
1147
|
+
else
|
|
1148
|
+
console.error("[StreamsWorker] Job failed:", error);
|
|
1047
1149
|
} finally {
|
|
1048
1150
|
stopDeadlineWatch?.();
|
|
1049
1151
|
clearInterval(heartbeat);
|
|
1050
1152
|
await renewal;
|
|
1051
1153
|
this.active = undefined;
|
|
1052
1154
|
}
|
|
1155
|
+
if (invalidHandoff && !ownershipLost && !released) {
|
|
1156
|
+
await updateStreamReservation(this.client, key, reservation, "quarantine", controlKey, this.visibilityMs);
|
|
1157
|
+
return;
|
|
1158
|
+
}
|
|
1053
1159
|
if (!ownershipLost && !released && controlKey && (cancellationRequested || await readStreamCancellation(this.client, controlKey))) {
|
|
1054
1160
|
if (!failureRecorded)
|
|
1055
1161
|
await this.failedJobs.recordFailure({
|
|
1056
1162
|
jobName: envelope.name,
|
|
1057
1163
|
jobId: envelope.jobId,
|
|
1164
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
1058
1165
|
payload: envelope.payload,
|
|
1059
1166
|
exception: cancellationError.message
|
|
1060
1167
|
});
|
|
@@ -1066,13 +1173,14 @@ class RedisStreamsWorker {
|
|
|
1066
1173
|
await this.failedJobs.recordFailure({
|
|
1067
1174
|
jobName: envelope.name,
|
|
1068
1175
|
jobId: envelope.jobId,
|
|
1176
|
+
...envelope.handoff === undefined ? {} : { handoff: envelope.handoff },
|
|
1069
1177
|
payload: envelope.payload,
|
|
1070
1178
|
exception: deadlineError.message
|
|
1071
1179
|
});
|
|
1072
1180
|
acknowledge = true;
|
|
1073
1181
|
}
|
|
1074
1182
|
if (acknowledge && !ownershipLost && !released)
|
|
1075
|
-
await updateStreamReservation(this.client, key, reservation, "ack", controlKey);
|
|
1183
|
+
await updateStreamReservation(this.client, key, reservation, "ack", controlKey, this.visibilityMs);
|
|
1076
1184
|
}
|
|
1077
1185
|
async run() {
|
|
1078
1186
|
this.running = true;
|
|
@@ -1104,7 +1212,7 @@ async function migrateLegacyQueueToStreams(client, options) {
|
|
|
1104
1212
|
if (!Number.isSafeInteger(batchSize) || batchSize < 1 || batchSize > 1000)
|
|
1105
1213
|
throw new Error("Queue conversion batchSize must be between 1 and 1000.");
|
|
1106
1214
|
let converted = 0;
|
|
1107
|
-
for (const key of options.queueKeys ??
|
|
1215
|
+
for (const key of [...options.queueKeys ?? QUEUE_KEYS])
|
|
1108
1216
|
converted += Number(await client.send("EVAL", [
|
|
1109
1217
|
CONVERT_LIST,
|
|
1110
1218
|
"4",
|
|
@@ -1137,7 +1245,7 @@ function reservationExpired(lease, now, visibilityMs) {
|
|
|
1137
1245
|
function queueInvalidKey(queueKey) {
|
|
1138
1246
|
return `${queueKey}:invalid`;
|
|
1139
1247
|
}
|
|
1140
|
-
async function reserveQueueJob(client, queueKey, owner) {
|
|
1248
|
+
async function reserveQueueJob(client, queueKey, owner, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
1141
1249
|
const raw = await client.send("EVAL", [
|
|
1142
1250
|
RESERVE_JOB,
|
|
1143
1251
|
"3",
|
|
@@ -1146,14 +1254,14 @@ async function reserveQueueJob(client, queueKey, owner) {
|
|
|
1146
1254
|
queueProcessingLeaseKey(queueKey),
|
|
1147
1255
|
crypto.randomUUID(),
|
|
1148
1256
|
owner,
|
|
1149
|
-
String(
|
|
1257
|
+
String(visibilityMs)
|
|
1150
1258
|
]);
|
|
1151
1259
|
if (typeof raw !== "string")
|
|
1152
1260
|
return null;
|
|
1153
1261
|
const reservation = JSON.parse(raw);
|
|
1154
1262
|
return { ...reservation, record: raw };
|
|
1155
1263
|
}
|
|
1156
|
-
async function updateQueueReservation(client, queueKey, reservation, action) {
|
|
1264
|
+
async function updateQueueReservation(client, queueKey, reservation, action, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
1157
1265
|
return Number(await client.send("EVAL", [
|
|
1158
1266
|
RENEW_OR_ACK,
|
|
1159
1267
|
"3",
|
|
@@ -1164,11 +1272,11 @@ async function updateQueueReservation(client, queueKey, reservation, action) {
|
|
|
1164
1272
|
reservation.owner,
|
|
1165
1273
|
reservation.record,
|
|
1166
1274
|
action,
|
|
1167
|
-
String(
|
|
1275
|
+
String(visibilityMs),
|
|
1168
1276
|
reservation.payload
|
|
1169
1277
|
])) === 1;
|
|
1170
1278
|
}
|
|
1171
|
-
async function pollListPriorities(client, queueKeys, owner, reservationId, reclaim) {
|
|
1279
|
+
async function pollListPriorities(client, queueKeys, owner, reservationId, reclaim, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
1172
1280
|
const keys = [];
|
|
1173
1281
|
for (const key of queueKeys) {
|
|
1174
1282
|
keys.push(key, queueProcessingKey(key), queueProcessingLeaseKey(key));
|
|
@@ -1178,7 +1286,7 @@ async function pollListPriorities(client, queueKeys, owner, reservationId, recla
|
|
|
1178
1286
|
String(keys.length),
|
|
1179
1287
|
...keys,
|
|
1180
1288
|
owner,
|
|
1181
|
-
String(
|
|
1289
|
+
String(visibilityMs),
|
|
1182
1290
|
reservationId,
|
|
1183
1291
|
reclaim ? "1" : "0"
|
|
1184
1292
|
]);
|
|
@@ -1203,7 +1311,7 @@ async function pollListPriorities(client, queueKeys, owner, reservationId, recla
|
|
|
1203
1311
|
const reservation = JSON.parse(record);
|
|
1204
1312
|
return { queueKey, reservation: { ...reservation, record } };
|
|
1205
1313
|
}
|
|
1206
|
-
async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS) {
|
|
1314
|
+
async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS, visibilityMs = DEFAULT_QUEUE_VISIBILITY_MS) {
|
|
1207
1315
|
let reclaimed = 0;
|
|
1208
1316
|
for (const key of queueKeys) {
|
|
1209
1317
|
reclaimed += Number(await client.send("EVAL", [
|
|
@@ -1212,31 +1320,46 @@ async function reclaimExpiredQueueReservations(client, queueKeys = QUEUE_KEYS) {
|
|
|
1212
1320
|
key,
|
|
1213
1321
|
queueProcessingKey(key),
|
|
1214
1322
|
queueProcessingLeaseKey(key),
|
|
1215
|
-
String(
|
|
1323
|
+
String(visibilityMs)
|
|
1216
1324
|
]));
|
|
1217
1325
|
}
|
|
1218
1326
|
return reclaimed;
|
|
1219
1327
|
}
|
|
1220
1328
|
|
|
1221
1329
|
class RedisQueue {
|
|
1330
|
+
registry;
|
|
1331
|
+
queueKeys;
|
|
1222
1332
|
client;
|
|
1223
|
-
constructor(redisUrl) {
|
|
1333
|
+
constructor(redisUrl, registry, queueKeys = defaultQueueKeys()) {
|
|
1334
|
+
this.registry = registry;
|
|
1335
|
+
this.queueKeys = queueKeys;
|
|
1336
|
+
this.queueKeys = Object.freeze([...queueKeys]);
|
|
1224
1337
|
this.client = new RedisClient2(redisUrl);
|
|
1225
1338
|
}
|
|
1226
1339
|
close() {
|
|
1227
1340
|
this.client.close();
|
|
1228
1341
|
}
|
|
1229
|
-
async dispatch(job, payload) {
|
|
1230
|
-
|
|
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);
|
|
1231
1352
|
if (!name) {
|
|
1232
1353
|
throw new Error("Job is not registered with the queue worker registry.");
|
|
1233
1354
|
}
|
|
1234
1355
|
const envelope = {
|
|
1235
1356
|
name,
|
|
1357
|
+
jobId: jobId ?? crypto.randomUUID(),
|
|
1236
1358
|
payload,
|
|
1237
|
-
attempts: 0
|
|
1359
|
+
attempts: 0,
|
|
1360
|
+
...options?.handoff === undefined ? {} : { handoff: options.handoff }
|
|
1238
1361
|
};
|
|
1239
|
-
const queueKey = queueKeyForPriority(job.priority);
|
|
1362
|
+
const queueKey = queueKeyForPriority(job.priority, this.queueKeys);
|
|
1240
1363
|
await this.client.lpush(queueKey, JSON.stringify(envelope));
|
|
1241
1364
|
}
|
|
1242
1365
|
}
|
|
@@ -1244,17 +1367,25 @@ class RedisQueue {
|
|
|
1244
1367
|
class QueueWorker {
|
|
1245
1368
|
redisUrl;
|
|
1246
1369
|
failedJobs;
|
|
1370
|
+
registry;
|
|
1247
1371
|
timeoutSeconds;
|
|
1248
1372
|
queueKeys;
|
|
1373
|
+
visibilityMs;
|
|
1374
|
+
retryPolicy;
|
|
1249
1375
|
owner = crypto.randomUUID();
|
|
1250
1376
|
running = false;
|
|
1251
1377
|
stopping = false;
|
|
1252
1378
|
client;
|
|
1253
|
-
constructor(redisUrl, failedJobs, timeoutSeconds = 5, queueKeys =
|
|
1379
|
+
constructor(redisUrl, failedJobs, registry, timeoutSeconds = 5, queueKeys = defaultQueueKeys(), retryPolicy = resolveQueueConfig(), visibilityMs = readQueueVisibilityMs()) {
|
|
1254
1380
|
this.redisUrl = redisUrl;
|
|
1255
1381
|
this.failedJobs = failedJobs;
|
|
1382
|
+
this.registry = registry;
|
|
1256
1383
|
this.timeoutSeconds = timeoutSeconds;
|
|
1257
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) });
|
|
1258
1389
|
this.client = new RedisClient2(redisUrl);
|
|
1259
1390
|
}
|
|
1260
1391
|
requestStop() {
|
|
@@ -1269,7 +1400,7 @@ class QueueWorker {
|
|
|
1269
1400
|
do {
|
|
1270
1401
|
if (this.stopping)
|
|
1271
1402
|
return null;
|
|
1272
|
-
const reserved = await pollListPriorities(this.client, this.queueKeys, this.owner, crypto.randomUUID(), reclaim);
|
|
1403
|
+
const reserved = await pollListPriorities(this.client, this.queueKeys, this.owner, crypto.randomUUID(), reclaim, this.visibilityMs);
|
|
1273
1404
|
reclaim = false;
|
|
1274
1405
|
if (reserved)
|
|
1275
1406
|
return reserved;
|
|
@@ -1286,12 +1417,13 @@ class QueueWorker {
|
|
|
1286
1417
|
if (!reserved)
|
|
1287
1418
|
return false;
|
|
1288
1419
|
const { queueKey, reservation } = reserved;
|
|
1289
|
-
const envelope = parseQueueJobEnvelope(reservation.payload);
|
|
1420
|
+
const envelope = parseQueueJobEnvelope(reservation.payload, this.registry);
|
|
1290
1421
|
if (!envelope) {
|
|
1291
|
-
await updateQueueReservation(this.client, queueKey, reservation, "quarantine");
|
|
1422
|
+
await updateQueueReservation(this.client, queueKey, reservation, "quarantine", this.visibilityMs);
|
|
1292
1423
|
return true;
|
|
1293
1424
|
}
|
|
1294
1425
|
let acknowledge = false;
|
|
1426
|
+
let invalidHandoff = false;
|
|
1295
1427
|
let ownershipLost = false;
|
|
1296
1428
|
let renewal;
|
|
1297
1429
|
const heartbeat = setInterval(() => {
|
|
@@ -1299,7 +1431,7 @@ class QueueWorker {
|
|
|
1299
1431
|
return;
|
|
1300
1432
|
renewal = (async () => {
|
|
1301
1433
|
try {
|
|
1302
|
-
if (!await updateQueueReservation(this.client, queueKey, reservation, "renew")) {
|
|
1434
|
+
if (!await updateQueueReservation(this.client, queueKey, reservation, "renew", this.visibilityMs)) {
|
|
1303
1435
|
ownershipLost = true;
|
|
1304
1436
|
}
|
|
1305
1437
|
} catch (error) {
|
|
@@ -1309,22 +1441,30 @@ class QueueWorker {
|
|
|
1309
1441
|
})().finally(() => {
|
|
1310
1442
|
renewal = undefined;
|
|
1311
1443
|
});
|
|
1312
|
-
}, Math.max(1, Math.floor(
|
|
1444
|
+
}, Math.max(1, Math.floor(this.visibilityMs / 3)));
|
|
1313
1445
|
try {
|
|
1314
|
-
await runQueueJob(envelope, this.failedJobs, {
|
|
1446
|
+
await runQueueJob(envelope, this.failedJobs, this.registry, {
|
|
1447
|
+
retryPolicy: this.retryPolicy,
|
|
1315
1448
|
onFailureRecorded: () => {
|
|
1316
1449
|
acknowledge = true;
|
|
1317
1450
|
}
|
|
1318
1451
|
});
|
|
1319
1452
|
acknowledge = true;
|
|
1320
1453
|
} catch (error) {
|
|
1321
|
-
|
|
1454
|
+
if (error instanceof QueueHandoffError3)
|
|
1455
|
+
invalidHandoff = true;
|
|
1456
|
+
else
|
|
1457
|
+
console.error("[QueueWorker] Job failed:", error);
|
|
1322
1458
|
} finally {
|
|
1323
1459
|
clearInterval(heartbeat);
|
|
1324
1460
|
await renewal;
|
|
1325
1461
|
}
|
|
1462
|
+
if (invalidHandoff && !ownershipLost) {
|
|
1463
|
+
await updateQueueReservation(this.client, queueKey, reservation, "quarantine", this.visibilityMs);
|
|
1464
|
+
return true;
|
|
1465
|
+
}
|
|
1326
1466
|
if (acknowledge && !ownershipLost) {
|
|
1327
|
-
await updateQueueReservation(this.client, queueKey, reservation, "ack");
|
|
1467
|
+
await updateQueueReservation(this.client, queueKey, reservation, "ack", this.visibilityMs);
|
|
1328
1468
|
}
|
|
1329
1469
|
return true;
|
|
1330
1470
|
}
|
|
@@ -1353,12 +1493,13 @@ class QueueWorker {
|
|
|
1353
1493
|
this.client.close();
|
|
1354
1494
|
}
|
|
1355
1495
|
}
|
|
1356
|
-
async function countPendingQueueJobs(redisUrl) {
|
|
1496
|
+
async function countPendingQueueJobs(redisUrl, config = resolveQueueRuntimeConfig({})) {
|
|
1497
|
+
const { transport, queueKeys } = snapshotQueueRuntimeConfig(config);
|
|
1357
1498
|
const client = new RedisClient2(redisUrl);
|
|
1358
1499
|
try {
|
|
1359
1500
|
let total = 0;
|
|
1360
|
-
for (const queueKey of
|
|
1361
|
-
total +=
|
|
1501
|
+
for (const queueKey of queueKeys) {
|
|
1502
|
+
total += transport === "streams" ? await readStreamQueueDepth(client, queueKey) : await client.llen(queueKey);
|
|
1362
1503
|
}
|
|
1363
1504
|
return total;
|
|
1364
1505
|
} finally {
|
|
@@ -1372,16 +1513,12 @@ class Job {
|
|
|
1372
1513
|
maxAttempts;
|
|
1373
1514
|
backoffMs;
|
|
1374
1515
|
priority;
|
|
1375
|
-
constructor() {
|
|
1376
|
-
const jobName = this.constructor.jobName;
|
|
1377
|
-
if (jobName) {
|
|
1378
|
-
jobRegistry.track(jobName, this);
|
|
1379
|
-
}
|
|
1380
|
-
}
|
|
1381
1516
|
}
|
|
1382
1517
|
|
|
1383
1518
|
class SyncQueue {
|
|
1384
|
-
async dispatch(job, payload) {
|
|
1519
|
+
async dispatch(job, payload, options) {
|
|
1520
|
+
if (options?.handoff !== undefined)
|
|
1521
|
+
throw new Error("Identity handoff requires an application queue.");
|
|
1385
1522
|
await job.handle(payload);
|
|
1386
1523
|
}
|
|
1387
1524
|
}
|
|
@@ -1406,7 +1543,9 @@ class AsyncQueue {
|
|
|
1406
1543
|
this.active.delete(running);
|
|
1407
1544
|
});
|
|
1408
1545
|
}
|
|
1409
|
-
async dispatch(job, payload) {
|
|
1546
|
+
async dispatch(job, payload, options) {
|
|
1547
|
+
if (options?.handoff !== undefined)
|
|
1548
|
+
throw new Error("Identity handoff requires an application queue.");
|
|
1410
1549
|
this.enqueue(() => job.handle(payload), (error) => {
|
|
1411
1550
|
console.error("[AsyncQueue] Job failed:", error);
|
|
1412
1551
|
});
|
|
@@ -1427,64 +1566,81 @@ function createQueue(driver) {
|
|
|
1427
1566
|
// ../../src/core/queue/resilientQueue.ts
|
|
1428
1567
|
class ResilientQueue extends AsyncQueue {
|
|
1429
1568
|
failedJobs;
|
|
1569
|
+
registry;
|
|
1430
1570
|
asyncDispatch;
|
|
1431
|
-
|
|
1571
|
+
retryPolicy;
|
|
1572
|
+
constructor(failedJobs, registry, asyncDispatch = false, retryPolicy = resolveQueueConfig()) {
|
|
1432
1573
|
super();
|
|
1433
1574
|
this.failedJobs = failedJobs;
|
|
1575
|
+
this.registry = registry;
|
|
1434
1576
|
this.asyncDispatch = asyncDispatch;
|
|
1577
|
+
this.retryPolicy = snapshotQueueRetryPolicy(retryPolicy);
|
|
1578
|
+
}
|
|
1579
|
+
async dispatch(job, payload, options) {
|
|
1580
|
+
await this.admit(job, payload, options);
|
|
1435
1581
|
}
|
|
1436
|
-
async
|
|
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) {
|
|
1437
1588
|
this.assertOpen();
|
|
1438
|
-
const name =
|
|
1589
|
+
const name = this.registry.resolveName(job);
|
|
1439
1590
|
if (!name) {
|
|
1440
1591
|
throw new Error("Job is not registered with the queue worker registry.");
|
|
1441
1592
|
}
|
|
1442
|
-
const envelope = {
|
|
1593
|
+
const envelope = snapshotQueueJobEnvelope({
|
|
1443
1594
|
name,
|
|
1595
|
+
jobId: jobId ?? crypto.randomUUID(),
|
|
1444
1596
|
payload,
|
|
1445
|
-
attempts: 0
|
|
1446
|
-
|
|
1597
|
+
attempts: 0,
|
|
1598
|
+
...options?.handoff === undefined ? {} : { handoff: options.handoff }
|
|
1599
|
+
});
|
|
1447
1600
|
if (this.asyncDispatch) {
|
|
1448
|
-
this.enqueue(() => runQueueJob(envelope, this.failedJobs), (error) => {
|
|
1601
|
+
this.enqueue(() => runQueueJob(envelope, this.failedJobs, this.registry, { retryPolicy: this.retryPolicy }), (error) => {
|
|
1449
1602
|
console.error("[ResilientQueue] Job failed:", error);
|
|
1450
1603
|
});
|
|
1451
1604
|
return;
|
|
1452
1605
|
}
|
|
1453
|
-
await runQueueJob(envelope, this.failedJobs);
|
|
1606
|
+
await runQueueJob(envelope, this.failedJobs, this.registry, { retryPolicy: this.retryPolicy });
|
|
1454
1607
|
}
|
|
1455
1608
|
}
|
|
1456
1609
|
|
|
1457
1610
|
// ../../src/core/queue/publicQueue.ts
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
function createTrackedJob(name, job) {
|
|
1462
|
-
return jobRegistry.track(name, job);
|
|
1611
|
+
import { createIdentityQueue, createQueueHandoff } from "@getstrata/core/queue/queueHandoff";
|
|
1612
|
+
function createTrackedJob(registry, name, job) {
|
|
1613
|
+
return registry.track(name, job);
|
|
1463
1614
|
}
|
|
1464
|
-
function createProductionQueue(driver, options
|
|
1465
|
-
options.
|
|
1466
|
-
const failedJobs = options.failedJobs ?? createFailedJobService();
|
|
1615
|
+
function createProductionQueue(driver, options) {
|
|
1616
|
+
const failedJobs = options.failedJobs;
|
|
1467
1617
|
if (driver === "redis") {
|
|
1468
1618
|
if (!options.redisUrl) {
|
|
1469
1619
|
throw new Error('QUEUE_DRIVER="redis" requires REDIS_URL to be set.');
|
|
1470
1620
|
}
|
|
1471
|
-
|
|
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);
|
|
1472
1623
|
}
|
|
1473
|
-
return new ResilientQueue(failedJobs, driver === "async");
|
|
1624
|
+
return new ResilientQueue(failedJobs, options.registry, driver === "async", snapshotQueueRetryPolicy(options.retryPolicy ?? resolveQueueConfig()));
|
|
1474
1625
|
}
|
|
1475
|
-
function createQueueWorker(redisUrl, failedJobs =
|
|
1476
|
-
|
|
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);
|
|
1477
1630
|
}
|
|
1478
1631
|
export {
|
|
1479
|
-
|
|
1632
|
+
FailedJobRepository,
|
|
1480
1633
|
failedJobService_default as FailedJobService,
|
|
1481
1634
|
QueueWorker,
|
|
1482
1635
|
RedisQueue,
|
|
1483
1636
|
ResilientQueue,
|
|
1484
1637
|
createFailedJobService,
|
|
1638
|
+
createIdentityQueue,
|
|
1485
1639
|
createProductionQueue,
|
|
1640
|
+
createQueueHandoff,
|
|
1641
|
+
createQueueServices,
|
|
1486
1642
|
createQueueWorker,
|
|
1643
|
+
createSystemQueueServices,
|
|
1487
1644
|
createTrackedJob,
|
|
1488
|
-
jobRegistry,
|
|
1489
1645
|
runQueueJob
|
|
1490
1646
|
};
|