|
1
|
import { ILockingModule, Logger } from "@medusajs/types" |
|
2
|
|
|
3
|
export class Orchestrator { |
|
4
|
/** |
|
5
|
* Reference to the locking module |
|
6
|
*/ |
|
7
|
#lockingModule: ILockingModule |
|
8
|
|
|
9
|
/** |
|
10
|
* Reference to the logger |
|
11
|
*/ |
|
12
|
#logger: Logger |
|
13
|
|
|
14
|
/** |
|
15
|
* Owner id when acquiring locks |
|
16
|
*/ |
|
17
|
#lockingOwner = `index-sync-${process.pid}` |
|
18
|
|
|
19
|
/** |
|
20
|
* The current state of the orchestrator |
|
21
|
* |
|
22
|
* - In "idle" state, one can call the "run" method. |
|
23
|
* - In "processing" state, the orchestrator is looping over the entities |
|
24
|
* and processing them. |
|
25
|
* - In "completed" state, the provided entities have been processed. |
|
26
|
* - The "error" state is set when the task runner throws an error. |
|
27
|
*/ |
|
28
|
#state: "idle" | "processing" | "completed" | "error" = "idle" |
|
29
|
|
|
30
|
/** |
|
31
|
* Options for the locking module and the task runner to execute the |
|
32
|
* task. |
|
33
|
* |
|
34
|
* - Lock duration is the maximum duration for which to hold the lock. |
|
35
|
* After this the lock will be removed. |
|
36
|
* |
|
37
|
* The entity is provided to the taskRunner only when the orchestrator |
|
38
|
* is able to acquire a lock. |
|
39
|
*/ |
|
40
|
#options: { |
|
41
|
lockDuration: number |
|
42
|
} |
|
43
|
|
|
44
|
/** |
|
45
|
* Index of the entity that is currently getting processed. |
|
46
|
*/ |
|
47
|
#currentIndex: number = 0 |
|
48
|
|
|
49
|
/** |
|
50
|
* Collection of entities to process in sequence. A lock is obtained |
|
51
|
* while an entity is getting synced to avoid multiple processes |
|
52
|
* from syncing the same entity |
|
53
|
*/ |
|
54
|
#entities: string[] = [] |
|
55
|
|
|
56
|
/** |
|
57
|
* The current state of the orchestrator |
|
58
|
*/ |
|
59
|
get state() { |
|
60
|
return this.#state |
|
61
|
} |
|
62
|
|
|
63
|
/** |
|
64
|
* Reference to the currently processed entity |
|
65
|
*/ |
|
66
|
get current() { |
|
67
|
return this.#entities[this.#currentIndex] |
|
68
|
} |
|
69
|
|
|
70
|
/** |
|
71
|
* Reference to the number of entities left for processing |
|
72
|
*/ |
|
73
|
get remainingCount() { |
|
74
|
return this.#entities.length - (this.#currentIndex + 1) |
|
75
|
} |
|
76
|
|
|
77
|
constructor( |
|
78
|
lockingModule: ILockingModule, |
|
79
|
entities: string[], |
|
80
|
options: { |
|
81
|
lockDuration: number |
|
82
|
logger: Logger |
|
83
|
} |
|
84
|
) { |
|
85
|
this.#lockingModule = lockingModule |
|
86
|
this.#entities = entities |
|
87
|
this.#options = options |
|
88
|
this.#logger = options.logger |
|
89
|
} |
|
90
|
|
|
91
|
/** |
|
92
|
* Acquires using the lock module. |
|
93
|
*/ |
|
94
|
async #acquireLock(forKey: string): Promise<boolean> { |
|
95
|
try { |
|
96
|
await this.#lockingModule.acquire(forKey, { |
|
97
|
expire: this.#options.lockDuration, |
|
98
|
ownerId: this.#lockingOwner, |
|
99
|
}) |
|
100
|
return true |
|
101
|
} catch { |
|
102
|
return false |
|
103
|
} |
|
104
|
} |
|
105
|
|
|
106
|
/** |
|
107
|
* Acquires or renew the lock for a given key. |
|
108
|
*/ |
|
109
|
async renewLock(forKey: string): Promise<boolean> { |
|
110
|
return this.#acquireLock(forKey) |
|
111
|
} |
|
112
|
|
|
113
|
/** |
|
114
|
* Processes the entity. If there are no entities |
|
115
|
* left, the orchestrator state will be set to completed. |
|
116
|
* |
|
117
|
* - Task runner is the implementation function to execute a task. |
|
118
|
* Orchestrator has no inbuilt execution logic and it relies on |
|
119
|
* the task runner for the same. |
|
120
|
*/ |
|
121
|
async #processEntity( |
|
122
|
taskRunner: (entity: string) => Promise<void>, |
|
123
|
entity: string |
|
124
|
) { |
|
125
|
const lockAcquired = await this.#acquireLock(entity) |
|
126
|
if (lockAcquired) { |
|
127
|
try { |
|
128
|
await taskRunner(entity) |
|
129
|
} catch (error) { |
|
130
|
this.#state = "error" |
|
131
|
throw error |
|
132
|
} finally { |
|
133
|
await this.#lockingModule |
|
134
|
.release(entity, { |
|
135
|
ownerId: this.#lockingOwner, |
|
136
|
}) |
|
137
|
.catch(() => { |
|
138
|
this.#logger.error( |
|
139
|
`[Index engine] failed to release lock for entity '${entity}'` |
|
140
|
) |
|
141
|
}) |
|
142
|
} |
|
143
|
} else { |
|
144
|
this.#logger.warn( |
|
145
|
`[Index engine] failed to acquire lock for entity '${entity}' on pid ${process.pid}. It means another process is already processing this entity or a lock is still present in your locking provider.` |
|
146
|
) |
|
147
|
} |
|
148
|
} |
|
149
|
|
|
150
|
/** |
|
151
|
* Run the orchestrator to process the entities one by one. |
|
152
|
* |
|
153
|
* - Task runner is the implementation function to execute a task. |
|
154
|
* Orchestrator has no inbuilt execution logic and it relies on |
|
155
|
* the task runner for the same. |
|
156
|
*/ |
|
157
|
async process(taskRunner: (entity: string) => Promise<void>) { |
|
158
|
if (this.state !== "idle") { |
|
159
|
throw new Error("Cannot re-run an already running orchestrator instance") |
|
160
|
} |
|
161
|
|
|
162
|
this.#state = "processing" |
|
163
|
|
|
164
|
for (let i = 0; i < this.#entities.length; i++) { |
|
165
|
this.#currentIndex = i |
|
166
|
const entity = this.#entities[i] |
|
167
|
if (!entity) { |
|
168
|
this.#state = "completed" |
|
169
|
break |
|
170
|
} |
|
171
|
|
|
172
|
await this.#processEntity(taskRunner, entity) |
|
173
|
} |
|
174
|
|
|
175
|
this.#state = "completed" |
|
176
|
} |
|
177
|
} |