@webiny/background-tasks-aws 0.0.0-unstable.0d717d18dd → 0.0.0-unstable.7c2def32a7

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.
@@ -1,13 +1,13 @@
1
1
  import type { Container } from "@webiny/feature/api";
2
- import { BackgroundTaskEventHandler } from "@webiny/event-handler-aws/abstractions/handlers/BackgroundTaskEventHandler.js";
2
+ import { BackgroundTaskEventHandler } from "@webiny/event-handler-core";
3
3
  import type { EventContext, NextFunction } from "@webiny/event-handler-core";
4
- import type { IBackgroundTaskEvent } from "@webiny/event-handler-aws/eventTypes/BackgroundTaskEventType.js";
4
+ import type { IBackgroundTaskEvent } from "@webiny/event-handler-core";
5
5
  declare class BackgroundTaskLambdaHandlerImpl implements BackgroundTaskEventHandler.Interface {
6
6
  private container;
7
7
  constructor(container: Container);
8
8
  execute(eventCtx: EventContext<IBackgroundTaskEvent>, _next: NextFunction): Promise<unknown>;
9
9
  }
10
10
  export declare const BackgroundTaskLambdaHandler: typeof BackgroundTaskLambdaHandlerImpl & {
11
- __abstraction: import("@webiny/di").Abstraction<import("@webiny/event-handler-aws/abstractions/handlers/BackgroundTaskEventHandler.js").IBackgroundTaskEventHandler>;
11
+ __abstraction: import("@webiny/di").Abstraction<import("@webiny/event-handler-core/features/backgroundTask/BackgroundTaskEventHandler").IBackgroundTaskEventHandler>;
12
12
  };
13
13
  export {};
@@ -1,7 +1,5 @@
1
1
  import { AwsLambdaContext } from "@webiny/event-handler-aws/abstractions/AwsLambdaContext.js";
2
- import { BackgroundTaskEventHandler } from "@webiny/event-handler-aws/abstractions/handlers/BackgroundTaskEventHandler.js";
3
- import { GraphQLContextEnhancer, GraphQLContextualSchema } from "@webiny/api-graphql";
4
- import { RequestContainer, runRequestContextInitializers } from "@webiny/event-handler-core";
2
+ import { BackgroundTaskEventHandler, RequestContainer } from "@webiny/event-handler-core";
5
3
  import { RawTenantId, RequestTenantLoader } from "@webiny/api-core/features/requestContext/index.js";
6
4
  import { TaskRunner } from "@webiny/background-tasks/api/runner/index.js";
7
5
  import { TaskEventValidation } from "@webiny/background-tasks/api/runner/TaskEventValidation.js";
@@ -18,14 +16,9 @@ class BackgroundTaskLambdaHandlerImpl {
18
16
  this.container.resolve(RawTenantId).set(taskEvent.tenant);
19
17
  await this.container.resolve(RequestTenantLoader).establish();
20
18
  }
21
- await runRequestContextInitializers(this.container, {
22
- continueOnError: true
23
- });
24
19
  const ctx = {
25
20
  container: this.container
26
21
  };
27
- for (const enhancer of this.container.resolveAll(GraphQLContextEnhancer))await enhancer.enhance(ctx);
28
- for (const schema of this.container.resolveAll(GraphQLContextualSchema))await schema.build(ctx);
29
22
  const lambdaContext = this.container.resolve(AwsLambdaContext);
30
23
  const timer = new LambdaTimer({
31
24
  getRemainingTimeInMillis: ()=>{
@@ -1 +1 @@
1
- {"version":3,"file":"BackgroundTaskLambdaHandler.js","sources":["../src/BackgroundTaskLambdaHandler.ts"],"sourcesContent":["import type { Container } from \"@webiny/feature/api\";\nimport { AwsLambdaContext } from \"@webiny/event-handler-aws/abstractions/AwsLambdaContext.js\";\nimport { BackgroundTaskEventHandler } from \"@webiny/event-handler-aws/abstractions/handlers/BackgroundTaskEventHandler.js\";\nimport { GraphQLContextEnhancer, GraphQLContextualSchema } from \"@webiny/api-graphql\";\nimport { RequestContainer, runRequestContextInitializers } from \"@webiny/event-handler-core\";\nimport {\n RawTenantId,\n RequestTenantLoader\n} from \"@webiny/api-core/features/requestContext/index.js\";\nimport type { EventContext, NextFunction } from \"@webiny/event-handler-core\";\nimport type { IBackgroundTaskEvent } from \"@webiny/event-handler-aws/eventTypes/BackgroundTaskEventType.js\";\nimport { TaskRunner } from \"@webiny/background-tasks/api/runner/index.js\";\nimport { TaskEventValidation } from \"@webiny/background-tasks/api/runner/TaskEventValidation.js\";\nimport type { Context } from \"@webiny/background-tasks/api/types.js\";\nimport { LambdaTimer } from \"~/timer/LambdaTimer.js\";\n\n/* Fallback ceiling used when there is no real Lambda context to read the remaining time from\n * (mirrors the default Lambda timeout budget handler-aws's CustomTimer uses). */\nconst FALLBACK_MAX_RUNNING_MILLISECONDS = 14 * 60 * 1000;\n\nclass BackgroundTaskLambdaHandlerImpl implements BackgroundTaskEventHandler.Interface {\n constructor(private container: Container) {}\n\n async execute(\n eventCtx: EventContext<IBackgroundTaskEvent>,\n _next: NextFunction\n ): Promise<unknown> {\n // Date-based fallback in case there is no real Lambda context (see below) — captured up\n // front so the fallback countdown starts at the beginning of this invocation.\n const fallbackStartTime = Date.now();\n // The SFN/EventBridge transport wraps the task as `{ name, payload }`; TaskRunner expects the\n // flat task event (webinyTaskId at top level), so unwrap `payload` (falling back to the event\n // itself if it's already flat).\n const taskEvent = (eventCtx.event as any)?.payload || eventCtx.event;\n\n // Background tasks have no HTTP request establisher. This is the bg-task EXTRACT step: put the\n // tenant id from the task event into RawTenantId, then run the shared LOAD step\n // (RequestTenantLoader) — same tenant-establishment path as every other transport. The\n // CRUD (TasksCrud) and downstream use cases resolve the current tenant, so it must be set\n // before the task runs.\n if (taskEvent?.tenant) {\n this.container.resolve(RawTenantId).set(taskEvent.tenant);\n await this.container.resolve(RequestTenantLoader).establish();\n }\n\n // Run the post-context initializers (register TasksCrud, FileModel, etc.). The HTTP layer does\n // this via RequestContextInitializerDecorator; the bg-task chain must do it too, before the\n // task runs — otherwise TaskControl can't resolve TasksCrud. continueOnError: a task doesn't\n // need every HTTP initializer (e.g. ACO/scheduler), and some throw in the bg-task context —\n // skip+log those so they don't fail the task, while TasksCrud/FileModel still register.\n await runRequestContextInitializers(this.container, { continueOnError: true });\n\n // TODO: remove once legacy ctx is gone — resolve services directly from the container.\n const ctx: Record<string, any> = { container: this.container };\n for (const enhancer of this.container.resolveAll(GraphQLContextEnhancer)) {\n await enhancer.enhance(ctx);\n }\n for (const schema of this.container.resolveAll(GraphQLContextualSchema)) {\n await schema.build(ctx);\n }\n\n // Use the real Lambda context's countdown when the invocation has one; otherwise fall back\n // to a Date-based countdown so the timer still winds down instead of staying static.\n const lambdaContext = this.container.resolve(AwsLambdaContext);\n const timer = new LambdaTimer({\n getRemainingTimeInMillis: () => {\n if (lambdaContext.isSet()) {\n return lambdaContext.get().getRemainingTimeInMillis();\n }\n return fallbackStartTime + FALLBACK_MAX_RUNNING_MILLISECONDS - Date.now();\n }\n });\n const runner = new TaskRunner(ctx as Context, timer, new TaskEventValidation());\n\n // Return the task result — the SFN reads `$.status` (continue/done/error) from it to drive\n // the state machine. Returning void makes the SFN see null → UnknownError → FAILED.\n return runner.run(taskEvent);\n }\n}\n\nexport const BackgroundTaskLambdaHandler = BackgroundTaskEventHandler.createImplementation({\n implementation: BackgroundTaskLambdaHandlerImpl,\n dependencies: [RequestContainer]\n});\n"],"names":["FALLBACK_MAX_RUNNING_MILLISECONDS","BackgroundTaskLambdaHandlerImpl","container","eventCtx","_next","fallbackStartTime","Date","taskEvent","RawTenantId","RequestTenantLoader","runRequestContextInitializers","ctx","enhancer","GraphQLContextEnhancer","schema","GraphQLContextualSchema","lambdaContext","AwsLambdaContext","timer","LambdaTimer","runner","TaskRunner","TaskEventValidation","BackgroundTaskLambdaHandler","BackgroundTaskEventHandler","RequestContainer"],"mappings":";;;;;;;;AAkBA,MAAMA,oCAAoC;AAE1C,MAAMC;IACF,YAAoBC,SAAoB,CAAE;aAAtBA,SAAS,GAATA;IAAuB;IAE3C,MAAM,QACFC,QAA4C,EAC5CC,KAAmB,EACH;QAGhB,MAAMC,oBAAoBC,KAAK,GAAG;QAIlC,MAAMC,YAAaJ,SAAS,KAAK,EAAU,WAAWA,SAAS,KAAK;QAOpE,IAAII,WAAW,QAAQ;YACnB,IAAI,CAAC,SAAS,CAAC,OAAO,CAACC,aAAa,GAAG,CAACD,UAAU,MAAM;YACxD,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,CAACE,qBAAqB,SAAS;QAC/D;QAOA,MAAMC,8BAA8B,IAAI,CAAC,SAAS,EAAE;YAAE,iBAAiB;QAAK;QAG5E,MAAMC,MAA2B;YAAE,WAAW,IAAI,CAAC,SAAS;QAAC;QAC7D,KAAK,MAAMC,YAAY,IAAI,CAAC,SAAS,CAAC,UAAU,CAACC,wBAC7C,MAAMD,SAAS,OAAO,CAACD;QAE3B,KAAK,MAAMG,UAAU,IAAI,CAAC,SAAS,CAAC,UAAU,CAACC,yBAC3C,MAAMD,OAAO,KAAK,CAACH;QAKvB,MAAMK,gBAAgB,IAAI,CAAC,SAAS,CAAC,OAAO,CAACC;QAC7C,MAAMC,QAAQ,IAAIC,YAAY;YAC1B,0BAA0B;gBACtB,IAAIH,cAAc,KAAK,IACnB,OAAOA,cAAc,GAAG,GAAG,wBAAwB;gBAEvD,OAAOX,oBAAoBL,oCAAoCM,KAAK,GAAG;YAC3E;QACJ;QACA,MAAMc,SAAS,IAAIC,WAAWV,KAAgBO,OAAO,IAAII;QAIzD,OAAOF,OAAO,GAAG,CAACb;IACtB;AACJ;AAEO,MAAMgB,8BAA8BC,2BAA2B,oBAAoB,CAAC;IACvF,gBAAgBvB;IAChB,cAAc;QAACwB;KAAiB;AACpC"}
1
+ {"version":3,"file":"BackgroundTaskLambdaHandler.js","sources":["../src/BackgroundTaskLambdaHandler.ts"],"sourcesContent":["import type { Container } from \"@webiny/feature/api\";\nimport { AwsLambdaContext } from \"@webiny/event-handler-aws/abstractions/AwsLambdaContext.js\";\nimport { BackgroundTaskEventHandler } from \"@webiny/event-handler-core\";\nimport { RequestContainer } from \"@webiny/event-handler-core\";\nimport {\n RawTenantId,\n RequestTenantLoader\n} from \"@webiny/api-core/features/requestContext/index.js\";\nimport type { EventContext, NextFunction } from \"@webiny/event-handler-core\";\nimport type { IBackgroundTaskEvent } from \"@webiny/event-handler-core\";\nimport { TaskRunner } from \"@webiny/background-tasks/api/runner/index.js\";\nimport { TaskEventValidation } from \"@webiny/background-tasks/api/runner/TaskEventValidation.js\";\nimport type { Context } from \"@webiny/background-tasks/api/types.js\";\nimport { LambdaTimer } from \"~/timer/LambdaTimer.js\";\n\n/* Fallback ceiling used when there is no real Lambda context to read the remaining time from\n * (mirrors the default Lambda timeout budget handler-aws's CustomTimer uses). */\nconst FALLBACK_MAX_RUNNING_MILLISECONDS = 14 * 60 * 1000;\n\nclass BackgroundTaskLambdaHandlerImpl implements BackgroundTaskEventHandler.Interface {\n constructor(private container: Container) {}\n\n async execute(\n eventCtx: EventContext<IBackgroundTaskEvent>,\n _next: NextFunction\n ): Promise<unknown> {\n // Date-based fallback in case there is no real Lambda context (see below) — captured up\n // front so the fallback countdown starts at the beginning of this invocation.\n const fallbackStartTime = Date.now();\n // The SFN/EventBridge transport wraps the task as `{ name, payload }`; TaskRunner expects the\n // flat task event (webinyTaskId at top level), so unwrap `payload` (falling back to the event\n // itself if it's already flat).\n const taskEvent = (eventCtx.event as any)?.payload || eventCtx.event;\n\n // Background tasks have no HTTP request establisher. This is the bg-task EXTRACT step: put the\n // tenant id from the task event into RawTenantId, then run the shared LOAD step\n // (RequestTenantLoader) — same tenant-establishment path as every other transport. The task\n // use cases and repositories resolve the current tenant, so it must be set before the task\n // runs.\n if (taskEvent?.tenant) {\n this.container.resolve(RawTenantId).set(taskEvent.tenant);\n await this.container.resolve(RequestTenantLoader).establish();\n }\n\n // TaskRunner still takes the legacy context object, which now only carries the container.\n const ctx: Record<string, any> = { container: this.container };\n\n // Use the real Lambda context's countdown when the invocation has one; otherwise fall back\n // to a Date-based countdown so the timer still winds down instead of staying static.\n const lambdaContext = this.container.resolve(AwsLambdaContext);\n const timer = new LambdaTimer({\n getRemainingTimeInMillis: () => {\n if (lambdaContext.isSet()) {\n return lambdaContext.get().getRemainingTimeInMillis();\n }\n return fallbackStartTime + FALLBACK_MAX_RUNNING_MILLISECONDS - Date.now();\n }\n });\n const runner = new TaskRunner(ctx as Context, timer, new TaskEventValidation());\n\n // Return the task result — the SFN reads `$.status` (continue/done/error) from it to drive\n // the state machine. Returning void makes the SFN see null → UnknownError → FAILED.\n return runner.run(taskEvent);\n }\n}\n\nexport const BackgroundTaskLambdaHandler = BackgroundTaskEventHandler.createImplementation({\n implementation: BackgroundTaskLambdaHandlerImpl,\n dependencies: [RequestContainer]\n});\n"],"names":["FALLBACK_MAX_RUNNING_MILLISECONDS","BackgroundTaskLambdaHandlerImpl","container","eventCtx","_next","fallbackStartTime","Date","taskEvent","RawTenantId","RequestTenantLoader","ctx","lambdaContext","AwsLambdaContext","timer","LambdaTimer","runner","TaskRunner","TaskEventValidation","BackgroundTaskLambdaHandler","BackgroundTaskEventHandler","RequestContainer"],"mappings":";;;;;;AAiBA,MAAMA,oCAAoC;AAE1C,MAAMC;IACF,YAAoBC,SAAoB,CAAE;aAAtBA,SAAS,GAATA;IAAuB;IAE3C,MAAM,QACFC,QAA4C,EAC5CC,KAAmB,EACH;QAGhB,MAAMC,oBAAoBC,KAAK,GAAG;QAIlC,MAAMC,YAAaJ,SAAS,KAAK,EAAU,WAAWA,SAAS,KAAK;QAOpE,IAAII,WAAW,QAAQ;YACnB,IAAI,CAAC,SAAS,CAAC,OAAO,CAACC,aAAa,GAAG,CAACD,UAAU,MAAM;YACxD,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,CAACE,qBAAqB,SAAS;QAC/D;QAGA,MAAMC,MAA2B;YAAE,WAAW,IAAI,CAAC,SAAS;QAAC;QAI7D,MAAMC,gBAAgB,IAAI,CAAC,SAAS,CAAC,OAAO,CAACC;QAC7C,MAAMC,QAAQ,IAAIC,YAAY;YAC1B,0BAA0B;gBACtB,IAAIH,cAAc,KAAK,IACnB,OAAOA,cAAc,GAAG,GAAG,wBAAwB;gBAEvD,OAAON,oBAAoBL,oCAAoCM,KAAK,GAAG;YAC3E;QACJ;QACA,MAAMS,SAAS,IAAIC,WAAWN,KAAgBG,OAAO,IAAII;QAIzD,OAAOF,OAAO,GAAG,CAACR;IACtB;AACJ;AAEO,MAAMW,8BAA8BC,2BAA2B,oBAAoB,CAAC;IACvF,gBAAgBlB;IAChB,cAAc;QAACmB;KAAiB;AACpC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/background-tasks-aws",
3
- "version": "0.0.0-unstable.0d717d18dd",
3
+ "version": "0.0.0-unstable.7c2def32a7",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -14,21 +14,20 @@
14
14
  },
15
15
  "license": "MIT",
16
16
  "dependencies": {
17
- "@webiny/api-core": "0.0.0-unstable.0d717d18dd",
18
- "@webiny/api-graphql": "0.0.0-unstable.0d717d18dd",
19
- "@webiny/aws-sdk": "0.0.0-unstable.0d717d18dd",
20
- "@webiny/background-tasks": "0.0.0-unstable.0d717d18dd",
21
- "@webiny/event-handler-aws": "0.0.0-unstable.0d717d18dd",
22
- "@webiny/event-handler-core": "0.0.0-unstable.0d717d18dd",
23
- "@webiny/feature": "0.0.0-unstable.0d717d18dd",
24
- "@webiny/utils": "0.0.0-unstable.0d717d18dd"
17
+ "@webiny/api-core": "0.0.0-unstable.7c2def32a7",
18
+ "@webiny/aws-sdk": "0.0.0-unstable.7c2def32a7",
19
+ "@webiny/background-tasks": "0.0.0-unstable.7c2def32a7",
20
+ "@webiny/event-handler-aws": "0.0.0-unstable.7c2def32a7",
21
+ "@webiny/event-handler-core": "0.0.0-unstable.7c2def32a7",
22
+ "@webiny/feature": "0.0.0-unstable.7c2def32a7",
23
+ "@webiny/utils": "0.0.0-unstable.7c2def32a7"
25
24
  },
26
25
  "devDependencies": {
27
- "@webiny/build-tools": "0.0.0-unstable.0d717d18dd",
28
- "@webiny/di": "1.0.2",
26
+ "@webiny/build-tools": "0.0.0-unstable.7c2def32a7",
27
+ "@webiny/di": "2.0.0",
29
28
  "rimraf": "6.1.3",
30
29
  "typescript": "7.0.2",
31
- "vitest": "4.1.10"
30
+ "vitest": "4.1.11"
32
31
  },
33
32
  "publishConfig": {
34
33
  "access": "public"
@@ -1,15 +1,17 @@
1
1
  import type { DescribeExecutionCommandOutput } from "@webiny/aws-sdk/client-sfn/index.js";
2
2
  import { TaskService } from "@webiny/background-tasks/api/domain/TaskService.js";
3
3
  import { TenantContext } from "@webiny/api-core/exports/api/tenancy.js";
4
+ import { Logger } from "@webiny/api-core/features/logger/index.js";
4
5
  export type IStepFunctionServiceFetchResult = DescribeExecutionCommandOutput;
5
6
  export interface IDetailWrapper<T> {
6
7
  detail: T;
7
8
  }
8
9
  declare class StepFunctionServiceImpl implements TaskService.Interface {
9
10
  private readonly tenantContext;
11
+ private readonly logger;
10
12
  private readonly trigger;
11
13
  private readonly get;
12
- constructor(tenantContext: TenantContext.Interface);
14
+ constructor(tenantContext: TenantContext.Interface, logger: Logger.Interface);
13
15
  send(task: TaskService.SendTaskParams, delay: number): Promise<{
14
16
  $metadata: import("@smithy/types").ResponseMetadata;
15
17
  executionArn: string | undefined;
@@ -3,9 +3,11 @@ import { generateAlphaNumericId } from "@webiny/utils";
3
3
  import { ServiceDiscovery } from "@webiny/api-core/features/serviceDiscovery/index.js";
4
4
  import { TaskService } from "@webiny/background-tasks/api/domain/TaskService.js";
5
5
  import { TenantContext } from "@webiny/api-core/exports/api/tenancy.js";
6
+ import { Logger } from "@webiny/api-core/features/logger/index.js";
6
7
  class StepFunctionServiceImpl {
7
- constructor(tenantContext){
8
+ constructor(tenantContext, logger){
8
9
  this.tenantContext = tenantContext;
10
+ this.logger = logger;
9
11
  const client = createStepFunctionClient();
10
12
  this.trigger = triggerStepFunctionFactory(client);
11
13
  this.get = describeExecutionFactory(client);
@@ -13,17 +15,17 @@ class StepFunctionServiceImpl {
13
15
  async send(task, delay) {
14
16
  const manifest = await ServiceDiscovery.load();
15
17
  if (!manifest) {
16
- console.error("Service manifest not found.");
18
+ this.logger.error("Service manifest not found.");
17
19
  return null;
18
20
  }
19
21
  const { bgTaskSfn } = manifest.api || {};
20
22
  if (!bgTaskSfn) {
21
- console.error("Background task state machine not found.");
23
+ this.logger.error("Background task state machine not found.");
22
24
  return null;
23
25
  }
24
26
  const tenant = this.tenantContext.getTenant();
25
27
  if (!tenant) {
26
- console.error("Tenant not found.");
28
+ this.logger.error("Tenant not found.");
27
29
  return null;
28
30
  }
29
31
  const input = {
@@ -46,15 +48,16 @@ class StepFunctionServiceImpl {
46
48
  name
47
49
  };
48
50
  } catch (ex) {
49
- console.log("Could not trigger a step function.");
50
- console.error(ex);
51
+ this.logger.error({
52
+ error: ex
53
+ }, "Could not trigger a step function.");
51
54
  return null;
52
55
  }
53
56
  }
54
57
  async fetch(task) {
55
58
  const executionArn = task.eventResponse?.executionArn;
56
59
  if (!executionArn) {
57
- console.error(`Execution ARN not found in task "${task.id}".`);
60
+ this.logger.error(`Execution ARN not found in task "${task.id}".`);
58
61
  return null;
59
62
  }
60
63
  try {
@@ -64,8 +67,9 @@ class StepFunctionServiceImpl {
64
67
  if (!result) return null;
65
68
  return JSON.parse(JSON.stringify(result));
66
69
  } catch (ex) {
67
- console.log("Could not get the execution details.");
68
- console.error(ex);
70
+ this.logger.error({
71
+ error: ex
72
+ }, "Could not get the execution details.");
69
73
  return null;
70
74
  }
71
75
  }
@@ -73,7 +77,8 @@ class StepFunctionServiceImpl {
73
77
  const StepFunctionService = TaskService.createImplementation({
74
78
  implementation: StepFunctionServiceImpl,
75
79
  dependencies: [
76
- TenantContext
80
+ TenantContext,
81
+ Logger
77
82
  ]
78
83
  });
79
84
  export { StepFunctionService };
@@ -1 +1 @@
1
- {"version":3,"file":"service/StepFunctionService.js","sources":["../../src/service/StepFunctionService.ts"],"sourcesContent":["import type { DescribeExecutionCommandOutput } from \"@webiny/aws-sdk/client-sfn/index.js\";\nimport {\n createStepFunctionClient,\n describeExecutionFactory,\n triggerStepFunctionFactory\n} from \"@webiny/aws-sdk/client-sfn/index.js\";\nimport type { ITaskEventInput } from \"@webiny/background-tasks/api/handler/types.js\";\nimport { generateAlphaNumericId } from \"@webiny/utils\";\nimport { ServiceDiscovery } from \"@webiny/api-core/features/serviceDiscovery/index.js\";\nimport { TaskService } from \"@webiny/background-tasks/api/domain/TaskService.js\";\nimport { TenantContext } from \"@webiny/api-core/exports/api/tenancy.js\";\n\nexport type IStepFunctionServiceFetchResult = DescribeExecutionCommandOutput;\n\nexport interface IDetailWrapper<T> {\n detail: T;\n}\n\nclass StepFunctionServiceImpl implements TaskService.Interface {\n private readonly trigger;\n private readonly get;\n\n public constructor(private readonly tenantContext: TenantContext.Interface) {\n // TODO client must be injectable at some point via some factory + cache\n const client = createStepFunctionClient();\n this.trigger = triggerStepFunctionFactory(client);\n this.get = describeExecutionFactory(client);\n }\n public async send(task: TaskService.SendTaskParams, delay: number) {\n const manifest = await ServiceDiscovery.load();\n if (!manifest) {\n console.error(\"Service manifest not found.\");\n return null;\n }\n const { bgTaskSfn } = manifest.api || {};\n if (!bgTaskSfn) {\n console.error(\"Background task state machine not found.\");\n return null;\n }\n const tenant = this.tenantContext.getTenant();\n if (!tenant) {\n console.error(\"Tenant not found.\");\n return null;\n }\n\n const input: ITaskEventInput = {\n webinyTaskId: task.id,\n webinyTaskDefinitionId: task.definitionId,\n tenant: tenant.id,\n delay\n };\n const name = `${task.definitionId}_${task.id}_${generateAlphaNumericId(10)}`;\n try {\n const result = await this.trigger<IDetailWrapper<ITaskEventInput>>({\n input: {\n detail: input\n },\n stateMachineArn: bgTaskSfn,\n name\n });\n return {\n ...result,\n name\n };\n } catch (ex) {\n console.log(\"Could not trigger a step function.\");\n console.error(ex);\n return null;\n }\n }\n\n public async fetch(task: TaskService.Task): Promise<IStepFunctionServiceFetchResult | null> {\n const executionArn = task.eventResponse?.executionArn;\n if (!executionArn) {\n console.error(`Execution ARN not found in task \"${task.id}\".`);\n return null;\n }\n try {\n const result = await this.get({\n executionArn\n });\n if (!result) {\n return null;\n }\n return JSON.parse(JSON.stringify(result));\n } catch (ex) {\n console.log(\"Could not get the execution details.\");\n console.error(ex);\n return null;\n }\n }\n}\n\nexport const StepFunctionService = TaskService.createImplementation({\n implementation: StepFunctionServiceImpl,\n dependencies: [TenantContext]\n});\n"],"names":["StepFunctionServiceImpl","tenantContext","client","createStepFunctionClient","triggerStepFunctionFactory","describeExecutionFactory","task","delay","manifest","ServiceDiscovery","console","bgTaskSfn","tenant","input","name","generateAlphaNumericId","result","ex","executionArn","JSON","StepFunctionService","TaskService","TenantContext"],"mappings":";;;;;AAkBA,MAAMA;IAIF,YAAoCC,aAAsC,CAAE;aAAxCA,aAAa,GAAbA;QAEhC,MAAMC,SAASC;QACf,IAAI,CAAC,OAAO,GAAGC,2BAA2BF;QAC1C,IAAI,CAAC,GAAG,GAAGG,yBAAyBH;IACxC;IACA,MAAa,KAAKI,IAAgC,EAAEC,KAAa,EAAE;QAC/D,MAAMC,WAAW,MAAMC,iBAAiB,IAAI;QAC5C,IAAI,CAACD,UAAU;YACXE,QAAQ,KAAK,CAAC;YACd,OAAO;QACX;QACA,MAAM,EAAEC,SAAS,EAAE,GAAGH,SAAS,GAAG,IAAI,CAAC;QACvC,IAAI,CAACG,WAAW;YACZD,QAAQ,KAAK,CAAC;YACd,OAAO;QACX;QACA,MAAME,SAAS,IAAI,CAAC,aAAa,CAAC,SAAS;QAC3C,IAAI,CAACA,QAAQ;YACTF,QAAQ,KAAK,CAAC;YACd,OAAO;QACX;QAEA,MAAMG,QAAyB;YAC3B,cAAcP,KAAK,EAAE;YACrB,wBAAwBA,KAAK,YAAY;YACzC,QAAQM,OAAO,EAAE;YACjBL;QACJ;QACA,MAAMO,OAAO,GAAGR,KAAK,YAAY,CAAC,CAAC,EAAEA,KAAK,EAAE,CAAC,CAAC,EAAES,uBAAuB,KAAK;QAC5E,IAAI;YACA,MAAMC,SAAS,MAAM,IAAI,CAAC,OAAO,CAAkC;gBAC/D,OAAO;oBACH,QAAQH;gBACZ;gBACA,iBAAiBF;gBACjBG;YACJ;YACA,OAAO;gBACH,GAAGE,MAAM;gBACTF;YACJ;QACJ,EAAE,OAAOG,IAAI;YACTP,QAAQ,GAAG,CAAC;YACZA,QAAQ,KAAK,CAACO;YACd,OAAO;QACX;IACJ;IAEA,MAAa,MAAMX,IAAsB,EAAmD;QACxF,MAAMY,eAAeZ,KAAK,aAAa,EAAE;QACzC,IAAI,CAACY,cAAc;YACfR,QAAQ,KAAK,CAAC,CAAC,iCAAiC,EAAEJ,KAAK,EAAE,CAAC,EAAE,CAAC;YAC7D,OAAO;QACX;QACA,IAAI;YACA,MAAMU,SAAS,MAAM,IAAI,CAAC,GAAG,CAAC;gBAC1BE;YACJ;YACA,IAAI,CAACF,QACD,OAAO;YAEX,OAAOG,KAAK,KAAK,CAACA,KAAK,SAAS,CAACH;QACrC,EAAE,OAAOC,IAAI;YACTP,QAAQ,GAAG,CAAC;YACZA,QAAQ,KAAK,CAACO;YACd,OAAO;QACX;IACJ;AACJ;AAEO,MAAMG,sBAAsBC,YAAY,oBAAoB,CAAC;IAChE,gBAAgBrB;IAChB,cAAc;QAACsB;KAAc;AACjC"}
1
+ {"version":3,"file":"service/StepFunctionService.js","sources":["../../src/service/StepFunctionService.ts"],"sourcesContent":["import type { DescribeExecutionCommandOutput } from \"@webiny/aws-sdk/client-sfn/index.js\";\nimport {\n createStepFunctionClient,\n describeExecutionFactory,\n triggerStepFunctionFactory\n} from \"@webiny/aws-sdk/client-sfn/index.js\";\nimport type { ITaskEventInput } from \"@webiny/background-tasks/api/handler/types.js\";\nimport { generateAlphaNumericId } from \"@webiny/utils\";\nimport { ServiceDiscovery } from \"@webiny/api-core/features/serviceDiscovery/index.js\";\nimport { TaskService } from \"@webiny/background-tasks/api/domain/TaskService.js\";\nimport { TenantContext } from \"@webiny/api-core/exports/api/tenancy.js\";\nimport { Logger } from \"@webiny/api-core/features/logger/index.js\";\n\nexport type IStepFunctionServiceFetchResult = DescribeExecutionCommandOutput;\n\nexport interface IDetailWrapper<T> {\n detail: T;\n}\n\nclass StepFunctionServiceImpl implements TaskService.Interface {\n private readonly trigger;\n private readonly get;\n\n public constructor(\n private readonly tenantContext: TenantContext.Interface,\n private readonly logger: Logger.Interface\n ) {\n // TODO client must be injectable at some point via some factory + cache\n const client = createStepFunctionClient();\n this.trigger = triggerStepFunctionFactory(client);\n this.get = describeExecutionFactory(client);\n }\n public async send(task: TaskService.SendTaskParams, delay: number) {\n const manifest = await ServiceDiscovery.load();\n if (!manifest) {\n this.logger.error(\"Service manifest not found.\");\n return null;\n }\n const { bgTaskSfn } = manifest.api || {};\n if (!bgTaskSfn) {\n this.logger.error(\"Background task state machine not found.\");\n return null;\n }\n const tenant = this.tenantContext.getTenant();\n if (!tenant) {\n this.logger.error(\"Tenant not found.\");\n return null;\n }\n\n const input: ITaskEventInput = {\n webinyTaskId: task.id,\n webinyTaskDefinitionId: task.definitionId,\n tenant: tenant.id,\n delay\n };\n const name = `${task.definitionId}_${task.id}_${generateAlphaNumericId(10)}`;\n try {\n const result = await this.trigger<IDetailWrapper<ITaskEventInput>>({\n input: {\n detail: input\n },\n stateMachineArn: bgTaskSfn,\n name\n });\n return {\n ...result,\n name\n };\n } catch (ex) {\n this.logger.error({ error: ex }, \"Could not trigger a step function.\");\n return null;\n }\n }\n\n public async fetch(task: TaskService.Task): Promise<IStepFunctionServiceFetchResult | null> {\n const executionArn = task.eventResponse?.executionArn;\n if (!executionArn) {\n this.logger.error(`Execution ARN not found in task \"${task.id}\".`);\n return null;\n }\n try {\n const result = await this.get({\n executionArn\n });\n if (!result) {\n return null;\n }\n return JSON.parse(JSON.stringify(result));\n } catch (ex) {\n this.logger.error({ error: ex }, \"Could not get the execution details.\");\n return null;\n }\n }\n}\n\nexport const StepFunctionService = TaskService.createImplementation({\n implementation: StepFunctionServiceImpl,\n dependencies: [TenantContext, Logger]\n});\n"],"names":["StepFunctionServiceImpl","tenantContext","logger","client","createStepFunctionClient","triggerStepFunctionFactory","describeExecutionFactory","task","delay","manifest","ServiceDiscovery","bgTaskSfn","tenant","input","name","generateAlphaNumericId","result","ex","executionArn","JSON","StepFunctionService","TaskService","TenantContext","Logger"],"mappings":";;;;;;AAmBA,MAAMA;IAIF,YACqBC,aAAsC,EACtCC,MAAwB,CAC3C;aAFmBD,aAAa,GAAbA;aACAC,MAAM,GAANA;QAGjB,MAAMC,SAASC;QACf,IAAI,CAAC,OAAO,GAAGC,2BAA2BF;QAC1C,IAAI,CAAC,GAAG,GAAGG,yBAAyBH;IACxC;IACA,MAAa,KAAKI,IAAgC,EAAEC,KAAa,EAAE;QAC/D,MAAMC,WAAW,MAAMC,iBAAiB,IAAI;QAC5C,IAAI,CAACD,UAAU;YACX,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;YAClB,OAAO;QACX;QACA,MAAM,EAAEE,SAAS,EAAE,GAAGF,SAAS,GAAG,IAAI,CAAC;QACvC,IAAI,CAACE,WAAW;YACZ,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;YAClB,OAAO;QACX;QACA,MAAMC,SAAS,IAAI,CAAC,aAAa,CAAC,SAAS;QAC3C,IAAI,CAACA,QAAQ;YACT,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;YAClB,OAAO;QACX;QAEA,MAAMC,QAAyB;YAC3B,cAAcN,KAAK,EAAE;YACrB,wBAAwBA,KAAK,YAAY;YACzC,QAAQK,OAAO,EAAE;YACjBJ;QACJ;QACA,MAAMM,OAAO,GAAGP,KAAK,YAAY,CAAC,CAAC,EAAEA,KAAK,EAAE,CAAC,CAAC,EAAEQ,uBAAuB,KAAK;QAC5E,IAAI;YACA,MAAMC,SAAS,MAAM,IAAI,CAAC,OAAO,CAAkC;gBAC/D,OAAO;oBACH,QAAQH;gBACZ;gBACA,iBAAiBF;gBACjBG;YACJ;YACA,OAAO;gBACH,GAAGE,MAAM;gBACTF;YACJ;QACJ,EAAE,OAAOG,IAAI;YACT,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;gBAAE,OAAOA;YAAG,GAAG;YACjC,OAAO;QACX;IACJ;IAEA,MAAa,MAAMV,IAAsB,EAAmD;QACxF,MAAMW,eAAeX,KAAK,aAAa,EAAE;QACzC,IAAI,CAACW,cAAc;YACf,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,iCAAiC,EAAEX,KAAK,EAAE,CAAC,EAAE,CAAC;YACjE,OAAO;QACX;QACA,IAAI;YACA,MAAMS,SAAS,MAAM,IAAI,CAAC,GAAG,CAAC;gBAC1BE;YACJ;YACA,IAAI,CAACF,QACD,OAAO;YAEX,OAAOG,KAAK,KAAK,CAACA,KAAK,SAAS,CAACH;QACrC,EAAE,OAAOC,IAAI;YACT,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC;gBAAE,OAAOA;YAAG,GAAG;YACjC,OAAO;QACX;IACJ;AACJ;AAEO,MAAMG,sBAAsBC,YAAY,oBAAoB,CAAC;IAChE,gBAAgBrB;IAChB,cAAc;QAACsB;QAAeC;KAAO;AACzC"}