diff --git a/lib/account-rotation.js b/lib/account-rotation.js index 801b20a..81b95ba 100644 --- a/lib/account-rotation.js +++ b/lib/account-rotation.js @@ -24,6 +24,21 @@ function createAccountPicker({ hosters, hosterSettings, hasCreds, indices }) { return pick; } +function selectFallbackAccount({ accounts, hoster, currentAccountId, failedKeys, preferredAccountId, excludedIds, hasCreds = () => true }) { + if (!Array.isArray(accounts)) return null; + const usable = account => account && account.id && account.id !== currentAccountId + && account.enabled !== false && hasCreds(hoster, account) + && !failedKeys?.has(`${hoster}:${account.id}`) && !excludedIds?.has(account.id); + const preferred = accounts.find(account => account?.id === preferredAccountId && usable(account)); + if (preferred) return preferred; + const currentIndex = accounts.findIndex(account => account?.id === currentAccountId); + for (let offset = 1; offset <= accounts.length; offset++) { + const account = accounts[(currentIndex + offset) % accounts.length]; + if (usable(account)) return account; + } + return null; +} + function classifyAccountFailure(error) { if (!error || error.transientNetwork === true || error.hosterTransient === true || error.fileRejected === true) return 'none'; if (error.otpRequired === true) return 'manual'; @@ -175,5 +190,6 @@ module.exports = { classifyAccountFailure, createAccountCooldownController, createAccountPicker, + selectFallbackAccount, enabledAccountsFor }; diff --git a/lib/upload-manager.js b/lib/upload-manager.js index 7aa90f7..223978c 100644 --- a/lib/upload-manager.js +++ b/lib/upload-manager.js @@ -12,7 +12,7 @@ const Semaphore = require('./semaphore'); const Throttle = require('./throttle'); const { probeFileHead } = require('./file-probe'); const { normalizeFailureDetails } = require('./upload-diagnostics'); -const { classifyAccountFailure } = require('./account-rotation'); +const { classifyAccountFailure, selectFallbackAccount } = require('./account-rotation'); const DEFAULT_SETTINGS = { retries: 3, @@ -97,6 +97,18 @@ class UploadManager extends EventEmitter { return this._accountOverrides.get(hoster) || null; } + _getFallbackAccount(hoster, currentAccountId, excludedIds) { + const override = this.getOverride(hoster); + return selectFallbackAccount({ + accounts: this.accountPools[hoster] || (override ? [override] : []), + hoster, + currentAccountId, + failedKeys: this._failedAccounts, + preferredAccountId: override?.id, + excludedIds + }); + } + getActiveJobCount() { return this.activeJobs.size; } @@ -123,7 +135,9 @@ class UploadManager extends EventEmitter { } _emitAccountSucceeded(task) { - if (task?.hoster && task?.accountId) this.emit('account-succeeded', { hoster: task.hoster, accountId: task.accountId }); + if (task?.hoster && task?.accountId && !this._failedAccounts.has(`${task.hoster}:${task.accountId}`)) { + this.emit('account-succeeded', { hoster: task.hoster, accountId: task.accountId }); + } } // True if the hoster has a usable override stored that differs from the @@ -604,7 +618,7 @@ class UploadManager extends EventEmitter { // with 500 queued jobs and 1 parallel slot, without this check every // job still hits the original dead account first. if (task.accountId && this._failedAccounts.has(task.hoster + ':' + task.accountId)) { - const override = this._accountOverrides.get(task.hoster); + const override = this._getFallbackAccount(task.hoster, task.accountId); if (override && !this._failedAccounts.has(task.hoster + ':' + override.id)) { this._rotLog('pre-job-swap', { jobId, hoster: task.hoster, fileName, fromAccountId: task.accountId, toAccountId: override.id @@ -952,6 +966,7 @@ class UploadManager extends EventEmitter { recordFinalResult('error', { error }); return; } + const attemptedAccounts = new Set([task.accountId]); while (task.accountId) { if (signal.aborted || this.stopAfterActive) break; // The rotated-to account failed with a file-class error (file @@ -995,7 +1010,7 @@ class UploadManager extends EventEmitter { lastError: lastError ? lastError.message : null }); } - const override = this._accountOverrides.get(task.hoster); + const override = this._getFallbackAccount(task.hoster, task.accountId, attemptedAccounts); if (!override) { this._rotLog('rotation-end', { jobId, hoster: task.hoster, fileName, reason: 'no-override-set', @@ -1023,6 +1038,7 @@ class UploadManager extends EventEmitter { fromAccountId: task.accountId, toAccountId: override.id }); task.accountId = override.id; + attemptedAccounts.add(override.id); task.username = override.username; task.password = override.password; task.apiKey = override.apiKey; diff --git a/main.js b/main.js index 1e35801..72d2e63 100644 --- a/main.js +++ b/main.js @@ -15,7 +15,7 @@ const VidmolyUploader = require('./lib/vidmoly-upload'); const VoeUploader = require('./lib/voe-upload'); const DoodstreamUploader = require('./lib/doodstream-upload'); const { createDoodstreamOtpCoordinator, selectUploadAuth } = require('./lib/account-auth'); -const { createAccountCooldownController, createAccountPicker } = require('./lib/account-rotation'); +const { createAccountCooldownController, createAccountPicker, selectFallbackAccount } = require('./lib/account-rotation'); const ClouddropUploader = require('./lib/clouddrop-upload'); const { checkForUpdate, prepareUpdate, launchPreparedUpdate, abortUpdate, createUpdateAnnouncementState } = require('./lib/updater'); const backupCrypto = require('./lib/backup-crypto'); @@ -1156,16 +1156,19 @@ function hosterAccountHasCreds(name, account) { } function getNextFallbackAccount(config, hosterName, failedAccountId) { - const accounts = config.hosters[hosterName]; - if (!Array.isArray(accounts)) return null; - const failedIndex = accounts.findIndex(a => a.id === failedAccountId); - if (failedIndex < 0) return null; - for (let i = failedIndex + 1; i < accounts.length; i++) { - if (accounts[i].enabled !== false && hosterAccountHasCreds(hosterName, accounts[i])) { - return accounts[i]; - } - } - return null; + const failedKeys = new Set([ + ..._accountCooldowns.activeKeys(), + ...(uploadManager?.getFailedAccountKeys() || []) + ]); + const preferred = uploadManager?.getOverride(hosterName) || _sessionAccountOverrides.get(hosterName); + return selectFallbackAccount({ + accounts: config.hosters?.[hosterName], + hoster: hosterName, + currentAccountId: failedAccountId, + failedKeys, + preferredAccountId: preferred?.id, + hasCreds: hosterAccountHasCreds + }); } function buildAccountPools(config) { @@ -1912,7 +1915,6 @@ ipcMain.handle('save-config', async (_event, config) => { if (sep < 0) continue; const hoster = key.slice(0, sep); const failedAccountId = key.slice(sep + 1); - if (uploadManager.getOverride(hoster)) continue; // already has a fallback const fallback = getNextFallbackAccount(cfg, hoster, failedAccountId); if (fallback) { rotLog(`main: config-updated → late fallback ${fallback.id} for ${hoster} (was stuck on ${failedAccountId})`); @@ -2396,7 +2398,6 @@ ipcMain.handle('start-upload', async (_event, payload) => { // _failedAccounts), so jobs still start on the primary as expected. const hostersInBatch = new Set(tasks.map(t => t.hoster).filter(Boolean)); for (const hoster of hostersInBatch) { - if (_sessionAccountOverrides.has(hoster)) continue; // already learned from past batch const accounts = config.hosters && config.hosters[hoster]; if (!Array.isArray(accounts) || accounts.length < 2) continue; const primary = accounts.find(a => a && a.enabled !== false && hosterAccountHasCreds(hoster, a));