refactor: batch record pings
This commit is contained in:
parent
be06ea5097
commit
1b26150499
4 changed files with 61 additions and 46 deletions
|
|
@ -702,6 +702,52 @@ function updatePhotos(dir, photoPairs) {
|
|||
)
|
||||
}
|
||||
|
||||
let pendingRecordPings = new Map()
|
||||
const enqueueRecordPing = ping => {
|
||||
pendingRecordPings.set(ping.deviceId, ping)
|
||||
}
|
||||
|
||||
// from @sindresorhus/is-empty-iterable
|
||||
const isEmptyIterable = iter => {
|
||||
for (const _ of iter) return false
|
||||
return true
|
||||
}
|
||||
|
||||
const batchRecordPendingPings = () => {
|
||||
let 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(
|
||||
`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(
|
||||
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))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getMachineName,
|
||||
getMachines,
|
||||
|
|
@ -719,4 +765,6 @@ module.exports = {
|
|||
updateDiagnostics,
|
||||
updateFailedQRScans,
|
||||
batchDiagnostics,
|
||||
enqueueRecordPing,
|
||||
batchRecordPendingPings,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue