@webiny/api-scheduler-standalone 0.0.0-unstable.5387e5f7c8 → 0.0.0-unstable.a85ef555b2

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.
@@ -8,30 +8,47 @@ export interface IPendingAction {
8
8
  }
9
9
  export interface IBreeSchedulerServiceParams {
10
10
  logger: Logger.Interface;
11
- onTrigger: (id: string, namespace: string, tenant: string) => Promise<void>;
11
+ runAction: (id: string, namespace: string, tenant: string) => Promise<void>;
12
12
  }
13
13
  /**
14
- * One-shot bree job per scheduled action — mirrors EventBridge behaviour. This is a single-process,
15
- * long-lived root SINGLETON (started once at boot): it holds the live timers for ALL tenants, so each
16
- * job records its own tenant and `onTrigger` is fired with it (the trigger runs outside any request,
17
- * so the tenant can't be read from a request context).
14
+ * One-shot bree job per scheduled action — mirrors EventBridge behaviour. This is a long-lived root
15
+ * SINGLETON (started once at boot): it holds this process's live timers for ALL tenants, so each job
16
+ * records its own tenant and `runAction` is called with it (the action runs outside any request, so
17
+ * the tenant can't be read from a request context).
18
+ *
19
+ * With several processes, each holds its own timers for the same actions: `recover` runs again on a
20
+ * timer to arm what this process has not seen, and whoever supplies `runAction` makes sure an action
21
+ * runs once.
18
22
  */
19
23
  export declare class BreeSchedulerService implements SchedulerService.Interface {
20
24
  private readonly bree;
21
25
  private readonly jobs;
22
26
  private readonly logger;
23
- private readonly onTrigger;
27
+ private readonly runAction;
28
+ private readonly running;
29
+ private stopped;
24
30
  constructor(params: IBreeSchedulerServiceParams);
25
31
  start(): Promise<void>;
26
32
  stop(): Promise<void>;
33
+ /**
34
+ * Resolves once every trigger already started has finished, or after `timeoutMs`. Call it after
35
+ * `stop`, which keeps new ones from starting.
36
+ */
37
+ waitForRunning(timeoutMs: number): Promise<void>;
27
38
  create(params: ISchedulerServiceCreateParams): Promise<void>;
28
39
  update(params: ISchedulerServiceUpdateParams): Promise<void>;
29
40
  delete(params: SchedulerService.DeleteParams): Promise<void>;
30
41
  exists(params: SchedulerService.ExistsParams): Promise<boolean>;
31
42
  /**
32
- * (Re)arm timers for a batch of persisted pending actions — called at boot (per tenant) to restore
33
- * schedules after a restart. Overdue actions fire immediately.
43
+ * (Re)arm timers for a batch of persisted pending actions. Runs at boot (per tenant) to restore
44
+ * schedules after a restart, and then on a timer, so this process also arms actions another
45
+ * process scheduled or moved. Overdue actions fire immediately.
46
+ *
47
+ * An action already armed for the same time is left as it is; one armed for a different time was
48
+ * moved by another process and is re-armed.
34
49
  */
35
50
  recover(pendingActions: IPendingAction[]): Promise<void>;
51
+ private arm;
52
+ private trigger;
36
53
  private safeRemove;
37
54
  }
@@ -5,8 +5,10 @@ const workerPath = fileURLToPath(new URL("./jobs/pollWorker.js", import.meta.url
5
5
  class BreeSchedulerService {
6
6
  constructor(params){
7
7
  this.jobs = new Map();
8
+ this.running = new Set();
9
+ this.stopped = false;
8
10
  this.logger = params.logger;
9
- this.onTrigger = params.onTrigger;
11
+ this.runAction = params.runAction;
10
12
  this.bree = new bree({
11
13
  root: false,
12
14
  jobs: [],
@@ -15,16 +17,32 @@ class BreeSchedulerService {
15
17
  const job = this.jobs.get(name);
16
18
  if (!job) return;
17
19
  this.jobs.delete(name);
18
- await this.onTrigger(name, job.namespace, job.tenant);
20
+ await this.trigger(name, job.namespace, job.tenant);
19
21
  }
20
22
  });
21
23
  }
22
24
  async start() {
25
+ this.stopped = false;
23
26
  await this.bree.start();
24
27
  }
25
28
  async stop() {
29
+ this.stopped = true;
26
30
  await this.bree.stop();
27
31
  }
32
+ async waitForRunning(timeoutMs) {
33
+ let timer;
34
+ const timeout = new Promise((resolve)=>{
35
+ timer = setTimeout(resolve, timeoutMs);
36
+ });
37
+ const running = Promise.allSettled([
38
+ ...this.running
39
+ ]);
40
+ await Promise.race([
41
+ running,
42
+ timeout
43
+ ]);
44
+ clearTimeout(timer);
45
+ }
28
46
  async create(params) {
29
47
  const { id, namespace, scheduleFor, tenant } = params;
30
48
  if (scheduleFor <= new Date()) throw new WebinyError(`Cannot create a schedule for "${id}" with date in the past`, "INVALID_SCHEDULE_DATE", {
@@ -37,16 +55,11 @@ class BreeSchedulerService {
37
55
  tenant
38
56
  });
39
57
  if (exists) return this.update(params);
40
- this.jobs.set(id, {
58
+ await this.arm(id, {
41
59
  namespace,
42
- tenant
60
+ tenant,
61
+ scheduledFor: scheduleFor
43
62
  });
44
- await this.bree.add({
45
- name: id,
46
- date: scheduleFor,
47
- path: workerPath
48
- });
49
- await this.bree.start(id);
50
63
  }
51
64
  async update(params) {
52
65
  const { id, scheduleFor } = params;
@@ -70,20 +83,40 @@ class BreeSchedulerService {
70
83
  async recover(pendingActions) {
71
84
  const now = new Date();
72
85
  for (const action of pendingActions){
86
+ if (this.stopped) return;
73
87
  if (action.scheduledFor <= now) {
74
- await this.onTrigger(action.id, action.namespace, action.tenant);
88
+ await this.trigger(action.id, action.namespace, action.tenant);
75
89
  continue;
76
90
  }
77
- this.jobs.set(action.id, {
78
- namespace: action.namespace,
79
- tenant: action.tenant
80
- });
81
- await this.bree.add({
82
- name: action.id,
83
- date: action.scheduledFor,
84
- path: workerPath
85
- });
86
- await this.bree.start(action.id);
91
+ const armed = this.jobs.get(action.id);
92
+ if (!armed || armed.scheduledFor.getTime() !== action.scheduledFor.getTime()) {
93
+ if (armed) await this.safeRemove(action.id);
94
+ await this.arm(action.id, {
95
+ namespace: action.namespace,
96
+ tenant: action.tenant,
97
+ scheduledFor: action.scheduledFor
98
+ });
99
+ }
100
+ }
101
+ }
102
+ async arm(id, job) {
103
+ if (this.stopped) return;
104
+ this.jobs.set(id, job);
105
+ await this.bree.add({
106
+ name: id,
107
+ date: job.scheduledFor,
108
+ path: workerPath
109
+ });
110
+ await this.bree.start(id);
111
+ }
112
+ async trigger(id, namespace, tenant) {
113
+ if (this.stopped) return;
114
+ const triggered = this.runAction(id, namespace, tenant);
115
+ this.running.add(triggered);
116
+ try {
117
+ await triggered;
118
+ } finally{
119
+ this.running.delete(triggered);
87
120
  }
88
121
  }
89
122
  async safeRemove(id) {
@@ -1 +1 @@
1
- {"version":3,"file":"BreeSchedulerService.js","sources":["../src/BreeSchedulerService.ts"],"sourcesContent":["import Bree from \"bree\";\nimport { fileURLToPath } from \"node:url\";\nimport { WebinyError } from \"@webiny/error\";\nimport {\n type ISchedulerServiceCreateParams,\n type ISchedulerServiceUpdateParams,\n SchedulerService\n} from \"@webiny/api-scheduler/shared/abstractions.js\";\nimport type { Logger } from \"@webiny/api-core/features/logger/abstractions.js\";\n\n// bree loads this file at runtime via `new Worker(path)`. Referencing it through `new URL` (rather\n// than path.join) makes the app bundler emit pollWorker into build/ as an asset, resolved relative\n// to this module (the server build sets assetPrefix \"auto\"); fileURLToPath then hands bree a plain\n// path string. Un-bundled (dev), import.meta.url is real and it resolves to the dist/jobs file.\nconst workerPath = fileURLToPath(new URL(\"./jobs/pollWorker.js\", import.meta.url));\n\nexport interface IPendingAction {\n id: string;\n namespace: string;\n tenant: string;\n scheduledFor: Date;\n}\n\ninterface IScheduledJob {\n namespace: string;\n tenant: string;\n}\n\nexport interface IBreeSchedulerServiceParams {\n logger: Logger.Interface;\n onTrigger: (id: string, namespace: string, tenant: string) => Promise<void>;\n}\n\n/**\n * One-shot bree job per scheduled action — mirrors EventBridge behaviour. This is a single-process,\n * long-lived root SINGLETON (started once at boot): it holds the live timers for ALL tenants, so each\n * job records its own tenant and `onTrigger` is fired with it (the trigger runs outside any request,\n * so the tenant can't be read from a request context).\n */\nexport class BreeSchedulerService implements SchedulerService.Interface {\n private readonly bree;\n private readonly jobs = new Map<string, IScheduledJob>();\n private readonly logger;\n private readonly onTrigger;\n\n public constructor(params: IBreeSchedulerServiceParams) {\n this.logger = params.logger;\n this.onTrigger = params.onTrigger;\n\n this.bree = new Bree({\n root: false,\n jobs: [],\n logger: false,\n workerMessageHandler: async ({ name }) => {\n const job = this.jobs.get(name);\n if (!job) {\n return;\n }\n\n this.jobs.delete(name);\n await this.onTrigger(name, job.namespace, job.tenant);\n }\n });\n }\n\n public async start(): Promise<void> {\n await this.bree.start();\n }\n\n public async stop(): Promise<void> {\n await this.bree.stop();\n }\n\n public async create(params: ISchedulerServiceCreateParams): Promise<void> {\n const { id, namespace, scheduleFor, tenant } = params;\n\n if (scheduleFor <= new Date()) {\n throw new WebinyError(\n `Cannot create a schedule for \"${id}\" with date in the past`,\n \"INVALID_SCHEDULE_DATE\",\n { scheduleFor, id }\n );\n }\n\n const exists = await this.exists({\n id,\n namespace,\n tenant\n });\n if (exists) {\n return this.update(params);\n }\n\n this.jobs.set(id, { namespace, tenant });\n\n await this.bree.add({\n name: id,\n date: scheduleFor,\n path: workerPath\n });\n\n await this.bree.start(id);\n }\n\n public async update(params: ISchedulerServiceUpdateParams): Promise<void> {\n const { id, scheduleFor } = params;\n\n if (scheduleFor <= new Date()) {\n throw new WebinyError(\n `Cannot update an existing schedule for \"${id}\" with date in the past`,\n \"INVALID_SCHEDULE_DATE\",\n { scheduleFor, id }\n );\n }\n\n await this.safeRemove(id);\n await this.create(params);\n }\n\n public async delete(params: SchedulerService.DeleteParams): Promise<void> {\n const { id } = params;\n const exists = await this.exists(params);\n if (!exists) {\n throw new WebinyError(`Cannot delete schedule \"${id}\" because it does not exist.`);\n }\n\n await this.safeRemove(id);\n }\n\n public async exists(params: SchedulerService.ExistsParams): Promise<boolean> {\n const { id } = params;\n return this.jobs.has(id);\n }\n\n /**\n * (Re)arm timers for a batch of persisted pending actions — called at boot (per tenant) to restore\n * schedules after a restart. Overdue actions fire immediately.\n */\n public async recover(pendingActions: IPendingAction[]): Promise<void> {\n const now = new Date();\n\n for (const action of pendingActions) {\n if (action.scheduledFor <= now) {\n /* Overdue — execute immediately. */\n await this.onTrigger(action.id, action.namespace, action.tenant);\n continue;\n }\n\n this.jobs.set(action.id, { namespace: action.namespace, tenant: action.tenant });\n\n await this.bree.add({\n name: action.id,\n date: action.scheduledFor,\n path: workerPath\n });\n\n await this.bree.start(action.id);\n }\n }\n\n private async safeRemove(id: string): Promise<void> {\n this.jobs.delete(id);\n\n try {\n await this.bree.stop(id);\n } catch {\n this.logger.debug(`Could not stop bree job \"${id}\" — it may have already fired.`);\n }\n\n try {\n await this.bree.remove(id);\n } catch {\n this.logger.debug(\n `Could not remove bree job \"${id}\" — it may have already been removed.`\n );\n }\n }\n}\n"],"names":["workerPath","fileURLToPath","URL","BreeSchedulerService","params","Map","Bree","name","job","id","namespace","scheduleFor","tenant","Date","WebinyError","exists","pendingActions","now","action"],"mappings":";;;AAcA,MAAMA,aAAaC,cAAc,IAAIC,IAAI,wBAAwB,YAAY,GAAG;AAyBzE,MAAMC;IAMT,YAAmBC,MAAmC,CAAE;aAJvC,IAAI,GAAG,IAAIC;QAKxB,IAAI,CAAC,MAAM,GAAGD,OAAO,MAAM;QAC3B,IAAI,CAAC,SAAS,GAAGA,OAAO,SAAS;QAEjC,IAAI,CAAC,IAAI,GAAG,IAAIE,KAAK;YACjB,MAAM;YACN,MAAM,EAAE;YACR,QAAQ;YACR,sBAAsB,OAAO,EAAEC,IAAI,EAAE;gBACjC,MAAMC,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAACD;gBAC1B,IAAI,CAACC,KACD;gBAGJ,IAAI,CAAC,IAAI,CAAC,MAAM,CAACD;gBACjB,MAAM,IAAI,CAAC,SAAS,CAACA,MAAMC,IAAI,SAAS,EAAEA,IAAI,MAAM;YACxD;QACJ;IACJ;IAEA,MAAa,QAAuB;QAChC,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK;IACzB;IAEA,MAAa,OAAsB;QAC/B,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI;IACxB;IAEA,MAAa,OAAOJ,MAAqC,EAAiB;QACtE,MAAM,EAAEK,EAAE,EAAEC,SAAS,EAAEC,WAAW,EAAEC,MAAM,EAAE,GAAGR;QAE/C,IAAIO,eAAe,IAAIE,QACnB,MAAM,IAAIC,YACN,CAAC,8BAA8B,EAAEL,GAAG,uBAAuB,CAAC,EAC5D,yBACA;YAAEE;YAAaF;QAAG;QAI1B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;YAEA,IAAI,CAAC,IAAI,CAAC,GAAG,CAACA,OAAO,EAAE,EAAE;gBAAE,WAAWA,OAAO,SAAS;gBAAE,QAAQA,OAAO,MAAM;YAAC;YAE9E,MAAM,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;gBAChB,MAAMA,OAAO,EAAE;gBACf,MAAMA,OAAO,YAAY;gBACzB,MAAMlB;YACV;YAEA,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,CAACkB,OAAO,EAAE;QACnC;IACJ;IAEA,MAAc,WAAWT,EAAU,EAAiB;QAChD,IAAI,CAAC,IAAI,CAAC,MAAM,CAACA;QAEjB,IAAI;YACA,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAACA;QACzB,EAAE,OAAM;YACJ,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,yBAAyB,EAAEA,GAAG,8BAA8B,CAAC;QACpF;QAEA,IAAI;YACA,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM,CAACA;QAC3B,EAAE,OAAM;YACJ,IAAI,CAAC,MAAM,CAAC,KAAK,CACb,CAAC,2BAA2B,EAAEA,GAAG,qCAAqC,CAAC;QAE/E;IACJ;AACJ"}
1
+ {"version":3,"file":"BreeSchedulerService.js","sources":["../src/BreeSchedulerService.ts"],"sourcesContent":["import Bree from \"bree\";\nimport { fileURLToPath } from \"node:url\";\nimport { WebinyError } from \"@webiny/error\";\nimport {\n type ISchedulerServiceCreateParams,\n type ISchedulerServiceUpdateParams,\n SchedulerService\n} from \"@webiny/api-scheduler/shared/abstractions.js\";\nimport type { Logger } from \"@webiny/api-core/features/logger/abstractions.js\";\n\n// bree loads this file at runtime via `new Worker(path)`. Referencing it through `new URL` (rather\n// than path.join) makes the app bundler emit pollWorker into build/ as an asset, resolved relative\n// to this module (the server build sets assetPrefix \"auto\"); fileURLToPath then hands bree a plain\n// path string. Un-bundled (dev), import.meta.url is real and it resolves to the dist/jobs file.\nconst workerPath = fileURLToPath(new URL(\"./jobs/pollWorker.js\", import.meta.url));\n\nexport interface IPendingAction {\n id: string;\n namespace: string;\n tenant: string;\n scheduledFor: Date;\n}\n\ninterface IScheduledJob {\n namespace: string;\n tenant: string;\n scheduledFor: Date;\n}\n\nexport interface IBreeSchedulerServiceParams {\n logger: Logger.Interface;\n runAction: (id: string, namespace: string, tenant: string) => Promise<void>;\n}\n\n/**\n * One-shot bree job per scheduled action — mirrors EventBridge behaviour. This is a long-lived root\n * SINGLETON (started once at boot): it holds this process's live timers for ALL tenants, so each job\n * records its own tenant and `runAction` is called with it (the action runs outside any request, so\n * the tenant can't be read from a request context).\n *\n * With several processes, each holds its own timers for the same actions: `recover` runs again on a\n * timer to arm what this process has not seen, and whoever supplies `runAction` makes sure an action\n * runs once.\n */\nexport class BreeSchedulerService implements SchedulerService.Interface {\n private readonly bree;\n private readonly jobs = new Map<string, IScheduledJob>();\n private readonly logger;\n private readonly runAction;\n // Triggers started and not finished yet, so a shutdown can wait for them.\n private readonly running = new Set<Promise<void>>();\n /*\n * Set by `stop`. A recovery that started before the stop runs overdue actions one by one and can\n * still be going after it; this keeps it from starting more actions or arming timers on a Bree\n * that is stopped. Actions it skips stay stored and are picked up by another process's recovery.\n */\n private stopped = false;\n\n public constructor(params: IBreeSchedulerServiceParams) {\n this.logger = params.logger;\n this.runAction = params.runAction;\n\n this.bree = new Bree({\n root: false,\n jobs: [],\n logger: false,\n /*\n * Bree runs each job as a script in a worker thread; it cannot call a function. Running an\n * action needs the DI container, database and tenancy, which only the main thread has, so\n * the job script (`jobs/pollWorker.js`) only posts \"poll\" and exits. Bree passes that\n * message here, in the main thread of the Webiny API process that started Bree, and the\n * action runs in this process. `name` is the job's name, which is the action id.\n */\n workerMessageHandler: async ({ name }) => {\n const job = this.jobs.get(name);\n if (!job) {\n return;\n }\n\n this.jobs.delete(name);\n await this.trigger(name, job.namespace, job.tenant);\n }\n });\n }\n\n public async start(): Promise<void> {\n this.stopped = false;\n await this.bree.start();\n }\n\n public async stop(): Promise<void> {\n this.stopped = true;\n await this.bree.stop();\n }\n\n /**\n * Resolves once every trigger already started has finished, or after `timeoutMs`. Call it after\n * `stop`, which keeps new ones from starting.\n */\n public async waitForRunning(timeoutMs: number): Promise<void> {\n let timer: ReturnType<typeof setTimeout> | undefined;\n const timeout = new Promise<void>(resolve => {\n timer = setTimeout(resolve, timeoutMs);\n });\n const running = Promise.allSettled([...this.running]);\n await Promise.race([running, timeout]);\n clearTimeout(timer);\n }\n\n public async create(params: ISchedulerServiceCreateParams): Promise<void> {\n const { id, namespace, scheduleFor, tenant } = params;\n\n if (scheduleFor <= new Date()) {\n throw new WebinyError(\n `Cannot create a schedule for \"${id}\" with date in the past`,\n \"INVALID_SCHEDULE_DATE\",\n { scheduleFor, id }\n );\n }\n\n const exists = await this.exists({\n id,\n namespace,\n tenant\n });\n if (exists) {\n return this.update(params);\n }\n\n await this.arm(id, { namespace, tenant, scheduledFor: scheduleFor });\n }\n\n public async update(params: ISchedulerServiceUpdateParams): Promise<void> {\n const { id, scheduleFor } = params;\n\n if (scheduleFor <= new Date()) {\n throw new WebinyError(\n `Cannot update an existing schedule for \"${id}\" with date in the past`,\n \"INVALID_SCHEDULE_DATE\",\n { scheduleFor, id }\n );\n }\n\n await this.safeRemove(id);\n await this.create(params);\n }\n\n public async delete(params: SchedulerService.DeleteParams): Promise<void> {\n const { id } = params;\n const exists = await this.exists(params);\n if (!exists) {\n throw new WebinyError(`Cannot delete schedule \"${id}\" because it does not exist.`);\n }\n\n await this.safeRemove(id);\n }\n\n public async exists(params: SchedulerService.ExistsParams): Promise<boolean> {\n const { id } = params;\n return this.jobs.has(id);\n }\n\n /**\n * (Re)arm timers for a batch of persisted pending actions. Runs at boot (per tenant) to restore\n * schedules after a restart, and then on a timer, so this process also arms actions another\n * process scheduled or moved. Overdue actions fire immediately.\n *\n * An action already armed for the same time is left as it is; one armed for a different time was\n * moved by another process and is re-armed.\n */\n public async recover(pendingActions: IPendingAction[]): Promise<void> {\n const now = new Date();\n\n for (const action of pendingActions) {\n if (this.stopped) {\n return;\n }\n if (action.scheduledFor <= now) {\n /* Overdue — execute immediately. */\n await this.trigger(action.id, action.namespace, action.tenant);\n continue;\n }\n\n const armed = this.jobs.get(action.id);\n if (armed && armed.scheduledFor.getTime() === action.scheduledFor.getTime()) {\n continue;\n }\n if (armed) {\n await this.safeRemove(action.id);\n }\n\n await this.arm(action.id, {\n namespace: action.namespace,\n tenant: action.tenant,\n scheduledFor: action.scheduledFor\n });\n }\n }\n\n private async arm(id: string, job: IScheduledJob): Promise<void> {\n if (this.stopped) {\n return;\n }\n this.jobs.set(id, job);\n\n await this.bree.add({\n name: id,\n date: job.scheduledFor,\n path: workerPath\n });\n\n await this.bree.start(id);\n }\n\n private async trigger(id: string, namespace: string, tenant: string): Promise<void> {\n if (this.stopped) {\n return;\n }\n const triggered = this.runAction(id, namespace, tenant);\n this.running.add(triggered);\n try {\n await triggered;\n } finally {\n this.running.delete(triggered);\n }\n }\n\n private async safeRemove(id: string): Promise<void> {\n this.jobs.delete(id);\n\n try {\n await this.bree.stop(id);\n } catch {\n this.logger.debug(`Could not stop bree job \"${id}\" — it may have already fired.`);\n }\n\n try {\n await this.bree.remove(id);\n } catch {\n this.logger.debug(\n `Could not remove bree job \"${id}\" — it may have already been removed.`\n );\n }\n }\n}\n"],"names":["workerPath","fileURLToPath","URL","BreeSchedulerService","params","Map","Set","Bree","name","job","timeoutMs","timer","timeout","Promise","resolve","setTimeout","running","clearTimeout","id","namespace","scheduleFor","tenant","Date","WebinyError","exists","pendingActions","now","action","armed","triggered"],"mappings":";;;AAcA,MAAMA,aAAaC,cAAc,IAAIC,IAAI,wBAAwB,YAAY,GAAG;AA8BzE,MAAMC;IAcT,YAAmBC,MAAmC,CAAE;aAZvC,IAAI,GAAG,IAAIC;aAIX,OAAO,GAAG,IAAIC;QAK9B,KACO,OAAO,GAAG;QAGd,IAAI,CAAC,MAAM,GAAGF,OAAO,MAAM;QAC3B,IAAI,CAAC,SAAS,GAAGA,OAAO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 "version": "0.0.0-unstable.a85ef555b2",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -18,13 +18,13 @@
18
18
  "description": "Standalone cron-based implementation for the Webiny action scheduler.",
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@webiny/api-core": "0.0.0-unstable.5387e5f7c8",
22
- "@webiny/api-scheduler": "0.0.0-unstable.5387e5f7c8",
23
- "@webiny/error": "0.0.0-unstable.5387e5f7c8",
21
+ "@webiny/api-core": "0.0.0-unstable.a85ef555b2",
22
+ "@webiny/api-scheduler": "0.0.0-unstable.a85ef555b2",
23
+ "@webiny/error": "0.0.0-unstable.a85ef555b2",
24
24
  "bree": "9.2.9"
25
25
  },
26
26
  "devDependencies": {
27
- "@webiny/build-tools": "0.0.0-unstable.5387e5f7c8",
27
+ "@webiny/build-tools": "0.0.0-unstable.a85ef555b2",
28
28
  "typescript": "7.0.2",
29
29
  "vitest": "4.1.11"
30
30
  },