(
workerId: number,
)
| 349 | } |
| 350 | |
| 351 | private async getNextWorkItem( |
| 352 | workerId: number, |
| 353 | ): Promise<{ job?: QueueJob; chunk?: WorkChunk }> { |
| 354 | const pendingChunk = Array.from(this.workChunks.values()).find( |
| 355 | (chunk) => chunk.status === "pending", |
| 356 | ); |
| 357 | |
| 358 | if (pendingChunk) { |
| 359 | pendingChunk.status = "processing"; |
| 360 | pendingChunk.workerId = workerId; |
| 361 | this.workerJobAssignment.set(workerId, pendingChunk.jobId); |
| 362 | return { chunk: pendingChunk }; |
| 363 | } |
| 364 | |
| 365 | const activeJobCount = new Set(this.workerJobAssignment.values()).size; |
| 366 | |
| 367 | if (activeJobCount >= this.workerCount) { |
| 368 | return {}; |
| 369 | } |
| 370 | |
| 371 | const job = this.queue.shift(); |
| 372 | if (!job) { |
| 373 | return {}; |
| 374 | } |
| 375 | |
| 376 | const futureActiveJobs = activeJobCount + 1; |
| 377 | |
| 378 | const workersPerJob = Math.floor(this.workerCount / futureActiveJobs); |
| 379 | const extraWorkers = this.workerCount % futureActiveJobs; |
| 380 | |
| 381 | const workersForJob = workersPerJob + (extraWorkers > 0 ? 1 : 0); |
| 382 | |
| 383 | job.allocatedWorkers = workersForJob; |
| 384 | this.jobWorkerCount.set(job.id, workersForJob); |
| 385 | |
| 386 | if (workersForJob > 1 && this.canParallelize(job)) { |
| 387 | const daysWithData = |
| 388 | job.data.exportData?.days?.filter( |
| 389 | (day) => day.heartbeats && day.heartbeats.length > 0, |
| 390 | ) || []; |
| 391 | this.createWorkChunks(job, daysWithData, "wakatime"); |
| 392 | |
| 393 | const firstChunk = Array.from(this.workChunks.values()).find( |
| 394 | (chunk) => chunk.jobId === job.id && chunk.status === "pending", |
| 395 | ); |
| 396 | |
| 397 | if (firstChunk) { |
| 398 | firstChunk.status = "processing"; |
| 399 | firstChunk.workerId = workerId; |
| 400 | this.workerJobAssignment.set(workerId, job.id); |
| 401 | return { chunk: firstChunk }; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | this.workerJobAssignment.set(workerId, job.id); |
| 406 | return { job }; |
| 407 | } |
| 408 |
no test coverage detected