fix: track applied transactions in session op context (#6029)

Signed-off-by: Alexander Onnikov <Alexander.Onnikov@xored.com>
This commit is contained in:
Alexander Onnikov 2024-07-09 23:23:42 +07:00 committed by GitHub
parent 51475b2979
commit ded0b286ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 7 deletions

View File

@ -719,7 +719,7 @@ export class TServerStorage implements ServerStorage {
const applyTxes: Tx[] = [] const applyTxes: Tx[] = []
const triggerControl: Omit<TriggerControl, 'txFactory' | 'ctx' | 'result' | 'apply'> = { const triggerControl: Omit<TriggerControl, 'txFactory' | 'ctx' | 'txes' | 'apply'> = {
operationContext: ctx, operationContext: ctx,
removedMap, removedMap,
workspace: this.workspaceId, workspace: this.workspaceId,
@ -752,7 +752,10 @@ export class TServerStorage implements ServerStorage {
...triggerControl, ...triggerControl,
ctx: ctx.ctx, ctx: ctx.ctx,
findAll: fAll(ctx.ctx), findAll: fAll(ctx.ctx),
result txes: {
apply: applyTxes,
result
}
}) })
result.push(...transactions) result.push(...transactions)

View File

@ -84,6 +84,7 @@ export class Triggers {
performAsync?: (ctx: SessionOperationContext) => Promise<Tx[]> performAsync?: (ctx: SessionOperationContext) => Promise<Tx[]>
}> { }> {
const result: Tx[] = [] const result: Tx[] = []
const apply: Tx[] = []
const suppressAsync = (ctx as SessionContextImpl).isAsyncContext ?? false const suppressAsync = (ctx as SessionContextImpl).isAsyncContext ?? false
@ -97,7 +98,8 @@ export class Triggers {
ctx: SessionOperationContext, ctx: SessionOperationContext,
matches: Tx[], matches: Tx[],
{ trigger, arrays }: TriggerRecord, { trigger, arrays }: TriggerRecord,
result: Tx[] result: Tx[],
apply: Tx[]
): Promise<void> => { ): Promise<void> => {
const group = groupByArray(matches, (it) => it.modifiedBy) const group = groupByArray(matches, (it) => it.modifiedBy)
@ -108,9 +110,13 @@ export class Triggers {
txFactory: new TxFactory(core.account.System, true), txFactory: new TxFactory(core.account.System, true),
findAll: async (clazz, query, options) => await ctrl.findAllCtx(ctx.ctx, clazz, query, options), findAll: async (clazz, query, options) => await ctrl.findAllCtx(ctx.ctx, clazz, query, options),
apply: async (tx, needResult) => { apply: async (tx, needResult) => {
apply.push(...tx)
return await ctrl.applyCtx(ctx, tx, needResult) return await ctrl.applyCtx(ctx, tx, needResult)
}, },
result txes: {
apply,
result
}
} }
for (const [k, v] of group.entries()) { for (const [k, v] of group.entries()) {
const m = arrays ? [v] : v const m = arrays ? [v] : v
@ -146,8 +152,10 @@ export class Triggers {
{}, {},
async (ctx) => { async (ctx) => {
const tresult: Tx[] = [] const tresult: Tx[] = []
await applyTrigger(ctx, matches, { trigger, arrays }, tresult) const tapply: Tx[] = []
await applyTrigger(ctx, matches, { trigger, arrays }, tresult, tapply)
result.push(...tresult) result.push(...tresult)
apply.push(...tapply)
}, },
{ count: matches.length, arrays } { count: matches.length, arrays }
) )
@ -161,10 +169,11 @@ export class Triggers {
? async (ctx) => { ? async (ctx) => {
// If we have async triggers let's sheculed them after IO phase. // If we have async triggers let's sheculed them after IO phase.
const result: Tx[] = [] const result: Tx[] = []
const apply: Tx[] = []
for (const request of asyncRequest) { for (const request of asyncRequest) {
try { try {
await ctx.with(request.trigger.resource, {}, async (ctx) => { await ctx.with(request.trigger.resource, {}, async (ctx) => {
await applyTrigger(ctx, request.matches, request, result) await applyTrigger(ctx, request.matches, request, result, apply)
}) })
} catch (err: any) { } catch (err: any) {
ctx.ctx.error('failed to process trigger', { ctx.ctx.error('failed to process trigger', {

View File

@ -173,7 +173,10 @@ export interface TriggerControl {
) => Promise<FindResult<T>> ) => Promise<FindResult<T>>
// Current set of transactions to being processed for apply/bulks // Current set of transactions to being processed for apply/bulks
result: Tx[] txes: {
apply: Tx[]
result: Tx[]
}
} }
/** /**