chore: enable/disable slots workers via env (#26621)
* chore: enable/disable slots workers via env * fix: address Cubic AI review feedback - Fix incorrect JSDoc comment for getSerializableContext method - Remove debug console.log statement from slots controller - Fix port suffix condition to preserve original behavior Co-Authored-By: unknown <> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
6 files changed
+125
-81
No files matched your search
@@ -1,25 +1,32 @@
|
||||
import { TRPC_ERROR_CODE, TRPC_ERROR_MAP, TRPCErrorCode } from "@/filters/trpc-exception.filter";
|
||||
import { AvailableSlotsService } from "@/lib/services/available-slots.service";
|
||||
import { SlotsOutputService_2024_04_15 } from "@/modules/slots/slots-2024-04-15/services/slots-output.service";
|
||||
import type { RangeSlots, TimeSlots } from "@/modules/slots/slots-2024-04-15/services/slots-output.service";
|
||||
import { SlotsWorkerService_2024_04_15 } from "@/modules/slots/slots-2024-04-15/services/slots-worker.service";
|
||||
import { SlotsService_2024_04_15 } from "@/modules/slots/slots-2024-04-15/services/slots.service";
|
||||
import { Query, Body, Controller, Get, Delete, Post, Req, Res, BadRequestException } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { ApiExcludeController as DocsExcludeController } from "@nestjs/swagger";
|
||||
import { ApiTags as DocsTags, ApiCreatedResponse, ApiOkResponse, ApiOperation } from "@nestjs/swagger";
|
||||
import { Response as ExpressResponse, Request as ExpressRequest } from "express";
|
||||
|
||||
import {
|
||||
SUCCESS_STATUS,
|
||||
VERSION_2024_06_14,
|
||||
VERSION_2024_04_15,
|
||||
VERSION_2024_06_11,
|
||||
VERSION_2024_06_14,
|
||||
VERSION_2024_08_13,
|
||||
} from "@calcom/platform-constants";
|
||||
import { TRPCError } from "@calcom/platform-libraries";
|
||||
import { RemoveSelectedSlotInput_2024_04_15, ReserveSlotInput_2024_04_15 } from "@calcom/platform-types";
|
||||
import { ApiResponse, GetAvailableSlotsInput_2024_04_15 } from "@calcom/platform-types";
|
||||
import {
|
||||
ApiResponse,
|
||||
GetAvailableSlotsInput_2024_04_15,
|
||||
RemoveSelectedSlotInput_2024_04_15,
|
||||
ReserveSlotInput_2024_04_15,
|
||||
} from "@calcom/platform-types";
|
||||
import { BadRequestException, Body, Controller, Delete, Get, Post, Query, Req, Res } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import {
|
||||
ApiCreatedResponse,
|
||||
ApiOkResponse,
|
||||
ApiOperation,
|
||||
ApiExcludeController as DocsExcludeController,
|
||||
} from "@nestjs/swagger";
|
||||
import { Request as ExpressRequest, Response as ExpressResponse } from "express";
|
||||
import { TRPC_ERROR_CODE, TRPC_ERROR_MAP, TRPCErrorCode } from "@/filters/trpc-exception.filter";
|
||||
import { AvailableSlotsService } from "@/lib/services/available-slots.service";
|
||||
import { SlotsService_2024_04_15 } from "@/modules/slots/slots-2024-04-15/services/slots.service";
|
||||
import type { RangeSlots, TimeSlots } from "@/modules/slots/slots-2024-04-15/services/slots-output.service";
|
||||
import { SlotsOutputService_2024_04_15 } from "@/modules/slots/slots-2024-04-15/services/slots-output.service";
|
||||
import { SlotsWorkerService_2024_04_15 } from "@/modules/slots/slots-2024-04-15/services/slots-worker.service";
|
||||
|
||||
@Controller({
|
||||
path: "/v2/slots",
|
||||
@@ -166,32 +173,29 @@ export class SlotsController_2024_04_15 {
|
||||
): Promise<ApiResponse<{ slots: TimeSlots["slots"] | RangeSlots["slots"] }>> {
|
||||
try {
|
||||
const isTeamEvent =
|
||||
query.isTeamEvent === undefined
|
||||
? await this.slotsService.checkIfIsTeamEvent(query.eventTypeId)
|
||||
: query.isTeamEvent;
|
||||
query.isTeamEvent ?? (await this.slotsService.checkIfIsTeamEvent(query.eventTypeId));
|
||||
|
||||
// Do not use workers in E2E, not supported by TS-JEST
|
||||
const availableSlotsService = this.config.get<boolean>("e2e") ? this.availableSlotsService : null;
|
||||
// Or if explicitly disabled via specific env var
|
||||
const shouldUseAvailableSlotsService =
|
||||
this.config.get<boolean>("e2e") || !this.config.get<boolean>("enableSlotsWorkers");
|
||||
const slotsArgs = {
|
||||
input: {
|
||||
...query,
|
||||
isTeamEvent,
|
||||
},
|
||||
ctx: {
|
||||
req,
|
||||
},
|
||||
};
|
||||
|
||||
const availableSlots = availableSlotsService
|
||||
? await availableSlotsService.getAvailableSlots({
|
||||
input: {
|
||||
...query,
|
||||
isTeamEvent,
|
||||
},
|
||||
ctx: {
|
||||
req,
|
||||
},
|
||||
})
|
||||
: await this.slotsWorkerService.getAvailableSlotsInWorker({
|
||||
input: {
|
||||
...query,
|
||||
isTeamEvent,
|
||||
},
|
||||
ctx: {
|
||||
req,
|
||||
},
|
||||
});
|
||||
let availableSlots: TimeSlots;
|
||||
|
||||
if (shouldUseAvailableSlotsService) {
|
||||
availableSlots = await this.availableSlotsService.getAvailableSlots(slotsArgs);
|
||||
} else {
|
||||
availableSlots = await this.slotsWorkerService.getAvailableSlotsInWorker(slotsArgs);
|
||||
}
|
||||
|
||||
const { slots } = await this.slotsOutputService.getOutputSlots(
|
||||
availableSlots,
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { Injectable, Logger, OnModuleDestroy } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import * as path from "node:path";
|
||||
import { Worker } from "node:worker_threads";
|
||||
|
||||
// Import WorkerOptions type
|
||||
import type { GetScheduleOptions } from "@calcom/trpc/server/routers/viewer/slots/types";
|
||||
import { Injectable, Logger, OnModuleDestroy } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
|
||||
import { TimeSlots } from "./slots-output.service";
|
||||
|
||||
@@ -43,11 +42,13 @@ export class SlotsWorkerService_2024_04_15 implements OnModuleDestroy {
|
||||
private availableWorkers: Worker[] = [];
|
||||
|
||||
constructor(private readonly config: ConfigService) {
|
||||
this.maxWorkers = process.env.SLOTS_WORKER_POOL_SIZE
|
||||
? parseInt(process.env.SLOTS_WORKER_POOL_SIZE, 10)
|
||||
: 4;
|
||||
this.maxWorkers = this.config.get<number>("slotsWorkerPoolSize") ?? 4;
|
||||
if (!this.config.get<boolean>("enableSlotsWorkers")) {
|
||||
this.maxWorkers = 0;
|
||||
}
|
||||
|
||||
// Workers are not initialized in E@E
|
||||
if (!this.config.get<boolean>("e2e")) {
|
||||
if (!this.config.get<boolean>("e2e") && this.maxWorkers > 0) {
|
||||
this.initializeWorkerPool();
|
||||
}
|
||||
}
|
||||
@@ -108,10 +109,12 @@ export class SlotsWorkerService_2024_04_15 implements OnModuleDestroy {
|
||||
this.logger.error(`Error terminating failed worker ${failedWorker.threadId}: ${err?.message}`);
|
||||
});
|
||||
} catch (error) {
|
||||
let errorMessage = String(error);
|
||||
if (error instanceof Error) {
|
||||
errorMessage = error.message;
|
||||
}
|
||||
this.logger.error(
|
||||
`Failed to invoke terminate method on failed worker ${failedWorker.threadId}: ${
|
||||
error instanceof Error ? error.message : String(error)
|
||||
}`
|
||||
`Failed to invoke terminate method on failed worker ${failedWorker.threadId}: ${errorMessage}`
|
||||
);
|
||||
}
|
||||
|
||||
@@ -119,18 +122,45 @@ export class SlotsWorkerService_2024_04_15 implements OnModuleDestroy {
|
||||
try {
|
||||
this.createNewWorker();
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`Failed to create replacement worker after failure: ${
|
||||
error instanceof Error ? error.message : String(error)
|
||||
}`,
|
||||
error instanceof Error ? error.stack : undefined
|
||||
);
|
||||
let errorMessage = String(error);
|
||||
let errorStack: string | undefined;
|
||||
|
||||
if (error instanceof Error) {
|
||||
errorMessage = error.message;
|
||||
errorStack = error.stack;
|
||||
}
|
||||
|
||||
this.logger.error(`Failed to create replacement worker after failure: ${errorMessage}`, errorStack);
|
||||
}
|
||||
|
||||
// After a worker fails, process the next task in case there are queued tasks
|
||||
this.processNextTask();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the request context into a serializable format for worker communication.
|
||||
* @param ctx The context from GetScheduleOptions containing request data.
|
||||
* @returns A serializable context object with cookies and headers, or undefined.
|
||||
*/
|
||||
private getSerializableContext(ctx: GetScheduleOptions["ctx"]): WorkerMessage["ctx"] {
|
||||
if (!ctx) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let reqData: NonNullable<NonNullable<WorkerMessage["ctx"]>["req"]> | undefined;
|
||||
|
||||
if (ctx.req) {
|
||||
reqData = {
|
||||
cookies: (ctx.req.cookies as Record<string, string>) || {},
|
||||
headers: (ctx.req.headers as Record<string, string>) || {},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
req: reqData,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the next task in the queue if there are available workers.
|
||||
* Assigns a task to an available worker and sets up 'once' listeners for its specific result.
|
||||
@@ -146,21 +176,12 @@ export class SlotsWorkerService_2024_04_15 implements OnModuleDestroy {
|
||||
}
|
||||
|
||||
// Prepare context for serialization
|
||||
const serializableCtx: WorkerMessage["ctx"] = task.options.ctx
|
||||
? {
|
||||
req: task.options.ctx.req
|
||||
? {
|
||||
cookies: (task.options.ctx.req.cookies as Record<string, string>) || {},
|
||||
headers: (task.options.ctx.req.headers as Record<string, string>) || {},
|
||||
}
|
||||
: undefined,
|
||||
}
|
||||
: undefined;
|
||||
const serializableCtx = this.getSerializableContext(task.options.ctx);
|
||||
|
||||
try {
|
||||
// Use 'once' listeners for task-specific responses and errors.
|
||||
// 'once' listeners automatically remove themselves after being invoked, preventing leaks.
|
||||
const messageListener = (result: WorkerResult) => {
|
||||
const messageListener = (result: WorkerResult): void => {
|
||||
this.availableWorkers.push(worker); // Return worker to the available pool
|
||||
if (result.success) {
|
||||
task.resolve(result.data as TimeSlots);
|
||||
@@ -170,7 +191,7 @@ export class SlotsWorkerService_2024_04_15 implements OnModuleDestroy {
|
||||
this.processNextTask(); // Attempt to process the next task
|
||||
};
|
||||
|
||||
const errorListener = (err: Error) => {
|
||||
const errorListener = (err: Error): void => {
|
||||
this.availableWorkers.push(worker); // Ensure worker is returned
|
||||
task.reject(new Error(`Worker thread error during task execution: ${err.message}`));
|
||||
this.processNextTask(); // Attempt to process the next task
|
||||
@@ -186,11 +207,13 @@ export class SlotsWorkerService_2024_04_15 implements OnModuleDestroy {
|
||||
} catch (error) {
|
||||
// If posting the message itself fails (e.g., serialization error)
|
||||
this.availableWorkers.push(worker); // Ensure worker is returned to pool
|
||||
task.reject(
|
||||
new Error(
|
||||
`Failed to dispatch task to worker: ${error instanceof Error ? error.message : String(error)}`
|
||||
)
|
||||
);
|
||||
|
||||
let errorMessage = String(error);
|
||||
if (error instanceof Error) {
|
||||
errorMessage = error.message;
|
||||
}
|
||||
|
||||
task.reject(new Error(`Failed to dispatch task to worker: ${errorMessage}`));
|
||||
this.processNextTask(); // Try to process next task if available
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user