Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import * as math from 'lib0/math'
import * as protocol from './protocol.js'
import * as env from 'lib0/environment'
import * as logging from 'lib0/logging'
import * as time from 'lib0/time'

const logWorker = logging.createModuleLogger('@y/redis/api/worker')
// const logApi = logging.createModuleLogger('@y/redis/api')
Expand Down Expand Up @@ -280,6 +279,7 @@ export class Api {
})
}
tasks.length > 0 && logWorker('Accepted tasks ', { tasks })
let reclaimCounts = 0
await promise.all(tasks.map(async task => {
const streamlen = await this.redis.xLen(task.stream)
if (streamlen === 0) {
Expand All @@ -289,6 +289,7 @@ export class Api {
.exec()
logWorker('Stream still empty, removing recurring task from queue ', { stream: task.stream })
} else {
reclaimCounts++
const { room, docid } = decodeRedisRoomStreamName(task.stream, this.prefix)
const { ydoc, storeReferences, redisLastId } = await this.getDoc(room, docid)
const lastId = math.max(number.parseInt(redisLastId.split('-')[0]), number.parseInt(task.id.split('-')[0]))
Expand Down Expand Up @@ -321,7 +322,7 @@ export class Api {
ydoc.destroy()
}
}))
return tasks
return { tasks, reclaimCounts }
}

async destroy () {
Expand Down Expand Up @@ -353,12 +354,10 @@ export class Worker {
this.client = client
logWorker('Created worker process ', { id: client.consumername, prefix: client.prefix, minMessageLifetime: client.redisMinMessageLifetime })
;(async () => {
const startRedisTime = await client.redis.time()
const timeDiff = startRedisTime.getTime() - time.getUnixTime()
while (!client._destroyed) {
try {
const tasks = await client.consumeWorkerQueue(opts)
if (tasks.length === 0 || (client.redisMinMessageLifetime > time.getUnixTime() + timeDiff - number.parseInt(tasks[0].id.split('-')[0]))) {
const { reclaimCounts } = await client.consumeWorkerQueue(opts)
if (reclaimCounts === 0) {
await promise.wait(client.redisWorkerTimeout)
}
} catch (e) {
Expand Down
Loading