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

UBERF-6888: Async triggers #5565

Merged
merged 1 commit into from
May 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
3 changes: 2 additions & 1 deletion models/server-activity/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function createModel (builder: Builder): void {
})

builder.createDoc(serverCore.class.Trigger, core.space.Model, {
trigger: serverActivity.trigger.ReferenceTrigger
trigger: serverActivity.trigger.ReferenceTrigger,
isAsync: true
})
}
3 changes: 2 additions & 1 deletion models/server-chunter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export function createModel (builder: Builder): void {
_class: core.class.TxCollectionCUD,
'tx._class': core.class.TxCreateDoc,
'tx.objectClass': chunter.class.ChatMessage
}
},
isAsync: true
})
}
72 changes: 34 additions & 38 deletions server-plugins/activity-resources/src/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,27 +597,25 @@ async function ActivityReferenceCreate (tx: TxCUD<Doc>, control: TriggerControl)
if (control.hierarchy.isDerived(ctx.objectClass, notification.class.InboxNotification)) return []
if (control.hierarchy.isDerived(ctx.objectClass, activity.class.ActivityReference)) return []

control.storageFx(async (adapter) => {
const txFactory = new TxFactory(control.txFactory.account)
const txFactory = new TxFactory(control.txFactory.account)

const doc = TxProcessor.createDoc2Doc(ctx)
const targetTx = guessReferenceTx(control.hierarchy, tx)

const txes: Tx[] = await getCreateReferencesTxes(
control,
adapter,
txFactory,
doc,
targetTx.objectId,
targetTx.objectClass,
targetTx.objectSpace,
tx
)
const doc = TxProcessor.createDoc2Doc(ctx)
const targetTx = guessReferenceTx(control.hierarchy, tx)

if (txes.length !== 0) {
await control.apply(txes, true)
}
})
const txes: Tx[] = await getCreateReferencesTxes(
control,
control.storageAdapter,
txFactory,
doc,
targetTx.objectId,
targetTx.objectClass,
targetTx.objectSpace,
tx
)

if (txes.length !== 0) {
await control.apply(txes, true)
}

return []
}
Expand Down Expand Up @@ -647,26 +645,24 @@ async function ActivityReferenceUpdate (tx: TxCUD<Doc>, control: TriggerControl)
return []
}

control.storageFx(async (adapter) => {
const txFactory = new TxFactory(control.txFactory.account)
const doc = TxProcessor.updateDoc2Doc(rawDoc, ctx)
const targetTx = guessReferenceTx(control.hierarchy, tx)

const txes: Tx[] = await getUpdateReferencesTxes(
control,
adapter,
txFactory,
doc,
targetTx.objectId,
targetTx.objectClass,
targetTx.objectSpace,
tx
)
const txFactory = new TxFactory(control.txFactory.account)
const doc = TxProcessor.updateDoc2Doc(rawDoc, ctx)
const targetTx = guessReferenceTx(control.hierarchy, tx)

if (txes.length !== 0) {
await control.apply(txes, true)
}
})
const txes: Tx[] = await getUpdateReferencesTxes(
control,
control.storageAdapter,
txFactory,
doc,
targetTx.objectId,
targetTx.objectClass,
targetTx.objectSpace,
tx
)

if (txes.length !== 0) {
await control.apply(txes, true)
}

return []
}
Expand Down
11 changes: 3 additions & 8 deletions server-plugins/attachment-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//

import type { Attachment } from '@hcengineering/attachment'
import type { Doc, Ref, Tx, TxRemoveDoc } from '@hcengineering/core'
import type { Tx, TxRemoveDoc } from '@hcengineering/core'
import { TxProcessor } from '@hcengineering/core'
import type { TriggerControl } from '@hcengineering/server-core'

Expand All @@ -24,7 +24,7 @@ import type { TriggerControl } from '@hcengineering/server-core'
*/
export async function OnAttachmentDelete (
tx: Tx,
{ findAll, hierarchy, fulltextFx, storageFx, removedMap, ctx }: TriggerControl
{ removedMap, ctx, storageAdapter, workspace }: TriggerControl
): Promise<Tx[]> {
const rmTx = TxProcessor.extractTx(tx) as TxRemoveDoc<Attachment>

Expand All @@ -34,13 +34,8 @@ export async function OnAttachmentDelete (
if (attach === undefined) {
return []
}
fulltextFx(async (adapter) => {
await adapter.remove([attach.file as Ref<Doc>])
})

storageFx(async (adapter, bucket) => {
await adapter.remove(ctx, bucket, [attach.file])
})
await storageAdapter.remove(ctx, workspace, [attach.file])

return []
}
Expand Down
15 changes: 8 additions & 7 deletions server-plugins/collaboration-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import type { TriggerControl } from '@hcengineering/server-core'
/**
* @public
*/
export async function OnDelete (tx: Tx, { hierarchy, storageFx, removedMap, ctx }: TriggerControl): Promise<Tx[]> {
export async function OnDelete (
tx: Tx,
{ hierarchy, storageAdapter, workspace, removedMap, ctx }: TriggerControl
): Promise<Tx[]> {
const rmTx = TxProcessor.extractTx(tx) as TxRemoveDoc<Doc>

if (rmTx._class !== core.class.TxRemoveDoc) {
Expand All @@ -44,12 +47,10 @@ export async function OnDelete (tx: Tx, { hierarchy, storageFx, removedMap, ctx
}
}

storageFx(async (adapter, bucket) => {
// TODO This is not accurate way to delete collaborative document
// Even though we are deleting it here, the document can be currently in use by someone else
// and when editing session ends, the collborator service will recreate the document again
await removeCollaborativeDoc(adapter, bucket, toDelete, ctx)
})
// TODO This is not accurate way to delete collaborative document
// Even though we are deleting it here, the document can be currently in use by someone else
// and when editing session ends, the collborator service will recreate the document again
await removeCollaborativeDoc(storageAdapter, workspace, toDelete, ctx)

return []
}
Expand Down
24 changes: 11 additions & 13 deletions server-plugins/contact-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export async function OnPersonAccountCreate (tx: Tx, control: TriggerControl): P
*/
export async function OnContactDelete (
tx: Tx,
{ findAll, hierarchy, storageFx, removedMap, txFactory, ctx }: TriggerControl
{ findAll, hierarchy, storageAdapter, workspace, removedMap, txFactory, ctx }: TriggerControl
): Promise<Tx[]> {
const rmTx = tx as TxRemoveDoc<Contact>

Expand All @@ -112,20 +112,18 @@ export async function OnContactDelete (
return []
}

storageFx(async (adapter, bucket) => {
await adapter.remove(ctx, bucket, [avatar])
await storageAdapter.remove(ctx, workspace, [avatar])

if (avatar != null) {
const extra = await adapter.list(ctx, bucket, avatar)
if (extra.length > 0) {
await adapter.remove(
ctx,
bucket,
Array.from(extra.entries()).map((it) => it[1]._id)
)
}
if (avatar != null) {
const extra = await storageAdapter.list(ctx, workspace, avatar)
if (extra.length > 0) {
await storageAdapter.remove(
ctx,
workspace,
Array.from(extra.entries()).map((it) => it[1]._id)
)
}
})
}

const result: Tx[] = []

Expand Down
2 changes: 1 addition & 1 deletion server/core/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ export interface DbConfiguration {
contentAdapters: Record<string, ContentTextAdapterConfiguration>
serviceAdapters: Record<string, ServiceAdapterConfig>
defaultContentAdapter: string
storageFactory?: () => StorageAdapter
storageFactory: () => StorageAdapter
}
2 changes: 1 addition & 1 deletion server/core/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function createServerStorage (
const adapters = new Map<string, DbAdapter>()
const modelDb = new ModelDb(hierarchy)

const storageAdapter = conf.storageFactory?.()
const storageAdapter = conf.storageFactory()

await ctx.with('create-adapters', {}, async (ctx) => {
for (const key in conf.adapters) {
Expand Down
Loading