@webiny/api-composition-standalone 0.0.0-stage → 0.0.0-unstable.4373247389

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 (62) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -2
  3. package/bulkActions/bulkActionsServer.d.ts +6 -0
  4. package/bulkActions/bulkActionsServer.js +26 -0
  5. package/bulkActions/bulkActionsServer.js.map +1 -0
  6. package/config/EnvStandaloneConfig.d.ts +23 -0
  7. package/config/EnvStandaloneConfig.js +70 -0
  8. package/config/EnvStandaloneConfig.js.map +1 -0
  9. package/config/abstractions.d.ts +40 -0
  10. package/config/abstractions.js +5 -0
  11. package/config/abstractions.js.map +1 -0
  12. package/config/assertFileStorage.d.ts +8 -0
  13. package/config/assertFileStorage.js +8 -0
  14. package/config/assertFileStorage.js.map +1 -0
  15. package/config/bakedSecrets.d.ts +8 -0
  16. package/config/bakedSecrets.js +17 -0
  17. package/config/bakedSecrets.js.map +1 -0
  18. package/createWebinyApiHandler.d.ts +20 -0
  19. package/createWebinyApiHandler.js +139 -0
  20. package/createWebinyApiHandler.js.map +1 -0
  21. package/crypto/StandaloneEncryptionConfig.d.ts +17 -0
  22. package/crypto/StandaloneEncryptionConfig.js +20 -0
  23. package/crypto/StandaloneEncryptionConfig.js.map +1 -0
  24. package/crypto/StandaloneHashingConfig.d.ts +16 -0
  25. package/crypto/StandaloneHashingConfig.js +20 -0
  26. package/crypto/StandaloneHashingConfig.js.map +1 -0
  27. package/crypto/feature.d.ts +5 -0
  28. package/crypto/feature.js +13 -0
  29. package/crypto/feature.js.map +1 -0
  30. package/index.d.ts +3 -0
  31. package/index.js +2 -0
  32. package/package.json +51 -5
  33. package/publicUrls/ConfigPublicUrls.d.ts +13 -0
  34. package/publicUrls/ConfigPublicUrls.js +22 -0
  35. package/publicUrls/ConfigPublicUrls.js.map +1 -0
  36. package/publicUrls/feature.d.ts +4 -0
  37. package/publicUrls/feature.js +11 -0
  38. package/publicUrls/feature.js.map +1 -0
  39. package/scheduler/RecoverScheduledActionsHandler.d.ts +27 -0
  40. package/scheduler/RecoverScheduledActionsHandler.js +77 -0
  41. package/scheduler/RecoverScheduledActionsHandler.js.map +1 -0
  42. package/scheduler/RunScheduledActionOnceDecorator.d.ts +34 -0
  43. package/scheduler/RunScheduledActionOnceDecorator.js +89 -0
  44. package/scheduler/RunScheduledActionOnceDecorator.js.map +1 -0
  45. package/scheduler/ScheduledActionRecoverInputHandler.d.ts +14 -0
  46. package/scheduler/ScheduledActionRecoverInputHandler.js +5 -0
  47. package/scheduler/ScheduledActionRecoverInputHandler.js.map +1 -0
  48. package/scheduler/ScheduledActionRecoverInputType.d.ts +17 -0
  49. package/scheduler/ScheduledActionRecoverInputType.js +18 -0
  50. package/scheduler/ScheduledActionRecoverInputType.js.map +1 -0
  51. package/scheduler/abstractions/SchedulerSingleton.d.ts +10 -0
  52. package/scheduler/abstractions/SchedulerSingleton.js +5 -0
  53. package/scheduler/abstractions/SchedulerSingleton.js.map +1 -0
  54. package/scheduler/schedulerServer.d.ts +25 -0
  55. package/scheduler/schedulerServer.js +81 -0
  56. package/scheduler/schedulerServer.js.map +1 -0
  57. package/tenantStorages/guardDeclaredTenantStorages.d.ts +36 -0
  58. package/tenantStorages/guardDeclaredTenantStorages.js +60 -0
  59. package/tenantStorages/guardDeclaredTenantStorages.js.map +1 -0
  60. package/websockets/createWebsocketsAuthenticator.d.ts +4 -0
  61. package/websockets/createWebsocketsAuthenticator.js +49 -0
  62. package/websockets/createWebsocketsAuthenticator.js.map +1 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto/StandaloneHashingConfig.js","sources":["../../src/crypto/StandaloneHashingConfig.ts"],"sourcesContent":["import { HashingConfig } from \"@webiny/api-core/features/hashing/index.js\";\nimport { BuildParams } from \"@webiny/api-core/features/buildParams/index.js\";\nimport { StandaloneConfig } from \"~/config/abstractions.js\";\n\n/**\n * Standalone: every hash is peppered. The pepper comes from env (`WEBINY_HASHING_PEPPER`), the cost\n * from the build.\n */\nclass StandaloneHashingConfigImpl implements HashingConfig.Interface {\n readonly pepper: string;\n readonly costN: number | null;\n\n constructor(config: StandaloneConfig.Interface, buildParams: BuildParams.Interface) {\n this.pepper = config.hashing.pepper;\n const costN = buildParams.get<number | string>(\"HashingCost\");\n this.costN = costN ? Number(costN) : null;\n }\n}\n\nexport const StandaloneHashingConfig = HashingConfig.createImplementation({\n implementation: StandaloneHashingConfigImpl,\n dependencies: [StandaloneConfig, BuildParams]\n});\n"],"names":["StandaloneHashingConfigImpl","config","buildParams","costN","Number","StandaloneHashingConfig","HashingConfig","StandaloneConfig","BuildParams"],"mappings":";;;AAQA,MAAMA;IAIF,YAAYC,MAAkC,EAAEC,WAAkC,CAAE;QAChF,IAAI,CAAC,MAAM,GAAGD,OAAO,OAAO,CAAC,MAAM;QACnC,MAAME,QAAQD,YAAY,GAAG,CAAkB;QAC/C,IAAI,CAAC,KAAK,GAAGC,QAAQC,OAAOD,SAAS;IACzC;AACJ;AAEO,MAAME,0BAA0BC,cAAc,oBAAoB,CAAC;IACtE,gBAAgBN;IAChB,cAAc;QAACO;QAAkBC;KAAY;AACjD"}
@@ -0,0 +1,5 @@
1
+ /** Replaces api-core's build-param crypto configs with ones that read secrets from env. */
2
+ export declare const StandaloneCryptoConfigFeature: {
3
+ name: string;
4
+ register(container: import("@webiny/di").Container): void;
5
+ };
@@ -0,0 +1,13 @@
1
+ import { createFeature } from "@webiny/feature/api";
2
+ import { StandaloneEncryptionConfig } from "./StandaloneEncryptionConfig.js";
3
+ import { StandaloneHashingConfig } from "./StandaloneHashingConfig.js";
4
+ const StandaloneCryptoConfigFeature = createFeature({
5
+ name: "StandaloneCryptoConfigFeature",
6
+ register (container) {
7
+ container.register(StandaloneEncryptionConfig);
8
+ container.register(StandaloneHashingConfig);
9
+ }
10
+ });
11
+ export { StandaloneCryptoConfigFeature };
12
+
13
+ //# sourceMappingURL=feature.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto/feature.js","sources":["../../src/crypto/feature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { StandaloneEncryptionConfig } from \"./StandaloneEncryptionConfig.js\";\nimport { StandaloneHashingConfig } from \"./StandaloneHashingConfig.js\";\n\n/** Replaces api-core's build-param crypto configs with ones that read secrets from env. */\nexport const StandaloneCryptoConfigFeature = createFeature({\n name: \"StandaloneCryptoConfigFeature\",\n register(container) {\n container.register(StandaloneEncryptionConfig);\n container.register(StandaloneHashingConfig);\n }\n});\n"],"names":["StandaloneCryptoConfigFeature","createFeature","container","StandaloneEncryptionConfig","StandaloneHashingConfig"],"mappings":";;;AAKO,MAAMA,gCAAgCC,cAAc;IACvD,MAAM;IACN,UAASC,SAAS;QACdA,UAAU,QAAQ,CAACC;QACnBD,UAAU,QAAQ,CAACE;IACvB;AACJ"}
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { createWebinyApiHandler } from "./createWebinyApiHandler.js";
2
+ export type { CreateWebinyApiHandlerConfig } from "./createWebinyApiHandler.js";
3
+ export { TenantStorageRemovedError, DECLARED_TENANT_STORAGES_KEY } from "./tenantStorages/guardDeclaredTenantStorages.js";
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { createWebinyApiHandler } from "./createWebinyApiHandler.js";
2
+ export { DECLARED_TENANT_STORAGES_KEY, TenantStorageRemovedError } from "./tenantStorages/guardDeclaredTenantStorages.js";
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
- "name": "@webiny/api-composition-standalone",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@webiny/api-composition-standalone",
3
+ "version": "0.0.0-unstable.4373247389",
4
+ "type": "module",
5
+ "main": "index.js",
6
+ "exports": {
7
+ ".": "./index.js",
8
+ "./*": "./*"
9
+ },
10
+ "types": "index.d.ts",
11
+ "description": "EXPERIMENTAL: Webiny API handler for the standalone Node HTTP server transport.",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/webiny/webiny-js.git"
15
+ },
16
+ "contributors": [
17
+ "Webiny Team"
18
+ ],
19
+ "license": "MIT",
20
+ "dependencies": {
21
+ "@webiny/api-composition-core": "0.0.0-unstable.4373247389",
22
+ "@webiny/api-core": "0.0.0-unstable.4373247389",
23
+ "@webiny/api-file-manager-standalone": "0.0.0-unstable.4373247389",
24
+ "@webiny/api-graphql": "0.0.0-unstable.4373247389",
25
+ "@webiny/api-headless-cms-bulk-actions-standalone": "0.0.0-unstable.4373247389",
26
+ "@webiny/api-kernel-core": "0.0.0-unstable.4373247389",
27
+ "@webiny/api-object-storage": "0.0.0-unstable.4373247389",
28
+ "@webiny/api-scheduler": "0.0.0-unstable.4373247389",
29
+ "@webiny/api-scheduler-standalone": "0.0.0-unstable.4373247389",
30
+ "@webiny/api-websockets-standalone": "0.0.0-unstable.4373247389",
31
+ "@webiny/api-websockets-standalone-redis": "0.0.0-unstable.4373247389",
32
+ "@webiny/background-tasks-standalone": "0.0.0-unstable.4373247389",
33
+ "@webiny/feature": "0.0.0-unstable.4373247389",
34
+ "@webiny/handler": "0.0.0-unstable.4373247389"
35
+ },
36
+ "peerDependencies": {
37
+ "@webiny/di": "^2.0.0"
38
+ },
39
+ "devDependencies": {
40
+ "@webiny/build-tools": "0.0.0-unstable.4373247389",
41
+ "@webiny/di": "2.0.0",
42
+ "rimraf": "6.1.4",
43
+ "typescript": "7.0.2",
44
+ "vitest": "5.1.1"
45
+ },
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "webiny": {
50
+ "publishFrom": "dist"
51
+ }
52
+ }
@@ -0,0 +1,13 @@
1
+ import { PublicUrls } from "@webiny/api-core/features/publicUrls/index.js";
2
+ import { StandaloneConfig } from "../config/abstractions.js";
3
+ /** Standalone: the URLs come from `WEBINY_API_URL` / `WEBINY_ADMIN_URL`, read at startup. */
4
+ declare class ConfigPublicUrlsImpl implements PublicUrls.Interface {
5
+ private config;
6
+ constructor(config: StandaloneConfig.Interface);
7
+ getApiUrl(): Promise<import("@webiny/api-core/features/publicUrls/PublicUrl").PublicUrl>;
8
+ getAdminUrl(): Promise<import("@webiny/api-core/features/publicUrls/PublicUrl").PublicUrl>;
9
+ }
10
+ export declare const ConfigPublicUrls: typeof ConfigPublicUrlsImpl & {
11
+ __abstraction: import("@webiny/di").Abstraction<import("@webiny/api-core/features/publicUrls/abstractions").IPublicUrls>;
12
+ };
13
+ export {};
@@ -0,0 +1,22 @@
1
+ import { PublicUrls } from "@webiny/api-core/features/publicUrls/index.js";
2
+ import { StandaloneConfig } from "../config/abstractions.js";
3
+ class ConfigPublicUrlsImpl {
4
+ constructor(config){
5
+ this.config = config;
6
+ }
7
+ async getApiUrl() {
8
+ return this.config.apiUrl;
9
+ }
10
+ async getAdminUrl() {
11
+ return this.config.adminUrl;
12
+ }
13
+ }
14
+ const ConfigPublicUrls = PublicUrls.createImplementation({
15
+ implementation: ConfigPublicUrlsImpl,
16
+ dependencies: [
17
+ StandaloneConfig
18
+ ]
19
+ });
20
+ export { ConfigPublicUrls };
21
+
22
+ //# sourceMappingURL=ConfigPublicUrls.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"publicUrls/ConfigPublicUrls.js","sources":["../../src/publicUrls/ConfigPublicUrls.ts"],"sourcesContent":["import { PublicUrls } from \"@webiny/api-core/features/publicUrls/index.js\";\nimport { StandaloneConfig } from \"~/config/abstractions.js\";\n\n/** Standalone: the URLs come from `WEBINY_API_URL` / `WEBINY_ADMIN_URL`, read at startup. */\nclass ConfigPublicUrlsImpl implements PublicUrls.Interface {\n constructor(private config: StandaloneConfig.Interface) {}\n\n async getApiUrl() {\n return this.config.apiUrl;\n }\n\n async getAdminUrl() {\n return this.config.adminUrl;\n }\n}\n\nexport const ConfigPublicUrls = PublicUrls.createImplementation({\n implementation: ConfigPublicUrlsImpl,\n dependencies: [StandaloneConfig]\n});\n"],"names":["ConfigPublicUrlsImpl","config","ConfigPublicUrls","PublicUrls","StandaloneConfig"],"mappings":";;AAIA,MAAMA;IACF,YAAoBC,MAAkC,CAAE;aAApCA,MAAM,GAANA;IAAqC;IAEzD,MAAM,YAAY;QACd,OAAO,IAAI,CAAC,MAAM,CAAC,MAAM;IAC7B;IAEA,MAAM,cAAc;QAChB,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ;IAC/B;AACJ;AAEO,MAAMC,mBAAmBC,WAAW,oBAAoB,CAAC;IAC5D,gBAAgBH;IAChB,cAAc;QAACI;KAAiB;AACpC"}
@@ -0,0 +1,4 @@
1
+ export declare const ConfigPublicUrlsFeature: {
2
+ name: string;
3
+ register(container: import("@webiny/di").Container): void;
4
+ };
@@ -0,0 +1,11 @@
1
+ import { createFeature } from "@webiny/feature/api";
2
+ import { ConfigPublicUrls } from "./ConfigPublicUrls.js";
3
+ const ConfigPublicUrlsFeature = createFeature({
4
+ name: "ConfigPublicUrlsFeature",
5
+ register (container) {
6
+ container.register(ConfigPublicUrls);
7
+ }
8
+ });
9
+ export { ConfigPublicUrlsFeature };
10
+
11
+ //# sourceMappingURL=feature.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"publicUrls/feature.js","sources":["../../src/publicUrls/feature.ts"],"sourcesContent":["import { createFeature } from \"@webiny/feature/api\";\nimport { ConfigPublicUrls } from \"./ConfigPublicUrls.js\";\n\nexport const ConfigPublicUrlsFeature = createFeature({\n name: \"ConfigPublicUrlsFeature\",\n register(container) {\n container.register(ConfigPublicUrls);\n }\n});\n"],"names":["ConfigPublicUrlsFeature","createFeature","container","ConfigPublicUrls"],"mappings":";;AAGO,MAAMA,0BAA0BC,cAAc;IACjD,MAAM;IACN,UAASC,SAAS;QACdA,UAAU,QAAQ,CAACC;IACvB;AACJ"}
@@ -0,0 +1,27 @@
1
+ import { ListTenantsUseCase } from "@webiny/api-core/features/tenancy/ListTenants/index.js";
2
+ import { TenantScopes } from "@webiny/api-core/features/tenancy/TenantScopes/index.js";
3
+ import { Logger } from "@webiny/api-core/features/logger/abstractions.js";
4
+ import { ScheduledActionRecoverInputHandler } from "./ScheduledActionRecoverInputHandler.js";
5
+ import { SchedulerSingleton } from "./abstractions/SchedulerSingleton.js";
6
+ /**
7
+ * Re-arms every tenant's pending scheduled actions in the scheduler singleton. It keeps its timers in
8
+ * memory, so a restart loses them all. Overdue actions run at once (see
9
+ * `TimerSchedulerService.recover`).
10
+ *
11
+ * Each tenant is recovered on its own: one that fails is logged and reported in the result, and the
12
+ * rest are still recovered. A fresh install has no tenants yet and recovers nothing.
13
+ */
14
+ declare class RecoverScheduledActionsHandlerImpl implements ScheduledActionRecoverInputHandler.Interface {
15
+ private readonly listTenants;
16
+ private readonly tenantScopes;
17
+ private readonly scheduler;
18
+ private readonly logger;
19
+ constructor(listTenants: ListTenantsUseCase.Interface, tenantScopes: TenantScopes.Interface, scheduler: SchedulerSingleton.Interface, logger: Logger.Interface);
20
+ execute(): Promise<ScheduledActionRecoverInputHandler.Result>;
21
+ private recoverTenant;
22
+ private recoverAllPending;
23
+ }
24
+ export declare const RecoverScheduledActionsHandler: typeof RecoverScheduledActionsHandlerImpl & {
25
+ __abstraction: import("@webiny/di").Abstraction<import("./ScheduledActionRecoverInputHandler.js").IScheduledActionRecoverInputHandler>;
26
+ };
27
+ export {};
@@ -0,0 +1,77 @@
1
+ import { ListTenantsUseCase } from "@webiny/api-core/features/tenancy/ListTenants/index.js";
2
+ import { TenantScopes } from "@webiny/api-core/features/tenancy/TenantScopes/index.js";
3
+ import { IdentityContext } from "@webiny/api-core/exports/api/security.js";
4
+ import { Logger } from "@webiny/api-core/features/logger/abstractions.js";
5
+ import { ListScheduledActionsUseCase } from "@webiny/api-scheduler/features/ListScheduledActions/index.js";
6
+ import { ScheduledActionRecoverInputHandler } from "./ScheduledActionRecoverInputHandler.js";
7
+ import { SchedulerSingleton } from "./abstractions/SchedulerSingleton.js";
8
+ const PAGE_SIZE = 1000;
9
+ function canRecover(tenant) {
10
+ return tenant.isInstalled && "disabled" !== tenant.status;
11
+ }
12
+ class RecoverScheduledActionsHandlerImpl {
13
+ constructor(listTenants, tenantScopes, scheduler, logger){
14
+ this.listTenants = listTenants;
15
+ this.tenantScopes = tenantScopes;
16
+ this.scheduler = scheduler;
17
+ this.logger = logger;
18
+ }
19
+ async execute() {
20
+ const tenantsResult = await this.listTenants.execute();
21
+ if (tenantsResult.isFail()) throw tenantsResult.error;
22
+ let recovered = 0;
23
+ const failedTenants = [];
24
+ for (const tenant of tenantsResult.value)if (canRecover(tenant)) try {
25
+ recovered += await this.recoverTenant(tenant);
26
+ } catch (error) {
27
+ failedTenants.push(tenant.id);
28
+ this.logger.error({
29
+ error,
30
+ tenant: tenant.id
31
+ }, "Could not recover the tenant's scheduled actions.");
32
+ }
33
+ return {
34
+ recovered,
35
+ failedTenants
36
+ };
37
+ }
38
+ recoverTenant(tenant) {
39
+ return this.tenantScopes.withTenant(tenant, (scope)=>scope.resolve(IdentityContext).withoutAuthorization(()=>this.recoverAllPending(tenant.id, scope.resolve(ListScheduledActionsUseCase))));
40
+ }
41
+ async recoverAllPending(tenant, listScheduledActions) {
42
+ let recovered = 0;
43
+ let after;
44
+ do {
45
+ const listResult = await listScheduledActions.execute({
46
+ where: {},
47
+ limit: PAGE_SIZE,
48
+ after
49
+ });
50
+ if (listResult.isFail()) throw listResult.error;
51
+ const page = [];
52
+ for (const action of listResult.value.items)page.push({
53
+ id: action.id,
54
+ namespace: action.namespace,
55
+ tenant,
56
+ scheduledFor: action.scheduledFor
57
+ });
58
+ await this.scheduler.recover(page);
59
+ recovered += page.length;
60
+ const { meta } = listResult.value;
61
+ after = meta.hasMoreItems && meta.cursor ? meta.cursor : void 0;
62
+ }while (after)
63
+ return recovered;
64
+ }
65
+ }
66
+ const RecoverScheduledActionsHandler = ScheduledActionRecoverInputHandler.createImplementation({
67
+ implementation: RecoverScheduledActionsHandlerImpl,
68
+ dependencies: [
69
+ ListTenantsUseCase,
70
+ TenantScopes,
71
+ SchedulerSingleton,
72
+ Logger
73
+ ]
74
+ });
75
+ export { RecoverScheduledActionsHandler };
76
+
77
+ //# sourceMappingURL=RecoverScheduledActionsHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler/RecoverScheduledActionsHandler.js","sources":["../../src/scheduler/RecoverScheduledActionsHandler.ts"],"sourcesContent":["import { ListTenantsUseCase } from \"@webiny/api-core/features/tenancy/ListTenants/index.js\";\nimport { TenantScopes } from \"@webiny/api-core/features/tenancy/TenantScopes/index.js\";\nimport type { Tenant } from \"@webiny/api-core/types/tenancy.js\";\nimport { IdentityContext } from \"@webiny/api-core/exports/api/security.js\";\nimport { Logger } from \"@webiny/api-core/features/logger/abstractions.js\";\nimport { ListScheduledActionsUseCase } from \"@webiny/api-scheduler/features/ListScheduledActions/index.js\";\nimport { ScheduledActionRecoverInputHandler } from \"./ScheduledActionRecoverInputHandler.js\";\nimport type { IPendingAction } from \"@webiny/api-scheduler-standalone\";\nimport { SchedulerSingleton } from \"./abstractions/SchedulerSingleton.js\";\n\n// Pending actions read per page.\nconst PAGE_SIZE = 1000;\n\n/*\n * A tenant that isn't installed has no scheduler model to read, and a disabled one must not run its\n * schedules. Both are left alone; a disabled tenant's schedules come back on the next restart after\n * it is re-enabled.\n */\nfunction canRecover(tenant: Tenant): boolean {\n return tenant.isInstalled && tenant.status !== \"disabled\";\n}\n\n/**\n * Re-arms every tenant's pending scheduled actions in the scheduler singleton. It keeps its timers in\n * memory, so a restart loses them all. Overdue actions run at once (see\n * `TimerSchedulerService.recover`).\n *\n * Each tenant is recovered on its own: one that fails is logged and reported in the result, and the\n * rest are still recovered. A fresh install has no tenants yet and recovers nothing.\n */\nclass RecoverScheduledActionsHandlerImpl implements ScheduledActionRecoverInputHandler.Interface {\n public constructor(\n private readonly listTenants: ListTenantsUseCase.Interface,\n private readonly tenantScopes: TenantScopes.Interface,\n private readonly scheduler: SchedulerSingleton.Interface,\n private readonly logger: Logger.Interface\n ) {}\n\n public async execute(): Promise<ScheduledActionRecoverInputHandler.Result> {\n // A failed lookup is not a fresh install: it throws, so the boot step reports it.\n const tenantsResult = await this.listTenants.execute();\n if (tenantsResult.isFail()) {\n throw tenantsResult.error;\n }\n\n let recovered = 0;\n const failedTenants: string[] = [];\n\n for (const tenant of tenantsResult.value) {\n if (!canRecover(tenant)) {\n continue;\n }\n try {\n recovered += await this.recoverTenant(tenant);\n } catch (error) {\n failedTenants.push(tenant.id);\n this.logger.error(\n { error, tenant: tenant.id },\n \"Could not recover the tenant's scheduled actions.\"\n );\n }\n }\n\n return { recovered, failedTenants };\n }\n\n private recoverTenant(tenant: Tenant): Promise<number> {\n return this.tenantScopes.withTenant(tenant, scope => {\n /*\n * Nobody is signed in at boot. Listing without authorization is what the run side does\n * too (ExecuteScheduledActionUseCase); otherwise the anonymous identity fails the\n * permission check.\n */\n return scope.resolve(IdentityContext).withoutAuthorization(() => {\n return this.recoverAllPending(\n tenant.id,\n scope.resolve(ListScheduledActionsUseCase)\n );\n });\n });\n }\n\n /*\n * Re-arms page by page, so a lookup that fails on a later page still leaves the earlier pages\n * armed. The failure is rethrown and logged by the boot step.\n */\n private async recoverAllPending(\n tenant: string,\n listScheduledActions: ListScheduledActionsUseCase.Interface\n ): Promise<number> {\n let recovered = 0;\n let after: string | undefined;\n\n do {\n const listResult = await listScheduledActions.execute({\n where: {},\n limit: PAGE_SIZE,\n after\n });\n if (listResult.isFail()) {\n throw listResult.error;\n }\n\n const page: IPendingAction[] = [];\n for (const action of listResult.value.items) {\n page.push({\n id: action.id,\n namespace: action.namespace,\n tenant,\n scheduledFor: action.scheduledFor\n });\n }\n\n await this.scheduler.recover(page);\n recovered += page.length;\n\n const { meta } = listResult.value;\n after = meta.hasMoreItems && meta.cursor ? meta.cursor : undefined;\n } while (after);\n\n return recovered;\n }\n}\n\nexport const RecoverScheduledActionsHandler =\n ScheduledActionRecoverInputHandler.createImplementation({\n implementation: RecoverScheduledActionsHandlerImpl,\n dependencies: [ListTenantsUseCase, TenantScopes, SchedulerSingleton, Logger]\n });\n"],"names":["PAGE_SIZE","canRecover","tenant","RecoverScheduledActionsHandlerImpl","listTenants","tenantScopes","scheduler","logger","tenantsResult","recovered","failedTenants","error","scope","IdentityContext","ListScheduledActionsUseCase","listScheduledActions","after","listResult","page","action","meta","undefined","RecoverScheduledActionsHandler","ScheduledActionRecoverInputHandler","ListTenantsUseCase","TenantScopes","SchedulerSingleton","Logger"],"mappings":";;;;;;;AAWA,MAAMA,YAAY;AAOlB,SAASC,WAAWC,MAAc;IAC9B,OAAOA,OAAO,WAAW,IAAIA,AAAkB,eAAlBA,OAAO,MAAM;AAC9C;AAUA,MAAMC;IACF,YACqBC,WAAyC,EACzCC,YAAoC,EACpCC,SAAuC,EACvCC,MAAwB,CAC3C;aAJmBH,WAAW,GAAXA;aACAC,YAAY,GAAZA;aACAC,SAAS,GAATA;aACAC,MAAM,GAANA;IAClB;IAEH,MAAa,UAA8D;QAEvE,MAAMC,gBAAgB,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO;QACpD,IAAIA,cAAc,MAAM,IACpB,MAAMA,cAAc,KAAK;QAG7B,IAAIC,YAAY;QAChB,MAAMC,gBAA0B,EAAE;QAElC,KAAK,MAAMR,UAAUM,cAAc,KAAK,CACpC,IAAKP,WAAWC,SAGhB,IAAI;YACAO,aAAa,MAAM,IAAI,CAAC,aAAa,CAACP;QAC1C,EAAE,OAAOS,OAAO;YACZD,cAAc,IAAI,CAACR,OAAO,EAAE;YAC5B,IAAI,CAAC,MAAM,CAAC,KAAK,CACb;gBAAES;gBAAO,QAAQT,OAAO,EAAE;YAAC,GAC3B;QAER;QAGJ,OAAO;YAAEO;YAAWC;QAAc;IACtC;IAEQ,cAAcR,MAAc,EAAmB;QACnD,OAAO,IAAI,CAAC,YAAY,CAAC,UAAU,CAACA,QAAQU,CAAAA,QAMjCA,MAAM,OAAO,CAACC,iBAAiB,oBAAoB,CAAC,IAChD,IAAI,CAAC,iBAAiB,CACzBX,OAAO,EAAE,EACTU,MAAM,OAAO,CAACE;IAI9B;IAMA,MAAc,kBACVZ,MAAc,EACda,oBAA2D,EAC5C;QACf,IAAIN,YAAY;QAChB,IAAIO;QAEJ,GAAG;YACC,MAAMC,aAAa,MAAMF,qBAAqB,OAAO,CAAC;gBAClD,OAAO,CAAC;gBACR,OAAOf;gBACPgB;YACJ;YACA,IAAIC,WAAW,MAAM,IACjB,MAAMA,WAAW,KAAK;YAG1B,MAAMC,OAAyB,EAAE;YACjC,KAAK,MAAMC,UAAUF,WAAW,KAAK,CAAC,KAAK,CACvCC,KAAK,IAAI,CAAC;gBACN,IAAIC,OAAO,EAAE;gBACb,WAAWA,OAAO,SAAS;gBAC3BjB;gBACA,cAAciB,OAAO,YAAY;YACrC;YAGJ,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,CAACD;YAC7BT,aAAaS,KAAK,MAAM;YAExB,MAAM,EAAEE,IAAI,EAAE,GAAGH,WAAW,KAAK;YACjCD,QAAQI,KAAK,YAAY,IAAIA,KAAK,MAAM,GAAGA,KAAK,MAAM,GAAGC;QAC7D,QAASL,MAAO;QAEhB,OAAOP;IACX;AACJ;AAEO,MAAMa,iCACTC,mCAAmC,oBAAoB,CAAC;IACpD,gBAAgBpB;IAChB,cAAc;QAACqB;QAAoBC;QAAcC;QAAoBC;KAAO;AAChF"}
@@ -0,0 +1,34 @@
1
+ import { ScheduledActionInputHandler } from "@webiny/api-kernel-core";
2
+ import type { IScheduledActionEvent } from "@webiny/api-kernel-core";
3
+ import type { IScheduledActionResult } from "@webiny/api-kernel-core";
4
+ import type { InputContext } from "@webiny/api-kernel-core";
5
+ import type { NextFunction } from "@webiny/api-kernel-core";
6
+ import { GetTenantByIdUseCase, TenantScopes } from "@webiny/api-core/exports/api/tenancy.js";
7
+ import { TenantContext } from "@webiny/api-core/features/tenancy/TenantContext/index.js";
8
+ import { GlobalKeyValueStore } from "@webiny/api-core/features/keyValueStore/index.js";
9
+ /**
10
+ * Runs a fired scheduled action at most once across all processes. Every standalone process holds its
11
+ * own timers for the same actions, so the same action fires in each of them; on AWS, EventBridge
12
+ * fires it once and this decorator is not registered.
13
+ *
14
+ * Before the action runs, it is read again. Gone means it was cancelled or already ran. Due later
15
+ * means another process moved it and this timer is stale. Otherwise the process takes a claim on the
16
+ * action at its due time, and only the process that gets it runs the action. Rescheduling changes
17
+ * the due time, so a moved action gets a fresh claim.
18
+ */
19
+ declare class RunScheduledActionOnceDecoratorImpl implements ScheduledActionInputHandler.Interface {
20
+ private readonly getTenantById;
21
+ private readonly tenantContext;
22
+ private readonly tenantScopes;
23
+ private readonly store;
24
+ private readonly decoratee;
25
+ constructor(getTenantById: GetTenantByIdUseCase.Interface, tenantContext: TenantContext.Interface, tenantScopes: TenantScopes.Interface, store: GlobalKeyValueStore.Interface, decoratee: ScheduledActionInputHandler.Interface);
26
+ execute(inputCtx: InputContext<IScheduledActionEvent>, next: NextFunction): Promise<IScheduledActionResult>;
27
+ private findDueTime;
28
+ private takeClaim;
29
+ private holdAfterFailure;
30
+ }
31
+ export declare const RunScheduledActionOnceDecorator: typeof RunScheduledActionOnceDecoratorImpl & {
32
+ __abstraction: import("@webiny/di").Abstraction<import("@webiny/api-kernel-core/features/scheduledAction/ScheduledActionInputHandler").IScheduledActionInputHandler>;
33
+ };
34
+ export {};
@@ -0,0 +1,89 @@
1
+ import { hostname } from "node:os";
2
+ import { SCHEDULED_ACTION_EVENT_IDENTIFIER, ScheduledActionInputHandler } from "@webiny/api-kernel-core";
3
+ import { GetTenantByIdUseCase, TenantScopes } from "@webiny/api-core/exports/api/tenancy.js";
4
+ import { TenantContext } from "@webiny/api-core/features/tenancy/TenantContext/index.js";
5
+ import { IdentityContext } from "@webiny/api-core/exports/api/security.js";
6
+ import { GlobalKeyValueStore, KeyValueConditionFailedError } from "@webiny/api-core/features/keyValueStore/index.js";
7
+ import { GetScheduledActionUseCase } from "@webiny/api-scheduler/features/GetScheduledAction/index.js";
8
+ const CLAIM_SCOPE = "scheduler/claims";
9
+ const RUN_CLAIM_MS = 600000;
10
+ const RETRY_AFTER_FAILURE_MS = 86400000;
11
+ const EARLY_TOLERANCE_MS = 1000;
12
+ const SUCCESS = {
13
+ success: true
14
+ };
15
+ const PROCESS_NAME = `${hostname()}:${process.pid}`;
16
+ const expiresIn = (ms)=>{
17
+ const expiresAtMs = Date.now() + ms;
18
+ return new Date(expiresAtMs);
19
+ };
20
+ class RunScheduledActionOnceDecoratorImpl {
21
+ constructor(getTenantById, tenantContext, tenantScopes, store, decoratee){
22
+ this.getTenantById = getTenantById;
23
+ this.tenantContext = tenantContext;
24
+ this.tenantScopes = tenantScopes;
25
+ this.store = store;
26
+ this.decoratee = decoratee;
27
+ }
28
+ async execute(inputCtx, next) {
29
+ const input = inputCtx.input[SCHEDULED_ACTION_EVENT_IDENTIFIER];
30
+ const tenantId = input.tenant || this.tenantContext.getTenant().id;
31
+ const dueAt = await this.findDueTime(tenantId, input);
32
+ if (!dueAt) return SUCCESS;
33
+ if (dueAt.getTime() > Date.now() + EARLY_TOLERANCE_MS) return SUCCESS;
34
+ const key = `${tenantId}/${input.id}/${dueAt.toISOString()}`;
35
+ const claimed = await this.takeClaim(key);
36
+ if (!claimed) return SUCCESS;
37
+ try {
38
+ return await this.decoratee.execute(inputCtx, next);
39
+ } catch (error) {
40
+ await this.holdAfterFailure(key);
41
+ throw error;
42
+ }
43
+ }
44
+ async findDueTime(tenantId, input) {
45
+ const tenantResult = await this.getTenantById.execute(tenantId);
46
+ if (tenantResult.isFail()) throw tenantResult.error;
47
+ return this.tenantScopes.withTenant(tenantResult.value, (scope)=>scope.resolve(IdentityContext).withoutAuthorization(async ()=>{
48
+ const result = await scope.resolve(GetScheduledActionUseCase).execute({
49
+ id: input.id,
50
+ namespace: input.namespace
51
+ });
52
+ if (result.isOk()) return result.value.scheduledFor;
53
+ if ("Scheduler/ScheduledAction/NotFound" === result.error.code) return null;
54
+ throw result.error;
55
+ }));
56
+ }
57
+ async takeClaim(key) {
58
+ const expiresAt = expiresIn(RUN_CLAIM_MS);
59
+ const result = await this.store.set(key, PROCESS_NAME, {
60
+ scope: CLAIM_SCOPE,
61
+ expiresAt,
62
+ condition: {
63
+ exists: false
64
+ }
65
+ });
66
+ if (result.isOk()) return true;
67
+ if (result.error instanceof KeyValueConditionFailedError) return false;
68
+ throw result.error;
69
+ }
70
+ async holdAfterFailure(key) {
71
+ const expiresAt = expiresIn(RETRY_AFTER_FAILURE_MS);
72
+ await this.store.set(key, PROCESS_NAME, {
73
+ scope: CLAIM_SCOPE,
74
+ expiresAt
75
+ });
76
+ }
77
+ }
78
+ const RunScheduledActionOnceDecorator = ScheduledActionInputHandler.createDecorator({
79
+ decorator: RunScheduledActionOnceDecoratorImpl,
80
+ dependencies: [
81
+ GetTenantByIdUseCase,
82
+ TenantContext,
83
+ TenantScopes,
84
+ GlobalKeyValueStore
85
+ ]
86
+ });
87
+ export { RunScheduledActionOnceDecorator };
88
+
89
+ //# sourceMappingURL=RunScheduledActionOnceDecorator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler/RunScheduledActionOnceDecorator.js","sources":["../../src/scheduler/RunScheduledActionOnceDecorator.ts"],"sourcesContent":["import { hostname } from \"node:os\";\nimport { ScheduledActionInputHandler } from \"@webiny/api-kernel-core\";\nimport { SCHEDULED_ACTION_EVENT_IDENTIFIER } from \"@webiny/api-kernel-core\";\nimport type { IScheduledActionEvent } from \"@webiny/api-kernel-core\";\nimport type { IScheduledActionEventPayload } from \"@webiny/api-kernel-core\";\nimport type { IScheduledActionResult } from \"@webiny/api-kernel-core\";\nimport type { InputContext } from \"@webiny/api-kernel-core\";\nimport type { NextFunction } from \"@webiny/api-kernel-core\";\nimport { GetTenantByIdUseCase, TenantScopes } from \"@webiny/api-core/exports/api/tenancy.js\";\nimport { TenantContext } from \"@webiny/api-core/features/tenancy/TenantContext/index.js\";\nimport { IdentityContext } from \"@webiny/api-core/exports/api/security.js\";\nimport { GlobalKeyValueStore } from \"@webiny/api-core/features/keyValueStore/index.js\";\nimport { KeyValueConditionFailedError } from \"@webiny/api-core/features/keyValueStore/index.js\";\nimport { GetScheduledActionUseCase } from \"@webiny/api-scheduler/features/GetScheduledAction/index.js\";\n\nconst CLAIM_SCOPE = \"scheduler/claims\";\n\n/*\n * How long one run of an action holds its claim. Long enough for any action to finish; a run that\n * succeeds deletes the action, so the claim only matters while it runs.\n */\nconst RUN_CLAIM_MS = 10 * 60_000;\n\n// After a failed run, other processes leave the action alone this long before trying it again.\nconst RETRY_AFTER_FAILURE_MS = 24 * 60 * 60_000;\n\n/*\n * Timers fire slightly early or late; an action due within this much is run. One due later than that\n * belongs to a timer another process moved, and this firing is stale.\n */\nconst EARLY_TOLERANCE_MS = 1000;\n\nconst SUCCESS: IScheduledActionResult = { success: true };\n\n// Names this process in the claims it writes, for whoever reads them in the database.\nconst PROCESS_NAME = `${hostname()}:${process.pid}`;\n\nconst expiresIn = (ms: number): Date => {\n const expiresAtMs = Date.now() + ms;\n return new Date(expiresAtMs);\n};\n\n/**\n * Runs a fired scheduled action at most once across all processes. Every standalone process holds its\n * own timers for the same actions, so the same action fires in each of them; on AWS, EventBridge\n * fires it once and this decorator is not registered.\n *\n * Before the action runs, it is read again. Gone means it was cancelled or already ran. Due later\n * means another process moved it and this timer is stale. Otherwise the process takes a claim on the\n * action at its due time, and only the process that gets it runs the action. Rescheduling changes\n * the due time, so a moved action gets a fresh claim.\n */\nclass RunScheduledActionOnceDecoratorImpl implements ScheduledActionInputHandler.Interface {\n public constructor(\n private readonly getTenantById: GetTenantByIdUseCase.Interface,\n private readonly tenantContext: TenantContext.Interface,\n private readonly tenantScopes: TenantScopes.Interface,\n private readonly store: GlobalKeyValueStore.Interface,\n private readonly decoratee: ScheduledActionInputHandler.Interface\n ) {}\n\n public async execute(\n inputCtx: InputContext<IScheduledActionEvent>,\n next: NextFunction\n ): Promise<IScheduledActionResult> {\n const input = inputCtx.input[SCHEDULED_ACTION_EVENT_IDENTIFIER];\n const tenantId = input.tenant || this.tenantContext.getTenant().id;\n\n const dueAt = await this.findDueTime(tenantId, input);\n if (!dueAt) {\n return SUCCESS;\n }\n if (dueAt.getTime() > Date.now() + EARLY_TOLERANCE_MS) {\n return SUCCESS;\n }\n\n const key = `${tenantId}/${input.id}/${dueAt.toISOString()}`;\n const claimed = await this.takeClaim(key);\n if (!claimed) {\n return SUCCESS;\n }\n\n try {\n return await this.decoratee.execute(inputCtx, next);\n } catch (error) {\n await this.holdAfterFailure(key);\n throw error;\n }\n }\n\n // The action's due time, or `null` when it no longer exists.\n private async findDueTime(\n tenantId: string,\n input: IScheduledActionEventPayload\n ): Promise<Date | null> {\n const tenantResult = await this.getTenantById.execute(tenantId);\n if (tenantResult.isFail()) {\n throw tenantResult.error;\n }\n\n return this.tenantScopes.withTenant(tenantResult.value, scope => {\n // GetScheduledAction checks permissions, and nobody is signed in when a timer fires.\n return scope.resolve(IdentityContext).withoutAuthorization(async () => {\n const result = await scope.resolve(GetScheduledActionUseCase).execute({\n id: input.id,\n namespace: input.namespace\n });\n if (result.isOk()) {\n return result.value.scheduledFor;\n }\n if (result.error.code === \"Scheduler/ScheduledAction/NotFound\") {\n return null;\n }\n throw result.error;\n });\n });\n }\n\n private async takeClaim(key: string): Promise<boolean> {\n const expiresAt = expiresIn(RUN_CLAIM_MS);\n const result = await this.store.set(key, PROCESS_NAME, {\n scope: CLAIM_SCOPE,\n expiresAt,\n condition: { exists: false }\n });\n if (result.isOk()) {\n return true;\n }\n if (result.error instanceof KeyValueConditionFailedError) {\n return false;\n }\n throw result.error;\n }\n\n /*\n * The action stays stored after a failed run, with the error on it, and every process's\n * recovery fires overdue actions each minute. Holding the claim keeps that to one retry a day.\n */\n private async holdAfterFailure(key: string): Promise<void> {\n const expiresAt = expiresIn(RETRY_AFTER_FAILURE_MS);\n await this.store.set(key, PROCESS_NAME, { scope: CLAIM_SCOPE, expiresAt });\n }\n}\n\nexport const RunScheduledActionOnceDecorator = ScheduledActionInputHandler.createDecorator({\n decorator: RunScheduledActionOnceDecoratorImpl,\n dependencies: [GetTenantByIdUseCase, TenantContext, TenantScopes, GlobalKeyValueStore]\n});\n"],"names":["CLAIM_SCOPE","RUN_CLAIM_MS","RETRY_AFTER_FAILURE_MS","EARLY_TOLERANCE_MS","SUCCESS","PROCESS_NAME","hostname","process","expiresIn","ms","expiresAtMs","Date","RunScheduledActionOnceDecoratorImpl","getTenantById","tenantContext","tenantScopes","store","decoratee","inputCtx","next","input","SCHEDULED_ACTION_EVENT_IDENTIFIER","tenantId","dueAt","key","claimed","error","tenantResult","scope","IdentityContext","result","GetScheduledActionUseCase","expiresAt","KeyValueConditionFailedError","RunScheduledActionOnceDecorator","ScheduledActionInputHandler","GetTenantByIdUseCase","TenantContext","TenantScopes","GlobalKeyValueStore"],"mappings":";;;;;;;AAeA,MAAMA,cAAc;AAMpB,MAAMC,eAAe;AAGrB,MAAMC,yBAAyB;AAM/B,MAAMC,qBAAqB;AAE3B,MAAMC,UAAkC;IAAE,SAAS;AAAK;AAGxD,MAAMC,eAAe,GAAGC,WAAW,CAAC,EAAEC,QAAQ,GAAG,EAAE;AAEnD,MAAMC,YAAY,CAACC;IACf,MAAMC,cAAcC,KAAK,GAAG,KAAKF;IACjC,OAAO,IAAIE,KAAKD;AACpB;AAYA,MAAME;IACF,YACqBC,aAA6C,EAC7CC,aAAsC,EACtCC,YAAoC,EACpCC,KAAoC,EACpCC,SAAgD,CACnE;aALmBJ,aAAa,GAAbA;aACAC,aAAa,GAAbA;aACAC,YAAY,GAAZA;aACAC,KAAK,GAALA;aACAC,SAAS,GAATA;IAClB;IAEH,MAAa,QACTC,QAA6C,EAC7CC,IAAkB,EACa;QAC/B,MAAMC,QAAQF,SAAS,KAAK,CAACG,kCAAkC;QAC/D,MAAMC,WAAWF,MAAM,MAAM,IAAI,IAAI,CAAC,aAAa,CAAC,SAAS,GAAG,EAAE;QAElE,MAAMG,QAAQ,MAAM,IAAI,CAAC,WAAW,CAACD,UAAUF;QAC/C,IAAI,CAACG,OACD,OAAOnB;QAEX,IAAImB,MAAM,OAAO,KAAKZ,KAAK,GAAG,KAAKR,oBAC/B,OAAOC;QAGX,MAAMoB,MAAM,GAAGF,SAAS,CAAC,EAAEF,MAAM,EAAE,CAAC,CAAC,EAAEG,MAAM,WAAW,IAAI;QAC5D,MAAME,UAAU,MAAM,IAAI,CAAC,SAAS,CAACD;QACrC,IAAI,CAACC,SACD,OAAOrB;QAGX,IAAI;YACA,OAAO,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,CAACc,UAAUC;QAClD,EAAE,OAAOO,OAAO;YACZ,MAAM,IAAI,CAAC,gBAAgB,CAACF;YAC5B,MAAME;QACV;IACJ;IAGA,MAAc,YACVJ,QAAgB,EAChBF,KAAmC,EACf;QACpB,MAAMO,eAAe,MAAM,IAAI,CAAC,aAAa,CAAC,OAAO,CAACL;QACtD,IAAIK,aAAa,MAAM,IACnB,MAAMA,aAAa,KAAK;QAG5B,OAAO,IAAI,CAAC,YAAY,CAAC,UAAU,CAACA,aAAa,KAAK,EAAEC,CAAAA,QAE7CA,MAAM,OAAO,CAACC,iBAAiB,oBAAoB,CAAC;gBACvD,MAAMC,SAAS,MAAMF,MAAM,OAAO,CAACG,2BAA2B,OAAO,CAAC;oBAClE,IAAIX,MAAM,EAAE;oBACZ,WAAWA,MAAM,SAAS;gBAC9B;gBACA,IAAIU,OAAO,IAAI,IACX,OAAOA,OAAO,KAAK,CAAC,YAAY;gBAEpC,IAAIA,AAAsB,yCAAtBA,OAAO,KAAK,CAAC,IAAI,EACjB,OAAO;gBAEX,MAAMA,OAAO,KAAK;YACtB;IAER;IAEA,MAAc,UAAUN,GAAW,EAAoB;QACnD,MAAMQ,YAAYxB,UAAUP;QAC5B,MAAM6B,SAAS,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAACN,KAAKnB,cAAc;YACnD,OAAOL;YACPgC;YACA,WAAW;gBAAE,QAAQ;YAAM;QAC/B;QACA,IAAIF,OAAO,IAAI,IACX,OAAO;QAEX,IAAIA,OAAO,KAAK,YAAYG,8BACxB,OAAO;QAEX,MAAMH,OAAO,KAAK;IACtB;IAMA,MAAc,iBAAiBN,GAAW,EAAiB;QACvD,MAAMQ,YAAYxB,UAAUN;QAC5B,MAAM,IAAI,CAAC,KAAK,CAAC,GAAG,CAACsB,KAAKnB,cAAc;YAAE,OAAOL;YAAagC;QAAU;IAC5E;AACJ;AAEO,MAAME,kCAAkCC,4BAA4B,eAAe,CAAC;IACvF,WAAWvB;IACX,cAAc;QAACwB;QAAsBC;QAAeC;QAAcC;KAAoB;AAC1F"}
@@ -0,0 +1,14 @@
1
+ import { Abstraction } from "@webiny/di";
2
+ import type { InputHandler } from "@webiny/api-kernel-core";
3
+ import type { IScheduledActionRecoverEvent } from "./ScheduledActionRecoverInputType.js";
4
+ export interface IScheduledActionRecoverResult {
5
+ recovered: number;
6
+ failedTenants: string[];
7
+ }
8
+ export interface IScheduledActionRecoverInputHandler extends InputHandler.Interface<IScheduledActionRecoverEvent, IScheduledActionRecoverResult> {
9
+ }
10
+ export declare const ScheduledActionRecoverInputHandler: Abstraction<IScheduledActionRecoverInputHandler>;
11
+ export declare namespace ScheduledActionRecoverInputHandler {
12
+ type Interface = IScheduledActionRecoverInputHandler;
13
+ type Result = IScheduledActionRecoverResult;
14
+ }
@@ -0,0 +1,5 @@
1
+ import { Abstraction } from "@webiny/di";
2
+ const ScheduledActionRecoverInputHandler = new Abstraction("ScheduledActionRecoverInputHandler");
3
+ export { ScheduledActionRecoverInputHandler };
4
+
5
+ //# sourceMappingURL=ScheduledActionRecoverInputHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler/ScheduledActionRecoverInputHandler.js","sources":["../../src/scheduler/ScheduledActionRecoverInputHandler.ts"],"sourcesContent":["import { Abstraction } from \"@webiny/di\";\nimport type { InputHandler } from \"@webiny/api-kernel-core\";\nimport type { IScheduledActionRecoverEvent } from \"./ScheduledActionRecoverInputType.js\";\n\nexport interface IScheduledActionRecoverResult {\n recovered: number;\n // Tenants whose schedules could not be read or re-armed; the others were still recovered.\n failedTenants: string[];\n}\n\nexport interface IScheduledActionRecoverInputHandler extends InputHandler.Interface<\n IScheduledActionRecoverEvent,\n IScheduledActionRecoverResult\n> {}\n\nexport const ScheduledActionRecoverInputHandler =\n new Abstraction<IScheduledActionRecoverInputHandler>(\"ScheduledActionRecoverInputHandler\");\n\nexport namespace ScheduledActionRecoverInputHandler {\n export type Interface = IScheduledActionRecoverInputHandler;\n export type Result = IScheduledActionRecoverResult;\n}\n"],"names":["ScheduledActionRecoverInputHandler","Abstraction"],"mappings":";AAeO,MAAMA,qCACT,IAAIC,YAAiD"}
@@ -0,0 +1,17 @@
1
+ import { InputType } from "@webiny/api-kernel-core";
2
+ export declare const SCHEDULED_ACTION_RECOVER_EVENT_IDENTIFIER = "WebinyScheduledActionRecover";
3
+ /**
4
+ * Asks the scheduler to re-arm the root tenant's pending scheduled actions. Dispatched once at
5
+ * boot: the scheduler keeps its timers in memory, so a restart loses every schedule that has not fired yet.
6
+ */
7
+ export interface IScheduledActionRecoverEvent {
8
+ [SCHEDULED_ACTION_RECOVER_EVENT_IDENTIFIER]: true;
9
+ }
10
+ declare class ScheduledActionRecoverInputTypeImpl implements InputType.Interface<IScheduledActionRecoverEvent> {
11
+ matches(input: any): input is IScheduledActionRecoverEvent;
12
+ getHandlerAbstraction(): import("@webiny/di").Abstraction<import("./ScheduledActionRecoverInputHandler.js").IScheduledActionRecoverInputHandler>;
13
+ }
14
+ export declare const ScheduledActionRecoverInputType: typeof ScheduledActionRecoverInputTypeImpl & {
15
+ __abstraction: import("@webiny/di").Abstraction<import("@webiny/api-kernel-core/features/kernel/InputType.js").IInputType<any>>;
16
+ };
17
+ export {};
@@ -0,0 +1,18 @@
1
+ import { InputType } from "@webiny/api-kernel-core";
2
+ import { ScheduledActionRecoverInputHandler } from "./ScheduledActionRecoverInputHandler.js";
3
+ const SCHEDULED_ACTION_RECOVER_EVENT_IDENTIFIER = "WebinyScheduledActionRecover";
4
+ class ScheduledActionRecoverInputTypeImpl {
5
+ matches(input) {
6
+ return input?.[SCHEDULED_ACTION_RECOVER_EVENT_IDENTIFIER] === true;
7
+ }
8
+ getHandlerAbstraction() {
9
+ return ScheduledActionRecoverInputHandler;
10
+ }
11
+ }
12
+ const ScheduledActionRecoverInputType = InputType.createImplementation({
13
+ implementation: ScheduledActionRecoverInputTypeImpl,
14
+ dependencies: []
15
+ });
16
+ export { SCHEDULED_ACTION_RECOVER_EVENT_IDENTIFIER, ScheduledActionRecoverInputType };
17
+
18
+ //# sourceMappingURL=ScheduledActionRecoverInputType.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler/ScheduledActionRecoverInputType.js","sources":["../../src/scheduler/ScheduledActionRecoverInputType.ts"],"sourcesContent":["import { InputType } from \"@webiny/api-kernel-core\";\n\nimport { ScheduledActionRecoverInputHandler } from \"./ScheduledActionRecoverInputHandler.js\";\n\nexport const SCHEDULED_ACTION_RECOVER_EVENT_IDENTIFIER = \"WebinyScheduledActionRecover\";\n\n/**\n * Asks the scheduler to re-arm the root tenant's pending scheduled actions. Dispatched once at\n * boot: the scheduler keeps its timers in memory, so a restart loses every schedule that has not fired yet.\n */\nexport interface IScheduledActionRecoverEvent {\n [SCHEDULED_ACTION_RECOVER_EVENT_IDENTIFIER]: true;\n}\n\nclass ScheduledActionRecoverInputTypeImpl implements InputType.Interface<IScheduledActionRecoverEvent> {\n matches(input: any): input is IScheduledActionRecoverEvent {\n return input?.[SCHEDULED_ACTION_RECOVER_EVENT_IDENTIFIER] === true;\n }\n\n getHandlerAbstraction() {\n return ScheduledActionRecoverInputHandler;\n }\n}\n\nexport const ScheduledActionRecoverInputType = InputType.createImplementation({\n implementation: ScheduledActionRecoverInputTypeImpl,\n dependencies: []\n});\n"],"names":["SCHEDULED_ACTION_RECOVER_EVENT_IDENTIFIER","ScheduledActionRecoverInputTypeImpl","input","ScheduledActionRecoverInputHandler","ScheduledActionRecoverInputType","InputType"],"mappings":";;AAIO,MAAMA,4CAA4C;AAUzD,MAAMC;IACF,QAAQC,KAAU,EAAyC;QACvD,OAAOA,OAAO,CAACF,0CAA0C,KAAK;IAClE;IAEA,wBAAwB;QACpB,OAAOG;IACX;AACJ;AAEO,MAAMC,kCAAkCC,UAAU,oBAAoB,CAAC;IAC1E,gBAAgBJ;IAChB,cAAc,EAAE;AACpB"}
@@ -0,0 +1,10 @@
1
+ import type { TimerSchedulerService } from "@webiny/api-scheduler-standalone";
2
+ /**
3
+ * DI token for the single root scheduler instance. Distinct from the `SchedulerService`
4
+ * abstraction (create/update/delete/exists): this exposes the concrete service so the recover route +
5
+ * boot step can call `recover()`, which isn't part of the SchedulerService contract.
6
+ */
7
+ export declare const SchedulerSingleton: import("@webiny/di").Abstraction<TimerSchedulerService>;
8
+ export declare namespace SchedulerSingleton {
9
+ type Interface = TimerSchedulerService;
10
+ }
@@ -0,0 +1,5 @@
1
+ import { createAbstraction } from "@webiny/feature/api";
2
+ const SchedulerSingleton = createAbstraction("SchedulerServer/Singleton");
3
+ export { SchedulerSingleton };
4
+
5
+ //# sourceMappingURL=SchedulerSingleton.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler/abstractions/SchedulerSingleton.js","sources":["../../../src/scheduler/abstractions/SchedulerSingleton.ts"],"sourcesContent":["import { createAbstraction } from \"@webiny/feature/api\";\nimport type { TimerSchedulerService } from \"@webiny/api-scheduler-standalone\";\n\n/**\n * DI token for the single root scheduler instance. Distinct from the `SchedulerService`\n * abstraction (create/update/delete/exists): this exposes the concrete service so the recover route +\n * boot step can call `recover()`, which isn't part of the SchedulerService contract.\n */\nexport const SchedulerSingleton = createAbstraction<TimerSchedulerService>(\n \"SchedulerServer/Singleton\"\n);\n\nexport namespace SchedulerSingleton {\n export type Interface = TimerSchedulerService;\n}\n"],"names":["SchedulerSingleton","createAbstraction"],"mappings":";AAQO,MAAMA,qBAAqBC,kBAC9B"}
@@ -0,0 +1,25 @@
1
+ import type { Container } from "@webiny/di";
2
+ /**
3
+ * ROOT wiring for the standalone (in-process timers) scheduler. Unlike AWS (per-request EventBridge
4
+ * binding), the server holds ONE long-lived scheduler for all tenants, started once at boot —
5
+ * the counterpart of the WebSockets connection manager. Registered as `SchedulerService` so
6
+ * per-request create/update/delete (during GraphQL mutations) manipulate that single live timer set.
7
+ *
8
+ * When a timer fires (outside any request), the singleton dispatches a `ScheduledActionEvent`. The
9
+ * handler is the one AWS runs for an EventBridge Scheduler invocation, in a fresh request container.
10
+ */
11
+ export declare function registerSchedulerServer(rootContainer: Container): void;
12
+ /**
13
+ * Boot step (onServer): start the timers, then re-arm the persisted schedules, at boot and then every
14
+ * minute. Recovery runs in the background: it executes overdue actions one by one, and the server
15
+ * should not wait for that before it starts listening. A failure is logged and never crashes startup.
16
+ *
17
+ * Running it every minute is what makes several processes work. Each holds its own timers, so an
18
+ * action scheduled or moved through one process reaches the others' timers within a minute, and an
19
+ * action whose process died is still fired, as an overdue one. `RunScheduledActionOnceDecorator`
20
+ * keeps the processes from all running it. Runs never overlap.
21
+ *
22
+ * Returns a function for the shutdown: it stops the timers and recovery, then waits up to
23
+ * `timeoutMs` for actions already running.
24
+ */
25
+ export declare function startSchedulerServer(rootContainer: Container): Promise<(timeoutMs: number) => Promise<void>>;