1 import { Event, SearchTypes } from "@medusajs/framework/types"
2 import {
3 SearchEventRoutes,
4 SearchIndexes,
5 SearchIngestionRuntime,
6 } from "@types"
7 import {
8 assertTaskAccepted,
9 resolveActiveDefinition,
10 retrieveIndexDefinition,
11 withActiveIndexRetry,
12 } from "./index"
13
14 // Built once so routing a delivered event is a lookup, not a scan.
15 export function buildEventRoutes(indexes: SearchIndexes): SearchEventRoutes {
16 const routes: SearchEventRoutes = new Map()
17
18 for (const definition of indexes.values()) {
19 for (const event of definition.events ?? []) {
20 const names = routes.get(event)
21
22 if (names) {
23 names.push(definition.name)
24 } else {
25 routes.set(event, [definition.name])
26 }
27 }
28 }
29
30 return routes
31 }
32
33 /**
34 * Turns one event into writes. Every write is waited on as far as its provider allows,
35 * so awaiting this is an acknowledgement.
36 */
37 export async function ingestEvent(
38 context: SearchIngestionRuntime,
39 { event, routes }: { event: Event<any>; routes: SearchEventRoutes }
40 ): Promise<SearchTypes.SearchTask[]> {
41 const names = routes.get(event.name)
42
43 // Only reachable by a direct call — the subscriber enrolls in declared events.
44 if (!names?.length) {
45 return []
46 }
47
48 const tasks: SearchTypes.SearchTask[] = []
49
50 for (const name of names) {
51 const definition = retrieveIndexDefinition(context.indexes, name)
52
53 // Resolving a definition rejects `events` without `consume`, so there is one.
54 const mutations = await definition.consume!(event, {
55 container: context.container,
56 index: definition,
57 })
58
59 const written = await withActiveIndexRetry(context, async () => {
60 const active = await resolveActiveDefinition(context, name, {
61 fresh: true,
62 })
63 const provider = context.providers.retrieve(active.provider)
64 const applied: SearchTypes.SearchTask[] = []
65
66 for (const mutation of mutations) {
67 if (mutation.action === "upsert" && !mutation.documents.length) {
68 continue
69 }
70
71 const task =
72 mutation.action === "upsert"
73 ? await provider.upsertDocuments({
74 index: active.physical_name,
75 definition: active,
76 documents: mutation.documents,
77 })
78 : await provider.deleteDocuments({
79 index: active.physical_name,
80 filters: mutation.filters,
81 })
82
83 applied.push(
84 await settleTask(provider, assertTaskAccepted(task, name), name)
85 )
86 }
87
88 return applied
89 })
90
91 tasks.push(...written)
92 }
93
94 return tasks
95 }
96
97 /**
98 * A deferred write is not in the index yet, so waiting is what makes the result an
99 * acknowledgement. Inline providers come back `succeeded` with nothing to wait for.
100 */
101 async function settleTask(
102 provider: SearchTypes.ISearchProvider,
103 task: SearchTypes.SearchTask,
104 index: string
105 ): Promise<SearchTypes.SearchTask> {
106 if (task.status === "succeeded" || !provider.waitForTask) {
107 return task
108 }
109
110 return assertTaskAccepted(await provider.waitForTask(task), index)
111 }