From bc3918b2aed033e1c7617c0610e2e363c9e605db Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 22 Jun 2023 15:02:27 +0200 Subject: [PATCH] Shuffle servers when checking available jobs To not favour a specific instance --- .../server/process/shared/process-studio.ts | 4 ++++ .../server/process/shared/process-vod.ts | 20 +++++++++++++++++++ packages/peertube-runner/server/server.ts | 4 ++-- shared/core-utils/common/array.ts | 14 +++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/packages/peertube-runner/server/process/shared/process-studio.ts b/packages/peertube-runner/server/process/shared/process-studio.ts index 9c745d031..ce014495e 100644 --- a/packages/peertube-runner/server/process/shared/process-studio.ts +++ b/packages/peertube-runner/server/process/shared/process-studio.ts @@ -20,10 +20,14 @@ export async function processStudioTranscoding (options: ProcessOptions (elements: T[]) { return Array.from(new Set(elements)) } +// Thanks: https://stackoverflow.com/a/12646864 +function shuffle (elements: T[]) { + const shuffled = [ ...elements ] + + for (let i = shuffled.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + + [ shuffled[i], shuffled[j] ] = [ shuffled[j], shuffled[i] ] + } + + return shuffled +} + export { uniqify, findCommonElement, + shuffle, arrayify }