chore: add logs for debugging tasker (#17283)

This commit is contained in:
Udit Takkar
2024-10-24 08:31:03 +00:00
committed by GitHub
parent 57ed383554
commit 4c528afe65
2 changed files with 18 additions and 10 deletions
@@ -15,15 +15,20 @@ export class InternalTasker implements Tasker {
};
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}`, task);
const taskHandlerGetter = tasksMap[task.type as keyof typeof tasksMap];
if (!taskHandlerGetter) throw new Error(`Task handler not found for type ${task.type}`);
const taskHandler = await taskHandlerGetter();
return taskHandler(task.payload)
.then(async () => {
await Task.succeed(task.id);
})
.catch(async (error) => {
console.info(`Error processing task ${task.id}: ${error}`);
await Task.retry(task.id, error instanceof Error ? error.message : "Unknown error");
});
});
+13 -10
View File
@@ -42,6 +42,7 @@ export class Task {
options: { scheduledAt?: Date; maxAttempts?: number } = {}
) {
const { scheduledAt, maxAttempts } = options;
console.info("Creating task", { type, payload, scheduledAt, maxAttempts });
const newTask = await db.task.create({
data: {
payload,
@@ -54,6 +55,7 @@ export class Task {
}
static async getNextBatch() {
console.info("Getting next batch of tasks", whereUpcomingTasks);
return db.task.findMany({
where: whereUpcomingTasks,
orderBy: {
@@ -134,15 +136,16 @@ export class Task {
}
static async cleanup() {
return db.task.deleteMany({
where: {
OR: [
// Get tasks that have succeeded
whereSucceeded,
// Get tasks where maxAttemps has been reached
whereMaxAttemptsReached,
],
},
});
// TODO: Uncomment this later
// return db.task.deleteMany({
// where: {
// OR: [
// // Get tasks that have succeeded
// whereSucceeded,
// // Get tasks where maxAttemps has been reached
// whereMaxAttemptsReached,
// ],
// },
// });
}
}