@medusajs/workflow-engine-inmemory 0.0.2-next-20240124093206

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/README.md +1 -0
  2. package/dist/index.d.ts +7 -0
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +43 -0
  5. package/dist/initialize/index.d.ts +6 -0
  6. package/dist/initialize/index.d.ts.map +1 -0
  7. package/dist/initialize/index.js +18 -0
  8. package/dist/joiner-config.d.ts +8 -0
  9. package/dist/joiner-config.d.ts.map +1 -0
  10. package/dist/joiner-config.js +34 -0
  11. package/dist/loaders/connection.d.ts +5 -0
  12. package/dist/loaders/connection.d.ts.map +1 -0
  13. package/dist/loaders/connection.js +41 -0
  14. package/dist/loaders/container.d.ts +3 -0
  15. package/dist/loaders/container.d.ts.map +1 -0
  16. package/dist/loaders/container.js +33 -0
  17. package/dist/loaders/index.d.ts +4 -0
  18. package/dist/loaders/index.d.ts.map +1 -0
  19. package/dist/loaders/index.js +19 -0
  20. package/dist/loaders/utils.d.ts +5 -0
  21. package/dist/loaders/utils.d.ts.map +1 -0
  22. package/dist/loaders/utils.js +9 -0
  23. package/dist/migrations/Migration20231228143900.d.ts +6 -0
  24. package/dist/migrations/Migration20231228143900.d.ts.map +1 -0
  25. package/dist/migrations/Migration20231228143900.js +39 -0
  26. package/dist/models/index.d.ts +2 -0
  27. package/dist/models/index.d.ts.map +1 -0
  28. package/dist/models/index.js +8 -0
  29. package/dist/models/workflow-execution.d.ts +19 -0
  30. package/dist/models/workflow-execution.d.ts.map +1 -0
  31. package/dist/models/workflow-execution.js +97 -0
  32. package/dist/module-definition.d.ts +3 -0
  33. package/dist/module-definition.d.ts.map +1 -0
  34. package/dist/module-definition.js +16 -0
  35. package/dist/repositories/index.d.ts +3 -0
  36. package/dist/repositories/index.d.ts.map +1 -0
  37. package/dist/repositories/index.js +7 -0
  38. package/dist/repositories/workflow-execution.d.ts +35 -0
  39. package/dist/repositories/workflow-execution.d.ts.map +1 -0
  40. package/dist/repositories/workflow-execution.js +9 -0
  41. package/dist/schema/index.d.ts +3 -0
  42. package/dist/schema/index.d.ts.map +1 -0
  43. package/dist/schema/index.js +28 -0
  44. package/dist/services/index.d.ts +4 -0
  45. package/dist/services/index.d.ts.map +1 -0
  46. package/dist/services/index.js +19 -0
  47. package/dist/services/workflow-execution.d.ts +16 -0
  48. package/dist/services/workflow-execution.d.ts.map +1 -0
  49. package/dist/services/workflow-execution.js +13 -0
  50. package/dist/services/workflow-orchestrator.d.ts +66 -0
  51. package/dist/services/workflow-orchestrator.d.ts.map +1 -0
  52. package/dist/services/workflow-orchestrator.js +352 -0
  53. package/dist/services/workflows-module.d.ts +43 -0
  54. package/dist/services/workflows-module.d.ts.map +1 -0
  55. package/dist/services/workflows-module.js +128 -0
  56. package/dist/types/index.d.ts +5 -0
  57. package/dist/types/index.d.ts.map +1 -0
  58. package/dist/types/index.js +2 -0
  59. package/dist/utils/index.d.ts +2 -0
  60. package/dist/utils/index.d.ts.map +1 -0
  61. package/dist/utils/index.js +17 -0
  62. package/dist/utils/workflow-orchestrator-storage.d.ts +25 -0
  63. package/dist/utils/workflow-orchestrator-storage.d.ts.map +1 -0
  64. package/dist/utils/workflow-orchestrator-storage.js +135 -0
  65. package/package.json +59 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAExC,MAAM,MAAM,sCAAsC,GAAG;IACnD,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB,CAAA"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export * from "./workflow-orchestrator-storage";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,iCAAiC,CAAA"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./workflow-orchestrator-storage"), exports);
@@ -0,0 +1,25 @@
1
+ import { DistributedTransaction, DistributedTransactionStorage, TransactionCheckpoint, TransactionStep } from "@medusajs/orchestration";
2
+ import { WorkflowExecutionService } from "../services";
3
+ export declare class InMemoryDistributedTransactionStorage extends DistributedTransactionStorage {
4
+ private workflowExecutionService_;
5
+ private workflowOrchestratorService_;
6
+ private storage;
7
+ private retries;
8
+ private timeouts;
9
+ constructor({ workflowExecutionService, }: {
10
+ workflowExecutionService: WorkflowExecutionService;
11
+ });
12
+ setWorkflowOrchestratorService(workflowOrchestratorService: any): void;
13
+ private saveToDb;
14
+ private deleteFromDb;
15
+ get(key: string): Promise<TransactionCheckpoint | undefined>;
16
+ list(): Promise<TransactionCheckpoint[]>;
17
+ save(key: string, data: TransactionCheckpoint, ttl?: number): Promise<void>;
18
+ scheduleRetry(transaction: DistributedTransaction, step: TransactionStep, timestamp: number, interval: number): Promise<void>;
19
+ clearRetry(transaction: DistributedTransaction, step: TransactionStep): Promise<void>;
20
+ scheduleTransactionTimeout(transaction: DistributedTransaction, timestamp: number, interval: number): Promise<void>;
21
+ clearTransactionTimeout(transaction: DistributedTransaction): Promise<void>;
22
+ scheduleStepTimeout(transaction: DistributedTransaction, step: TransactionStep, timestamp: number, interval: number): Promise<void>;
23
+ clearStepTimeout(transaction: DistributedTransaction, step: TransactionStep): Promise<void>;
24
+ }
25
+ //# sourceMappingURL=workflow-orchestrator-storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workflow-orchestrator-storage.d.ts","sourceRoot":"","sources":["../../src/utils/workflow-orchestrator-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EACtB,6BAA6B,EAC7B,qBAAqB,EACrB,eAAe,EAChB,MAAM,yBAAyB,CAAA;AAEhC,OAAO,EACL,wBAAwB,EAEzB,MAAM,WAAW,CAAA;AAGlB,qBAAa,qCAAsC,SAAQ,6BAA6B;IACtF,OAAO,CAAC,yBAAyB,CAA0B;IAC3D,OAAO,CAAC,4BAA4B,CAA6B;IAEjE,OAAO,CAAC,OAAO,CAAgD;IAC/D,OAAO,CAAC,OAAO,CAAkC;IACjD,OAAO,CAAC,QAAQ,CAAkC;gBAEtC,EACV,wBAAwB,GACzB,EAAE;QACD,wBAAwB,EAAE,wBAAwB,CAAA;KACnD;IAMD,8BAA8B,CAAC,2BAA2B,KAAA;YAI5C,QAAQ;YAeR,YAAY;IASpB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC;IAI5D,IAAI,IAAI,OAAO,CAAC,qBAAqB,EAAE,CAAC;IAIxC,IAAI,CACR,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,qBAAqB,EAC3B,GAAG,CAAC,EAAE,MAAM,GACX,OAAO,CAAC,IAAI,CAAC;IAiCV,aAAa,CACjB,WAAW,EAAE,sBAAsB,EACnC,IAAI,EAAE,eAAe,EACrB,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC;IAcV,UAAU,CACd,WAAW,EAAE,sBAAsB,EACnC,IAAI,EAAE,eAAe,GACpB,OAAO,CAAC,IAAI,CAAC;IAWV,0BAA0B,CAC9B,WAAW,EAAE,sBAAsB,EACnC,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC;IAcV,uBAAuB,CAC3B,WAAW,EAAE,sBAAsB,GAClC,OAAO,CAAC,IAAI,CAAC;IAWV,mBAAmB,CACvB,WAAW,EAAE,sBAAsB,EACnC,IAAI,EAAE,eAAe,EACrB,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC;IAcV,gBAAgB,CACpB,WAAW,EAAE,sBAAsB,EACnC,IAAI,EAAE,eAAe,GACpB,OAAO,CAAC,IAAI,CAAC;CAUjB"}
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InMemoryDistributedTransactionStorage = void 0;
4
+ const orchestration_1 = require("@medusajs/orchestration");
5
+ const utils_1 = require("@medusajs/utils");
6
+ // eslint-disable-next-line max-len
7
+ class InMemoryDistributedTransactionStorage extends orchestration_1.DistributedTransactionStorage {
8
+ constructor({ workflowExecutionService, }) {
9
+ super();
10
+ this.storage = new Map();
11
+ this.retries = new Map();
12
+ this.timeouts = new Map();
13
+ this.workflowExecutionService_ = workflowExecutionService;
14
+ }
15
+ setWorkflowOrchestratorService(workflowOrchestratorService) {
16
+ this.workflowOrchestratorService_ = workflowOrchestratorService;
17
+ }
18
+ async saveToDb(data) {
19
+ await this.workflowExecutionService_.upsert([
20
+ {
21
+ workflow_id: data.flow.modelId,
22
+ transaction_id: data.flow.transactionId,
23
+ execution: data.flow,
24
+ context: {
25
+ data: data.context,
26
+ errors: data.errors,
27
+ },
28
+ state: data.flow.state,
29
+ },
30
+ ]);
31
+ }
32
+ async deleteFromDb(data) {
33
+ await this.workflowExecutionService_.delete([
34
+ {
35
+ workflow_id: data.flow.modelId,
36
+ transaction_id: data.flow.transactionId,
37
+ },
38
+ ]);
39
+ }
40
+ async get(key) {
41
+ return this.storage.get(key);
42
+ }
43
+ async list() {
44
+ return Array.from(this.storage.values());
45
+ }
46
+ async save(key, data, ttl) {
47
+ this.storage.set(key, data);
48
+ let retentionTime;
49
+ /**
50
+ * Store the retention time only if the transaction is done, failed or reverted.
51
+ * From that moment, this tuple can be later on archived or deleted after the retention time.
52
+ */
53
+ const hasFinished = [
54
+ utils_1.TransactionState.DONE,
55
+ utils_1.TransactionState.FAILED,
56
+ utils_1.TransactionState.REVERTED,
57
+ ].includes(data.flow.state);
58
+ if (hasFinished) {
59
+ retentionTime = data.flow.options?.retentionTime;
60
+ Object.assign(data, {
61
+ retention_time: retentionTime,
62
+ });
63
+ }
64
+ if (hasFinished && !retentionTime) {
65
+ await this.deleteFromDb(data);
66
+ }
67
+ else {
68
+ await this.saveToDb(data);
69
+ }
70
+ if (hasFinished) {
71
+ this.storage.delete(key);
72
+ }
73
+ }
74
+ async scheduleRetry(transaction, step, timestamp, interval) {
75
+ const { modelId: workflowId, transactionId } = transaction;
76
+ const inter = setTimeout(async () => {
77
+ await this.workflowOrchestratorService_.run(workflowId, {
78
+ transactionId,
79
+ throwOnError: false,
80
+ });
81
+ }, interval * 1e3);
82
+ const key = `${workflowId}:${transactionId}:${step.id}`;
83
+ this.retries.set(key, inter);
84
+ }
85
+ async clearRetry(transaction, step) {
86
+ const { modelId: workflowId, transactionId } = transaction;
87
+ const key = `${workflowId}:${transactionId}:${step.id}`;
88
+ const inter = this.retries.get(key);
89
+ if (inter) {
90
+ clearTimeout(inter);
91
+ this.retries.delete(key);
92
+ }
93
+ }
94
+ async scheduleTransactionTimeout(transaction, timestamp, interval) {
95
+ const { modelId: workflowId, transactionId } = transaction;
96
+ const inter = setTimeout(async () => {
97
+ await this.workflowOrchestratorService_.run(workflowId, {
98
+ transactionId,
99
+ throwOnError: false,
100
+ });
101
+ }, interval * 1e3);
102
+ const key = `${workflowId}:${transactionId}`;
103
+ this.timeouts.set(key, inter);
104
+ }
105
+ async clearTransactionTimeout(transaction) {
106
+ const { modelId: workflowId, transactionId } = transaction;
107
+ const key = `${workflowId}:${transactionId}`;
108
+ const inter = this.timeouts.get(key);
109
+ if (inter) {
110
+ clearTimeout(inter);
111
+ this.timeouts.delete(key);
112
+ }
113
+ }
114
+ async scheduleStepTimeout(transaction, step, timestamp, interval) {
115
+ const { modelId: workflowId, transactionId } = transaction;
116
+ const inter = setTimeout(async () => {
117
+ await this.workflowOrchestratorService_.run(workflowId, {
118
+ transactionId,
119
+ throwOnError: false,
120
+ });
121
+ }, interval * 1e3);
122
+ const key = `${workflowId}:${transactionId}:${step.id}`;
123
+ this.timeouts.set(key, inter);
124
+ }
125
+ async clearStepTimeout(transaction, step) {
126
+ const { modelId: workflowId, transactionId } = transaction;
127
+ const key = `${workflowId}:${transactionId}:${step.id}`;
128
+ const inter = this.timeouts.get(key);
129
+ if (inter) {
130
+ clearTimeout(inter);
131
+ this.timeouts.delete(key);
132
+ }
133
+ }
134
+ }
135
+ exports.InMemoryDistributedTransactionStorage = InMemoryDistributedTransactionStorage;
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@medusajs/workflow-engine-inmemory",
3
+ "version": "0.0.2-next-20240124093206",
4
+ "description": "Medusa Workflow Orchestrator module",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "engines": {
11
+ "node": ">=16"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/medusajs/medusa",
16
+ "directory": "packages/workflow-engine-inmemory"
17
+ },
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "author": "Medusa",
22
+ "license": "MIT",
23
+ "scripts": {
24
+ "watch": "tsc --build --watch",
25
+ "watch:test": "tsc --build tsconfig.spec.json --watch",
26
+ "prepublishOnly": "cross-env NODE_ENV=production tsc --build && tsc-alias -p tsconfig.json",
27
+ "build": "rimraf dist && tsc --build && tsc-alias -p tsconfig.json",
28
+ "test": "jest --passWithNoTests --runInBand --bail --forceExit -- src/**/__tests__/**/*.ts",
29
+ "test:integration": "jest --runInBand --forceExit -- integration-tests/**/__tests__/**/*.ts",
30
+ "migration:generate": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:generate",
31
+ "migration:initial": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create --initial",
32
+ "migration:create": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create",
33
+ "migration:up": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:up",
34
+ "orm:cache:clear": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm cache:clear"
35
+ },
36
+ "devDependencies": {
37
+ "@mikro-orm/cli": "5.9.7",
38
+ "cross-env": "^5.2.1",
39
+ "jest": "^29.6.3",
40
+ "medusa-test-utils": "^1.1.40",
41
+ "rimraf": "^3.0.2",
42
+ "ts-jest": "^29.1.1",
43
+ "ts-node": "^10.9.1",
44
+ "tsc-alias": "^1.8.6",
45
+ "typescript": "^5.1.6"
46
+ },
47
+ "dependencies": {
48
+ "@medusajs/modules-sdk": "1.12.7-next-20240124093206",
49
+ "@medusajs/types": "1.11.11-next-20240124093206",
50
+ "@medusajs/utils": "1.11.4-next-20240124093206",
51
+ "@medusajs/workflows-sdk": "0.1.2-next-20240124093206",
52
+ "@mikro-orm/core": "5.9.7",
53
+ "@mikro-orm/migrations": "5.9.7",
54
+ "@mikro-orm/postgresql": "5.9.7",
55
+ "awilix": "^8.0.0",
56
+ "dotenv": "^16.1.4",
57
+ "knex": "2.4.2"
58
+ }
59
+ }