Skip to content

Commit

Permalink
Possible fix saga side effects for #2163 (#2176)
Browse files Browse the repository at this point in the history
  • Loading branch information
IhostVlad authored Dec 13, 2021
1 parent b22a148 commit 3c0b722
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/core/core/src/saga/create-event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const createEventHandler = (
const sideEffectsTimestamp = await runtime.getSideEffectsTimestamp()

const isEnabled = !isNaN(+sideEffectsTimestamp)
? +sideEffectsTimestamp <= +event.timestamp
? +sideEffectsTimestamp < +event.timestamp
: true

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type NotifierRuntime = {

export const LAMBDA_TO_STEP_FUNCTION_COST_EXPENSE_THRESHOLD_MS = 3000

export const waitForSubscriber = async (isSaga = false) =>
await new Promise((resolve) => setTimeout(resolve, isSaga ? 10000 : 1000))
export const waitForSubscriber = async () =>
await new Promise((resolve) => setTimeout(resolve, 1000))

export const checkError = (error: any, value: string) =>
error != null &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { getLog, createRuntime } from '@resolve-js/runtime-base'

import { schedulerFactory } from './scheduler-factory'
import {
eventSubscriberNotifierFactory,
waitForSubscriber,
} from './event-subscriber-notifier-factory'
import { eventSubscriberNotifierFactory } from './event-subscriber-notifier-factory'
import { putDurationMetrics } from './metrics'
import { handleWebsocketEvent } from './websocket-event-handler'
import { handleApiGatewayEvent } from './api-gateway-handler'
Expand Down Expand Up @@ -141,29 +138,14 @@ export const lambdaWorker = async (
const { resolveSource, eventSubscriber, ...buildParameters } = lambdaEvent
void resolveSource

const currentEventSubscriber = coldStartContext.eventListeners.get(
eventSubscriber
) ?? { isSaga: false }

let executorResult = await runtime.eventSubscriber.build({
const executorResult = await runtime.eventSubscriber.build({
...buildParameters,
eventSubscriber,
coldStart,
})

log.verbose(`executorResult: ${JSON.stringify(executorResult)}`)

if (currentEventSubscriber.isSaga) {
await waitForSubscriber(true)
executorResult = await runtime.eventSubscriber.build({
...buildParameters,
eventSubscriber,
coldStart,
})

log.verbose(`sagaExecutorResult: ${JSON.stringify(executorResult)}`)
}

return executorResult
} else if (
Array.isArray(lambdaEvent.Records) &&
Expand Down
4 changes: 2 additions & 2 deletions packages/tools/testing-tools/src/runtime/get-event-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export const getEventStore = async (
const event = events[timestampIndex]

event.timestamp =
eventByAggregateIdVersion.get(
(eventByAggregateIdVersion.get(
`${event.aggregateId}:${event.aggregateVersion}`
)?.timestamp ?? timestampIndex
)?.timestamp ?? timestampIndex) + 1
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
exports[`givenEvents tests should match snapshot 1`] = `
Array [
Object {
"timestamp": 0,
"timestamp": 1,
"value": "test-1",
},
Object {
"timestamp": 1,
"timestamp": 2,
"value": "test-2",
},
Object {
"timestamp": 2,
"timestamp": 3,
"value": "test-3",
},
]
Expand All @@ -20,15 +20,15 @@ Array [
exports[`givenEvents tests should match snapshot with specified timestamp 1`] = `
Array [
Object {
"timestamp": 10,
"timestamp": 11,
"value": "test-1",
},
Object {
"timestamp": 20,
"timestamp": 21,
"value": "test-2",
},
Object {
"timestamp": 30,
"timestamp": 31,
"value": "test-3",
},
]
Expand Down

0 comments on commit 3c0b722

Please sign in to comment.