更新备注:的确使用了 promise.all + reduce 方法取出的全量数据,但是目前库里只有3条数据,还是会偶发性的报 -501004;
18:11 分 集中报了一波错,大概 14 条:
{
"message": "errCode: -501004 exceed concurrent request limit | errMsg: [LimitExceeded.NoValidConnection] Connection num overrun. Please improve specifications, but if the problem cannot be solved, contact us.; ",
"name": "",
"stack": "Error: errCode: -501004 exceed concurrent request limit | errMsg: [LimitExceeded.NoValidConnection] Connection num overrun. Please improve specifications, but if the problem cannot be solved, contact us.; \n at Object.returnAsCloudSDKError (/var/user/node_modules/wx-server-sdk/index.js:6052:16)\n at Object.checkError (/var/user/node_modules/wx-server-sdk/index.js:1378:23)\n at Promise (/var/user/node_modules/wx-server-sdk/index.js:1508:33)\n at \n at process._tickCallback (internal/process/next_tick.js:188:7)",
"userInfo": {
"appId": "wx4bfbc8e64eff19eb"
}
}
然后登陆云函数后台查看日志发现,下面提示
日志内容 Request ID: 4c830adf-14b7-4fff-b2cd-3487add8034f
执行时间: 9372.41ms内存使用: 37.93 MB
返回结果 null
日志
START RequestId: 4c830adf-14b7-4fff-b2cd-3487add8034f
Event RequestId: 4c830adf-14b7-4fff-b2cd-3487add8034f
2020-01-16T10:11:53.154Z Database operation database.queryDocument is longer than 3s. Please check query performance and your network environment.
END RequestId: 4c830adf-14b7-4fff-b2cd-3487add8034f
Report RequestId: 4c830adf-14b7-4fff-b2cd-3487add8034f Duration:9372ms Memory:256MB MaxMemoryUsed:37.929688MB
大概从 17: 57 开始,执行时间由 70ms 升到 3、4 秒,中间甚至十几秒,到 18: 11 分后定时任务停止。
超过了同时运行上限(1000)https://developers.weixin.qq.com/miniprogram/dev/wxcloud/billing/quota.html
遇到同样的错误。这代码有问题吗?
const cloud = require('wx-server-sdk') cloud.init({ // API 调用都保持和云函数当前所在环境一致 env: cloud.DYNAMIC_CURRENT_ENV }) const db = cloud.database() const MAX_LIMIT = 1000 exports.main = async (event, context) => { // 获取当天 0 点的时间戳 const today = new Date(new Date().setHours(0, 0, 0, 0)) / 1000; // 先取出集合记录总数 const countResult = await db.collection('Task') .where({LastModifiedDate:db.command.gt(today)}) .count() const total = countResult.total console.log(total) // 计算需分几次取 const batchTimes = Math.ceil(total / 1000) // 承载所有读操作的 promise 的数组 const tasks = [] for (let i = 0; i < batchTimes; i++) { const promise = db.collection('Task') .where({LastModifiedDate:db.command.gt(today)}) .skip(i * MAX_LIMIT).limit(MAX_LIMIT).get() tasks.push(promise) } // 等待所有 return (await Promise.all(tasks)).reduce((acc, cur) => { return { data: acc.data.concat(cur.data), errMsg: acc.errMsg, } }) }
我也遇到这个问题了。。。
2020.01.19 16:35 又报了一波,没有官方人员解答一下吗,搞得定时任务不敢正式使用了,频繁的崩溃,唉
是不是超出云开发的套餐限制了?