Files
calendar/packages/features/tasker/task-processor.ts
T
Keith WilliamsGitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1739114490 perf: separate task creation from processing to eliminate compilation overhead (#23485)
* refactor: separate task creation from processing to eliminate compilation overhead

- Create TaskProcessor class to handle task processing logic
- Move processQueue and cleanup methods to TaskProcessor
- Remove task handler imports from InternalTasker creation path
- Eliminate 2.5-3s compilation overhead by only loading task handlers during processing
- Maintain existing tasker.create() API for all callers
- Use composition pattern for clean separation of concerns

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

refactor: completely separate TaskProcessor from InternalTasker

- Remove processQueue() and cleanup() methods from Tasker interface
- Eliminate TaskProcessor dependency from InternalTasker class
- Update API endpoints (cron.ts, cleanup.ts) to use TaskProcessor directly
- Update README documentation to show correct TaskProcessor usage
- Complete separation ensures task creation no longer imports task handlers
- Eliminates 2.5-3s compilation overhead while preserving all functionality

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* Moved the cleanup method back to internal tasker

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2025-09-01 07:08:06 +00:00

43 lines
1.8 KiB
TypeScript

import { Task } from "./repository";
import tasksMap, { tasksConfig } from "./tasks";
/**
* TaskProcessor handles the processing of tasks from the queue.
* This is separated from task creation to avoid importing all task handlers
* when only creating tasks, which eliminates compilation overhead.
*/
export class TaskProcessor {
async processQueue(): Promise<void> {
const tasks = await Task.getNextBatch();
console.info(`Processing ${tasks.length} tasks`, tasks);
const tasksPromises = tasks.map(async (task) => {
console.info(
`Processing task ${task.id}, attempt:${task.attempts} maxAttempts:${task.maxAttempts} lastFailedAttempt:${task.lastFailedAttemptAt}`,
task
);
const taskHandlerGetter = tasksMap[task.type as keyof typeof tasksMap];
if (!taskHandlerGetter) throw new Error(`Task handler not found for type ${task.type}`);
const taskConfig = tasksConfig[task.type as keyof typeof tasksConfig];
const taskHandler = await taskHandlerGetter();
return taskHandler(task.payload)
.then(async () => {
await Task.succeed(task.id);
})
.catch(async (error) => {
console.info(`Retrying task ${task.id}: ${error}`);
await Task.retry({
taskId: task.id,
lastError: error instanceof Error ? error.message : "Unknown error",
minRetryIntervalMins:
taskConfig && "minRetryIntervalMins" in taskConfig ? taskConfig.minRetryIntervalMins : null,
});
});
});
const settled = await Promise.allSettled(tasksPromises);
const failed = settled.filter((result) => result.status === "rejected");
const succeded = settled.filter((result) => result.status === "fulfilled");
console.info({ failed, succeded });
}
}