@webiny/background-tasks-standalone 0.0.0-unstable.5387e5f7c8

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.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Background tasks for the standalone server. Register at the root container: the loop is one
3
+ * long-lived object for the whole process, and it needs the root's `EventDispatcher`.
4
+ */
5
+ export declare const BackgroundTasksStandaloneFeature: {
6
+ name: string;
7
+ register(container: import("@webiny/di").Container): void;
8
+ };
@@ -0,0 +1,17 @@
1
+ import { createFeature } from "@webiny/feature/api";
2
+ import { BackgroundTaskEventType } from "@webiny/event-handler-core";
3
+ import { DispatchingTaskLoop } from "./loop/DispatchingTaskLoop.js";
4
+ import { InProcessTaskService } from "./service/InProcessTaskService.js";
5
+ import { InProcessBackgroundTaskHandler } from "./handlers/InProcessBackgroundTaskHandler.js";
6
+ const BackgroundTasksStandaloneFeature = createFeature({
7
+ name: "BackgroundTasksServer",
8
+ register (container) {
9
+ container.register(BackgroundTaskEventType);
10
+ container.register(InProcessBackgroundTaskHandler);
11
+ container.register(DispatchingTaskLoop).inSingletonScope();
12
+ container.register(InProcessTaskService);
13
+ }
14
+ });
15
+ export { BackgroundTasksStandaloneFeature };
16
+
17
+ //# sourceMappingURL=BackgroundTasksStandaloneFeature.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BackgroundTasksStandaloneFeature.js","sources":["../src/BackgroundTasksStandaloneFeature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { BackgroundTaskEventType } from \"@webiny/event-handler-core\";\nimport { DispatchingTaskLoop } from \"~/loop/DispatchingTaskLoop.js\";\nimport { InProcessTaskService } from \"~/service/InProcessTaskService.js\";\nimport { InProcessBackgroundTaskHandler } from \"~/handlers/InProcessBackgroundTaskHandler.js\";\n\n/**\n * Background tasks for the standalone server. Register at the root container: the loop is one\n * long-lived object for the whole process, and it needs the root's `EventDispatcher`.\n */\nexport const BackgroundTasksStandaloneFeature = createFeature({\n name: \"BackgroundTasksServer\",\n register(container) {\n container.register(BackgroundTaskEventType);\n container.register(InProcessBackgroundTaskHandler);\n container.register(DispatchingTaskLoop).inSingletonScope();\n container.register(InProcessTaskService);\n }\n});\n"],"names":["BackgroundTasksStandaloneFeature","createFeature","container","BackgroundTaskEventType","InProcessBackgroundTaskHandler","DispatchingTaskLoop","InProcessTaskService"],"mappings":";;;;;AAUO,MAAMA,mCAAmCC,cAAc;IAC1D,MAAM;IACN,UAASC,SAAS;QACdA,UAAU,QAAQ,CAACC;QACnBD,UAAU,QAAQ,CAACE;QACnBF,UAAU,QAAQ,CAACG,qBAAqB,gBAAgB;QACxDH,UAAU,QAAQ,CAACI;IACvB;AACJ"}
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Webiny
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # @webiny/background-tasks-standalone
2
+
3
+ > [!NOTE]
4
+ > This package is part of the [Webiny](https://www.webiny.com) monorepo.
5
+ > It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
6
+
7
+ 📘 **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
8
+
9
+ ---
10
+
11
+ _This README file is automatically generated during the publish process._
@@ -0,0 +1,15 @@
1
+ import type { IBackgroundTaskEvent } from "@webiny/event-handler-core";
2
+ /**
3
+ * Drives a triggered task to completion inside this process: runs one iteration, waits as long as
4
+ * the iteration asks, and runs the next until the task stops asking to continue. The standalone
5
+ * counterpart of the Step Functions state machine that drives a task on AWS.
6
+ *
7
+ * `start` returns at once. The task runs after the request that triggered it has finished.
8
+ */
9
+ export interface ITaskLoop {
10
+ start(event: IBackgroundTaskEvent): void;
11
+ }
12
+ export declare const TaskLoop: import("@webiny/di").Abstraction<ITaskLoop>;
13
+ export declare namespace TaskLoop {
14
+ type Interface = ITaskLoop;
15
+ }
@@ -0,0 +1,5 @@
1
+ import { createAbstraction } from "@webiny/feature/api";
2
+ const TaskLoop = createAbstraction("BackgroundTasks/TaskLoop");
3
+ export { TaskLoop };
4
+
5
+ //# sourceMappingURL=TaskLoop.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"domain/TaskLoop.js","sources":["../../src/domain/TaskLoop.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { IBackgroundTaskEvent } from \"@webiny/event-handler-core\";\n\n/**\n * Drives a triggered task to completion inside this process: runs one iteration, waits as long as\n * the iteration asks, and runs the next until the task stops asking to continue. The standalone\n * counterpart of the Step Functions state machine that drives a task on AWS.\n *\n * `start` returns at once. The task runs after the request that triggered it has finished.\n */\nexport interface ITaskLoop {\n start(event: IBackgroundTaskEvent): void;\n}\n\nexport const TaskLoop = createAbstraction<ITaskLoop>(\"BackgroundTasks/TaskLoop\");\n\nexport namespace TaskLoop {\n export type Interface = ITaskLoop;\n}\n"],"names":["TaskLoop","createAbstraction"],"mappings":";AAcO,MAAMA,WAAWC,kBAA6B"}
@@ -0,0 +1,22 @@
1
+ import type { Container } from "@webiny/feature/api";
2
+ import { BackgroundTaskEventHandler } from "@webiny/event-handler-core";
3
+ import type { EventContext } from "@webiny/event-handler-core";
4
+ import type { IBackgroundTaskEvent } from "@webiny/event-handler-core";
5
+ import { RawTenantId } from "@webiny/api-core/features/requestContext/index.js";
6
+ import { RequestTenantLoader } from "@webiny/api-core/features/requestContext/index.js";
7
+ /**
8
+ * Runs one iteration of a background task in the request container the dispatcher built for it.
9
+ * The standalone counterpart of `BackgroundTaskLambdaHandler`: same tenant set-up and runner, with
10
+ * a process timer in place of the Lambda countdown.
11
+ */
12
+ declare class InProcessBackgroundTaskHandlerImpl implements BackgroundTaskEventHandler.Interface {
13
+ private readonly container;
14
+ private readonly rawTenantId;
15
+ private readonly tenantLoader;
16
+ constructor(container: Container, rawTenantId: RawTenantId.Interface, tenantLoader: RequestTenantLoader.Interface);
17
+ execute(eventCtx: EventContext<IBackgroundTaskEvent>): Promise<unknown>;
18
+ }
19
+ export declare const InProcessBackgroundTaskHandler: typeof InProcessBackgroundTaskHandlerImpl & {
20
+ __abstraction: import("@webiny/di").Abstraction<import("@webiny/event-handler-core/features/backgroundTask/BackgroundTaskEventHandler").IBackgroundTaskEventHandler>;
21
+ };
22
+ export {};
@@ -0,0 +1,34 @@
1
+ import { BackgroundTaskEventHandler, RequestContainer } from "@webiny/event-handler-core";
2
+ import { RawTenantId, RequestTenantLoader } from "@webiny/api-core/features/requestContext/index.js";
3
+ import { TaskRunner } from "@webiny/background-tasks/api/runner/index.js";
4
+ import { TaskEventValidation } from "@webiny/background-tasks/api/runner/TaskEventValidation.js";
5
+ import { ProcessTimer } from "../timer/ProcessTimer.js";
6
+ class InProcessBackgroundTaskHandlerImpl {
7
+ constructor(container, rawTenantId, tenantLoader){
8
+ this.container = container;
9
+ this.rawTenantId = rawTenantId;
10
+ this.tenantLoader = tenantLoader;
11
+ }
12
+ async execute(eventCtx) {
13
+ const taskEvent = eventCtx.event;
14
+ this.rawTenantId.set(taskEvent.tenant);
15
+ await this.tenantLoader.establish();
16
+ const ctx = {
17
+ container: this.container
18
+ };
19
+ const timer = new ProcessTimer();
20
+ const runner = new TaskRunner(ctx, timer, new TaskEventValidation());
21
+ return runner.run(taskEvent);
22
+ }
23
+ }
24
+ const InProcessBackgroundTaskHandler = BackgroundTaskEventHandler.createImplementation({
25
+ implementation: InProcessBackgroundTaskHandlerImpl,
26
+ dependencies: [
27
+ RequestContainer,
28
+ RawTenantId,
29
+ RequestTenantLoader
30
+ ]
31
+ });
32
+ export { InProcessBackgroundTaskHandler };
33
+
34
+ //# sourceMappingURL=InProcessBackgroundTaskHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"handlers/InProcessBackgroundTaskHandler.js","sources":["../../src/handlers/InProcessBackgroundTaskHandler.ts"],"sourcesContent":["import type { Container } from \"@webiny/feature/api\";\nimport { BackgroundTaskEventHandler } from \"@webiny/event-handler-core\";\nimport { RequestContainer } from \"@webiny/event-handler-core\";\nimport type { EventContext } from \"@webiny/event-handler-core\";\nimport type { IBackgroundTaskEvent } from \"@webiny/event-handler-core\";\nimport { RawTenantId } from \"@webiny/api-core/features/requestContext/index.js\";\nimport { RequestTenantLoader } from \"@webiny/api-core/features/requestContext/index.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 { ProcessTimer } from \"~/timer/ProcessTimer.js\";\n\n/**\n * Runs one iteration of a background task in the request container the dispatcher built for it.\n * The standalone counterpart of `BackgroundTaskLambdaHandler`: same tenant set-up and runner, with\n * a process timer in place of the Lambda countdown.\n */\nclass InProcessBackgroundTaskHandlerImpl implements BackgroundTaskEventHandler.Interface {\n public constructor(\n private readonly container: Container,\n private readonly rawTenantId: RawTenantId.Interface,\n private readonly tenantLoader: RequestTenantLoader.Interface\n ) {}\n\n public async execute(eventCtx: EventContext<IBackgroundTaskEvent>): Promise<unknown> {\n const taskEvent = eventCtx.event;\n\n this.rawTenantId.set(taskEvent.tenant);\n await this.tenantLoader.establish();\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 const timer = new ProcessTimer();\n const runner = new TaskRunner(ctx as Context, timer, new TaskEventValidation());\n\n // The loop reads `status`, `wait` and `delay` from this to decide what happens next.\n return runner.run(taskEvent);\n }\n}\n\nexport const InProcessBackgroundTaskHandler = BackgroundTaskEventHandler.createImplementation({\n implementation: InProcessBackgroundTaskHandlerImpl,\n dependencies: [RequestContainer, RawTenantId, RequestTenantLoader]\n});\n"],"names":["InProcessBackgroundTaskHandlerImpl","container","rawTenantId","tenantLoader","eventCtx","taskEvent","ctx","timer","ProcessTimer","runner","TaskRunner","TaskEventValidation","InProcessBackgroundTaskHandler","BackgroundTaskEventHandler","RequestContainer","RawTenantId","RequestTenantLoader"],"mappings":";;;;;AAiBA,MAAMA;IACF,YACqBC,SAAoB,EACpBC,WAAkC,EAClCC,YAA2C,CAC9D;aAHmBF,SAAS,GAATA;aACAC,WAAW,GAAXA;aACAC,YAAY,GAAZA;IAClB;IAEH,MAAa,QAAQC,QAA4C,EAAoB;QACjF,MAAMC,YAAYD,SAAS,KAAK;QAEhC,IAAI,CAAC,WAAW,CAAC,GAAG,CAACC,UAAU,MAAM;QACrC,MAAM,IAAI,CAAC,YAAY,CAAC,SAAS;QAGjC,MAAMC,MAA2B;YAAE,WAAW,IAAI,CAAC,SAAS;QAAC;QAE7D,MAAMC,QAAQ,IAAIC;QAClB,MAAMC,SAAS,IAAIC,WAAWJ,KAAgBC,OAAO,IAAII;QAGzD,OAAOF,OAAO,GAAG,CAACJ;IACtB;AACJ;AAEO,MAAMO,iCAAiCC,2BAA2B,oBAAoB,CAAC;IAC1F,gBAAgBb;IAChB,cAAc;QAACc;QAAkBC;QAAaC;KAAoB;AACtE"}
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { BackgroundTasksStandaloneFeature } from "./BackgroundTasksStandaloneFeature.js";
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export { BackgroundTasksStandaloneFeature } from "./BackgroundTasksStandaloneFeature.js";
@@ -0,0 +1,17 @@
1
+ import { EventDispatcher } from "@webiny/event-handler-core";
2
+ import type { IBackgroundTaskEvent } from "@webiny/event-handler-core";
3
+ import { TaskLoop } from "../domain/TaskLoop.js";
4
+ /**
5
+ * Each iteration is one dispatched `BackgroundTaskEvent`, handled in a fresh request container like
6
+ * an HTTP request would be. Root singleton: one loop drives every task in the process.
7
+ */
8
+ declare class DispatchingTaskLoopImpl implements TaskLoop.Interface {
9
+ private readonly dispatcher;
10
+ constructor(dispatcher: EventDispatcher.Interface);
11
+ start(event: IBackgroundTaskEvent): void;
12
+ private run;
13
+ }
14
+ export declare const DispatchingTaskLoop: typeof DispatchingTaskLoopImpl & {
15
+ __abstraction: import("@webiny/di").Abstraction<import("~/domain/TaskLoop.js").ITaskLoop>;
16
+ };
17
+ export {};
@@ -0,0 +1,48 @@
1
+ import { EventDispatcher } from "@webiny/event-handler-core";
2
+ import { TaskLoop } from "../domain/TaskLoop.js";
3
+ import { ProcessTimer } from "../timer/ProcessTimer.js";
4
+ const MAX_TASK_DURATION_MS = 86400000;
5
+ function sleep(ms) {
6
+ return new Promise((resolve)=>setTimeout(resolve, ms));
7
+ }
8
+ class DispatchingTaskLoopImpl {
9
+ constructor(dispatcher){
10
+ this.dispatcher = dispatcher;
11
+ }
12
+ start(event) {
13
+ this.run(event);
14
+ }
15
+ async run(firstEvent) {
16
+ const taskId = firstEvent.webinyTaskId;
17
+ const timer = new ProcessTimer(MAX_TASK_DURATION_MS);
18
+ let event = firstEvent;
19
+ while(timer.getRemainingMilliseconds() > 0){
20
+ let result;
21
+ try {
22
+ result = await this.dispatcher.dispatch(event);
23
+ } catch (error) {
24
+ console.error(`Background task "${taskId}" failed:`, error);
25
+ return;
26
+ }
27
+ const status = result?.status;
28
+ if ("done" === status || "aborted" === status) return;
29
+ if ("continue" !== status) return void console.error(`Background task "${taskId}" stopped with status "${status}":`, result);
30
+ event = {
31
+ ...event,
32
+ delay: result?.delay ?? -1
33
+ };
34
+ const wait = result?.wait ?? 0;
35
+ if (wait > 0) await sleep(1000 * wait);
36
+ }
37
+ console.error(`Background task "${taskId}" exceeded the maximum duration.`);
38
+ }
39
+ }
40
+ const DispatchingTaskLoop = TaskLoop.createImplementation({
41
+ implementation: DispatchingTaskLoopImpl,
42
+ dependencies: [
43
+ EventDispatcher
44
+ ]
45
+ });
46
+ export { DispatchingTaskLoop };
47
+
48
+ //# sourceMappingURL=DispatchingTaskLoop.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loop/DispatchingTaskLoop.js","sources":["../../src/loop/DispatchingTaskLoop.ts"],"sourcesContent":["import { EventDispatcher } from \"@webiny/event-handler-core\";\nimport type { IBackgroundTaskEvent } from \"@webiny/event-handler-core\";\nimport { TaskLoop } from \"~/domain/TaskLoop.js\";\nimport { ProcessTimer } from \"~/timer/ProcessTimer.js\";\n\n// 24 hours, the most one task may run in total.\nconst MAX_TASK_DURATION_MS = 86_400_000;\n\n// What the runner answers for one iteration; only the fields the loop reads.\ninterface IIterationResult {\n status?: string;\n wait?: number;\n delay?: number;\n}\n\nfunction sleep(ms: number): Promise<void> {\n return new Promise(resolve => setTimeout(resolve, ms));\n}\n\n/**\n * Each iteration is one dispatched `BackgroundTaskEvent`, handled in a fresh request container like\n * an HTTP request would be. Root singleton: one loop drives every task in the process.\n */\nclass DispatchingTaskLoopImpl implements TaskLoop.Interface {\n public constructor(private readonly dispatcher: EventDispatcher.Interface) {}\n\n public start(event: IBackgroundTaskEvent): void {\n void this.run(event);\n }\n\n private async run(firstEvent: IBackgroundTaskEvent): Promise<void> {\n const taskId = firstEvent.webinyTaskId;\n const timer = new ProcessTimer(MAX_TASK_DURATION_MS);\n let event = firstEvent;\n\n while (timer.getRemainingMilliseconds() > 0) {\n let result: IIterationResult | undefined;\n try {\n result = await this.dispatcher.dispatch<IIterationResult>(event);\n } catch (error) {\n console.error(`Background task \"${taskId}\" failed:`, error);\n return;\n }\n\n const status = result?.status;\n if (status === \"done\" || status === \"aborted\") {\n return;\n }\n if (status !== \"continue\") {\n console.error(\n `Background task \"${taskId}\" stopped with status \"${status}\":`,\n result\n );\n return;\n }\n\n /*\n * The first event carries the trigger's delay, and the runner answers it with `continue`\n * and `wait: delay` without running anything. Every later event must carry the delay the\n * runner sent back (-1), as the Step Functions state does on AWS. Resending the original\n * delay would make the runner answer `continue` forever.\n */\n event = { ...event, delay: result?.delay ?? -1 };\n\n const wait = result?.wait ?? 0;\n if (wait > 0) {\n await sleep(wait * 1000);\n }\n }\n\n console.error(`Background task \"${taskId}\" exceeded the maximum duration.`);\n }\n}\n\nexport const DispatchingTaskLoop = TaskLoop.createImplementation({\n implementation: DispatchingTaskLoopImpl,\n dependencies: [EventDispatcher]\n});\n"],"names":["MAX_TASK_DURATION_MS","sleep","ms","Promise","resolve","setTimeout","DispatchingTaskLoopImpl","dispatcher","event","firstEvent","taskId","timer","ProcessTimer","result","error","console","status","wait","DispatchingTaskLoop","TaskLoop","EventDispatcher"],"mappings":";;;AAMA,MAAMA,uBAAuB;AAS7B,SAASC,MAAMC,EAAU;IACrB,OAAO,IAAIC,QAAQC,CAAAA,UAAWC,WAAWD,SAASF;AACtD;AAMA,MAAMI;IACF,YAAoCC,UAAqC,CAAE;aAAvCA,UAAU,GAAVA;IAAwC;IAErE,MAAMC,KAA2B,EAAQ;QACvC,IAAI,CAAC,GAAG,CAACA;IAClB;IAEA,MAAc,IAAIC,UAAgC,EAAiB;QAC/D,MAAMC,SAASD,WAAW,YAAY;QACtC,MAAME,QAAQ,IAAIC,aAAaZ;QAC/B,IAAIQ,QAAQC;QAEZ,MAAOE,MAAM,wBAAwB,KAAK,EAAG;YACzC,IAAIE;YACJ,IAAI;gBACAA,SAAS,MAAM,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAmBL;YAC9D,EAAE,OAAOM,OAAO;gBACZC,QAAQ,KAAK,CAAC,CAAC,iBAAiB,EAAEL,OAAO,SAAS,CAAC,EAAEI;gBACrD;YACJ;YAEA,MAAME,SAASH,QAAQ;YACvB,IAAIG,AAAW,WAAXA,UAAqBA,AAAW,cAAXA,QACrB;YAEJ,IAAIA,AAAW,eAAXA,QAAuB,YACvBD,QAAQ,KAAK,CACT,CAAC,iBAAiB,EAAEL,OAAO,uBAAuB,EAAEM,OAAO,EAAE,CAAC,EAC9DH;YAWRL,QAAQ;gBAAE,GAAGA,KAAK;gBAAE,OAAOK,QAAQ,SAAS;YAAG;YAE/C,MAAMI,OAAOJ,QAAQ,QAAQ;YAC7B,IAAII,OAAO,GACP,MAAMhB,MAAMgB,AAAO,OAAPA;QAEpB;QAEAF,QAAQ,KAAK,CAAC,CAAC,iBAAiB,EAAEL,OAAO,gCAAgC,CAAC;IAC9E;AACJ;AAEO,MAAMQ,sBAAsBC,SAAS,oBAAoB,CAAC;IAC7D,gBAAgBb;IAChB,cAAc;QAACc;KAAgB;AACnC"}
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@webiny/background-tasks-standalone",
3
+ "version": "0.0.0-unstable.5387e5f7c8",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": "./index.js",
7
+ "./*": "./*"
8
+ },
9
+ "description": "Node server transport for Webiny background tasks (worker-thread orchestrator).",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/webiny/webiny-js.git",
13
+ "directory": "packages/background-tasks-standalone"
14
+ },
15
+ "license": "MIT",
16
+ "dependencies": {
17
+ "@webiny/api-core": "0.0.0-unstable.5387e5f7c8",
18
+ "@webiny/background-tasks": "0.0.0-unstable.5387e5f7c8",
19
+ "@webiny/event-handler-core": "0.0.0-unstable.5387e5f7c8",
20
+ "@webiny/feature": "0.0.0-unstable.5387e5f7c8",
21
+ "@webiny/utils": "0.0.0-unstable.5387e5f7c8"
22
+ },
23
+ "devDependencies": {
24
+ "@webiny/build-tools": "0.0.0-unstable.5387e5f7c8",
25
+ "@webiny/di": "2.0.0",
26
+ "rimraf": "6.1.3",
27
+ "typescript": "7.0.2",
28
+ "vitest": "4.1.11"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "webiny": {
34
+ "publishFrom": "dist"
35
+ }
36
+ }
@@ -0,0 +1,19 @@
1
+ import { TaskService } from "@webiny/background-tasks/api/domain/TaskService.js";
2
+ import { TenantContext } from "@webiny/api-core/exports/api/tenancy.js";
3
+ import { TaskLoop } from "../domain/TaskLoop.js";
4
+ /**
5
+ * Starts a triggered task in this process. Builds the same event Step Functions would deliver on
6
+ * AWS and hands it to the task loop. `endpoint`, `executionName` and `stateMachineId` only satisfy
7
+ * the runner's event validation; nothing reads them off AWS.
8
+ */
9
+ declare class InProcessTaskServiceImpl implements TaskService.Interface {
10
+ private readonly tenantContext;
11
+ private readonly taskLoop;
12
+ constructor(tenantContext: TenantContext.Interface, taskLoop: TaskLoop.Interface);
13
+ send(task: TaskService.SendTaskParams, delay: number): Promise<unknown>;
14
+ fetch(): Promise<null>;
15
+ }
16
+ export declare const InProcessTaskService: typeof InProcessTaskServiceImpl & {
17
+ __abstraction: import("@webiny/di").Abstraction<import("@webiny/background-tasks/api/domain/TaskService.js").ITaskService>;
18
+ };
19
+ export {};
@@ -0,0 +1,39 @@
1
+ import { TaskService } from "@webiny/background-tasks/api/domain/TaskService.js";
2
+ import { TenantContext } from "@webiny/api-core/exports/api/tenancy.js";
3
+ import { TaskLoop } from "../domain/TaskLoop.js";
4
+ class InProcessTaskServiceImpl {
5
+ constructor(tenantContext, taskLoop){
6
+ this.tenantContext = tenantContext;
7
+ this.taskLoop = taskLoop;
8
+ }
9
+ async send(task, delay) {
10
+ const tenant = this.tenantContext.getTenant();
11
+ if (!tenant) return null;
12
+ const event = {
13
+ webinyTaskId: task.id,
14
+ webinyTaskDefinitionId: task.definitionId,
15
+ tenant: tenant.id,
16
+ delay,
17
+ endpoint: "in-process",
18
+ executionName: task.id,
19
+ stateMachineId: ""
20
+ };
21
+ this.taskLoop.start(event);
22
+ return {
23
+ taskId: task.id
24
+ };
25
+ }
26
+ async fetch() {
27
+ return null;
28
+ }
29
+ }
30
+ const InProcessTaskService = TaskService.createImplementation({
31
+ implementation: InProcessTaskServiceImpl,
32
+ dependencies: [
33
+ TenantContext,
34
+ TaskLoop
35
+ ]
36
+ });
37
+ export { InProcessTaskService };
38
+
39
+ //# sourceMappingURL=InProcessTaskService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service/InProcessTaskService.js","sources":["../../src/service/InProcessTaskService.ts"],"sourcesContent":["import type { IBackgroundTaskEvent } from \"@webiny/event-handler-core\";\nimport { TaskService } from \"@webiny/background-tasks/api/domain/TaskService.js\";\nimport { TenantContext } from \"@webiny/api-core/exports/api/tenancy.js\";\nimport { TaskLoop } from \"~/domain/TaskLoop.js\";\n\n/**\n * Starts a triggered task in this process. Builds the same event Step Functions would deliver on\n * AWS and hands it to the task loop. `endpoint`, `executionName` and `stateMachineId` only satisfy\n * the runner's event validation; nothing reads them off AWS.\n */\nclass InProcessTaskServiceImpl implements TaskService.Interface {\n public constructor(\n private readonly tenantContext: TenantContext.Interface,\n private readonly taskLoop: TaskLoop.Interface\n ) {}\n\n public async send(task: TaskService.SendTaskParams, delay: number): Promise<unknown> {\n const tenant = this.tenantContext.getTenant();\n if (!tenant) {\n return null;\n }\n\n const event: IBackgroundTaskEvent = {\n webinyTaskId: task.id,\n webinyTaskDefinitionId: task.definitionId,\n tenant: tenant.id,\n delay,\n endpoint: \"in-process\",\n executionName: task.id,\n stateMachineId: \"\"\n };\n\n this.taskLoop.start(event);\n\n return { taskId: task.id };\n }\n\n public async fetch(): Promise<null> {\n // A running task has no execution record outside the task itself.\n return null;\n }\n}\n\nexport const InProcessTaskService = TaskService.createImplementation({\n implementation: InProcessTaskServiceImpl,\n dependencies: [TenantContext, TaskLoop]\n});\n"],"names":["InProcessTaskServiceImpl","tenantContext","taskLoop","task","delay","tenant","event","InProcessTaskService","TaskService","TenantContext","TaskLoop"],"mappings":";;;AAUA,MAAMA;IACF,YACqBC,aAAsC,EACtCC,QAA4B,CAC/C;aAFmBD,aAAa,GAAbA;aACAC,QAAQ,GAARA;IAClB;IAEH,MAAa,KAAKC,IAAgC,EAAEC,KAAa,EAAoB;QACjF,MAAMC,SAAS,IAAI,CAAC,aAAa,CAAC,SAAS;QAC3C,IAAI,CAACA,QACD,OAAO;QAGX,MAAMC,QAA8B;YAChC,cAAcH,KAAK,EAAE;YACrB,wBAAwBA,KAAK,YAAY;YACzC,QAAQE,OAAO,EAAE;YACjBD;YACA,UAAU;YACV,eAAeD,KAAK,EAAE;YACtB,gBAAgB;QACpB;QAEA,IAAI,CAAC,QAAQ,CAAC,KAAK,CAACG;QAEpB,OAAO;YAAE,QAAQH,KAAK,EAAE;QAAC;IAC7B;IAEA,MAAa,QAAuB;QAEhC,OAAO;IACX;AACJ;AAEO,MAAMI,uBAAuBC,YAAY,oBAAoB,CAAC;IACjE,gBAAgBR;IAChB,cAAc;QAACS;QAAeC;KAAS;AAC3C"}
@@ -0,0 +1,8 @@
1
+ import type { Timer } from "@webiny/utils/features/Timer/abstraction.js";
2
+ export declare class ProcessTimer implements Timer.Interface {
3
+ private readonly startTime;
4
+ private readonly maxDurationMs;
5
+ constructor(maxDurationMs?: number);
6
+ getRemainingMilliseconds(): number;
7
+ getRemainingSeconds(): number;
8
+ }
@@ -0,0 +1,19 @@
1
+ const DEFAULT_MAX_DURATION_MS = 86400000;
2
+ class ProcessTimer {
3
+ constructor(maxDurationMs = DEFAULT_MAX_DURATION_MS){
4
+ this.startTime = process.hrtime();
5
+ this.maxDurationMs = maxDurationMs;
6
+ }
7
+ getRemainingMilliseconds() {
8
+ const elapsed = process.hrtime(this.startTime);
9
+ const elapsedMs = 1000 * elapsed[0] + elapsed[1] / 1000000;
10
+ const remaining = this.maxDurationMs - elapsedMs;
11
+ return remaining > 0 ? remaining : 0;
12
+ }
13
+ getRemainingSeconds() {
14
+ return Math.floor(this.getRemainingMilliseconds() / 1000);
15
+ }
16
+ }
17
+ export { ProcessTimer };
18
+
19
+ //# sourceMappingURL=ProcessTimer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timer/ProcessTimer.js","sources":["../../src/timer/ProcessTimer.ts"],"sourcesContent":["import type { Timer } from \"@webiny/utils/features/Timer/abstraction.js\";\n\nconst DEFAULT_MAX_DURATION_MS = 86_400_000; /* 24 hours. */\n\nexport class ProcessTimer implements Timer.Interface {\n private readonly startTime: [number, number];\n private readonly maxDurationMs: number;\n\n public constructor(maxDurationMs: number = DEFAULT_MAX_DURATION_MS) {\n this.startTime = process.hrtime();\n this.maxDurationMs = maxDurationMs;\n }\n\n public getRemainingMilliseconds(): number {\n const elapsed = process.hrtime(this.startTime);\n const elapsedMs = elapsed[0] * 1000 + elapsed[1] / 1_000_000;\n const remaining = this.maxDurationMs - elapsedMs;\n return remaining > 0 ? remaining : 0;\n }\n\n public getRemainingSeconds(): number {\n return Math.floor(this.getRemainingMilliseconds() / 1000);\n }\n}\n"],"names":["DEFAULT_MAX_DURATION_MS","ProcessTimer","maxDurationMs","process","elapsed","elapsedMs","remaining","Math"],"mappings":"AAEA,MAAMA,0BAA0B;AAEzB,MAAMC;IAIT,YAAmBC,gBAAwBF,uBAAuB,CAAE;QAChE,IAAI,CAAC,SAAS,GAAGG,QAAQ,MAAM;QAC/B,IAAI,CAAC,aAAa,GAAGD;IACzB;IAEO,2BAAmC;QACtC,MAAME,UAAUD,QAAQ,MAAM,CAAC,IAAI,CAAC,SAAS;QAC7C,MAAME,YAAYD,AAAa,OAAbA,OAAO,CAAC,EAAE,GAAUA,OAAO,CAAC,EAAE,GAAG;QACnD,MAAME,YAAY,IAAI,CAAC,aAAa,GAAGD;QACvC,OAAOC,YAAY,IAAIA,YAAY;IACvC;IAEO,sBAA8B;QACjC,OAAOC,KAAK,KAAK,CAAC,IAAI,CAAC,wBAAwB,KAAK;IACxD;AACJ"}