diff --git a/src/config/index.ts b/src/config/index.ts index 9651e25..9538e26 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -18,7 +18,9 @@ export const configs = { maxFileSize: BigInt(getEnvOrExit("TASK_MAX_FILE_SIZE", "10485760", false)), providerMnemonic: getEnvOrExit("TASK_PROVIDER_MNEMONIC", ""), providerMinBalance: getEnvOrExit("TASK_PROVIDER_MIN_BALANCE", "1000000000"), - submitStorageProofBefore: getEnvOrExit("TASK_SUBMIT_STORAGE_PROOF_BEFORE", "1800") + submitStorageProofBefore: getEnvOrExit("TASK_SUBMIT_STORAGE_PROOF_BEFORE", "1800"), + maxDownloadHeaderTimes: getEnvOrExit("TASK_MAX_DOWNLOAD_HEADER_TIMES", "10"), + maxDownloadChildTimes: getEnvOrExit("TASK_MAX_DOWNLOAD_CHILD_TIMES", "10"), }, tonStorageUtilsApi: getEnvOrExit('TON_STORAGE_UTILS_API', 'http://localhost:8192'), } diff --git a/src/dao/task.ts b/src/dao/task.ts index 410951d..2be36ab 100644 --- a/src/dao/task.ts +++ b/src/dao/task.ts @@ -45,7 +45,17 @@ export class Task { type: DataTypes.BIGINT, allowNull: false, defaultValue: 0 - } + }, + download_header_retry_times: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: 0 + }, + download_child_retry_times: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: 0 + }, }, { indexes: [ { diff --git a/src/service/storage.ts b/src/service/storage.ts index a97419d..12052b6 100644 --- a/src/service/storage.ts +++ b/src/service/storage.ts @@ -11,6 +11,7 @@ import { } from "../merkle/tonsutils"; import {logger} from "../util/logger"; import {Op} from "sequelize"; +import {configs} from "../config"; export async function downloadTorrentHeaders() { while(true) { @@ -39,7 +40,22 @@ export async function downloadTorrentHeaders() { } await addTonBag(torrentHash); } catch (e) { - logger.error(`Failed to download meta bag ${torrentHash}: ${e}`); + logger.error(`Failed to download meta bag ${torrentHash}: ${e.message}`); + const retryTimes = task.download_header_retry_times + 1; + let item: any = { + download_header_retry_times: retryTimes + } + if (retryTimes > Number(configs.task.maxDownloadHeaderTimes)) { + item = { + ...item, + task_state: TaskState.download_torrent_header_failed + } + } + await Task.model.update(item, { + where: { + id: task.id + } + }); continue; } await Task.model.update({ @@ -103,7 +119,22 @@ export async function downloadChildFiles() { } await downloadChildTonBag(torrentHash); } catch (e) { - logger.error(`Failed to download child bag ${torrentHash}: ${e}`); + logger.error(`Failed to download child bag ${torrentHash}: ${e.message}`); + const retryTimes = task.download_child_retry_times + 1; + let item: any = { + download_child_retry_times: retryTimes + } + if (retryTimes > Number(configs.task.maxDownloadChildTimes)) { + item = { + ...item, + task_state: TaskState.download_torrent_child_file_failed + } + } + await Task.model.update(item, { + where: { + id: task.id + } + }); continue; } await Task.model.update({ diff --git a/src/type/common.ts b/src/type/common.ts index a07f354..bfc29ee 100644 --- a/src/type/common.ts +++ b/src/type/common.ts @@ -22,4 +22,6 @@ export enum TaskState { period_finish = 6, task_finish = 7, more_than_max_storage_provider_count = -1, + download_torrent_header_failed = -2, + download_torrent_child_file_failed = -3, }