refactor: replace batch() with async/await
This commit is contained in:
parent
af8a8d395c
commit
8cf68e480a
1 changed files with 13 additions and 20 deletions
|
|
@ -721,38 +721,31 @@ const isEmptyIterable = iter => {
|
|||
}
|
||||
|
||||
const batchRecordPendingPings = () => {
|
||||
let pings = pendingRecordPings.values()
|
||||
const pings = pendingRecordPings.values()
|
||||
pendingRecordPings = new Map()
|
||||
if (isEmptyIterable(pings)) return Promise.resolve()
|
||||
|
||||
const prepareChunk = (t, chunk) =>
|
||||
chunk
|
||||
.flatMap(({ deviceId, last_online, version, model }) => [
|
||||
t.none(
|
||||
return db.task(async t => {
|
||||
for (const { deviceId, last_online, version, model } of pings) {
|
||||
await t
|
||||
.none(
|
||||
`INSERT INTO machine_pings (device_id, device_time)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (device_id) DO
|
||||
UPDATE SET device_time = $2,
|
||||
updated = now()`,
|
||||
[deviceId, last_online],
|
||||
),
|
||||
t.none(
|
||||
)
|
||||
.catch(err => logger.error(err))
|
||||
await t
|
||||
.none(
|
||||
pgp.helpers.update({ last_online, version, model }, null, 'devices') +
|
||||
'WHERE device_id = ${deviceId}',
|
||||
{ deviceId },
|
||||
),
|
||||
])
|
||||
.toArray()
|
||||
|
||||
const MaxBatchSize = 500
|
||||
return db
|
||||
.task(async t => {
|
||||
while (!isEmptyIterable(pings)) {
|
||||
const chunk = pings.take(MaxBatchSize)
|
||||
await t.batch(prepareChunk(t, chunk)).catch(err => logger.error(err))
|
||||
}
|
||||
})
|
||||
.catch(err => logger.error(err))
|
||||
)
|
||||
.catch(err => logger.error(err))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue