mirror of https://github.com/kiwix/libkiwix.git
Merge pull request #292 from kiwix/aria2-comment
More error handling for aria2 cmd
This commit is contained in:
commit
f0ac66aea1
|
@ -97,6 +97,7 @@ class Downloader
|
||||||
|
|
||||||
size_t getNbDownload() { return m_knownDownloads.size(); }
|
size_t getNbDownload() { return m_knownDownloads.size(); }
|
||||||
std::vector<std::string> getDownloadIds();
|
std::vector<std::string> getDownloadIds();
|
||||||
|
const std::string &getAria2LaunchCmd();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<std::string, std::unique_ptr<Download>> m_knownDownloads;
|
std::map<std::string, std::unique_ptr<Download>> m_knownDownloads;
|
||||||
|
|
|
@ -78,18 +78,32 @@ Aria2::Aria2():
|
||||||
callCmd.push_back("--max-concurrent-downloads=42");
|
callCmd.push_back("--max-concurrent-downloads=42");
|
||||||
callCmd.push_back("--rpc-max-request-size=6M");
|
callCmd.push_back("--rpc-max-request-size=6M");
|
||||||
callCmd.push_back("--file-allocation=none");
|
callCmd.push_back("--file-allocation=none");
|
||||||
|
for (auto &cmd : callCmd) {
|
||||||
|
m_launchCmd.append(cmd).append(" ");
|
||||||
|
}
|
||||||
mp_aria = Subprocess::run(callCmd);
|
mp_aria = Subprocess::run(callCmd);
|
||||||
mp_curl = curl_easy_init();
|
mp_curl = curl_easy_init();
|
||||||
|
char errbuf[CURL_ERROR_SIZE];
|
||||||
curl_easy_setopt(mp_curl, CURLOPT_URL, "http://localhost/rpc");
|
curl_easy_setopt(mp_curl, CURLOPT_URL, "http://localhost/rpc");
|
||||||
curl_easy_setopt(mp_curl, CURLOPT_PORT, m_port);
|
curl_easy_setopt(mp_curl, CURLOPT_PORT, m_port);
|
||||||
curl_easy_setopt(mp_curl, CURLOPT_POST, 1L);
|
curl_easy_setopt(mp_curl, CURLOPT_POST, 1L);
|
||||||
|
curl_easy_setopt(mp_curl, CURLOPT_ERRORBUFFER, errbuf);
|
||||||
|
|
||||||
int watchdog = 50;
|
int watchdog = 50;
|
||||||
while(--watchdog) {
|
while(--watchdog) {
|
||||||
sleep(10);
|
sleep(10);
|
||||||
|
errbuf[0] = 0;
|
||||||
auto res = curl_easy_perform(mp_curl);
|
auto res = curl_easy_perform(mp_curl);
|
||||||
if (res == CURLE_OK) {
|
if (res == CURLE_OK) {
|
||||||
break;
|
break;
|
||||||
|
} else if (watchdog == 1) {
|
||||||
|
std::cerr <<" curl_easy_perform() failed." << std::endl;
|
||||||
|
fprintf(stderr, "\nlibcurl: (%d) ", res);
|
||||||
|
if (errbuf[0] != 0) {
|
||||||
|
std::cerr << errbuf << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cerr << curl_easy_strerror(res) << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!watchdog) {
|
if (!watchdog) {
|
||||||
|
|
|
@ -26,6 +26,7 @@ class Aria2
|
||||||
std::string m_downloadDir;
|
std::string m_downloadDir;
|
||||||
CURL* mp_curl;
|
CURL* mp_curl;
|
||||||
pthread_mutex_t m_lock;
|
pthread_mutex_t m_lock;
|
||||||
|
std::string m_launchCmd;
|
||||||
|
|
||||||
std::string doRequest(const MethodCall& methodCall);
|
std::string doRequest(const MethodCall& methodCall);
|
||||||
|
|
||||||
|
@ -43,6 +44,7 @@ class Aria2
|
||||||
void pause(const std::string& gid);
|
void pause(const std::string& gid);
|
||||||
void unpause(const std::string& gid);
|
void unpause(const std::string& gid);
|
||||||
void remove(const std::string& gid);
|
void remove(const std::string& gid);
|
||||||
|
const std::string &getLaunchCmd() { return m_launchCmd; };
|
||||||
};
|
};
|
||||||
|
|
||||||
}; //end namespace kiwix
|
}; //end namespace kiwix
|
||||||
|
|
|
@ -127,17 +127,24 @@ void Download::cancelDownload()
|
||||||
Downloader::Downloader() :
|
Downloader::Downloader() :
|
||||||
mp_aria(new Aria2())
|
mp_aria(new Aria2())
|
||||||
{
|
{
|
||||||
for (auto gid : mp_aria->tellActive()) {
|
try {
|
||||||
m_knownDownloads[gid] = std::unique_ptr<Download>(new Download(mp_aria, gid));
|
for (auto gid : mp_aria->tellActive()) {
|
||||||
m_knownDownloads[gid]->updateStatus();
|
m_knownDownloads[gid] = std::unique_ptr<Download>(new Download(mp_aria, gid));
|
||||||
|
m_knownDownloads[gid]->updateStatus();
|
||||||
|
}
|
||||||
|
} catch (std::exception& e) {
|
||||||
|
std::cerr << "aria2 tellActive failed : " << e.what();
|
||||||
}
|
}
|
||||||
for (auto gid : mp_aria->tellWaiting()) {
|
try {
|
||||||
m_knownDownloads[gid] = std::unique_ptr<Download>(new Download(mp_aria, gid));
|
for (auto gid : mp_aria->tellWaiting()) {
|
||||||
m_knownDownloads[gid]->updateStatus();
|
m_knownDownloads[gid] = std::unique_ptr<Download>(new Download(mp_aria, gid));
|
||||||
|
m_knownDownloads[gid]->updateStatus();
|
||||||
|
}
|
||||||
|
} catch (std::exception& e) {
|
||||||
|
std::cerr << "aria2 tellWaiting failed : " << e.what();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Destructor */
|
/* Destructor */
|
||||||
Downloader::~Downloader()
|
Downloader::~Downloader()
|
||||||
{
|
{
|
||||||
|
@ -156,6 +163,11 @@ std::vector<std::string> Downloader::getDownloadIds() {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::string &Downloader::getAria2LaunchCmd()
|
||||||
|
{
|
||||||
|
return mp_aria->getLaunchCmd();
|
||||||
|
}
|
||||||
|
|
||||||
Download* Downloader::startDownload(const std::string& uri)
|
Download* Downloader::startDownload(const std::string& uri)
|
||||||
{
|
{
|
||||||
for (auto& p: m_knownDownloads) {
|
for (auto& p: m_knownDownloads) {
|
||||||
|
|
Loading…
Reference in New Issue