Skip to content

Commit

Permalink
Merge pull request #924 from KomodoPlatform/coingecko_auto_retry
Browse files Browse the repository at this point in the history
Coingecko auto retry
  • Loading branch information
Milerius authored May 18, 2021
2 parents 211694a + da33bb1 commit 5b8195a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 43 deletions.
12 changes: 5 additions & 7 deletions atomic_defi_design/qml/Portfolio/AmountChart.qml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import "../Constants"
// Portfolio
InnerBackground {
id: portfolio_asset_chart
property bool isProgress: true
property bool isProgress: false
function drawChart() {
areaLine.clear()
areaLine3.clear()
Expand Down Expand Up @@ -55,7 +55,7 @@ InnerBackground {
if(API.app.portfolio_pg.charts.length===0){
restart()
}else {
portfolio_asset_chart.isProgress = true
portfolio_asset_chart.isProgress = false
drawTimer.restart()
}
}
Expand All @@ -71,17 +71,15 @@ InnerBackground {
target: API.app.portfolio_pg
function onChart_busy_fetchingChanged() {
if(!API.app.portfolio_pg.chart_busy_fetching){
portfolio_asset_chart.isProgress = true
portfolio_asset_chart.isProgress = false
chart2Timer.restart()
}
}
}

Component.onCompleted: {
portfolio_asset_chart.isProgress = true
chart2Timer.restart()


portfolio_asset_chart.isProgress = false
// chart2Timer.restart()
}
property real mX: 0
property real mY: 0
Expand Down
22 changes: 20 additions & 2 deletions src/core/atomicdex/services/price/coingecko/coingecko.provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,26 @@ namespace atomic_dex
coingecko_provider::internal_update(
const std::vector<std::string>& ids, const std::unordered_map<std::string, std::string>& registry, bool should_move, std::vector<std::string> tickers)
{
static std::atomic_uint16_t nb_try = 0;
nb_try += 1;
if (!ids.empty())
{
SPDLOG_INFO("Processing internal_update");

auto error_functor = [this, ids, registry, should_move, tickers](pplx::task<void> previous_task)
{
try
{
previous_task.wait();
}
catch (const std::exception& e)
{
SPDLOG_ERROR("pplx task error from coingecko::api::async_market_infos: {} - nb_try {}", e.what(), nb_try.load());
using namespace std::chrono_literals;
std::this_thread::sleep_for(1s);
this->internal_update(ids, registry, should_move, tickers);
};
};
t_coingecko_market_infos_request request{.ids = std::move(ids)};
const auto answer_functor = [this, registry, should_move, tickers](web::http::http_response resp) {
std::string body = TO_STD_STR(resp.extract_string(true).get());
Expand Down Expand Up @@ -137,19 +153,21 @@ namespace atomic_dex
{
dispatcher_.trigger<fiat_rate_updated>("");
}
SPDLOG_INFO("Coingecko rates successfully updated");
SPDLOG_INFO("Coingecko rates successfully updated after nb_try: {}", nb_try.load());
nb_try = 0;
}
else
{
SPDLOG_ERROR("Error during the rpc call to coingecko: {}", body);
}
};
coingecko::api::async_market_infos(std::move(request)).then(answer_functor).then(&handle_exception_pplx_task);
coingecko::api::async_market_infos(std::move(request)).then(answer_functor).then(error_functor);
}
else
{
//! If it's only test coin
dispatcher_.trigger<coin_fully_initialized>(tickers);
nb_try = 0;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace atomic_dex
{
SPDLOG_INFO("coingecko_wallet_charts_service created");
m_update_clock = std::chrono::high_resolution_clock::now();
this->disable();
}

coingecko_wallet_charts_service::~coingecko_wallet_charts_service() { SPDLOG_INFO("coingecko_wallet_charts_service destroyed"); }
Expand Down Expand Up @@ -305,47 +306,50 @@ namespace atomic_dex
void
coingecko_wallet_charts_service::manual_refresh(const std::string& from)
{
SPDLOG_INFO("manual refresh from: {}", from);
const auto wallet_obj = m_wallet_performance.get();
const bool is_valid = wallet_obj.contains("wallet_evolution");
const auto balance_fiat_all = m_system_manager.get_system<portfolio_page>().get_main_balance_fiat_all();
if (is_valid && from.find("set_chart_category") == std::string::npos)
if (this->is_enabled())
{
const auto previous = wallet_obj["wallet_evolution"].toObject().value("last_total_balance_fiat_all").toString();
if (previous == balance_fiat_all)
{
SPDLOG_INFO("Skipping refresh, balance doesn't change between last calls");
return;
}
}
if (m_is_busy)
{
SPDLOG_WARN("Service is busy, try later");
return;
}
auto functor = [this]()
{
try
SPDLOG_INFO("manual refresh from: {}", from);
const auto wallet_obj = m_wallet_performance.get();
const bool is_valid = wallet_obj.contains("wallet_evolution");
const auto balance_fiat_all = m_system_manager.get_system<portfolio_page>().get_main_balance_fiat_all();
if (is_valid && from.find("set_chart_category") == std::string::npos)
{
const auto previous = wallet_obj["wallet_evolution"].toObject().value("last_total_balance_fiat_all").toString();
if (previous == balance_fiat_all)
{
SPDLOG_INFO("Waiting for previous call to be finished");
m_executor.wait_for_all();
m_taskflow.clear();
m_chart_data_registry->clear();
m_min_value = "0";
m_max_value = "0";
m_wallet_performance = QJsonObject();
SPDLOG_INFO("Skipping refresh, balance doesn't change between last calls");
return;
}
fetch_all_charts_data();
m_update_clock = std::chrono::high_resolution_clock::now();
}
catch (const std::exception& error)
if (m_is_busy)
{
SPDLOG_ERROR("Exception caught: {}", error.what());
SPDLOG_WARN("Service is busy, try later");
return;
}
};
//[[maybe_unused]] auto res = std::async(functor);
functor();
auto functor = [this]()
{
try
{
{
SPDLOG_INFO("Waiting for previous call to be finished");
m_executor.wait_for_all();
m_taskflow.clear();
m_chart_data_registry->clear();
m_min_value = "0";
m_max_value = "0";
m_wallet_performance = QJsonObject();
}
fetch_all_charts_data();
m_update_clock = std::chrono::high_resolution_clock::now();
}
catch (const std::exception& error)
{
SPDLOG_ERROR("Exception caught: {}", error.what());
}
};
//[[maybe_unused]] auto res = std::async(functor);
functor();
}
}

bool
Expand Down

0 comments on commit 5b8195a

Please sign in to comment.