Skip to content

Commit

Permalink
Active downloads are paused before starting aria2c
Browse files Browse the repository at this point in the history
The aria session file is edited before starting aria2c. This ensures
responsive aria2c RPC server even after a crash.
  • Loading branch information
veloman-yunkan committed Jul 8, 2024
1 parent 42295c9 commit 65a777d
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/aria2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "xmlrpc.h"
#include <iostream>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <thread>
#include <chrono>
Expand All @@ -29,6 +30,31 @@

namespace kiwix {

namespace {

void pauseAnyActiveDownloads(const std::string& ariaSessionFilePath)

Check warning on line 35 in src/aria2.cpp

View check run for this annotation

Codecov / codecov/patch

src/aria2.cpp#L35

Added line #L35 was not covered by tests
{
std::ifstream inputFile(ariaSessionFilePath);
if ( !inputFile )
return;

Check warning on line 39 in src/aria2.cpp

View check run for this annotation

Codecov / codecov/patch

src/aria2.cpp#L39

Added line #L39 was not covered by tests

std::ostringstream ss;
std::string line;

Check warning on line 42 in src/aria2.cpp

View check run for this annotation

Codecov / codecov/patch

src/aria2.cpp#L42

Added line #L42 was not covered by tests
while ( std::getline(inputFile, line) ) {
if ( !startsWith(line, " pause=") ) {
ss << line << "\n";
}
if ( !line.empty() && line[0] != ' ' && line[0] != '#' ) {
ss << " pause=true\n";
}
}

std::ofstream outputFile(ariaSessionFilePath);
outputFile << ss.str();
}

} // unnamed namespace

Aria2::Aria2():
mp_aria(nullptr),
m_port(42042),
Expand All @@ -41,6 +67,7 @@ Aria2::Aria2():
std::string rpc_port = "--rpc-listen-port=" + to_string(m_port);
std::string download_dir = "--dir=" + getDataDirectory();
std::string session_file = appendToDirectory(getDataDirectory(), "kiwix.session");
pauseAnyActiveDownloads(session_file);
std::string session = "--save-session=" + session_file;
std::string inputFile = "--input-file=" + session_file;
// std::string log_dir = "--log=\"" + logDir + "\"";
Expand Down Expand Up @@ -127,9 +154,6 @@ Aria2::Aria2():

void Aria2::close()
{
MethodCall methodCall("aria2.pauseAll", m_secret);
doRequest(methodCall);

saveSession();
shutdown();
}
Expand Down

0 comments on commit 65a777d

Please sign in to comment.