Prechádzať zdrojové kódy

refactor: Store job started and completed at timestamp

Owen Diffey 2 rokov pred
rodič
commit
2dccaceff1
2 zmenil súbory, kde vykonal 19 pridanie a 9 odobranie
  1. 17 8
      backend/src/Job.ts
  2. 2 1
      backend/src/JobQueue.ts

+ 17 - 8
backend/src/Job.ts

@@ -41,6 +41,10 @@ export default class Job {
 
 	protected createdAt: number;
 
+	protected startedAt?: number;
+
+	protected completedAt?: number;
+
 	protected logBook: LogBook;
 
 	protected jobStatistics: JobStatistics;
@@ -102,7 +106,7 @@ export default class Job {
 				return v.toString(16);
 			}
 		);
-		this.setStatus(JobStatus.QUEUED);
+		this.status = JobStatus.QUEUED;
 		this.createdAt = Date.now();
 	}
 
@@ -166,8 +170,9 @@ export default class Job {
 	 * @returns Promise
 	 */
 	public async execute() {
+		if (this.startedAt) throw new Error("Job has already been executed.");
 		this.setStatus(JobStatus.ACTIVE);
-		const startedAt = Date.now();
+		this.startedAt = Date.now();
 		return (
 			this.jobFunction
 				.apply(this.module, [this.context, this.payload])
@@ -193,12 +198,14 @@ export default class Job {
 					throw err;
 				})
 				.finally(() => {
+					this.completedAt = Date.now();
 					this.jobStatistics.updateStats(this.getName(), "total");
-					this.jobStatistics.updateStats(
-						this.getName(),
-						"averageTime",
-						Date.now() - startedAt
-					);
+					if (this.startedAt)
+						this.jobStatistics.updateStats(
+							this.getName(),
+							"averageTime",
+							this.completedAt - this.startedAt
+						);
 					this.setStatus(JobStatus.COMPLETED);
 				})
 		);
@@ -240,7 +247,9 @@ export default class Job {
 			name: this.getName(),
 			status: this.getStatus(),
 			moduleStatus: this.module.getStatus(),
-			createdAt: this.createdAt
+			createdAt: this.createdAt,
+			startedAt: this.startedAt,
+			completedAt: this.completedAt
 		};
 	}
 }

+ 2 - 1
backend/src/JobQueue.ts

@@ -189,8 +189,9 @@ export default class JobQueue {
 					const activeJobIndex = this.active.indexOf(job);
 					if (activeJobIndex > -1) {
 						this.active.splice(activeJobIndex, 1);
-						this.process();
 					}
+
+					this.process();
 				});
 			// Stop the for loop
 			if (this.active.length >= this.concurrency) break;