浏览代码

fix: JobQueue.runJob race condition

Owen Diffey 4 月之前
父节点
当前提交
578da3ea73
共有 1 个文件被更改,包括 11 次插入5 次删除
  1. 11 5
      backend/src/JobQueue.ts

+ 11 - 5
backend/src/JobQueue.ts

@@ -62,11 +62,16 @@ export class JobQueue {
 	): Promise<unknown> {
 		assertJobDerived(JobClass);
 		return new Promise<unknown>((resolve, reject) => {
-			this.queueJob(JobClass as JobDerived, payload, options)
-				.then(uuid => {
-					this._callbacks[uuid] = { resolve, reject };
-				})
-				.catch(reject);
+			assertJobDerived(JobClass);
+			const job = new (JobClass as JobDerived)(payload, options);
+
+			this._callbacks[job.getUuid()] = { resolve, reject };
+
+			JobStatistics.updateStats(job.getPath(), JobStatisticsType.QUEUED);
+
+			this._queue.push(job);
+
+			this._process();
 		});
 	}
 
@@ -85,6 +90,7 @@ export class JobQueue {
 		JobStatistics.updateStats(job.getPath(), JobStatisticsType.QUEUED);
 
 		this._queue.push(job);
+
 		this._process();
 
 		return job.getUuid();