Skip to content

Commit

Permalink
Add a mechanism to allow temporary queue at 1m trigger for failing …
Browse files Browse the repository at this point in the history
…operations.
  • Loading branch information
diegomanuel committed Feb 4, 2021
1 parent 8e1e4cc commit edb82b3
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 17 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ Therefore, you will agree upon your own fully responsibility at the very moment
No personal data collect and/or usage is done in any way, that's why this `add-on` doesn't require any _"controversial"_ permission from your side.
The only _sensitive scopes_ according to **Google** are:
* `script.external_request` :: Needed to **fetch data from Binance API** into the spreadsheet (GET requests only).
* `script.scriptapp` :: Needed to **install and run 3 triggers** to keep data updated in the spreadsheet (every 1, 5 and 10 minutes).
* `script.scriptapp` :: Needed to **install and run triggers** to keep data updated in the spreadsheet.

**NOTE:** This is an _open-source_ project, so you will always be available to keep and eye to the code and audit it.
If you have any concerns, please feel free to open a ticket in the [issues](https://github.com/diegomanuel/binance-to-google-sheets/issues) section or email me.
Expand All @@ -203,13 +203,12 @@ No other service acts as an intermediary between your Google spreadsheet and Bin
**NOTE:** If you have any concerns, please feel free to open a ticket in the [issues](https://github.com/diegomanuel/binance-to-google-sheets/issues) section or email me.


## Binance Account - Get 5% discount on fees!
## Binance Account - Get 10% discount on fees!

Don't you have a **Binance** account yet?
Register using the **referal link** below and get a **5% discount on fees** for **all** your trades!
Register using the **referal link** below and get a **10% discount on fees** for **all** your trades!

[**https://www.binance.com/en/register?ref=NJE1D9CS**](https://www.binance.com/en/register?ref=NJE1D9CS)
<a href="https://www.binance.com/en/register?ref=NJE1D9CS" target="_blank"><img src="img/binance-join.png" alt="Join to Binance!" title="Join to Binance!"/></a>
[**https://www.binance.com/en/register?ref=SM93PRAV**](https://www.binance.com/en/register?ref=SM93PRAV)


## Enjoy - Donate - Buy me a beer! =]
Expand Down
Binary file removed img/binance-join.png
Binary file not shown.
2 changes: 1 addition & 1 deletion misc/config.gs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

const DEBUG = false;
const VERSION = "v0.3.1";
const VERSION = "v0.3.2";
const REPO_URL = "https://github.com/diegomanuel/binance-to-google-sheets";
const API_KEY_NAME = "BIN_API_KEY";
const API_SECRET_NAME = "BIN_API_SECRET";
Expand Down
34 changes: 32 additions & 2 deletions misc/scheduler.gs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
function BinScheduler(OPTIONS) {
OPTIONS = OPTIONS || {}; // Init options
const SCHEDULES_PROP_NAME = "BIN_SCHEDULER_ENTRIES";
const RESCHEDULES_PROP_NAME = "BIN_SCHEDULER_ENTRIES_RETRY";
const LAST_RUN_PROP_NAME = "BIN_SCHEDULER_LAST_RUN";

return {
Expand All @@ -18,6 +19,7 @@ function BinScheduler(OPTIONS) {
setSchedule,
cleanSchedules,
rescheduleFailed,
clearFailed,
isStalled
};

Expand Down Expand Up @@ -81,6 +83,10 @@ function BinScheduler(OPTIONS) {
* Returns the scheduled interval for given operation (or all schedules if no operation given)
*/
function getSchedule(operation) {
const rescheduled = _getRescheduled();
if (operation && rescheduled[operation]) { // This operation failed before and was re-sheduled!
return rescheduled[operation];
}
const props = _getDocPropService().getProperty(SCHEDULES_PROP_NAME);
const schedules = props ? JSON.parse(props) : {};
return operation ? schedules[operation] : schedules;
Expand All @@ -106,10 +112,29 @@ function BinScheduler(OPTIONS) {
}

/**
* Re-schedule failed executions so they can be retried ASAP (at 1 minute trigger)
* Re-schedule failed execution for given operation so it can be retried ASAP (at 1m trigger)
*/
function rescheduleFailed(operation) {
// @TODO WIP!
const reschedules = _getRescheduled(); // Get all current re-scheduled operations
reschedules[operation] = "1m"; // Retry this operation at 1m trigger!
Logger.log("Setting new retry schedule for: "+operation);
Logger.log("Updated re-schedules: "+JSON.stringify(reschedules));
return _getDocPropService().setProperty(RESCHEDULES_PROP_NAME, JSON.stringify(reschedules));
}

/**
* Clears failed execution schedule for given operation (if any)
* NOTE: This function could cause problems on parallel executions!
*/
function clearFailed(operation) {
const reschedules = _getRescheduled(); // Get all current re-scheduled operations
if (reschedules[operation]) {
delete reschedules[operation]; // Clear this operation!
Logger.log("Clearing retry schedule for: "+operation);
Logger.log("Updated re-schedules: "+JSON.stringify(reschedules));
return _getDocPropService().setProperty(RESCHEDULES_PROP_NAME, JSON.stringify(reschedules));
}
return false;
}

/**
Expand All @@ -120,6 +145,11 @@ function BinScheduler(OPTIONS) {
return !lastRun || lastRun < (new Date()).getTime() - 1000*60*5; // 5 minutes in milliseconds
}

function _getRescheduled() {
const reprops = _getDocPropService().getProperty(RESCHEDULES_PROP_NAME);
return reprops ? JSON.parse(reprops) : {};
}

/**
* Updates the last run timestamp
*/
Expand Down
15 changes: 13 additions & 2 deletions tasks/do-24h-stats.gs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function BinDo24hStats() {
* Returns this function period (the one that's used by the refresh triggers)
*/
function period() {
return BinScheduler().getSchedule(tag()) || "5m";
return BinScheduler().getSchedule(tag()) || "30m";
}

/**
Expand All @@ -26,14 +26,25 @@ function BinDo24hStats() {
* @return The list of 24hs stats for given symbols
*/
function run(range_or_cell, options) {
const bs = BinScheduler();
try {
bs.clearFailed(tag());
return execute(range_or_cell, options);
} catch(err) { // Re-schedule this failed run!
bs.rescheduleFailed(tag());
throw err;
}
}

function execute(range_or_cell, options) {
const ticker_against = options["ticker"];
Logger.log("[BinDo24hStats] Running..");
if (!range_or_cell) { // @TODO This limitation could be removed if cache is changed by other storage
throw new Error("A range with crypto names must be given!");
}
const lock = BinUtils().getUserLock(lock_retries--);
if (!lock) { // Could not acquire lock! => Retry
return run(range_or_cell, options);
return execute(range_or_cell, options);
}

const opts = {
Expand Down
15 changes: 13 additions & 2 deletions tasks/do-account-info.gs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function BinDoAccountInfo() {
* Returns this function period (the one that's used by the refresh triggers)
*/
function period() {
return BinScheduler().getSchedule(tag()) || "5m";
return BinScheduler().getSchedule(tag()) || "15m";
}

/**
Expand All @@ -25,10 +25,21 @@ function BinDoAccountInfo() {
* @return A list with account information
*/
function run(options) {
const bs = BinScheduler();
try {
bs.clearFailed(tag());
return execute(options);
} catch(err) { // Re-schedule this failed run!
bs.rescheduleFailed(tag());
throw err;
}
}

function execute(options) {
Logger.log("[BinDoAccountInfo] Running..");
const lock = BinUtils().getUserLock(lock_retries--);
if (!lock) { // Could not acquire lock! => Retry
return run(options);
return execute(options);
}

const opts = {CACHE_TTL: 55};
Expand Down
13 changes: 12 additions & 1 deletion tasks/do-current-prices.gs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,21 @@ function BinDoCurrentPrices() {
* @return The list of current prices for all or given symbols/tickers.
*/
function run(symbol_or_range, options) {
const bs = BinScheduler();
try {
bs.clearFailed(tag());
return execute(symbol_or_range, options);
} catch(err) { // Re-schedule this failed run!
bs.rescheduleFailed(tag());
throw err;
}
}

function execute(symbol_or_range, options) {
Logger.log("[BinDoCurrentPrices] Running..");
const lock = BinUtils().getUserLock(lock_retries--);
if (!lock) { // Could not acquire lock! => Retry
return run(symbol_or_range, options);
return execute(symbol_or_range, options);
}

const opts = {CACHE_TTL: 55, "public": true};
Expand Down
13 changes: 12 additions & 1 deletion tasks/do-orders-done.gs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ function BinDoOrdersDone() {
* @return The list of all current done orders for all or given symbols/tickers.
*/
function run(range_or_cell, options) {
const bs = BinScheduler();
try {
bs.clearFailed(tag());
return execute(range_or_cell, options);
} catch(err) { // Re-schedule this failed run!
bs.rescheduleFailed(tag());
throw err;
}
}

function execute(range_or_cell, options) {
const ticker_against = options["ticker"];
const limit = _getMaxItems(options); // Get max items limit
Logger.log("[BinDoOrdersDone] Running..");
Expand All @@ -36,7 +47,7 @@ function BinDoOrdersDone() {
}
const lock = BinUtils().getUserLock(lock_retries--);
if (!lock) { // Could not acquire lock! => Retry
return run(range_or_cell, options);
return execute(range_or_cell, options);
}

const range = BinUtils().getRangeOrCell(range_or_cell) || [];
Expand Down
13 changes: 12 additions & 1 deletion tasks/do-orders-open.gs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,21 @@ function BinDoOrdersOpen() {
* @return The list of all current open orders for all or given symbol/ticker.
*/
function run(symbol, options) {
const bs = BinScheduler();
try {
bs.clearFailed(tag());
return execute(symbol, options);
} catch(err) { // Re-schedule this failed run!
bs.rescheduleFailed(tag());
throw err;
}
}

function execute(symbol, options) {
Logger.log("[BinDoOrdersOpen] Running..");
const lock = BinUtils().getUserLock(lock_retries--);
if (!lock) { // Could not acquire lock! => Retry
return run(symbol, options);
return execute(symbol, options);
}

const opts = {CACHE_TTL: 55};
Expand Down
4 changes: 2 additions & 2 deletions ui/menu.gs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ function showDonate() {
"\n"+
"------------------------------------------------\n"+
"Don't you have a Binance account yet?\n"+
"Register using the referal link below and get a 5% discount on fees for all your trades!\n"+
"https://www.binance.com/en/register?ref=NJE1D9CS\n"+
"Register using the referal link below and get a 10% discount on fees for all your trades!\n"+
"https://www.binance.com/en/register?ref=SM93PRAV\n"+
"------------------------------------------------\n"+
"\n"+
"This software was published and released under the GPL-3.0 License.\n"+
Expand Down

0 comments on commit edb82b3

Please sign in to comment.