|
1 | 1 | import { Backend } from "@sidequest/backend"; |
2 | | -import { |
3 | | - JobData, |
4 | | - JobTransitionFactory, |
5 | | - logger, |
6 | | - QueueConfig, |
7 | | - RetryTransition, |
8 | | - RunTransition, |
9 | | - SnoozeTransition, |
10 | | -} from "@sidequest/core"; |
| 2 | +import { JobData, JobTransitionFactory, logger, QueueConfig, RetryTransition, RunTransition } from "@sidequest/core"; |
11 | 3 | import EventEmitter from "events"; |
12 | 4 | import { inspect } from "util"; |
13 | 5 | import { NonNullableEngineConfig } from "../engine"; |
@@ -77,44 +69,49 @@ export class ExecutorManager { |
77 | 69 | } |
78 | 70 |
|
79 | 71 | /** |
80 | | - * Executes a job in the given queue. |
| 72 | + * Prepares a job for execution by marking it as active and adding it to a queue slot. |
81 | 73 | * @param queueConfig The queue configuration. |
82 | | - * @param job The job data to execute. |
| 74 | + * @param job The job data. |
83 | 75 | */ |
84 | | - async execute(queueConfig: QueueConfig, job: JobData): Promise<void> { |
85 | | - logger("Executor Manager").debug(`Submitting job ${job.id} for execution in queue ${queueConfig.name}`); |
| 76 | + queueJob(queueConfig: QueueConfig, job: JobData) { |
86 | 77 | if (!this.activeByQueue[queueConfig.name]) { |
87 | 78 | this.activeByQueue[queueConfig.name] = new Set(); |
88 | 79 | } |
89 | | - |
90 | | - if (this.availableSlotsByQueue(queueConfig) <= 0 || this.availableSlotsGlobal() <= 0) { |
91 | | - logger("Executor Manager").debug(`No available slots for job ${job.id} in queue ${queueConfig.name}`); |
92 | | - await JobTransitioner.apply(this.backend, job, new SnoozeTransition(0)); |
93 | | - return; |
94 | | - } |
95 | | - |
96 | 80 | this.activeByQueue[queueConfig.name].add(job.id); |
97 | 81 | this.activeJobs.add(job.id); |
| 82 | + } |
98 | 83 |
|
99 | | - job = await JobTransitioner.apply(this.backend, job, new RunTransition()); |
100 | | - |
101 | | - const signal = new EventEmitter(); |
102 | | - let isRunning = true; |
103 | | - const cancellationCheck = async () => { |
104 | | - while (isRunning) { |
105 | | - const watchedJob = await this.backend.getJob(job.id); |
106 | | - if (watchedJob!.state === "canceled") { |
107 | | - logger("Executor Manager").debug(`Emitting abort signal for job ${job.id}`); |
108 | | - signal.emit("abort"); |
109 | | - isRunning = false; |
110 | | - return; |
| 84 | + /** |
| 85 | + * Executes a job in the given queue. |
| 86 | + * @param queueConfig The queue configuration. |
| 87 | + * @param job The job data to execute. |
| 88 | + */ |
| 89 | + async execute(queueConfig: QueueConfig, job: JobData): Promise<void> { |
| 90 | + let isRunning = false; |
| 91 | + try { |
| 92 | + logger("Executor Manager").debug(`Submitting job ${job.id} for execution in queue ${queueConfig.name}`); |
| 93 | + // We call prepareJob here again to make sure the jobs are in the queues. |
| 94 | + // This might not be necessary, but for the sake of consistency we do it. |
| 95 | + this.queueJob(queueConfig, job); |
| 96 | + |
| 97 | + job = await JobTransitioner.apply(this.backend, job, new RunTransition()); |
| 98 | + |
| 99 | + isRunning = true; |
| 100 | + const signal = new EventEmitter(); |
| 101 | + const cancellationCheck = async () => { |
| 102 | + while (isRunning) { |
| 103 | + const watchedJob = await this.backend.getJob(job.id); |
| 104 | + if (watchedJob!.state === "canceled") { |
| 105 | + logger("Executor Manager").debug(`Emitting abort signal for job ${job.id}`); |
| 106 | + signal.emit("abort"); |
| 107 | + isRunning = false; |
| 108 | + return; |
| 109 | + } |
| 110 | + await new Promise((r) => setTimeout(r, 1000)); |
111 | 111 | } |
112 | | - await new Promise((r) => setTimeout(r, 1000)); |
113 | | - } |
114 | | - }; |
115 | | - void cancellationCheck(); |
| 112 | + }; |
| 113 | + void cancellationCheck(); |
116 | 114 |
|
117 | | - try { |
118 | 115 | logger("Executor Manager").debug(`Running job ${job.id} in queue ${queueConfig.name}`); |
119 | 116 |
|
120 | 117 | const runPromise = this.runnerPool.run(job, signal); |
|
0 commit comments