فهرست منبع

refactor: Ensure module and dependencies can run jobs

Owen Diffey 1 سال پیش
والد
کامیت
bfd4b63aa5
4فایلهای تغییر یافته به همراه24 افزوده شده و 3 حذف شده
  1. 16 0
      backend/src/BaseModule.ts
  2. 5 0
      backend/src/Job.ts
  3. 2 2
      backend/src/JobQueue.ts
  4. 1 1
      backend/src/modules/APIModule.ts

+ 16 - 0
backend/src/BaseModule.ts

@@ -199,6 +199,22 @@ export default abstract class BaseModule {
 		return this._jobs;
 	}
 
+	/**
+	 * canRunJobs - Determine if module can run jobs
+	 */
+	public canRunJobs() {
+		return this.getDependentModules().reduce(
+			(canRunJobs: boolean, moduleName: keyof Modules): boolean => {
+				if (canRunJobs === false) return false;
+
+				return !!this._moduleManager
+					.getModule(moduleName)
+					?.canRunJobs();
+			},
+			this.getStatus() === ModuleStatus.STARTED
+		);
+	}
+
 	/**
 	 * startup - Startup module
 	 */

+ 5 - 0
backend/src/Job.ts

@@ -170,8 +170,13 @@ export default class Job {
 	 */
 	public async execute() {
 		if (this._startedAt) throw new Error("Job has already been executed.");
+
+		if (!this.getModule().canRunJobs())
+			throw new Error("Module can not currently run jobs.");
+
 		this._setStatus(JobStatus.ACTIVE);
 		this._startedAt = performance.now();
+
 		return (
 			this._jobFunction
 				.apply(this._module, [this._context, this._payload])

+ 2 - 2
backend/src/JobQueue.ts

@@ -1,4 +1,4 @@
-import BaseModule, { ModuleStatus } from "@/BaseModule";
+import BaseModule from "@/BaseModule";
 import Job, { JobStatus } from "@/Job";
 import { JobOptions } from "@/types/JobOptions";
 import { Jobs, Modules } from "@/types/Modules";
@@ -170,7 +170,7 @@ export default class JobQueue {
 
 			// If the module of the job is not started, we can't run the job, so go to the next job in the queue
 			// eslint-disable-next-line no-continue
-			if (job.getModule().getStatus() !== ModuleStatus.STARTED) continue;
+			if (!job.getModule().canRunJobs()) continue;
 
 			// Remove the job from the queue and add it to the active jobs array
 			this._queue.splice(this._queue.indexOf(job), 1);

+ 1 - 1
backend/src/modules/APIModule.ts

@@ -19,7 +19,7 @@ export default class APIModule extends BaseModule {
 	public constructor() {
 		super("api");
 
-		this._dependentModules = ["data", "events", "websocket"];
+		this._dependentModules = ["cache", "data", "events", "websocket"];
 
 		this._subscriptions = {};