@medusajs/workflow-engine-inmemory 0.0.5-snapshot-20240607093839 → 0.0.5-snapshot-20240701122250

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { moduleDefinition } from "./module-definition";
2
2
  export default moduleDefinition;
3
3
  export * from "./loaders";
4
+ export * from "./models";
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AAEtD,eAAe,gBAAgB,CAAA;AAE/B,cAAc,WAAW,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAA;AAEtD,eAAe,gBAAgB,CAAA;AAE/B,cAAc,WAAW,CAAA;AACzB,cAAc,UAAU,CAAA"}
package/dist/index.js CHANGED
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  const module_definition_1 = require("./module-definition");
18
18
  exports.default = module_definition_1.moduleDefinition;
19
19
  __exportStar(require("./loaders"), exports);
20
+ __exportStar(require("./models"), exports);
@@ -1,8 +1,4 @@
1
- import { ModuleJoinerConfig } from "@medusajs/types";
2
1
  import { MapToConfig } from "@medusajs/utils";
3
- export declare const LinkableKeys: {
4
- workflow_execution_id: string;
5
- };
2
+ export declare const joinerConfig: Omit<import("@medusajs/types").ModuleJoinerConfig, "serviceName" | "primaryKeys" | "alias" | "linkableKeys"> & Required<Pick<import("@medusajs/types").ModuleJoinerConfig, "serviceName" | "primaryKeys" | "alias" | "linkableKeys">>;
6
3
  export declare const entityNameToLinkableKeysMap: MapToConfig;
7
- export declare const joinerConfig: ModuleJoinerConfig;
8
4
  //# sourceMappingURL=joiner-config.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"joiner-config.d.ts","sourceRoot":"","sources":["../src/joiner-config.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAA;AAI7C,eAAO,MAAM,YAAY;;CAExB,CAAA;AAWD,eAAO,MAAM,2BAA2B,EAAE,WAAmC,CAAA;AAE7E,eAAO,MAAM,YAAY,EAAE,kBAY1B,CAAA"}
1
+ {"version":3,"file":"joiner-config.d.ts","sourceRoot":"","sources":["../src/joiner-config.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,WAAW,EAEZ,MAAM,iBAAiB,CAAA;AAExB,eAAO,MAAM,YAAY,uOAA8C,CAAA;AAEvE,eAAO,MAAM,2BAA2B,EAAE,WACqB,CAAA"}
@@ -1,34 +1,6 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.joinerConfig = exports.entityNameToLinkableKeysMap = exports.LinkableKeys = void 0;
7
- const modules_sdk_1 = require("@medusajs/modules-sdk");
8
- const _models_1 = require("./models");
9
- const schema_1 = __importDefault(require("./schema"));
10
- exports.LinkableKeys = {
11
- workflow_execution_id: _models_1.WorkflowExecution.name,
12
- };
13
- const entityLinkableKeysMap = {};
14
- Object.entries(exports.LinkableKeys).forEach(([key, value]) => {
15
- entityLinkableKeysMap[value] ??= [];
16
- entityLinkableKeysMap[value].push({
17
- mapTo: key,
18
- valueFrom: key.split("_").pop(),
19
- });
20
- });
21
- exports.entityNameToLinkableKeysMap = entityLinkableKeysMap;
22
- exports.joinerConfig = {
23
- serviceName: modules_sdk_1.Modules.WORKFLOW_ENGINE,
24
- primaryKeys: ["id"],
25
- schema: schema_1.default,
26
- linkableKeys: exports.LinkableKeys,
27
- alias: {
28
- name: ["workflow_execution", "workflow_executions"],
29
- args: {
30
- entity: _models_1.WorkflowExecution.name,
31
- methodSuffix: "WorkflowExecution",
32
- },
33
- },
34
- };
3
+ exports.entityNameToLinkableKeysMap = exports.joinerConfig = void 0;
4
+ const utils_1 = require("@medusajs/utils");
5
+ exports.joinerConfig = (0, utils_1.defineJoinerConfig)(utils_1.Modules.WORKFLOW_ENGINE);
6
+ exports.entityNameToLinkableKeysMap = (0, utils_1.buildEntitiesNameToLinkableKeysMap)(exports.joinerConfig.linkableKeys);
@@ -1 +1 @@
1
- {"version":3,"file":"workflow-orchestrator.d.ts","sourceRoot":"","sources":["../../src/services/workflow-orchestrator.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EACtB,4BAA4B,EAE5B,eAAe,EAChB,MAAM,yBAAyB,CAAA;AAChC,OAAO,EAAE,aAAa,EAAE,OAAO,EAAmB,MAAM,iBAAiB,CAAA;AAEzE,OAAO,EAEL,cAAc,EAEd,KAAK,cAAc,EACpB,MAAM,yBAAyB,CAAA;AAEhC,OAAO,EAAE,qCAAqC,EAAE,MAAM,UAAU,CAAA;AAEhE,MAAM,MAAM,8BAA8B,CAAC,CAAC,IAAI,IAAI,CAClD,cAAc,CAAC,CAAC,CAAC,EACjB,WAAW,CACZ,GAAG;IACF,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,SAAS,CAAC,EAAE,aAAa,CAAA;CAC1B,CAAA;AAED,KAAK,0BAA0B,CAAC,CAAC,IAAI,IAAI,CACvC,8BAA8B,CAAC,CAAC,CAAC,EACjC,eAAe,GAAG,OAAO,CAC1B,CAAA;AAED,KAAK,mBAAmB,GAAG;IACzB,UAAU,EAAE,MAAM,CAAA;IAClB,aAAa,EAAE,MAAM,CAAA;IACrB,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,QAAQ,GAAG,YAAY,CAAA;CAChC,CAAA;AAED,KAAK,aAAa,GAAG;IACnB,SAAS,EAAE,MAAM,4BAA4B,CAAA;IAC7C,UAAU,EAAE,MAAM,CAAA;IAClB,aAAa,CAAC,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  this.subscribers = new Map();
24
24
  inMemoryDistributedTransactionStorage.setWorkflowOrchestratorService(this);
25
25
  orchestration_1.DistributedTransaction.setStorage(inMemoryDistributedTransactionStorage);
26
+ orchestration_1.WorkflowScheduler.setStorage(inMemoryDistributedTransactionStorage);
26
27
  }
27
28
  async run(workflowIdOrWorkflow, options, sharedContext = {}) {
28
29
  let { input, context, transactionId, resultFrom, throwOnError, events: eventHandlers, container, } = options ?? {};
@@ -30,7 +31,7 @@ class WorkflowOrchestratorService {
30
31
  ? workflowIdOrWorkflow
31
32
  : workflowIdOrWorkflow.getName();
32
33
  if (!workflowId) {
33
- throw new Error("Workflow ID is required");
34
+ throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `Workflow ID is required`);
34
35
  }
35
36
  context ??= {};
36
37
  context.transactionId ??= transactionId ?? (0, ulid_1.ulid)();
@@ -41,7 +42,7 @@ class WorkflowOrchestratorService {
41
42
  });
42
43
  const exportedWorkflow = workflows_sdk_1.MedusaWorkflow.getWorkflow(workflowId);
43
44
  if (!exportedWorkflow) {
44
- throw new Error(`Workflow with id "${workflowId}" not found.`);
45
+ throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `Workflow with id "${workflowId}" not found.`);
45
46
  }
46
47
  const flow = exportedWorkflow(container);
47
48
  const ret = await flow.run({
@@ -1,24 +1,25 @@
1
- import { Context, DAL, FindConfig, IWorkflowEngineService, InternalModuleDeclaration, ModuleJoinerConfig, ModulesSdkTypes, WorkflowsSdkTypes } from "@medusajs/types";
1
+ import { Context, DAL, InternalModuleDeclaration, ModuleJoinerConfig, ModulesSdkTypes, WorkflowsSdkTypes } from "@medusajs/types";
2
+ import { ModulesSdkUtils } from "@medusajs/utils";
2
3
  import type { ReturnWorkflow, UnwrapWorkflowInputDataType } from "@medusajs/workflows-sdk";
4
+ import { WorkflowExecution } from "../models";
3
5
  import { WorkflowOrchestratorService } from ".";
4
6
  type InjectedDependencies = {
5
7
  baseRepository: DAL.RepositoryService;
6
- workflowExecutionService: ModulesSdkTypes.InternalModuleService<any>;
8
+ workflowExecutionService: ModulesSdkTypes.IMedusaInternalService<any>;
7
9
  workflowOrchestratorService: WorkflowOrchestratorService;
8
10
  };
9
- export declare class WorkflowsModuleService implements IWorkflowEngineService {
11
+ declare const WorkflowsModuleService_base: new (...args: any[]) => ModulesSdkUtils.AbstractModuleService<{
12
+ WorkflowExecution: {
13
+ dto: WorkflowExecution;
14
+ };
15
+ }>;
16
+ export declare class WorkflowsModuleService<TWorkflowExecution extends WorkflowExecution = WorkflowExecution> extends WorkflowsModuleService_base {
10
17
  protected readonly moduleDeclaration: InternalModuleDeclaration;
11
18
  protected baseRepository_: DAL.RepositoryService;
12
- protected workflowExecutionService_: ModulesSdkTypes.InternalModuleService<any>;
19
+ protected workflowExecutionService_: ModulesSdkTypes.IMedusaInternalService<TWorkflowExecution>;
13
20
  protected workflowOrchestratorService_: WorkflowOrchestratorService;
14
21
  constructor({ baseRepository, workflowExecutionService, workflowOrchestratorService, }: InjectedDependencies, moduleDeclaration: InternalModuleDeclaration);
15
22
  __joinerConfig(): ModuleJoinerConfig;
16
- retrieveWorkflowExecution(idOrObject: string | {
17
- workflow_id: string;
18
- transaction_id: string;
19
- }, config?: FindConfig<WorkflowsSdkTypes.WorkflowExecutionDTO>, sharedContext?: Context): Promise<WorkflowsSdkTypes.WorkflowExecutionDTO>;
20
- listWorkflowExecution(filters?: WorkflowsSdkTypes.FilterableWorkflowExecutionProps, config?: FindConfig<WorkflowsSdkTypes.WorkflowExecutionDTO>, sharedContext?: Context): Promise<WorkflowsSdkTypes.WorkflowExecutionDTO[]>;
21
- listAndCountWorkflowExecution(filters?: WorkflowsSdkTypes.FilterableWorkflowExecutionProps, config?: FindConfig<WorkflowsSdkTypes.WorkflowExecutionDTO>, sharedContext?: Context): Promise<[WorkflowsSdkTypes.WorkflowExecutionDTO[], number]>;
22
23
  run<TWorkflow extends string | ReturnWorkflow<any, any, any>>(workflowIdOrWorkflow: TWorkflow, options?: WorkflowsSdkTypes.WorkflowOrchestratorRunDTO<TWorkflow extends ReturnWorkflow<any, any, any> ? UnwrapWorkflowInputDataType<TWorkflow> : unknown>, context?: Context): Promise<any>;
23
24
  getRunningTransaction(workflowId: string, transactionId: string, context?: Context): Promise<import("@medusajs/orchestration").DistributedTransaction>;
24
25
  setStepSuccess({ idempotencyKey, stepResponse, options, }: {
@@ -1 +1 @@
1
- {"version":3,"file":"workflows-module.d.ts","sourceRoot":"","sources":["../../src/services/workflows-module.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,GAAG,EACH,UAAU,EACV,sBAAsB,EACtB,yBAAyB,EACzB,kBAAkB,EAClB,eAAe,EACf,iBAAiB,EAClB,MAAM,iBAAiB,CAAA;AAQxB,OAAO,KAAK,EACV,cAAc,EACd,2BAA2B,EAC5B,MAAM,yBAAyB,CAAA;AAChC,OAAO,EAAE,2BAA2B,EAAE,MAAM,WAAW,CAAA;AAGvD,KAAK,oBAAoB,GAAG;IAC1B,cAAc,EAAE,GAAG,CAAC,iBAAiB,CAAA;IACrC,wBAAwB,EAAE,eAAe,CAAC,qBAAqB,CAAC,GAAG,CAAC,CAAA;IACpE,2BAA2B,EAAE,2BAA2B,CAAA;CACzD,CAAA;AAED,qBAAa,sBAAuB,YAAW,sBAAsB;IAWjE,SAAS,CAAC,QAAQ,CAAC,iBAAiB,EAAE,yBAAyB;IAVjE,SAAS,CAAC,eAAe,EAAE,GAAG,CAAC,iBAAiB,CAAA;IAChD,SAAS,CAAC,yBAAyB,EAAE,eAAe,CAAC,qBAAqB,CAAC,GAAG,CAAC,CAAA;IAC/E,SAAS,CAAC,4BAA4B,EAAE,2BAA2B,CAAA;gBAGjE,EACE,cAAc,EACd,wBAAwB,EACxB,2BAA2B,GAC5B,EAAE,oBAAoB,EACJ,iBAAiB,EAAE,yBAAyB;IAOjE,cAAc,IAAI,kBAAkB;IAK9B,yBAAyB,CAC7B,UAAU,EACN,MAAM,GACN;QACE,WAAW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AAc,CAClB,EACE,cAAc,EACd,YAAY,EACZ,OAAO,GACR,EAAE;QACD,cAAc,EAAE,MAAM,GAAG,MAAM,CAAA;QAC/B,YAAY,EAAE,OAAO,CAAA;QACrB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;KAC9B,EACgB,OAAO,GAAE,OAAY;IAalC,SAAS,CACb,IAAI,EAAE;QACJ,UAAU,EAAE,MAAM,CAAA;QAClB,aAAa,CAAC,EAAE,MAAM,CAAA;QACtB,UAAU,EAAE,QAAQ,CAAA;QACpB,YAAY,CAAC,EAAE,MAAM,CAAA;KACtB,EACgB,OAAO,GAAE,OAAY;IAMlC,WAAW,CACf,IAAI,EAAE;QACJ,UAAU,EAAE,MAAM,CAAA;QAClB,aAAa,CAAC,EAAE,MAAM,CAAA;QACtB,cAAc,EAAE,MAAM,GAAG,QAAQ,CAAA;KAClC,EACgB,OAAO,GAAE,OAAY;CAIzC"}
1
+ {"version":3,"file":"workflows-module.d.ts","sourceRoot":"","sources":["../../src/services/workflows-module.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,GAAG,EACH,yBAAyB,EACzB,kBAAkB,EAClB,eAAe,EACf,iBAAiB,EAClB,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAGL,eAAe,EAChB,MAAM,iBAAiB,CAAA;AACxB,OAAO,KAAK,EACV,cAAc,EACd,2BAA2B,EAC5B,MAAM,yBAAyB,CAAA;AAChC,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAC3C,OAAO,EAAE,2BAA2B,EAAE,MAAM,WAAW,CAAA;AAGvD,KAAK,oBAAoB,GAAG;IAC1B,cAAc,EAAE,GAAG,CAAC,iBAAiB,CAAA;IACrC,wBAAwB,EAAE,eAAe,CAAC,sBAAsB,CAAC,GAAG,CAAC,CAAA;IACrE,2BAA2B,EAAE,2BAA2B,CAAA;CACzD,CAAA;;uBAKoB;QAAE,GAAG,EAAE,iBAAiB,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O,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;KAC9B,EACgB,OAAO,GAAE,OAAY;IAalC,SAAS,CACb,IAAI,EAAE;QACJ,UAAU,EAAE,MAAM,CAAA;QAClB,aAAa,CAAC,EAAE,MAAM,CAAA;QACtB,UAAU,EAAE,QAAQ,CAAA;QACpB,YAAY,CAAC,EAAE,MAAM,CAAA;KACtB,EACgB,OAAO,GAAE,OAAY;IAMlC,WAAW,CACf,IAAI,EAAE;QACJ,UAAU,EAAE,MAAM,CAAA;QAClB,aAAa,CAAC,EAAE,MAAM,CAAA;QACtB,cAAc,EAAE,MAAM,GAAG,QAAQ,CAAA;KAClC,EACgB,OAAO,GAAE,OAAY;CAIzC"}
@@ -16,9 +16,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.WorkflowsModuleService = void 0;
17
17
  const types_1 = require("@medusajs/types");
18
18
  const utils_1 = require("@medusajs/utils");
19
+ const _models_1 = require("../models");
19
20
  const joiner_config_1 = require("../joiner-config");
20
- class WorkflowsModuleService {
21
+ class WorkflowsModuleService extends utils_1.ModulesSdkUtils.MedusaService({ WorkflowExecution: _models_1.WorkflowExecution }, joiner_config_1.entityNameToLinkableKeysMap) {
21
22
  constructor({ baseRepository, workflowExecutionService, workflowOrchestratorService, }, moduleDeclaration) {
23
+ // @ts-ignore
24
+ super(...arguments);
22
25
  this.moduleDeclaration = moduleDeclaration;
23
26
  this.baseRepository_ = baseRepository;
24
27
  this.workflowExecutionService_ = workflowExecutionService;
@@ -27,65 +30,6 @@ class WorkflowsModuleService {
27
30
  __joinerConfig() {
28
31
  return joiner_config_1.joinerConfig;
29
32
  }
30
- async retrieveWorkflowExecution(idOrObject, config = {}, sharedContext = {}) {
31
- const objValue = (0, utils_1.isString)(idOrObject)
32
- ? { id: idOrObject }
33
- : {
34
- workflow_id: idOrObject.workflow_id,
35
- transaction_id: idOrObject.transaction_id,
36
- };
37
- const wfExecution = await this.workflowExecutionService_.list(objValue, config, sharedContext);
38
- if (wfExecution.length === 0) {
39
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_FOUND, `WorkflowExecution with ${Object.keys(objValue).join(", ")}: ${Object.values(objValue).join(", ")} was not found`);
40
- }
41
- // eslint-disable-next-line max-len
42
- return await this.baseRepository_.serialize(wfExecution[0], {
43
- populate: true,
44
- });
45
- }
46
- async listWorkflowExecution(filters = {}, config = {}, sharedContext = {}) {
47
- if (filters.transaction_id) {
48
- if (Array.isArray(filters.transaction_id)) {
49
- filters.transaction_id = {
50
- $in: filters.transaction_id,
51
- };
52
- }
53
- }
54
- if (filters.workflow_id) {
55
- if (Array.isArray(filters.workflow_id)) {
56
- filters.workflow_id = {
57
- $in: filters.workflow_id,
58
- };
59
- }
60
- }
61
- const wfExecutions = await this.workflowExecutionService_.list(filters, config, sharedContext);
62
- return await this.baseRepository_.serialize(wfExecutions, {
63
- populate: true,
64
- });
65
- }
66
- async listAndCountWorkflowExecution(filters = {}, config = {}, sharedContext = {}) {
67
- if (filters.transaction_id) {
68
- if (Array.isArray(filters.transaction_id)) {
69
- filters.transaction_id = {
70
- $in: filters.transaction_id,
71
- };
72
- }
73
- }
74
- if (filters.workflow_id) {
75
- if (Array.isArray(filters.workflow_id)) {
76
- filters.workflow_id = {
77
- $in: filters.workflow_id,
78
- };
79
- }
80
- }
81
- const [wfExecutions, count] = await this.workflowExecutionService_.listAndCount(filters, config, sharedContext);
82
- return [
83
- await this.baseRepository_.serialize(wfExecutions, {
84
- populate: true,
85
- }),
86
- count,
87
- ];
88
- }
89
33
  async run(workflowIdOrWorkflow, options = {}, context = {}) {
90
34
  const ret = await this.workflowOrchestratorService_.run(workflowIdOrWorkflow, options, context);
91
35
  return ret;
@@ -115,27 +59,6 @@ class WorkflowsModuleService {
115
59
  }
116
60
  }
117
61
  exports.WorkflowsModuleService = WorkflowsModuleService;
118
- __decorate([
119
- (0, utils_1.InjectManager)("baseRepository_"),
120
- __param(2, (0, utils_1.MedusaContext)()),
121
- __metadata("design:type", Function),
122
- __metadata("design:paramtypes", [Object, Object, Object]),
123
- __metadata("design:returntype", Promise)
124
- ], WorkflowsModuleService.prototype, "retrieveWorkflowExecution", null);
125
- __decorate([
126
- (0, utils_1.InjectManager)("baseRepository_"),
127
- __param(2, (0, utils_1.MedusaContext)()),
128
- __metadata("design:type", Function),
129
- __metadata("design:paramtypes", [Object, Object, Object]),
130
- __metadata("design:returntype", Promise)
131
- ], WorkflowsModuleService.prototype, "listWorkflowExecution", null);
132
- __decorate([
133
- (0, utils_1.InjectManager)("baseRepository_"),
134
- __param(2, (0, utils_1.MedusaContext)()),
135
- __metadata("design:type", Function),
136
- __metadata("design:paramtypes", [Object, Object, Object]),
137
- __metadata("design:returntype", Promise)
138
- ], WorkflowsModuleService.prototype, "listAndCountWorkflowExecution", null);
139
62
  __decorate([
140
63
  (0, utils_1.InjectSharedContext)(),
141
64
  __param(2, (0, utils_1.MedusaContext)()),
@@ -1,25 +1,34 @@
1
- import { DistributedTransaction, DistributedTransactionStorage, TransactionCheckpoint, TransactionStep } from "@medusajs/orchestration";
2
- import { ModulesSdkTypes } from "@medusajs/types";
3
- export declare class InMemoryDistributedTransactionStorage extends DistributedTransactionStorage {
1
+ import { DistributedTransaction, IDistributedSchedulerStorage, IDistributedTransactionStorage, SchedulerOptions, TransactionCheckpoint, TransactionOptions, TransactionStep } from "@medusajs/orchestration";
2
+ import { Logger, ModulesSdkTypes } from "@medusajs/types";
3
+ export declare class InMemoryDistributedTransactionStorage implements IDistributedTransactionStorage, IDistributedSchedulerStorage {
4
4
  private workflowExecutionService_;
5
+ private logger_;
5
6
  private workflowOrchestratorService_;
6
7
  private storage;
8
+ private scheduled;
7
9
  private retries;
8
10
  private timeouts;
9
- constructor({ workflowExecutionService, }: {
10
- workflowExecutionService: ModulesSdkTypes.InternalModuleService<any>;
11
+ constructor({ workflowExecutionService, logger, }: {
12
+ workflowExecutionService: ModulesSdkTypes.IMedusaInternalService<any>;
13
+ logger: Logger;
11
14
  });
12
15
  setWorkflowOrchestratorService(workflowOrchestratorService: any): void;
13
16
  private saveToDb;
14
17
  private deleteFromDb;
15
- get(key: string): Promise<TransactionCheckpoint | undefined>;
18
+ get(key: string, options?: TransactionOptions): Promise<TransactionCheckpoint | undefined>;
16
19
  list(): Promise<TransactionCheckpoint[]>;
17
- save(key: string, data: TransactionCheckpoint, ttl?: number): Promise<void>;
20
+ save(key: string, data: TransactionCheckpoint, ttl?: number, options?: TransactionOptions): Promise<void>;
18
21
  scheduleRetry(transaction: DistributedTransaction, step: TransactionStep, timestamp: number, interval: number): Promise<void>;
19
22
  clearRetry(transaction: DistributedTransaction, step: TransactionStep): Promise<void>;
20
23
  scheduleTransactionTimeout(transaction: DistributedTransaction, timestamp: number, interval: number): Promise<void>;
21
24
  clearTransactionTimeout(transaction: DistributedTransaction): Promise<void>;
22
25
  scheduleStepTimeout(transaction: DistributedTransaction, step: TransactionStep, timestamp: number, interval: number): Promise<void>;
23
26
  clearStepTimeout(transaction: DistributedTransaction, step: TransactionStep): Promise<void>;
27
+ schedule(jobDefinition: string | {
28
+ jobId: string;
29
+ }, schedulerOptions: SchedulerOptions): Promise<void>;
30
+ remove(jobId: string): Promise<void>;
31
+ removeAll(): Promise<void>;
32
+ jobHandler(jobId: string): Promise<void>;
24
33
  }
25
34
  //# sourceMappingURL=workflow-orchestrator-storage.d.ts.map
@@ -1 +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;AAChC,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAKjD,qBAAa,qCAAsC,SAAQ,6BAA6B;IACtF,OAAO,CAAC,yBAAyB,CAA4C;IAC7E,OAAO,CAAC,4BAA4B,CAA6B;IAEjE,OAAO,CAAC,O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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;IAYV,QAAQ,CACZ,aAAa,EAAE,MAAM,GAAG;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,EACzC,gBAAgB,EAAE,gBAAgB,GACjC,OAAO,CAAC,IAAI,CAAC;IAsBV,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAUpC,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;IAM1B,UAAU,CAAC,KAAK,EAAE,MAAM;CA4C/B"}
@@ -1,16 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InMemoryDistributedTransactionStorage = void 0;
4
- const orchestration_1 = require("@medusajs/orchestration");
5
4
  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();
5
+ const cron_parser_1 = require("cron-parser");
6
+ class InMemoryDistributedTransactionStorage {
7
+ constructor({ workflowExecutionService, logger, }) {
10
8
  this.storage = new Map();
9
+ this.scheduled = new Map();
11
10
  this.retries = new Map();
12
11
  this.timeouts = new Map();
13
12
  this.workflowExecutionService_ = workflowExecutionService;
13
+ this.logger_ = logger;
14
14
  }
15
15
  setWorkflowOrchestratorService(workflowOrchestratorService) {
16
16
  this.workflowOrchestratorService_ = workflowOrchestratorService;
@@ -37,30 +37,38 @@ class InMemoryDistributedTransactionStorage extends orchestration_1.DistributedT
37
37
  },
38
38
  ]);
39
39
  }
40
- /*private stringifyWithSymbol(key, value) {
41
- if (key === "__type" && typeof value === "symbol") {
42
- return Symbol.keyFor(value)
43
- }
44
-
45
- return value
46
- }
47
-
48
- private jsonWithSymbol(key, value) {
49
- if (key === "__type" && typeof value === "string") {
50
- return Symbol.for(value)
51
- }
52
-
53
- return value
54
- }*/
55
- async get(key) {
56
- return this.storage.get(key);
40
+ async get(key, options) {
41
+ const data = this.storage.get(key);
42
+ if (data) {
43
+ return data;
44
+ }
45
+ const { idempotent } = options ?? {};
46
+ if (!idempotent) {
47
+ return;
48
+ }
49
+ const [_, workflowId, transactionId] = key.split(":");
50
+ const trx = await this.workflowExecutionService_
51
+ .retrieve({
52
+ workflow_id: workflowId,
53
+ transaction_id: transactionId,
54
+ }, {
55
+ select: ["execution", "context"],
56
+ })
57
+ .catch(() => undefined);
58
+ if (trx) {
59
+ return {
60
+ flow: trx.execution,
61
+ context: trx.context.data,
62
+ errors: trx.context.errors,
63
+ };
64
+ }
65
+ return;
57
66
  }
58
67
  async list() {
59
68
  return Array.from(this.storage.values());
60
69
  }
61
- async save(key, data, ttl) {
70
+ async save(key, data, ttl, options) {
62
71
  this.storage.set(key, data);
63
- let retentionTime;
64
72
  /**
65
73
  * Store the retention time only if the transaction is done, failed or reverted.
66
74
  * From that moment, this tuple can be later on archived or deleted after the retention time.
@@ -70,15 +78,15 @@ class InMemoryDistributedTransactionStorage extends orchestration_1.DistributedT
70
78
  utils_1.TransactionState.FAILED,
71
79
  utils_1.TransactionState.REVERTED,
72
80
  ].includes(data.flow.state);
81
+ const { retentionTime, idempotent } = options ?? {};
73
82
  if (hasFinished) {
74
- retentionTime = data.flow.options?.retentionTime;
75
83
  Object.assign(data, {
76
84
  retention_time: retentionTime,
77
85
  });
78
86
  }
79
87
  const stringifiedData = JSON.stringify(data);
80
88
  const parsedData = JSON.parse(stringifiedData);
81
- if (hasFinished && !retentionTime) {
89
+ if (hasFinished && !retentionTime && !idempotent) {
82
90
  await this.deleteFromDb(parsedData);
83
91
  }
84
92
  else {
@@ -148,5 +156,71 @@ class InMemoryDistributedTransactionStorage extends orchestration_1.DistributedT
148
156
  this.timeouts.delete(key);
149
157
  }
150
158
  }
159
+ /* Scheduler storage methods */
160
+ async schedule(jobDefinition, schedulerOptions) {
161
+ const jobId = typeof jobDefinition === "string" ? jobDefinition : jobDefinition.jobId;
162
+ // In order to ensure that the schedule configuration is always up to date, we first cancel an existing job, if there was one
163
+ // any only then we add the new one.
164
+ await this.remove(jobId);
165
+ const expression = (0, cron_parser_1.parseExpression)(schedulerOptions.cron);
166
+ const nextExecution = expression.next().getTime() - Date.now();
167
+ const timer = setTimeout(async () => {
168
+ this.jobHandler(jobId);
169
+ }, nextExecution);
170
+ this.scheduled.set(jobId, {
171
+ timer,
172
+ expression,
173
+ numberOfExecutions: 0,
174
+ config: schedulerOptions,
175
+ });
176
+ }
177
+ async remove(jobId) {
178
+ const job = this.scheduled.get(jobId);
179
+ if (!job) {
180
+ return;
181
+ }
182
+ clearTimeout(job.timer);
183
+ this.scheduled.delete(jobId);
184
+ }
185
+ async removeAll() {
186
+ for (const [key, job] of this.scheduled) {
187
+ await this.remove(key);
188
+ }
189
+ }
190
+ async jobHandler(jobId) {
191
+ const job = this.scheduled.get(jobId);
192
+ if (!job) {
193
+ return;
194
+ }
195
+ if (job.config?.numberOfExecutions !== undefined &&
196
+ job.config.numberOfExecutions <= job.numberOfExecutions) {
197
+ this.scheduled.delete(jobId);
198
+ return;
199
+ }
200
+ const nextExecution = job.expression.next().getTime() - Date.now();
201
+ const timer = setTimeout(async () => {
202
+ this.jobHandler(jobId);
203
+ }, nextExecution);
204
+ this.scheduled.set(jobId, {
205
+ timer,
206
+ expression: job.expression,
207
+ numberOfExecutions: (job.numberOfExecutions ?? 0) + 1,
208
+ config: job.config,
209
+ });
210
+ try {
211
+ // With running the job after setting a new timer we basically allow for concurrent runs, unless we add idempotency keys once they are supported.
212
+ await this.workflowOrchestratorService_.run(jobId, {
213
+ throwOnError: false,
214
+ });
215
+ }
216
+ catch (e) {
217
+ if (e instanceof utils_1.MedusaError && e.type === utils_1.MedusaError.Types.NOT_FOUND) {
218
+ this.logger_?.warn(`Tried to execute a scheduled workflow with ID ${jobId} that does not exist, removing it from the scheduler.`);
219
+ await this.remove(jobId);
220
+ return;
221
+ }
222
+ throw e;
223
+ }
224
+ }
151
225
  }
152
226
  exports.InMemoryDistributedTransactionStorage = InMemoryDistributedTransactionStorage;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medusajs/workflow-engine-inmemory",
3
- "version": "0.0.5-snapshot-20240607093839",
3
+ "version": "0.0.5-snapshot-20240701122250",
4
4
  "description": "Medusa Workflow Orchestrator module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,7 +8,7 @@
8
8
  "dist"
9
9
  ],
10
10
  "engines": {
11
- "node": ">=16"
11
+ "node": ">=20"
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
@@ -34,25 +34,25 @@
34
34
  "orm:cache:clear": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm cache:clear"
35
35
  },
36
36
  "devDependencies": {
37
- "@medusajs/types": "1.12.0-snapshot-20240607093839",
37
+ "@medusajs/types": "1.12.0-snapshot-20240701122250",
38
38
  "@mikro-orm/cli": "5.9.7",
39
39
  "cross-env": "^5.2.1",
40
- "jest": "^29.6.3",
41
- "medusa-test-utils": "1.1.45-snapshot-20240607093839",
40
+ "jest": "^29.7.0",
41
+ "medusa-test-utils": "1.1.45-snapshot-20240701122250",
42
42
  "rimraf": "^3.0.2",
43
- "ts-jest": "^29.1.1",
44
43
  "ts-node": "^10.9.1",
45
44
  "tsc-alias": "^1.8.6",
46
45
  "typescript": "^5.1.6"
47
46
  },
48
47
  "dependencies": {
49
- "@medusajs/modules-sdk": "1.13.0-snapshot-20240607093839",
50
- "@medusajs/utils": "1.12.0-snapshot-20240607093839",
51
- "@medusajs/workflows-sdk": "0.1.7-snapshot-20240607093839",
48
+ "@medusajs/modules-sdk": "1.13.0-snapshot-20240701122250",
49
+ "@medusajs/utils": "1.12.0-snapshot-20240701122250",
50
+ "@medusajs/workflows-sdk": "0.1.7-snapshot-20240701122250",
52
51
  "@mikro-orm/core": "5.9.7",
53
52
  "@mikro-orm/migrations": "5.9.7",
54
53
  "@mikro-orm/postgresql": "5.9.7",
55
54
  "awilix": "^8.0.0",
55
+ "cron-parser": "^4.9.0",
56
56
  "dotenv": "^16.4.5",
57
57
  "knex": "2.4.2"
58
58
  }
@@ -1,3 +0,0 @@
1
- export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils";
2
- export { WorkflowExecutionRepository } from "./workflow-execution";
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/repositories/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,IAAI,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAC1E,OAAO,EAAE,2BAA2B,EAAE,MAAM,sBAAsB,CAAA"}
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WorkflowExecutionRepository = exports.BaseRepository = void 0;
4
- var utils_1 = require("@medusajs/utils");
5
- Object.defineProperty(exports, "BaseRepository", { enumerable: true, get: function () { return utils_1.MikroOrmBaseRepository; } });
6
- var workflow_execution_1 = require("./workflow-execution");
7
- Object.defineProperty(exports, "WorkflowExecutionRepository", { enumerable: true, get: function () { return workflow_execution_1.WorkflowExecutionRepository; } });
@@ -1,8 +0,0 @@
1
- import { DALUtils } from "@medusajs/utils";
2
- declare const WorkflowExecutionRepository_base: new ({ manager }: {
3
- manager: any;
4
- }) => DALUtils.MikroOrmBaseRepository<object>;
5
- export declare class WorkflowExecutionRepository extends WorkflowExecutionRepository_base {
6
- }
7
- export {};
8
- //# sourceMappingURL=workflow-execution.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"workflow-execution.d.ts","sourceRoot":"","sources":["../../src/repositories/workflow-execution.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;;;;AAI1C,qBAAa,2BAA4B,SAAQ,gCAEhD;CAAG"}
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WorkflowExecutionRepository = void 0;
4
- const utils_1 = require("@medusajs/utils");
5
- const _models_1 = require("../models");
6
- // eslint-disable-next-line max-len
7
- class WorkflowExecutionRepository extends utils_1.DALUtils.mikroOrmBaseRepositoryFactory(_models_1.WorkflowExecution) {
8
- }
9
- exports.WorkflowExecutionRepository = WorkflowExecutionRepository;