UNPKG

@qso-soft/shared

Version:

Shared library for QSO-soft

102 lines 4.55 kB
import dayjs from 'dayjs'; import duration from 'dayjs/plugin/duration'; import { saveFailedWalletsToCSV } from '../file-handlers'; import { clearSavedModules, getSavedModules, savePreparedModules, updateSavedModulesFinishStatus, } from '../modules/save-modules'; import { initLocalLogger, showLogSelectedModules } from '../show-logs'; import { sleep } from '../utils'; import { prepareSavedWalletsWithModules, prepareWalletsData, prepareWalletsWithModules } from '../wallets'; import { restartLast } from './restart-last'; import { startWithThreads } from './threads'; dayjs.extend(duration); export const runMainScript = async (props) => { const { clientToPrepareWallets, logsFolderName, routesField, routeHandler, startModulesCallback, projectName, ...restProps } = props; const jsonWallets = await prepareWalletsData({ logsFolderName, projectName, client: clientToPrepareWallets, }); const { threads } = QsoGlobal.settings; const routes = QsoGlobal.settings[routesField]; for (const route of routes) { const logger = initLocalLogger(logsFolderName, `${route}`); logger.setLoggerMeta({ moduleName: 'Main' }); try { const routeSettings = routeHandler(route); clearSavedModules(projectName); showLogSelectedModules(routeSettings, route, logger); const areModulesEmpty = routeSettings.modules.length === 0; if (areModulesEmpty) { logger.error('Modules can not be empty', { status: 'failed' }); continue; } const walletsWithModules = await prepareWalletsWithModules({ ...restProps, route, routeSettings, logger, jsonWallets, projectName, delayBetweenTransactions: QsoGlobal.settings.delay.betweenTransactions, shouldShuffleModules: QsoGlobal.settings.shuffle.modules, shouldShuffleWallets: QsoGlobal.settings.shuffle.wallets, }); if (!walletsWithModules?.length) { logger.error('Unable to prepare wallets'); continue; } if (QsoGlobal.settings.useSavedModules) { savePreparedModules({ walletsWithModules, route, projectName, }); } logger.success(`Starting script in [${threads}] threads`); const results = await startWithThreads({ size: threads, array: walletsWithModules, callback: async (walletWithModules, _, currentWalletIndex) => startModulesCallback({ walletWithModules, logsFolderName, walletsTotalCount: walletsWithModules.length, currentWalletIndex, }), logger, }); if (QsoGlobal.settings.useSavedModules) { updateSavedModulesFinishStatus({ projectName }); } saveFailedWalletsToCSV({ results: results.flat(), logger, projectName }); } catch (error) { logger.error(`${error}`, { status: 'failed' }); continue; } } if (QsoGlobal.settings.useRestartInMain) { let savedModules = getSavedModules(projectName); let walletsWithModulesToRestart = prepareSavedWalletsWithModules(savedModules); while (walletsWithModulesToRestart?.length) { await restartLast({ logsFolderName, projectName, startModulesCallback, clientToPrepareWallets, savedModules, walletsWithModules: walletsWithModulesToRestart, }); savedModules = getSavedModules(projectName); walletsWithModulesToRestart = prepareSavedWalletsWithModules(savedModules); } } const delayBetweenRestarts = QsoGlobal.settings.delay.betweenRestarts; const logger = initLocalLogger(logsFolderName, 'main'); logger.setLoggerMeta({ moduleName: 'Main' }); if (delayBetweenRestarts > 0) { const secondsFromHours = dayjs.duration(delayBetweenRestarts, 'hour').asSeconds(); await sleep(secondsFromHours, {}, logger, `Fell asleep for ${delayBetweenRestarts}h before script restarts`); await runMainScript(props); } return; }; //# sourceMappingURL=main-script.js.map