Skip to content

Multiple queue consumers per supervisor #1947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/supervisor/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const Env = z.object({
TRIGGER_DEQUEUE_ENABLED: BoolEnv.default("true"),
TRIGGER_DEQUEUE_INTERVAL_MS: z.coerce.number().int().default(1000),
TRIGGER_DEQUEUE_MAX_RUN_COUNT: z.coerce.number().int().default(10),
TRIGGER_DEQUEUE_MAX_CONSUMER_COUNT: z.coerce.number().int().default(1),

// Optional services
TRIGGER_WARM_START_URL: z.string().optional(),
Expand Down
1 change: 1 addition & 0 deletions apps/supervisor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class ManagedSupervisor {
dequeueIntervalMs: env.TRIGGER_DEQUEUE_INTERVAL_MS,
queueConsumerEnabled: env.TRIGGER_DEQUEUE_ENABLED,
maxRunCount: env.TRIGGER_DEQUEUE_MAX_RUN_COUNT,
maxConsumerCount: env.TRIGGER_DEQUEUE_MAX_CONSUMER_COUNT,
runNotificationsEnabled: env.TRIGGER_WORKLOAD_API_ENABLED,
preDequeue: async () => {
if (this.isKubernetes) {
Expand Down
22 changes: 13 additions & 9 deletions packages/core/src/v3/runEngineWorker/supervisor/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type SupervisorSessionOptions = SupervisorClientCommonOptions & {
preDequeue?: PreDequeueFn;
preSkip?: PreSkipFn;
maxRunCount?: number;
maxConsumerCount?: number;
};

export class SupervisorSession extends EventEmitter<WorkerEvents> {
Expand All @@ -27,7 +28,7 @@ export class SupervisorSession extends EventEmitter<WorkerEvents> {
private runNotificationsSocket?: Socket<WorkerServerToClientEvents, WorkerClientToServerEvents>;

private readonly queueConsumerEnabled: boolean;
private readonly queueConsumer: RunQueueConsumer;
private readonly queueConsumers: RunQueueConsumer[];

private readonly heartbeat: IntervalService;
private readonly heartbeatIntervalSeconds: number;
Expand All @@ -39,13 +40,15 @@ export class SupervisorSession extends EventEmitter<WorkerEvents> {
this.queueConsumerEnabled = opts.queueConsumerEnabled ?? true;

this.httpClient = new SupervisorHttpClient(opts);
this.queueConsumer = new RunQueueConsumer({
client: this.httpClient,
preDequeue: opts.preDequeue,
preSkip: opts.preSkip,
onDequeue: this.onDequeue.bind(this),
intervalMs: opts.dequeueIntervalMs,
maxRunCount: opts.maxRunCount,
this.queueConsumers = Array.from({ length: opts.maxConsumerCount ?? 1 }, () => {
return new RunQueueConsumer({
client: this.httpClient,
preDequeue: opts.preDequeue,
preSkip: opts.preSkip,
onDequeue: this.onDequeue.bind(this),
intervalMs: opts.dequeueIntervalMs,
maxRunCount: opts.maxRunCount,
});
});

// TODO: This should be dynamic and set by (or at least overridden by) the platform
Expand Down Expand Up @@ -181,7 +184,7 @@ export class SupervisorSession extends EventEmitter<WorkerEvents> {

if (this.queueConsumerEnabled) {
console.log("[SupervisorSession] Queue consumer enabled");
this.queueConsumer.start();
await Promise.allSettled(this.queueConsumers.map(async (q) => q.start()));
this.heartbeat.start();
} else {
console.warn("[SupervisorSession] Queue consumer disabled");
Expand All @@ -196,6 +199,7 @@ export class SupervisorSession extends EventEmitter<WorkerEvents> {
}

async stop() {
await Promise.allSettled(this.queueConsumers.map(async (q) => q.stop()));
this.heartbeat.stop();
this.runNotificationsSocket?.disconnect();
}
Expand Down