Skip to content

Commit

Permalink
Add storage daemon download max times limit
Browse files Browse the repository at this point in the history
Signed-off-by: KayleCoder <[email protected]>
  • Loading branch information
KayleCoder committed Jul 31, 2024
1 parent ffc1c2b commit 294e811
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
}
12 changes: 11 additions & 1 deletion src/dao/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down
35 changes: 33 additions & 2 deletions src/service/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down
2 changes: 2 additions & 0 deletions src/type/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

0 comments on commit 294e811

Please sign in to comment.