英文:
How to solve the aws appsync mutation timeout issue?
问题
这是我用于从DynamoDB中删除项目的函数。我将要删除的对象列表传递给deleteExpiredFcm函数。从日志消息中,我知道变异存在某种冲突,导致超时问题。此外,我实现了分页以避免超时问题,因为对象列表可能包含数百甚至数千个项目。我漏掉了什么?
async function deleteExpiredFcm(expiredFcm) {
console.log("删除过期的FCM...");
const batchSize = 10; // 根据需要调整批次大小
const totalBatches = Math.ceil(expiredFcm.length / batchSize);
for (let i = 0; i < totalBatches; i++) {
const batch = expiredFcm.slice(i * batchSize, (i + 1) * batchSize);
const mutationPromises = batch.map(async (element) => {
const mutationObj = deleteExpiredFcmObj;
const variables = {
input: {
id: element.id,
},
};
let retries = 3; // 重试次数
let success = false;
while (retries > 0 && !success) {
try {
const { data } = await client.mutate({
mutation: mutationObj,
variables: variables,
});
console.log(
"(变异): 删除数据 ----------->",
data.deleteFcmNotification
);
success = true;
} catch (error) {
if (
error.graphQLErrors.some(
(err) => err.errorType === "ConflictUnhandled"
)
) {
console.log("发生冲突,正在重试...");
retries--;
} else {
console.error("删除FCM时出错:", error);
break;
}
}
}
});
await Promise.all(mutationPromises); // 等待每个批次的完成
}
}
编辑:
我在变异输入中添加了_version,所以现在它可以删除项目了。然而,我仍然遇到超时问题,
{
"errorMessage": "2023-06-13T12:56:25.895Z 7c81deb4-ccea-4cf0-ac59-f72c5e1becf9 任务在3.05秒后超时"
}
如何摆脱它?
英文:
I have a lambda that performs mutations to delete a bunch of items, but the timeout issue always occurs.
This is the function I use to delete items from the dynamoDB. I pass a list of objects to be removed into the deleteExpiredFcm function. From the log message, I know the mutation is having some kind of conflict that causes the timeout issue. Also, I implemented pagination to avoid the timeout issue, as the object list may consist of hundreds or even thousands of items. What am I missing?
async function deleteExpiredFcm(expiredFcm) {
console.log("Deleting expired FCM...");
const batchSize = 10; // Adjust the batch size as needed
const totalBatches = Math.ceil(expiredFcm.length / batchSize);
for (let i = 0; i < totalBatches; i++) {
const batch = expiredFcm.slice(i * batchSize, (i + 1) * batchSize);
const mutationPromises = batch.map(async (element) => {
const mutationObj = deleteExpiredFcmObj;
const variables = {
input: {
id: element.id,
},
};
let retries = 3; // Number of retries
let success = false;
while (retries > 0 && !success) {
try {
const { data } = await client.mutate({
mutation: mutationObj,
variables: variables,
});
console.log(
"(Mutation): Deleting Data ----------->",
data.deleteFcmNotification
);
success = true;
} catch (error) {
if (
error.graphQLErrors.some(
(err) => err.errorType === "ConflictUnhandled"
)
) {
console.log("Conflict occurred, retrying...");
retries--;
} else {
console.error("Error deleting FCM:", error);
break;
}
}
}
});
await Promise.all(mutationPromises); // Await the completion of each batch
}
}
Edit:
I added _version to the mutation input, so it deletes items now. However I'm still getting the timeout issue,
{
"errorMessage": "2023-06-13T12:56:25.895Z 7c81deb4-ccea-4cf0-ac59-f72c5e1becf9 Task timed out after 3.05 seconds"
}
How can I get rid of it?
答案1
得分: 0
为解决GraphQL错误,我在变异输入中添加了 _version。
const variables = {
input: {
id: element.id,
_version: element._version,
},
};
为解决超时问题,我只是增加了Lambda通用配置中的超时限制。这暂时解决了问题。请告诉我是否有更适当的方法来解决超时问题。
英文:
To solve the GraphQL error, I added _version to the mutation input.
const variables = {
input: {
id: element.id,
_version: element._version,
},
};
To solve the timeout issue, I simply increased the timeout limit in the lambda general configuration. This solves the issue for now. Please let me know if there is a more proper way to solve the timeout issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论