Przeglądaj źródła

feat(core): Enhance scheduled task execution with RequestContext

- Updated the `ScheduledTask` interface to accept a structured argument for execution
- Refactored the `execute` method in `ScheduledTask` to utilize the new argument structure
- Modified the `cleanSessionsTask` to align with the new execution signature.
David Höck 9 miesięcy temu
rodzic
commit
24e4fc017a

+ 47 - 4
packages/core/src/scheduler/scheduled-task.ts

@@ -1,6 +1,36 @@
 import CronTime from 'cron-time-generator';
 
+import { RequestContext } from '../api';
 import { Injector } from '../common/index';
+import { RequestContextService } from '../service/helpers/request-context/request-context.service';
+import { ChannelService } from '../service/services/channel.service';
+
+/**
+ * @description
+ * The arguments passed to the execute method of a scheduled task.
+ *
+ * @since 3.3.0
+ * @docsCategory scheduled-tasks
+ * @docsPage ScheduledTask
+ */
+export interface ScheduledTaskExecutionArgs<C extends Record<string, any> = Record<string, any>> {
+    /**
+     * @description
+     * The injector instance.
+     */
+    injector: Injector;
+    /**
+     * @description
+     * A RequestContext instance that is configured for the scheduled task.
+     */
+    scheduledContext: RequestContext;
+    /**
+     * @description
+     * The parameters for the scheduled task.
+     */
+    params: C;
+}
+
 /**
  * @description
  * The configuration for a scheduled task.
@@ -62,7 +92,7 @@ export interface ScheduledTaskConfig<C extends Record<string, any> = Record<stri
      * @description
      * The function that will be executed when the scheduled task is run.
      */
-    execute(injector: Injector, config: C): Promise<any>;
+    execute(args: ScheduledTaskExecutionArgs<C>): Promise<any>;
 }
 
 /**
@@ -76,7 +106,7 @@ export interface ScheduledTaskConfig<C extends Record<string, any> = Record<stri
  * const task = new ScheduledTask({
  *     id: 'test-job',
  *     schedule: cron => cron.every(2).minutes(),
- *     execute: async (injector, params) => {
+ *     execute: async ({ injector, scheduledContext, params }) => {
  *         // some logic here
  *     },
  * });
@@ -99,7 +129,20 @@ export class ScheduledTask<C extends Record<string, any> = Record<string, any>>
     }
 
     async execute(injector: Injector) {
-        return this.config.execute(injector, this.config.params ?? ({} as any));
+        const requestContextService = injector.get(RequestContextService);
+        const channelService = injector.get(ChannelService);
+        const defaultChannel = await channelService.getDefaultChannel();
+
+        const scheduledContext = await requestContextService.create({
+            apiType: 'admin',
+            channelOrToken: defaultChannel,
+        });
+
+        return this.config.execute({
+            injector,
+            scheduledContext,
+            params: this.config.params ?? ({} as any),
+        });
     }
 
     /**
@@ -114,7 +157,7 @@ export class ScheduledTask<C extends Record<string, any> = Record<string, any>>
      * const task = new ScheduledTask({
      *     id: 'test-job',
      *     schedule: cron => cron.every(2).minutes(),
-     *     execute: async (injector, params) => {
+     *     execute: async ({ injector, scheduledContext, params }) => {
      *         // some logic here
      *     },
      * });

+ 1 - 1
packages/core/src/scheduler/tasks/clean-sessions-task.ts

@@ -41,7 +41,7 @@ export const cleanSessionsTask = new ScheduledTask({
         batchSize: 10_000,
     },
     schedule: cron => cron.everyDayAt(0, 0),
-    async execute(injector, params) {
+    async execute({ injector, params }) {
         const sessionService = injector.get(SessionService);
         await sessionService.triggerCleanSessionsJob(params.batchSize);
         return { result: 'Triggered clean sessions job' };