Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next step on long builds #2056

Merged
merged 2 commits into from
Sep 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const serializeError = (error: Error & { code: number }) =>
}
: null

const immediatelyStopError = new Error('ImmediatelyStopError')
const immediatelyStopTimeout = 45 * 1000

const buildInit: (
currentPool: {
ledgerTableNameAsId: string
Expand Down Expand Up @@ -674,12 +677,13 @@ const build: ExternalMethods['build'] = async (
modelInterop,
next,
eventstoreAdapter,
getVacantTimeInMillis,
inputGetVacantTimeInMillis,
buildInfo
) => {
const log = getLog('build')
log.debug(`Start building`)

const getVacantTimeInMillis = () =>
Math.max(inputGetVacantTimeInMillis() - immediatelyStopTimeout, 0)
eventstoreAdapter.establishTimeLimit(getVacantTimeInMillis)
const { eventsWithCursors, ...inputMetricData } = buildInfo
const metricData = {
Expand Down Expand Up @@ -831,17 +835,34 @@ const build: ExternalMethods['build'] = async (
buildMethod = buildEvents
}

await buildMethod(
currentPool,
basePool,
readModelName,
store,
modelInterop,
next,
eventstoreAdapter,
getVacantTimeInMillis,
buildInfo
)
let barrierTimeout: ReturnType<typeof setTimeout> | null = null

try {
await Promise.race([
new Promise((_, reject) => {
barrierTimeout = setTimeout(() => {
reject(immediatelyStopError)
barrierTimeout = null
}, inputGetVacantTimeInMillis())
}),
buildMethod(
currentPool,
basePool,
readModelName,
store,
modelInterop,
next,
eventstoreAdapter,
getVacantTimeInMillis,
buildInfo
),
])
} finally {
if (barrierTimeout != null) {
clearTimeout(barrierTimeout)
barrierTimeout = null
}
}

try {
await inlineLedgerRunQuery(`
Expand All @@ -855,6 +876,14 @@ const build: ExternalMethods['build'] = async (
}
}
} catch (error) {
if (error === immediatelyStopError) {
try {
await basePool.connection.end()
} catch (e) {}
await next()
return
}

if (
error == null ||
!(
Expand Down