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

Trace timeline errors #351

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion packages/timeline-state-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
"p-timeout": "^3.2.0",
"simple-oauth2": "^5.0.0",
"sprintf-js": "^1.1.3",
"superfly-timeline": "^9.0.1",
"superfly-timeline": "9.0.2",
"threadedclass": "^1.2.1",
"timeline-state-resolver-types": "9.2.0-alpha.0",
"tslib": "^2.6.2",
Expand Down
71 changes: 57 additions & 14 deletions packages/timeline-state-resolver/src/AsyncResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,75 @@ import {
ResolvedTimelineObject,
ResolverCache,
resolveTimeline,
ResolveError,
ResolveOptions,
} from 'superfly-timeline'
import { TimelineTriggerTimeResult } from './conductor'
import { TSRTimeline, TSRTimelineContent, TSRTimelineObj } from 'timeline-state-resolver-types'
import { EventEmitter } from 'eventemitter3'

export class AsyncResolver {
export type AsyncResolverEvents = {
error: [string]
}

// This is a debug flag, to trace some helpful information when resolving fails.
// If should be set to false in production.
const TRACE_RESOLVING = true

export class AsyncResolver extends EventEmitter<AsyncResolverEvents> {
private readonly onSetTimelineTriggerTime: (res: TimelineTriggerTimeResult) => void

private cache: Partial<ResolverCache> = {}

public constructor(onSetTimelineTriggerTime: (res: TimelineTriggerTimeResult) => void) {
super()
this.onSetTimelineTriggerTime = onSetTimelineTriggerTime
}

public resolveTimeline(resolveTime: number, timeline: TSRTimeline, limitTime: number, useCache: boolean) {
const objectsFixed = this._fixNowObjects(timeline, resolveTime)

const resolvedTimeline = resolveTimeline(timeline, {
limitCount: 999,
limitTime: limitTime,
time: resolveTime,
cache: useCache ? this.cache : undefined,
})

return {
resolvedTimeline,
objectsFixed,
try {
const objectsFixed = this._fixNowObjects(timeline, resolveTime)

const resolvedTimeline = this._resolveTimeline(timeline, {
limitCount: 999,
limitTime: limitTime,
time: resolveTime,
cache: useCache ? this.cache : undefined,
traceResolving: TRACE_RESOLVING,
})

return {
resolvedTimeline,
objectsFixed,
}
} catch (e) {
if (e instanceof ResolveError) {
// Trace some helpful information related to the error:
this.emit('error', 'Error resolveTrace: ' + JSON.stringify(e.resolvedTimeline.statistics.resolveTrace))
}
throw e
}
}

private _resolveTimeline(timeline: TSRTimeline, options: ResolveOptions) {
try {
return resolveTimeline(timeline, options)
} catch (e) {
if (!options.traceResolving) {
// Try again, but now with tracing:
options.traceResolving = true

// Note, we expect this to throw again:
const try2 = resolveTimeline(timeline, options)

// Oh, this is weird, it worked on second try! Log the error:
this.emit(
'error',
'Error when resolving timeline, but on second try with tracing, it worked! Original error:' + e
)
return try2
}
throw e
}
}

Expand Down Expand Up @@ -104,7 +147,7 @@ export class AsyncResolver {
wouldLikeToIterateAgain = false
dontIterateAgain = true

resolvedTimeline = resolveTimeline(Array.from(timeLineMap.values()), {
resolvedTimeline = this._resolveTimeline(Array.from(timeLineMap.values()), {
time: now,
})

Expand Down
20 changes: 18 additions & 2 deletions packages/timeline-state-resolver/src/conductor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import * as _ from 'underscore'
import { getResolvedState, ResolvedTimeline, ResolvedTimelineObjectInstance, TimelineObject } from 'superfly-timeline'
import {
getResolvedState,
ResolvedTimeline,
ResolvedTimelineObjectInstance,
TimelineObject,
TimelineState,
} from 'superfly-timeline'
import { EventEmitter } from 'eventemitter3'
import { MemUsageReport, threadedClass, ThreadedClass, ThreadedClassConfig, ThreadedClassManager } from 'threadedclass'
import PQueue from 'p-queue'
Expand Down Expand Up @@ -260,6 +266,9 @@ export class Conductor extends EventEmitter<ConductorEvents> {
instanceName: 'resolver',
}
)
await this._resolver.on('error', (e) => {
this.emit('error', 'AsyncResolver error: ' + e)
})

ThreadedClassManager.onEvent(this._resolver, 'thread_closed', () => {
// This is called if a child crashes - we are using autoRestart, so we just log
Expand Down Expand Up @@ -927,7 +936,14 @@ export class Conductor extends EventEmitter<ConductorEvents> {
_.each(timeline, (o) => applyRecursively(o, fixNow))
}

const tlState = getResolvedState(resolvedTimeline, resolveTime, 1)
let tlState: TimelineState<TSRTimelineContent>
try {
tlState = getResolvedState(resolvedTimeline, resolveTime, 1)
} catch (e) {
// Trace some helpful information related to the error:
this.emit('error', 'Error resolveTrace: ' + JSON.stringify(resolvedTimeline.statistics.resolveTrace))
throw e
}
await pPrepareForHandleStates

statTimeTimelineResolved = Date.now()
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11110,12 +11110,12 @@ asn1@evs-broadcast/node-asn1:
languageName: node
linkType: hard

"superfly-timeline@npm:^9.0.1":
version: 9.0.1
resolution: "superfly-timeline@npm:9.0.1"
"superfly-timeline@npm:9.0.2":
version: 9.0.2
resolution: "superfly-timeline@npm:9.0.2"
dependencies:
tslib: ^2.6.0
checksum: 4267eed691fe9ce9f89bf17c8aed1a98206938dd6d850c64b083e4fd3a3dc5329801c76c757450c9520375bad100ce512cc6d6a3e4a997bdfa14a4e7d65f09f2
checksum: d628d467d5384f5667bc10b877478c5b8b0a91774b5d5c5e9d9d3134b8f1b760225f2fbbb0f9ccd3e55f930c9f3719f81b9347b94ea853fbc0a18bc121d97665
languageName: node
linkType: hard

Expand Down Expand Up @@ -11415,7 +11415,7 @@ asn1@evs-broadcast/node-asn1:
p-timeout: ^3.2.0
simple-oauth2: ^5.0.0
sprintf-js: ^1.1.3
superfly-timeline: ^9.0.1
superfly-timeline: 9.0.2
threadedclass: ^1.2.1
timeline-state-resolver-types: 9.2.0-alpha.0
tslib: ^2.6.2
Expand Down
Loading