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

View File

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

View File

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