@contember/engine-actions 2.2.0-alpha.2 → 2.2.0-alpha.21
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/CLAUDE.md +99 -0
- package/dist/development/index.cjs +28 -13
- package/dist/development/index.cjs.map +1 -1
- package/dist/development/index.js +29 -14
- package/dist/development/index.js.map +1 -1
- package/dist/development/src/ActionsExecutionContainerHookFactory.cjs +14 -2
- package/dist/development/src/ActionsExecutionContainerHookFactory.cjs.map +1 -1
- package/dist/development/src/ActionsExecutionContainerHookFactory.js +14 -2
- package/dist/development/src/ActionsExecutionContainerHookFactory.js.map +1 -1
- package/dist/development/src/ActionsMetrics.cjs +118 -0
- package/dist/development/src/ActionsMetrics.cjs.map +1 -0
- package/dist/development/src/ActionsMetrics.js +118 -0
- package/dist/development/src/ActionsMetrics.js.map +1 -0
- package/dist/development/src/dispatch/DispatchWorkerSupervisor.cjs +13 -7
- package/dist/development/src/dispatch/DispatchWorkerSupervisor.cjs.map +1 -1
- package/dist/development/src/dispatch/DispatchWorkerSupervisor.js +13 -7
- package/dist/development/src/dispatch/DispatchWorkerSupervisor.js.map +1 -1
- package/dist/development/src/dispatch/EventDispatcher.cjs +93 -24
- package/dist/development/src/dispatch/EventDispatcher.cjs.map +1 -1
- package/dist/development/src/dispatch/EventDispatcher.js +93 -24
- package/dist/development/src/dispatch/EventDispatcher.js.map +1 -1
- package/dist/development/src/dispatch/EventsRepository.cjs +87 -22
- package/dist/development/src/dispatch/EventsRepository.cjs.map +1 -1
- package/dist/development/src/dispatch/EventsRepository.js +87 -22
- package/dist/development/src/dispatch/EventsRepository.js.map +1 -1
- package/dist/development/src/dispatch/ProjectDispatcher.cjs +92 -22
- package/dist/development/src/dispatch/ProjectDispatcher.cjs.map +1 -1
- package/dist/development/src/dispatch/ProjectDispatcher.js +92 -22
- package/dist/development/src/dispatch/ProjectDispatcher.js.map +1 -1
- package/dist/development/src/dispatch/WebhookFetcher.cjs +6 -1
- package/dist/development/src/dispatch/WebhookFetcher.cjs.map +1 -1
- package/dist/development/src/dispatch/WebhookFetcher.js +7 -2
- package/dist/development/src/dispatch/WebhookFetcher.js.map +1 -1
- package/dist/development/src/dispatch/WebhookTargetHandler.cjs +154 -25
- package/dist/development/src/dispatch/WebhookTargetHandler.cjs.map +1 -1
- package/dist/development/src/dispatch/WebhookTargetHandler.js +154 -25
- package/dist/development/src/dispatch/WebhookTargetHandler.js.map +1 -1
- package/dist/development/src/dispatch/redactRequestError.cjs +49 -0
- package/dist/development/src/dispatch/redactRequestError.cjs.map +1 -0
- package/dist/development/src/dispatch/redactRequestError.js +49 -0
- package/dist/development/src/dispatch/redactRequestError.js.map +1 -0
- package/dist/development/src/dispatch/truncateText.cjs +12 -0
- package/dist/development/src/dispatch/truncateText.cjs.map +1 -0
- package/dist/development/src/dispatch/truncateText.js +12 -0
- package/dist/development/src/dispatch/truncateText.js.map +1 -0
- package/dist/development/src/graphql/http/ActionsGraphQLHandlerFactory.cjs +1 -1
- package/dist/development/src/graphql/http/ActionsGraphQLHandlerFactory.cjs.map +1 -1
- package/dist/development/src/graphql/http/ActionsGraphQLHandlerFactory.js +2 -2
- package/dist/development/src/graphql/http/ActionsGraphQLHandlerFactory.js.map +1 -1
- package/dist/development/src/graphql/http/ActionsWebsocketControllerFactory.cjs +68 -28
- package/dist/development/src/graphql/http/ActionsWebsocketControllerFactory.cjs.map +1 -1
- package/dist/development/src/graphql/http/ActionsWebsocketControllerFactory.js +68 -28
- package/dist/development/src/graphql/http/ActionsWebsocketControllerFactory.js.map +1 -1
- package/dist/development/src/graphql/resolvers/ResolversFactory.cjs +1 -1
- package/dist/development/src/graphql/resolvers/ResolversFactory.cjs.map +1 -1
- package/dist/development/src/graphql/resolvers/ResolversFactory.js +1 -1
- package/dist/development/src/graphql/resolvers/ResolversFactory.js.map +1 -1
- package/dist/development/src/graphql/resolvers/mutation/SetVariablesMutationResolver.cjs +7 -2
- package/dist/development/src/graphql/resolvers/mutation/SetVariablesMutationResolver.cjs.map +1 -1
- package/dist/development/src/graphql/resolvers/mutation/SetVariablesMutationResolver.js +7 -2
- package/dist/development/src/graphql/resolvers/mutation/SetVariablesMutationResolver.js.map +1 -1
- package/dist/development/src/graphql/resolvers/query/EventsQueryResolver.cjs +1 -1
- package/dist/development/src/graphql/resolvers/query/EventsQueryResolver.cjs.map +1 -1
- package/dist/development/src/graphql/resolvers/query/EventsQueryResolver.js +1 -1
- package/dist/development/src/graphql/resolvers/query/EventsQueryResolver.js.map +1 -1
- package/dist/development/src/graphql/resolvers/query/VariablesQueryResolver.cjs +1 -4
- package/dist/development/src/graphql/resolvers/query/VariablesQueryResolver.cjs.map +1 -1
- package/dist/development/src/graphql/resolvers/query/VariablesQueryResolver.js +1 -4
- package/dist/development/src/graphql/resolvers/query/VariablesQueryResolver.js.map +1 -1
- package/dist/development/src/graphql/schema/actions.graphql.cjs +27 -11
- package/dist/development/src/graphql/schema/actions.graphql.cjs.map +1 -1
- package/dist/development/src/graphql/schema/actions.graphql.js +27 -11
- package/dist/development/src/graphql/schema/actions.graphql.js.map +1 -1
- package/dist/development/src/model/VariablesManager.cjs +59 -5
- package/dist/development/src/model/VariablesManager.cjs.map +1 -1
- package/dist/development/src/model/VariablesManager.js +60 -6
- package/dist/development/src/model/VariablesManager.js.map +1 -1
- package/dist/development/src/triggers/TriggerPayloadPersister.cjs +3 -1
- package/dist/development/src/triggers/TriggerPayloadPersister.cjs.map +1 -1
- package/dist/development/src/triggers/TriggerPayloadPersister.js +3 -1
- package/dist/development/src/triggers/TriggerPayloadPersister.js.map +1 -1
- package/dist/production/index.cjs +28 -13
- package/dist/production/index.cjs.map +1 -1
- package/dist/production/index.js +29 -14
- package/dist/production/index.js.map +1 -1
- package/dist/production/src/ActionsExecutionContainerHookFactory.cjs +14 -2
- package/dist/production/src/ActionsExecutionContainerHookFactory.cjs.map +1 -1
- package/dist/production/src/ActionsExecutionContainerHookFactory.js +14 -2
- package/dist/production/src/ActionsExecutionContainerHookFactory.js.map +1 -1
- package/dist/production/src/ActionsMetrics.cjs +118 -0
- package/dist/production/src/ActionsMetrics.cjs.map +1 -0
- package/dist/production/src/ActionsMetrics.js +118 -0
- package/dist/production/src/ActionsMetrics.js.map +1 -0
- package/dist/production/src/dispatch/DispatchWorkerSupervisor.cjs +13 -7
- package/dist/production/src/dispatch/DispatchWorkerSupervisor.cjs.map +1 -1
- package/dist/production/src/dispatch/DispatchWorkerSupervisor.js +13 -7
- package/dist/production/src/dispatch/DispatchWorkerSupervisor.js.map +1 -1
- package/dist/production/src/dispatch/EventDispatcher.cjs +93 -24
- package/dist/production/src/dispatch/EventDispatcher.cjs.map +1 -1
- package/dist/production/src/dispatch/EventDispatcher.js +93 -24
- package/dist/production/src/dispatch/EventDispatcher.js.map +1 -1
- package/dist/production/src/dispatch/EventsRepository.cjs +87 -22
- package/dist/production/src/dispatch/EventsRepository.cjs.map +1 -1
- package/dist/production/src/dispatch/EventsRepository.js +87 -22
- package/dist/production/src/dispatch/EventsRepository.js.map +1 -1
- package/dist/production/src/dispatch/ProjectDispatcher.cjs +92 -22
- package/dist/production/src/dispatch/ProjectDispatcher.cjs.map +1 -1
- package/dist/production/src/dispatch/ProjectDispatcher.js +92 -22
- package/dist/production/src/dispatch/ProjectDispatcher.js.map +1 -1
- package/dist/production/src/dispatch/WebhookFetcher.cjs +6 -1
- package/dist/production/src/dispatch/WebhookFetcher.cjs.map +1 -1
- package/dist/production/src/dispatch/WebhookFetcher.js +7 -2
- package/dist/production/src/dispatch/WebhookFetcher.js.map +1 -1
- package/dist/production/src/dispatch/WebhookTargetHandler.cjs +154 -25
- package/dist/production/src/dispatch/WebhookTargetHandler.cjs.map +1 -1
- package/dist/production/src/dispatch/WebhookTargetHandler.js +154 -25
- package/dist/production/src/dispatch/WebhookTargetHandler.js.map +1 -1
- package/dist/production/src/dispatch/redactRequestError.cjs +49 -0
- package/dist/production/src/dispatch/redactRequestError.cjs.map +1 -0
- package/dist/production/src/dispatch/redactRequestError.js +49 -0
- package/dist/production/src/dispatch/redactRequestError.js.map +1 -0
- package/dist/production/src/dispatch/truncateText.cjs +12 -0
- package/dist/production/src/dispatch/truncateText.cjs.map +1 -0
- package/dist/production/src/dispatch/truncateText.js +12 -0
- package/dist/production/src/dispatch/truncateText.js.map +1 -0
- package/dist/production/src/graphql/http/ActionsGraphQLHandlerFactory.cjs +1 -1
- package/dist/production/src/graphql/http/ActionsGraphQLHandlerFactory.cjs.map +1 -1
- package/dist/production/src/graphql/http/ActionsGraphQLHandlerFactory.js +2 -2
- package/dist/production/src/graphql/http/ActionsGraphQLHandlerFactory.js.map +1 -1
- package/dist/production/src/graphql/http/ActionsWebsocketControllerFactory.cjs +68 -28
- package/dist/production/src/graphql/http/ActionsWebsocketControllerFactory.cjs.map +1 -1
- package/dist/production/src/graphql/http/ActionsWebsocketControllerFactory.js +68 -28
- package/dist/production/src/graphql/http/ActionsWebsocketControllerFactory.js.map +1 -1
- package/dist/production/src/graphql/resolvers/ResolversFactory.cjs +1 -1
- package/dist/production/src/graphql/resolvers/ResolversFactory.cjs.map +1 -1
- package/dist/production/src/graphql/resolvers/ResolversFactory.js +1 -1
- package/dist/production/src/graphql/resolvers/ResolversFactory.js.map +1 -1
- package/dist/production/src/graphql/resolvers/mutation/SetVariablesMutationResolver.cjs +7 -2
- package/dist/production/src/graphql/resolvers/mutation/SetVariablesMutationResolver.cjs.map +1 -1
- package/dist/production/src/graphql/resolvers/mutation/SetVariablesMutationResolver.js +7 -2
- package/dist/production/src/graphql/resolvers/mutation/SetVariablesMutationResolver.js.map +1 -1
- package/dist/production/src/graphql/resolvers/query/EventsQueryResolver.cjs +1 -1
- package/dist/production/src/graphql/resolvers/query/EventsQueryResolver.cjs.map +1 -1
- package/dist/production/src/graphql/resolvers/query/EventsQueryResolver.js +1 -1
- package/dist/production/src/graphql/resolvers/query/EventsQueryResolver.js.map +1 -1
- package/dist/production/src/graphql/resolvers/query/VariablesQueryResolver.cjs +1 -4
- package/dist/production/src/graphql/resolvers/query/VariablesQueryResolver.cjs.map +1 -1
- package/dist/production/src/graphql/resolvers/query/VariablesQueryResolver.js +1 -4
- package/dist/production/src/graphql/resolvers/query/VariablesQueryResolver.js.map +1 -1
- package/dist/production/src/graphql/schema/actions.graphql.cjs +27 -11
- package/dist/production/src/graphql/schema/actions.graphql.cjs.map +1 -1
- package/dist/production/src/graphql/schema/actions.graphql.js +27 -11
- package/dist/production/src/graphql/schema/actions.graphql.js.map +1 -1
- package/dist/production/src/model/VariablesManager.cjs +59 -5
- package/dist/production/src/model/VariablesManager.cjs.map +1 -1
- package/dist/production/src/model/VariablesManager.js +60 -6
- package/dist/production/src/model/VariablesManager.js.map +1 -1
- package/dist/production/src/triggers/TriggerPayloadPersister.cjs +3 -1
- package/dist/production/src/triggers/TriggerPayloadPersister.cjs.map +1 -1
- package/dist/production/src/triggers/TriggerPayloadPersister.js +3 -1
- package/dist/production/src/triggers/TriggerPayloadPersister.js.map +1 -1
- package/dist/tests/cases/unit/actionsMetrics.test.d.ts +2 -0
- package/dist/tests/cases/unit/actionsMetrics.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/actionsWebsocketController.test.d.ts +2 -0
- package/dist/tests/cases/unit/actionsWebsocketController.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/dispatchTracing.test.d.ts +2 -0
- package/dist/tests/cases/unit/dispatchTracing.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/eventsRepository.test.d.ts +2 -0
- package/dist/tests/cases/unit/eventsRepository.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/projectDispatcher.test.d.ts +2 -0
- package/dist/tests/cases/unit/projectDispatcher.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/redactRequestError.test.d.ts +2 -0
- package/dist/tests/cases/unit/redactRequestError.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/truncateText.test.d.ts +2 -0
- package/dist/tests/cases/unit/truncateText.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/variablesManager.test.d.ts +2 -0
- package/dist/tests/cases/unit/variablesManager.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/webhookEgress.test.d.ts +2 -0
- package/dist/tests/cases/unit/webhookEgress.test.d.ts.map +1 -0
- package/dist/tests/cases/unit/webhookTracing.test.d.ts +2 -0
- package/dist/tests/cases/unit/webhookTracing.test.d.ts.map +1 -0
- package/dist/tests/src/event.d.ts +4 -0
- package/dist/tests/src/event.d.ts.map +1 -0
- package/dist/tests/src/gql.d.ts.map +1 -1
- package/dist/tests/src/tracer.d.ts +7 -0
- package/dist/tests/src/tracer.d.ts.map +1 -0
- package/dist/tests/src/utils.d.ts +6 -0
- package/dist/tests/src/utils.d.ts.map +1 -0
- package/dist/tests/src/uuid.d.ts.map +1 -1
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/ActionsExecutionContainerHookFactory.d.ts +3 -1
- package/dist/types/ActionsExecutionContainerHookFactory.d.ts.map +1 -1
- package/dist/types/ActionsMetrics.d.ts +65 -0
- package/dist/types/ActionsMetrics.d.ts.map +1 -0
- package/dist/types/ListenerStoreProvider.d.ts.map +1 -1
- package/dist/types/authorization/ActionsPermissionsFactory.d.ts.map +1 -1
- package/dist/types/dispatch/DispatchWorkerSupervisor.d.ts.map +1 -1
- package/dist/types/dispatch/EventDispatcher.d.ts +15 -4
- package/dist/types/dispatch/EventDispatcher.d.ts.map +1 -1
- package/dist/types/dispatch/EventsRepository.d.ts +34 -3
- package/dist/types/dispatch/EventsRepository.d.ts.map +1 -1
- package/dist/types/dispatch/LazyDispatchWorker.d.ts.map +1 -1
- package/dist/types/dispatch/ProjectDispatcher.d.ts +19 -5
- package/dist/types/dispatch/ProjectDispatcher.d.ts.map +1 -1
- package/dist/types/dispatch/TargetHandlerResolver.d.ts.map +1 -1
- package/dist/types/dispatch/WebhookFetcher.d.ts +7 -2
- package/dist/types/dispatch/WebhookFetcher.d.ts.map +1 -1
- package/dist/types/dispatch/WebhookTargetHandler.d.ts +14 -1
- package/dist/types/dispatch/WebhookTargetHandler.d.ts.map +1 -1
- package/dist/types/dispatch/redactRequestError.d.ts +8 -0
- package/dist/types/dispatch/redactRequestError.d.ts.map +1 -0
- package/dist/types/dispatch/truncateText.d.ts +6 -0
- package/dist/types/dispatch/truncateText.d.ts.map +1 -0
- package/dist/types/graphql/http/ActionsApiMiddlewareFactory.d.ts.map +1 -1
- package/dist/types/graphql/http/ActionsContextResolver.d.ts.map +1 -1
- package/dist/types/graphql/http/ActionsGraphQLHandlerFactory.d.ts.map +1 -1
- package/dist/types/graphql/http/ActionsWebsocketControllerFactory.d.ts +7 -5
- package/dist/types/graphql/http/ActionsWebsocketControllerFactory.d.ts.map +1 -1
- package/dist/types/graphql/resolvers/ResolversFactory.d.ts.map +1 -1
- package/dist/types/graphql/resolvers/mutation/ProcessBatchMutationResolver.d.ts.map +1 -1
- package/dist/types/graphql/resolvers/mutation/RetryEventMutationResolver.d.ts.map +1 -1
- package/dist/types/graphql/resolvers/mutation/SetVariablesMutationResolver.d.ts +3 -1
- package/dist/types/graphql/resolvers/mutation/SetVariablesMutationResolver.d.ts.map +1 -1
- package/dist/types/graphql/resolvers/mutation/StopEventMutationResolver.d.ts.map +1 -1
- package/dist/types/graphql/resolvers/query/EventsQueryResolver.d.ts.map +1 -1
- package/dist/types/graphql/resolvers/query/VariablesQueryResolver.d.ts +1 -4
- package/dist/types/graphql/resolvers/query/VariablesQueryResolver.d.ts.map +1 -1
- package/dist/types/graphql/schema/actions.graphql.d.ts.map +1 -1
- package/dist/types/graphql/schema/index.d.ts +27 -17
- package/dist/types/graphql/schema/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/model/EventByIdQuery.d.ts.map +1 -1
- package/dist/types/model/EventsInProcessingQuery.d.ts.map +1 -1
- package/dist/types/model/EventsToProcessQuery.d.ts.map +1 -1
- package/dist/types/model/FailedEventsQuery.d.ts.map +1 -1
- package/dist/types/model/SetVariableCommand.d.ts.map +1 -1
- package/dist/types/model/VariablesManager.d.ts +18 -3
- package/dist/types/model/VariablesManager.d.ts.map +1 -1
- package/dist/types/model/VariablesQuery.d.ts.map +1 -1
- package/dist/types/triggers/TriggerHandler.d.ts.map +1 -1
- package/dist/types/triggers/TriggerIndirectChangesFetcher.d.ts.map +1 -1
- package/dist/types/triggers/TriggerListenersBuilder.d.ts.map +1 -1
- package/dist/types/triggers/TriggerListenersFactory.d.ts.map +1 -1
- package/dist/types/triggers/TriggerListenersStore.d.ts.map +1 -1
- package/dist/types/triggers/TriggerPayloadBuilder.d.ts.map +1 -1
- package/dist/types/triggers/TriggerPayloadManager.d.ts.map +1 -1
- package/dist/types/triggers/TriggerPayloadPersister.d.ts +3 -1
- package/dist/types/triggers/TriggerPayloadPersister.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/dist/types/utils/map.d.ts.map +1 -1
- package/dist/types/utils/notifyChannel.d.ts.map +1 -1
- package/graphql.codegen.yml +1 -1
- package/package.json +18 -16
- package/src/ActionsExecutionContainerHookFactory.ts +19 -2
- package/src/ActionsMetrics.ts +160 -0
- package/src/dispatch/DispatchWorkerSupervisor.ts +14 -7
- package/src/dispatch/EventDispatcher.ts +104 -26
- package/src/dispatch/EventsRepository.ts +146 -28
- package/src/dispatch/ProjectDispatcher.ts +115 -22
- package/src/dispatch/WebhookFetcher.ts +11 -3
- package/src/dispatch/WebhookTargetHandler.ts +182 -28
- package/src/dispatch/redactRequestError.ts +65 -0
- package/src/dispatch/truncateText.ts +13 -0
- package/src/graphql/http/ActionsGraphQLHandlerFactory.ts +2 -2
- package/src/graphql/http/ActionsWebsocketControllerFactory.ts +73 -30
- package/src/graphql/resolvers/ResolversFactory.ts +1 -1
- package/src/graphql/resolvers/mutation/SetVariablesMutationResolver.ts +8 -1
- package/src/graphql/resolvers/query/EventsQueryResolver.ts +1 -1
- package/src/graphql/resolvers/query/VariablesQueryResolver.ts +1 -4
- package/src/graphql/schema/actions.graphql.ts +26 -12
- package/src/graphql/schema/index.ts +30 -17
- package/src/index.ts +37 -12
- package/src/model/VariablesManager.ts +72 -6
- package/src/triggers/TriggerPayloadPersister.ts +6 -0
- package/src/tsconfig.json +1 -0
- package/tests/cases/unit/actionsMetrics.test.ts +116 -0
- package/tests/cases/unit/actionsWebsocketController.test.ts +270 -0
- package/tests/cases/unit/dispatchTracing.test.ts +277 -0
- package/tests/cases/unit/eventsRepository.test.ts +158 -0
- package/tests/cases/unit/projectDispatcher.test.ts +166 -0
- package/tests/cases/unit/redactRequestError.test.ts +66 -0
- package/tests/cases/unit/truncateText.test.ts +25 -0
- package/tests/cases/unit/variablesManager.test.ts +154 -0
- package/tests/cases/unit/webhookEgress.test.ts +80 -0
- package/tests/cases/unit/webhookHandler.test.ts +328 -33
- package/tests/cases/unit/webhookTracing.test.ts +163 -0
- package/tests/src/event.ts +27 -0
- package/tests/src/tracer.ts +11 -0
- package/tests/src/utils.ts +5 -0
|
@@ -3,26 +3,45 @@ import { EventRow, HandledEvent } from './types.js'
|
|
|
3
3
|
import { Actions } from '@contember/schema'
|
|
4
4
|
import { eventsToProcessSpecification, eventsToProcessStateSpecification } from '../model/EventsToProcessSpecification.js'
|
|
5
5
|
import { notify } from '../utils/notifyChannel.js'
|
|
6
|
+
import { Logger } from '@contember/logger'
|
|
6
7
|
|
|
7
8
|
const ACK_TIMEOUT_MS = 1_000 * 60 * 10 // 10 minutes
|
|
8
9
|
const DEFAULT_REPEAT_INTERVAL_MS = 5_000 // 5 seconds
|
|
9
10
|
const DEFAULT_MAX_ATTEMPTS = 10
|
|
10
11
|
const DEFAULT_BATCH_SIZE = 1
|
|
12
|
+
/** How long a target with an undefined variable is held back before it is checked again without a variable change. */
|
|
13
|
+
const UNDEFINED_VARIABLE_RECHECK_MS = 1_000 * 60 * 10 // 10 minutes
|
|
14
|
+
/** The last log entry of an event held by {@link EventsRepository.holdForUndefinedVariables}. */
|
|
15
|
+
const heldMarkerCondition = `log -> -1 -> 'undefinedVariables' IS NOT NULL`
|
|
11
16
|
|
|
12
17
|
export class EventsRepository {
|
|
13
|
-
public async fetchBatch(actions: Actions.Schema, db: Client): Promise<FetchBatchResult> {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
public async fetchBatch(actions: Actions.Schema, db: Client, logger: Logger): Promise<FetchBatchResult> {
|
|
19
|
+
// Events referencing a target that no longer exists in the schema are terminally failed and
|
|
20
|
+
// skipped over; count them so the caller can report them as terminal failures.
|
|
21
|
+
let unknownTargetFailed = 0
|
|
22
|
+
while (true) {
|
|
23
|
+
const primaryEvent = (await this.fetchInternal(db, 1))[0]
|
|
24
|
+
if (!primaryEvent) {
|
|
25
|
+
return { ok: false, backoffMs: await this.fetchBackOff(db), unknownTargetFailed }
|
|
26
|
+
}
|
|
27
|
+
const target = actions.targets[primaryEvent.target]
|
|
28
|
+
if (!target) {
|
|
29
|
+
if (await this.markFailedOnUnknownTarget(db, primaryEvent.target, primaryEvent.id)) {
|
|
30
|
+
unknownTargetFailed++
|
|
31
|
+
// Never retried: the schema has to name the target again before such an event can be delivered.
|
|
32
|
+
logger.error('Action event failed permanently, its target is not in the schema', {
|
|
33
|
+
eventId: primaryEvent.id,
|
|
34
|
+
target: primaryEvent.target,
|
|
35
|
+
trigger: primaryEvent.trigger,
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
continue
|
|
39
|
+
}
|
|
40
|
+
const batchSize = (target.batchSize ?? DEFAULT_BATCH_SIZE) - 1
|
|
41
|
+
// A batch is delivered to the primary event's target, so it may only take events of that target.
|
|
42
|
+
const batch = batchSize > 0 ? await this.fetchInternal(db, batchSize, primaryEvent.target) : []
|
|
43
|
+
return { ok: true, events: [primaryEvent, ...batch], target, unknownTargetFailed }
|
|
22
44
|
}
|
|
23
|
-
const batchSize = (target.batchSize ?? DEFAULT_BATCH_SIZE) - 1
|
|
24
|
-
const batch = batchSize > 0 ? await this.fetchInternal(db, batchSize) : []
|
|
25
|
-
return { ok: true, events: [primaryEvent, ...batch], target }
|
|
26
45
|
}
|
|
27
46
|
|
|
28
47
|
private async fetchBackOff(db: Client): Promise<number | undefined> {
|
|
@@ -60,22 +79,102 @@ export class EventsRepository {
|
|
|
60
79
|
.execute(db)
|
|
61
80
|
}
|
|
62
81
|
|
|
63
|
-
public async persistProcessed(db: Client, events: HandledEvent[]): Promise<
|
|
82
|
+
public async persistProcessed(db: Client, events: HandledEvent[], logger: Logger): Promise<PersistProcessedResult> {
|
|
64
83
|
return await db.transaction(async trx => {
|
|
65
84
|
await trx.query(Connection.REPEATABLE_READ)
|
|
66
85
|
const succeed = events.filter(it => it.result.ok)
|
|
67
86
|
await this.markSucceed(trx, succeed.map(it => it.row))
|
|
68
87
|
|
|
88
|
+
let retried = 0
|
|
89
|
+
let failed = 0
|
|
69
90
|
for (const event of events) {
|
|
70
91
|
if (!event.result.ok) {
|
|
71
|
-
await this.markFailed(trx, event)
|
|
92
|
+
const terminal = await this.markFailed(trx, event)
|
|
93
|
+
if (terminal) {
|
|
94
|
+
failed++
|
|
95
|
+
// Nothing else reports a given-up event; the target is named, never described by URL or headers.
|
|
96
|
+
logger.error('Action event failed permanently, no attempts left', {
|
|
97
|
+
eventId: event.row.id,
|
|
98
|
+
target: event.row.target,
|
|
99
|
+
trigger: event.row.trigger,
|
|
100
|
+
attempts: event.row.num_retries + 1,
|
|
101
|
+
code: event.result.code,
|
|
102
|
+
errorMessage: event.result.errorMessage,
|
|
103
|
+
})
|
|
104
|
+
} else {
|
|
105
|
+
retried++
|
|
106
|
+
}
|
|
72
107
|
}
|
|
73
108
|
}
|
|
74
|
-
return
|
|
109
|
+
return { succeeded: succeed.length, retried, failed }
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Holds back the due events of a target that interpolates an undefined variable, the given batch included,
|
|
115
|
+
* without spending an attempt. Events arriving while the target is held join that hold, so a target is checked
|
|
116
|
+
* again once per interval and each check rewrites only what came due. {@link resumeHeldForUndefinedVariables}
|
|
117
|
+
* releases them all once variables change.
|
|
118
|
+
*/
|
|
119
|
+
public async holdForUndefinedVariables(db: Client, { target, batch, variables }: {
|
|
120
|
+
target: string
|
|
121
|
+
batch: string[]
|
|
122
|
+
variables: readonly string[]
|
|
123
|
+
}): Promise<HoldResult> {
|
|
124
|
+
const now = new Date()
|
|
125
|
+
const entry = JSON.stringify({ ok: false, errorMessage: `Undefined variable ${variables.join(', ')}`, undefinedVariables: variables })
|
|
126
|
+
return await db.transaction(async trx => {
|
|
127
|
+
const current = await SelectBuilder.create<{ visible_at: Date }>()
|
|
128
|
+
.from('actions_event')
|
|
129
|
+
.select('visible_at')
|
|
130
|
+
.where(it =>
|
|
131
|
+
it
|
|
132
|
+
.in('state', ['created', 'retrying'])
|
|
133
|
+
.compare('target', Operator.eq, target)
|
|
134
|
+
.compare('visible_at', Operator.gt, now)
|
|
135
|
+
.raw(heldMarkerCondition)
|
|
136
|
+
)
|
|
137
|
+
.orderBy('visible_at')
|
|
138
|
+
.limit(1)
|
|
139
|
+
.getResult(trx)
|
|
140
|
+
const until = current[0]?.visible_at ?? new Date(now.getTime() + UNDEFINED_VARIABLE_RECHECK_MS)
|
|
141
|
+
await this.requeue(trx, batch, until)
|
|
142
|
+
const held = await UpdateBuilder.create()
|
|
143
|
+
.table('actions_event')
|
|
144
|
+
.values<EventRow>({
|
|
145
|
+
visible_at: until,
|
|
146
|
+
// a re-check that finds the same variables missing does not grow the log
|
|
147
|
+
log: it => it.raw('CASE WHEN log -> -1 = ?::jsonb THEN log ELSE log || ?::jsonb END', entry, entry),
|
|
148
|
+
})
|
|
149
|
+
.where(it =>
|
|
150
|
+
it
|
|
151
|
+
.in('state', ['created', 'retrying'])
|
|
152
|
+
.compare('target', Operator.eq, target)
|
|
153
|
+
.or(it => it.in('id', batch).compare('visible_at', Operator.lte, now))
|
|
154
|
+
)
|
|
155
|
+
.execute(trx)
|
|
156
|
+
return { held, joined: current.length > 0 }
|
|
75
157
|
})
|
|
76
158
|
}
|
|
77
159
|
|
|
78
|
-
|
|
160
|
+
/** Makes events held by {@link holdForUndefinedVariables} due now. Returns how many were released. */
|
|
161
|
+
public async resumeHeldForUndefinedVariables(db: Client): Promise<number> {
|
|
162
|
+
const now = new Date()
|
|
163
|
+
return await UpdateBuilder.create()
|
|
164
|
+
.table('actions_event')
|
|
165
|
+
.values<EventRow>({
|
|
166
|
+
visible_at: now,
|
|
167
|
+
})
|
|
168
|
+
.where(it =>
|
|
169
|
+
it
|
|
170
|
+
.in('state', ['created', 'retrying'])
|
|
171
|
+
.compare('visible_at', Operator.gt, now)
|
|
172
|
+
.raw(heldMarkerCondition)
|
|
173
|
+
)
|
|
174
|
+
.execute(db)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
public async requeue(db: Client, events: string[], visibleAt: Date = new Date()): Promise<void> {
|
|
79
178
|
await UpdateBuilder.create()
|
|
80
179
|
.table('actions_event')
|
|
81
180
|
.values<EventRow>({
|
|
@@ -92,7 +191,7 @@ export class EventsRepository {
|
|
|
92
191
|
'retrying',
|
|
93
192
|
),
|
|
94
193
|
last_state_change: new Date(),
|
|
95
|
-
visible_at:
|
|
194
|
+
visible_at: visibleAt,
|
|
96
195
|
})
|
|
97
196
|
.where(it => it.in('id', events))
|
|
98
197
|
.execute(db)
|
|
@@ -124,42 +223,44 @@ export class EventsRepository {
|
|
|
124
223
|
.execute(db)
|
|
125
224
|
}
|
|
126
225
|
|
|
127
|
-
|
|
226
|
+
/** Returns `true` when the event reached a terminal `failed` state, `false` when it will be retried. */
|
|
227
|
+
private async markFailed(db: Client, event: HandledEvent): Promise<boolean> {
|
|
128
228
|
const numRetries = event.row.num_retries + 1
|
|
129
229
|
const now = new Date()
|
|
130
230
|
const nextAttempt = new Date()
|
|
131
231
|
nextAttempt.setTime(
|
|
132
232
|
nextAttempt.getTime() + (event.target?.initialRepeatIntervalMs ?? DEFAULT_REPEAT_INTERVAL_MS) * Math.pow(2, event.row.num_retries),
|
|
133
233
|
)
|
|
234
|
+
const terminal = numRetries >= (event.target?.maxAttempts ?? DEFAULT_MAX_ATTEMPTS)
|
|
134
235
|
await UpdateBuilder.create()
|
|
135
236
|
.table('actions_event')
|
|
136
237
|
.values<EventRow>({
|
|
137
238
|
last_state_change: now,
|
|
138
239
|
num_retries: numRetries,
|
|
139
240
|
log: JSON.stringify([...event.row.log, event.result]) as any,
|
|
140
|
-
...(
|
|
241
|
+
...(terminal
|
|
141
242
|
? {
|
|
142
|
-
state: '
|
|
143
|
-
visible_at: nextAttempt,
|
|
243
|
+
state: 'failed',
|
|
144
244
|
}
|
|
145
245
|
: {
|
|
146
|
-
state: '
|
|
246
|
+
state: 'retrying',
|
|
247
|
+
visible_at: nextAttempt,
|
|
147
248
|
}),
|
|
148
249
|
})
|
|
149
250
|
.where({ id: event.row.id })
|
|
150
251
|
.execute(db)
|
|
252
|
+
return terminal
|
|
151
253
|
}
|
|
152
254
|
|
|
153
|
-
private async markFailedOnUnknownTarget(db: Client, targetName: string, primaryEventId: string) {
|
|
154
|
-
await UpdateBuilder.create()
|
|
255
|
+
private async markFailedOnUnknownTarget(db: Client, targetName: string, primaryEventId: string): Promise<boolean> {
|
|
256
|
+
const affectedRows = await UpdateBuilder.create()
|
|
155
257
|
.table('actions_event')
|
|
156
258
|
.where(it =>
|
|
157
259
|
it.or(it =>
|
|
158
260
|
it
|
|
159
261
|
.and(it =>
|
|
160
262
|
it
|
|
161
|
-
.in('state', ['
|
|
162
|
-
.compare('visible_at', Operator.lte, 'now')
|
|
263
|
+
.in('state', ['processing'])
|
|
163
264
|
.compare('target', Operator.eq, targetName)
|
|
164
265
|
)
|
|
165
266
|
.and(it =>
|
|
@@ -177,9 +278,26 @@ export class EventsRepository {
|
|
|
177
278
|
resolved_at: new Date(),
|
|
178
279
|
})
|
|
179
280
|
.execute(db)
|
|
281
|
+
return affectedRows > 0
|
|
180
282
|
}
|
|
181
283
|
}
|
|
182
284
|
|
|
183
285
|
export type FetchBatchResult =
|
|
184
|
-
| { ok: true; events: EventRow[]; target: Actions.AnyTarget }
|
|
185
|
-
| { ok: false; backoffMs?: number }
|
|
286
|
+
| { ok: true; events: EventRow[]; target: Actions.AnyTarget; unknownTargetFailed: number }
|
|
287
|
+
| { ok: false; backoffMs?: number; unknownTargetFailed: number }
|
|
288
|
+
|
|
289
|
+
export type HoldResult = {
|
|
290
|
+
/** Events held by this call. */
|
|
291
|
+
held: number
|
|
292
|
+
/** The target was already held, these events joined that hold. */
|
|
293
|
+
joined: boolean
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export type PersistProcessedResult = {
|
|
297
|
+
/** Events delivered successfully (terminal). */
|
|
298
|
+
succeeded: number
|
|
299
|
+
/** Events whose delivery attempt failed and that will be retried later. */
|
|
300
|
+
retried: number
|
|
301
|
+
/** Events whose delivery attempt failed and that reached a terminal `failed` state. */
|
|
302
|
+
failed: number
|
|
303
|
+
}
|
|
@@ -1,20 +1,74 @@
|
|
|
1
1
|
import { DatabaseContext } from '@contember/engine-system-api'
|
|
2
2
|
import { EventDispatcher } from './EventDispatcher.js'
|
|
3
3
|
import { ContentSchemaResolver } from '@contember/engine-http'
|
|
4
|
-
import { Runnable, RunnableArgs, Running } from '@contember/engine-common'
|
|
4
|
+
import { Runnable, RunnableArgs, Running, Supervisor, SupervisorOptions } from '@contember/engine-common'
|
|
5
5
|
import { Listener } from '@contember/database'
|
|
6
6
|
import { NOTIFY_CHANNEL_NAME } from '../utils/notifyChannel.js'
|
|
7
|
+
import { ActionsMetrics, ProjectActionsMetrics } from '../ActionsMetrics.js'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Upper bound on how long the loop parks while idle. Even when the queue is empty (and we'd
|
|
11
|
+
* otherwise wait indefinitely on a `pg_notify`), we re-check at least this often, so a lost
|
|
12
|
+
* notification self-heals and the heartbeat keeps refreshing while the worker is genuinely idle.
|
|
13
|
+
*/
|
|
14
|
+
const MAX_IDLE_SLEEP_MS = 30_000
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* How long `end()` waits for an in-flight batch. A webhook may take its full target timeout, which would
|
|
18
|
+
* outlive a typical shutdown grace period. Events of an abandoned batch stay in processing and are
|
|
19
|
+
* delivered again once their ACK timeout expires.
|
|
20
|
+
*/
|
|
21
|
+
const END_TIMEOUT_MS = 10_000
|
|
22
|
+
|
|
23
|
+
export interface ProjectDispatcherOptions {
|
|
24
|
+
readonly endTimeoutMs?: number
|
|
25
|
+
}
|
|
7
26
|
|
|
8
27
|
export class ProjectDispatcherFactory {
|
|
9
28
|
constructor(
|
|
10
29
|
private readonly dispatcher: EventDispatcher,
|
|
30
|
+
private readonly metrics: ActionsMetrics,
|
|
11
31
|
) {
|
|
12
32
|
}
|
|
13
33
|
|
|
34
|
+
/**
|
|
35
|
+
* A supervised dispatch worker for one project. Its heartbeat series is removed only when the worker is ended for good:
|
|
36
|
+
* after a crash the supervisor drops the crashed run, so an end() during the restart backoff never reaches the dispatcher.
|
|
37
|
+
*/
|
|
14
38
|
public create(
|
|
15
|
-
{ db, contentSchemaResolver, projectSlug }: {
|
|
16
|
-
|
|
17
|
-
|
|
39
|
+
{ db, contentSchemaResolver, projectSlug, supervisorOptions }: {
|
|
40
|
+
db: DatabaseContext
|
|
41
|
+
contentSchemaResolver: ContentSchemaResolver
|
|
42
|
+
projectSlug: string
|
|
43
|
+
supervisorOptions: SupervisorOptions
|
|
44
|
+
},
|
|
45
|
+
): Runnable {
|
|
46
|
+
const metrics = this.metrics.forProject(projectSlug)
|
|
47
|
+
const supervisor = new Supervisor(new ProjectDispatcher(this.dispatcher, db, contentSchemaResolver, projectSlug, metrics), supervisorOptions)
|
|
48
|
+
return {
|
|
49
|
+
run: async (args: RunnableArgs): Promise<Running> => {
|
|
50
|
+
let running: Running
|
|
51
|
+
try {
|
|
52
|
+
running = await supervisor.run(args)
|
|
53
|
+
} catch (e) {
|
|
54
|
+
metrics.dispose()
|
|
55
|
+
throw e
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
end: async () => {
|
|
59
|
+
try {
|
|
60
|
+
await running.end()
|
|
61
|
+
} finally {
|
|
62
|
+
metrics.dispose()
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public removeProjectMetrics(projectSlug: string): void {
|
|
71
|
+
this.metrics.removeProject(projectSlug)
|
|
18
72
|
}
|
|
19
73
|
}
|
|
20
74
|
|
|
@@ -24,11 +78,31 @@ export class ProjectDispatcher implements Runnable {
|
|
|
24
78
|
private readonly db: DatabaseContext,
|
|
25
79
|
private readonly contentSchemaResolver: ContentSchemaResolver,
|
|
26
80
|
private readonly projectSlug: string,
|
|
81
|
+
private readonly metrics: ProjectActionsMetrics,
|
|
82
|
+
private readonly options: ProjectDispatcherOptions = {},
|
|
27
83
|
) {
|
|
28
84
|
}
|
|
29
85
|
|
|
86
|
+
private async waitForDispatchToFinish(dispatchFinished: Promise<void>): Promise<boolean> {
|
|
87
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
88
|
+
try {
|
|
89
|
+
return await Promise.race([
|
|
90
|
+
dispatchFinished.then(() => true),
|
|
91
|
+
new Promise<boolean>(resolve => {
|
|
92
|
+
timer = setTimeout(() => resolve(false), this.options.endTimeoutMs ?? END_TIMEOUT_MS)
|
|
93
|
+
}),
|
|
94
|
+
])
|
|
95
|
+
} finally {
|
|
96
|
+
clearTimeout(timer)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
30
100
|
public async run({ logger, onError, onClose }: RunnableArgs): Promise<Running> {
|
|
31
101
|
return await new Promise<Running>(async (resolve, reject) => {
|
|
102
|
+
let resolveDispatchFinished = () => {}
|
|
103
|
+
const dispatchFinished = new Promise<void>(resolve => {
|
|
104
|
+
resolveDispatchFinished = resolve
|
|
105
|
+
})
|
|
32
106
|
let resolvePending = () => {}
|
|
33
107
|
let rejectPending = (e: any) => {}
|
|
34
108
|
|
|
@@ -44,6 +118,7 @@ export class ProjectDispatcher implements Runnable {
|
|
|
44
118
|
let succeedTotal = 0
|
|
45
119
|
let failedTotal = 0
|
|
46
120
|
let aborted = false
|
|
121
|
+
let endPromise: Promise<void> | undefined
|
|
47
122
|
|
|
48
123
|
try {
|
|
49
124
|
await this.db.scope(async db => {
|
|
@@ -55,40 +130,54 @@ export class ProjectDispatcher implements Runnable {
|
|
|
55
130
|
.run({ onError: rejectPendingRef, onClose: resolvePendingRef })
|
|
56
131
|
|
|
57
132
|
resolve({
|
|
58
|
-
end:
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
133
|
+
end: () => {
|
|
134
|
+
endPromise ??= (async () => {
|
|
135
|
+
aborted = true
|
|
136
|
+
resolvePendingRef()
|
|
137
|
+
const finished = await this.waitForDispatchToFinish(dispatchFinished)
|
|
138
|
+
if (!finished) {
|
|
139
|
+
logger.warn('Worker did not finish its batch in time, events still in processing will be delivered again')
|
|
140
|
+
}
|
|
141
|
+
logger.info('Worker terminated', {
|
|
142
|
+
succeed: succeedTotal,
|
|
143
|
+
failed: failedTotal,
|
|
144
|
+
})
|
|
145
|
+
})()
|
|
146
|
+
return endPromise
|
|
65
147
|
},
|
|
66
148
|
})
|
|
67
149
|
|
|
68
150
|
try {
|
|
69
151
|
while (!aborted) {
|
|
70
|
-
|
|
152
|
+
this.metrics.heartbeat()
|
|
153
|
+
const { succeeded, retried, failedAfterAttempt, failedUnknownTarget, backoffMs } = await this.dispatcher.processBatch({
|
|
71
154
|
db,
|
|
72
155
|
contentSchemaResolver: this.contentSchemaResolver,
|
|
73
156
|
logger,
|
|
157
|
+
project: { slug: this.projectSlug },
|
|
74
158
|
})
|
|
75
|
-
|
|
76
|
-
|
|
159
|
+
const terminalFailed = failedAfterAttempt + failedUnknownTarget
|
|
160
|
+
this.metrics.succeeded(succeeded)
|
|
161
|
+
this.metrics.deliveryAttemptFailed(retried + failedAfterAttempt)
|
|
162
|
+
this.metrics.failed(terminalFailed)
|
|
163
|
+
succeedTotal += succeeded
|
|
164
|
+
failedTotal += terminalFailed
|
|
77
165
|
|
|
78
166
|
// listener error occurred during batch processing, rethrow
|
|
79
167
|
if (pendingError) {
|
|
80
168
|
throw pendingError
|
|
81
169
|
}
|
|
170
|
+
if (aborted) {
|
|
171
|
+
break
|
|
172
|
+
}
|
|
82
173
|
|
|
83
|
-
// queue is empty, wait
|
|
174
|
+
// queue is empty (or next retry is in the future), wait — but never longer than
|
|
175
|
+
// MAX_IDLE_SLEEP_MS, so a lost notification self-heals and the heartbeat stays fresh.
|
|
84
176
|
if (backoffMs !== 0) {
|
|
177
|
+
const waitMs = Math.min(backoffMs ?? MAX_IDLE_SLEEP_MS, MAX_IDLE_SLEEP_MS)
|
|
85
178
|
await new Promise<void>((resolve, reject) => {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (backoffMs !== undefined) {
|
|
89
|
-
const timeoutHandle = setTimeout(resolve, backoffMs)
|
|
90
|
-
cleanup = () => clearTimeout(timeoutHandle)
|
|
91
|
-
}
|
|
179
|
+
const timeoutHandle = setTimeout(resolve, waitMs)
|
|
180
|
+
const cleanup = () => clearTimeout(timeoutHandle)
|
|
92
181
|
resolvePending = () => {
|
|
93
182
|
resolve()
|
|
94
183
|
cleanup()
|
|
@@ -102,12 +191,16 @@ export class ProjectDispatcher implements Runnable {
|
|
|
102
191
|
}
|
|
103
192
|
onClose?.()
|
|
104
193
|
} catch (e) {
|
|
105
|
-
|
|
194
|
+
this.metrics.crashed()
|
|
106
195
|
onError(e)
|
|
196
|
+
} finally {
|
|
197
|
+
await listener.end()
|
|
107
198
|
}
|
|
108
199
|
})
|
|
109
200
|
} catch (e) {
|
|
110
201
|
reject(e)
|
|
202
|
+
} finally {
|
|
203
|
+
resolveDispatchFinished()
|
|
111
204
|
}
|
|
112
205
|
})
|
|
113
206
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { EgressFetch, EgressGuard, EgressRequestInit } from '@contember/engine-common'
|
|
2
|
+
|
|
1
3
|
export type FetcherResponse = {
|
|
2
4
|
readonly headers: Headers
|
|
3
5
|
readonly ok: boolean
|
|
@@ -7,12 +9,15 @@ export type FetcherResponse = {
|
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
export interface WebhookFetcher {
|
|
10
|
-
fetch(url: string, init:
|
|
12
|
+
fetch(url: string, init: EgressRequestInit): Promise<FetcherResponse>
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
export class WebhookFetcherNative implements WebhookFetcher {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
constructor(private readonly fetchImpl: EgressFetch) {
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async fetch(url: string, init: EgressRequestInit): Promise<FetcherResponse> {
|
|
20
|
+
const response = await this.fetchImpl(url, init)
|
|
16
21
|
return {
|
|
17
22
|
ok: response.ok,
|
|
18
23
|
headers: response.headers,
|
|
@@ -22,3 +27,6 @@ export class WebhookFetcherNative implements WebhookFetcher {
|
|
|
22
27
|
}
|
|
23
28
|
}
|
|
24
29
|
}
|
|
30
|
+
|
|
31
|
+
/** Webhooks go through the egress guard when the operator enabled it, otherwise through the global `fetch`. */
|
|
32
|
+
export const createWebhookFetcher = (egressGuard: EgressGuard | null): WebhookFetcher => new WebhookFetcherNative(egressGuard?.fetch ?? fetch)
|