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