common : add download cancellation and temp file cleanup (#21813)
Signed-off-by: Adrien Gallouët <angt@huggingface.co>
This commit is contained in:
@@ -258,6 +258,9 @@ static bool common_pull_file(httplib::Client & cli,
|
|||||||
if (progress_step >= p.total / 1000 || p.downloaded == p.total) {
|
if (progress_step >= p.total / 1000 || p.downloaded == p.total) {
|
||||||
if (callback) {
|
if (callback) {
|
||||||
callback->on_update(p);
|
callback->on_update(p);
|
||||||
|
if (callback->is_cancelled()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
progress_step = 0;
|
progress_step = 0;
|
||||||
}
|
}
|
||||||
@@ -373,6 +376,9 @@ static int common_download_file_single_online(const std::string & url,
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < max_attempts; ++i) {
|
for (int i = 0; i < max_attempts; ++i) {
|
||||||
|
if (opts.callback && opts.callback->is_cancelled()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (i) {
|
if (i) {
|
||||||
LOG_WRN("%s: retrying after %d seconds...\n", __func__, delay);
|
LOG_WRN("%s: retrying after %d seconds...\n", __func__, delay);
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(delay));
|
std::this_thread::sleep_for(std::chrono::seconds(delay));
|
||||||
@@ -412,6 +418,12 @@ static int common_download_file_single_online(const std::string & url,
|
|||||||
if (opts.callback) {
|
if (opts.callback) {
|
||||||
opts.callback->on_done(p, success);
|
opts.callback->on_done(p, success);
|
||||||
}
|
}
|
||||||
|
if (opts.callback && opts.callback->is_cancelled() &&
|
||||||
|
std::filesystem::exists(path_temporary)) {
|
||||||
|
if (remove(path_temporary.c_str()) != 0) {
|
||||||
|
LOG_ERR("%s: unable to delete temporary file: %s\n", __func__, path_temporary.c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!success) {
|
if (!success) {
|
||||||
LOG_ERR("%s: download failed after %d attempts\n", __func__, max_attempts);
|
LOG_ERR("%s: download failed after %d attempts\n", __func__, max_attempts);
|
||||||
return -1; // max attempts reached
|
return -1; // max attempts reached
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ public:
|
|||||||
virtual void on_start(const common_download_progress & p) = 0;
|
virtual void on_start(const common_download_progress & p) = 0;
|
||||||
virtual void on_update(const common_download_progress & p) = 0;
|
virtual void on_update(const common_download_progress & p) = 0;
|
||||||
virtual void on_done(const common_download_progress & p, bool ok) = 0;
|
virtual void on_done(const common_download_progress & p, bool ok) = 0;
|
||||||
|
virtual bool is_cancelled() const { return false; }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct common_remote_params {
|
struct common_remote_params {
|
||||||
|
|||||||
Reference in New Issue
Block a user