Split Scheduled task into different classes and moved environment read variable to util class

This commit is contained in:
elroy 2017-07-10 22:24:04 +02:00
parent 849859f272
commit ca4082b13f
9 changed files with 418 additions and 220 deletions

View File

@ -0,0 +1,37 @@
package nl.komtek.gpi.schedules;
import nl.komtek.gpi.services.GunbotProxyService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Created by Elroy on 23-6-2017.
*/
@Component
public class AllScheduledTasks {
private Logger logger = LogManager.getLogger(AllScheduledTasks.class);
@Autowired
private GunbotProxyService gunbotProxyService;
@Scheduled(fixedRate = Long.MAX_VALUE)
public void itWorks() {
if (gunbotProxyService.isUsingMultipleMarkets()) {
logger.info("Running in multimarket MODE!");
}
logger.info("Good job, It works!");
}
@Scheduled(fixedDelay = 2000)
public void updateTicker() {
try {
gunbotProxyService.getTickerScheduled();
} catch (Exception e) {
logger.error(e);
}
}
}

View File

@ -0,0 +1,69 @@
package nl.komtek.gpi.schedules;
import nl.komtek.gpi.services.GunbotProxyService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Created by Elroy on 23-6-2017.
*/
@Component
@ConditionalOnProperty(name="default_BTC_apiKey", havingValue = "")
public class BTCScheduledTasks {
private static final String market = "BTC";
private Logger logger = LogManager.getLogger(BTCScheduledTasks.class);
@Autowired
private GunbotProxyService gunbotProxyService;
@Scheduled(fixedDelay = 9000)
public void updateCompleteBalances() {
try {
if (gunbotProxyService.isActiveMarket(market)) {
gunbotProxyService.getCompleteBalancesScheduled(market);
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 1500)
public void updateTradeHistory() {
try {
if (gunbotProxyService.isActiveMarket(market)) {
gunbotProxyService.getTradeHistoryScheduled(market);
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 2000)
public void updateOpenOrders() {
try {
if (gunbotProxyService.isActiveMarket(market)) {
gunbotProxyService.getOpenOrdersScheduled(market);
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 10000)
public void updateBalancesBTC() {
try {
if (gunbotProxyService.isActiveMarket(market)) {
gunbotProxyService.getBalancesScheduled(market);
}
} catch (Exception e) {
logger.error(e);
}
}
}

View File

@ -0,0 +1,59 @@
package nl.komtek.gpi.schedules;
import nl.komtek.gpi.services.GunbotProxyService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Created by Elroy on 23-6-2017.
*/
@Component
@ConditionalOnProperty(name="default_apiKey", havingValue = "")
public class DefaultScheduledTasks {
private static final String market = "default";
private Logger logger = LogManager.getLogger(DefaultScheduledTasks.class);
@Autowired
private GunbotProxyService gunbotProxyService;
@Scheduled(fixedDelay = 9000)
public void updateCompleteBalances() {
try {
gunbotProxyService.getCompleteBalancesScheduled(market);
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 1500)
public void updateTradeHistory() {
try {
gunbotProxyService.getTradeHistoryScheduled(market);
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 2000)
public void updateOpenOrders() {
try {
gunbotProxyService.getOpenOrdersScheduled(market);
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 10000)
public void updateBalances() {
try {
gunbotProxyService.getBalancesScheduled(market);
} catch (Exception e) {
logger.error(e);
}
}
}

View File

@ -0,0 +1,66 @@
package nl.komtek.gpi.schedules;
import nl.komtek.gpi.services.GunbotProxyService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Created by Elroy on 23-6-2017.
*/
@Component
@ConditionalOnProperty(name = "default_ETH_apiKey", havingValue = "")
public class ETHScheduledTasks {
private Logger logger = LogManager.getLogger(ETHScheduledTasks.class);
@Autowired
private GunbotProxyService gunbotProxyService;
@Scheduled(fixedDelay = 9200)
public void updateCompleteBalancesETH() {
try {
if (gunbotProxyService.isActiveMarket("ETH")) {
gunbotProxyService.getCompleteBalancesScheduled("ETH");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 1550)
public void updateTradeHistoryETH() {
try {
if (gunbotProxyService.isActiveMarket("ETH")) {
gunbotProxyService.getTradeHistoryScheduled("ETH");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 2050)
public void updateOpenOrdersETH() {
try {
if (gunbotProxyService.isActiveMarket("ETH")) {
gunbotProxyService.getOpenOrdersScheduled("ETH");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 10000)
public void updateBalancesETH() {
try {
if (gunbotProxyService.isActiveMarket("ETH")) {
gunbotProxyService.getBalancesScheduled("ETH");
}
} catch (Exception e) {
logger.error(e);
}
}
}

View File

@ -0,0 +1,66 @@
package nl.komtek.gpi.schedules;
import nl.komtek.gpi.services.GunbotProxyService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Created by Elroy on 23-6-2017.
*/
@Component
@ConditionalOnProperty(name="default_USDT_apiKey", havingValue = "")
public class USDTScheduledTasks {
private Logger logger = LogManager.getLogger(USDTScheduledTasks.class);
@Autowired
private GunbotProxyService gunbotProxyService;
@Scheduled(fixedDelay = 9600)
public void updateCompleteBalancesUSDT() {
try {
if (gunbotProxyService.isActiveMarket("USDT")) {
gunbotProxyService.getCompleteBalancesScheduled("USDT");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 1650)
public void updateTradeHistoryUSDT() {
try {
if (gunbotProxyService.isActiveMarket("USDT")) {
gunbotProxyService.getTradeHistoryScheduled("USDT");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 2150)
public void updateOpenOrdersUSDT() {
try {
if (gunbotProxyService.isActiveMarket("USDT")) {
gunbotProxyService.getOpenOrdersScheduled("USDT");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 10000)
public void updateBalancesUSDT() {
try {
if (gunbotProxyService.isActiveMarket("USDT")) {
gunbotProxyService.getBalancesScheduled("USDT");
}
} catch (Exception e) {
logger.error(e);
}
}
}

View File

@ -0,0 +1,66 @@
package nl.komtek.gpi.schedules;
import nl.komtek.gpi.services.GunbotProxyService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Created by Elroy on 23-6-2017.
*/
@Component
@ConditionalOnProperty(name="default_XMR_apiKey", havingValue = "")
public class XMRScheduledTasks {
private Logger logger = LogManager.getLogger(XMRScheduledTasks.class);
@Autowired
private GunbotProxyService gunbotProxyService;
@Scheduled(fixedDelay = 9400)
public void updateCompleteBalancesXMR() {
try {
if (gunbotProxyService.isActiveMarket("XMR")) {
gunbotProxyService.getCompleteBalancesScheduled("XMR");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 1600)
public void updateTradeHistoryXMR() {
try {
if (gunbotProxyService.isActiveMarket("XMR")) {
gunbotProxyService.getTradeHistoryScheduled("XMR");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 2100)
public void updateOpenOrdersXMR() {
try {
if (gunbotProxyService.isActiveMarket("XMR")) {
gunbotProxyService.getOpenOrdersScheduled("XMR");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 10000)
public void updateBalancesXMR() {
try {
if (gunbotProxyService.isActiveMarket("XMR")) {
gunbotProxyService.getBalancesScheduled("XMR");
}
} catch (Exception e) {
logger.error(e);
}
}
}

View File

@ -9,6 +9,7 @@ import com.google.gson.JsonParser;
import net.jodah.failsafe.Failsafe;
import net.jodah.failsafe.RetryPolicy;
import nl.komtek.gpi.utils.ProxyHandledException;
import nl.komtek.gpi.utils.Util;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -19,7 +20,6 @@ import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
@ -39,7 +39,7 @@ public class GunbotProxyService {
@Autowired
private ApplicationContext applicationContext;
@Autowired
Environment environment;
Util util;
private Map<String, PoloniexTradingAPIClient> poloniexDefaultAPIClients = new HashMap<>();
private Map<String, PoloniexTradingAPIClient> poloniexTradingAPIClients = new HashMap<>();
private Map<String, String> marketMapping = new HashMap<>();
@ -55,17 +55,11 @@ public class GunbotProxyService {
private void initService() {
if (!createMarketMapping()) {
String apiKey = getEnvProperty("default_apiKey");
String apiSecret = getEnvProperty("default_apiSecret");
String apiKey = util.getEnvProperty("default_apiKey");
String apiSecret = util.getEnvProperty("default_apiSecret");
poloniexDefaultAPIClients.put("default", new PoloniexTradingAPIClient(apiKey, apiSecret));
if (poloniexDefaultAPIClients.get("default") == null) {
logger.error("The default apiKey and Secret is required");
SpringApplication.exit(applicationContext);
return;
}
if (!createDefaultTradingClients()) {
return;
}
createDefaultTradingClients();
} else {
if (!createMarketDefaultApiClients("BTC")) {
return;
@ -91,8 +85,8 @@ public class GunbotProxyService {
}
private boolean createMarketDefaultApiClients(String market) {
String apiKey = getEnvProperty("default_" + market + "_apiKey");
String apiSecret = getEnvProperty("default_" + market + "_apiSecret");
String apiKey = util.getEnvProperty("default_" + market + "_apiKey");
String apiSecret = util.getEnvProperty("default_" + market + "_apiSecret");
if (!StringUtils.isEmpty(apiKey) && !StringUtils.isEmpty(apiSecret)) {
if (!marketMapping.values().contains(market)) {
logger.error(String.format("Please setup the %s market correctly", market));
@ -110,8 +104,8 @@ public class GunbotProxyService {
for (String market : markets) {
for (int i = 1; i <= 10; i++) {
String apiKey = getEnvProperty(String.format("%s_apiKey%d", market, i));
String apiSecret = getEnvProperty(String.format("%s_apiSecret%d", market, i));
String apiKey = util.getEnvProperty(String.format("%s_apiKey%d", market, i));
String apiSecret = util.getEnvProperty(String.format("%s_apiSecret%d", market, i));
if (StringUtils.isEmpty(apiKey) || StringUtils.isEmpty(apiSecret)) {
break;
}
@ -123,19 +117,15 @@ public class GunbotProxyService {
}
private boolean createDefaultTradingClients() {
for (int i = 1; i <= 100; i++) {
String apiKey = getEnvProperty(String.format("apiKey%d", i));
String apiSecret = getEnvProperty(String.format("apiSecret%d", i));
for (int i = 1; i <= 10; i++) {
String apiKey = util.getEnvProperty(String.format("apiKey%d", i));
String apiSecret = util.getEnvProperty(String.format("apiSecret%d", i));
if (apiKey == null || apiSecret == null) {
break;
}
poloniexTradingAPIClients.put(apiKey, new PoloniexTradingAPIClient(apiKey, apiSecret));
}
if (poloniexTradingAPIClients.size() == 0) {
logger.error("Please setup at least 1 other apiKey for a better experience");
SpringApplication.exit(applicationContext);
}
return poloniexTradingAPIClients.size() > 0;
}
@ -329,7 +319,7 @@ public class GunbotProxyService {
}
private PoloniexTradingAPIClient getTradingClient(String currencyPair) {
String apiKey = getEnvProperty(currencyPair + "_apiKey");
String apiKey = util.getEnvProperty(currencyPair + "_apiKey");
if (apiKey != null) {
return poloniexTradingAPIClients.get(apiKey);
} else {
@ -352,11 +342,13 @@ public class GunbotProxyService {
return tradingAPIClient;
}
private String analyzeResult(String result) {
if (result != null && result.contains("Nonce")) {
throw new ProxyHandledException("nonce error: " + result);
} else if (result == null) {
public String analyzeResult(String result) {
if (result == null) {
throw new ProxyHandledException("No value was returned");
} else if (result.contains("Nonce")) {
throw new ProxyHandledException("nonce error: " + result);
} else if (result.contains("error")) {
throw new RuntimeException(result);
}
return result;
}
@ -396,8 +388,19 @@ public class GunbotProxyService {
return market;
}
private String getEnvProperty(String key) {
String value = StringUtils.trimAllWhitespace(environment.getProperty(key));
return StringUtils.replace(value, "\"", "");
public String checkDefaultKey(String market) {
PoloniexTradingAPIClient tradingAPIClient = getMarketDefaultTradingClient(market);
String result = Failsafe.with(retryPolicy)
.onFailedAttempt(this::handleException)
.get(() -> analyzeResult(tradingAPIClient.returnOpenOrders("ALL")));
return result;
}
public String checkTradingKey(String apiKey) {
PoloniexTradingAPIClient tradingAPIClient = poloniexTradingAPIClients.get(apiKey);
String result = Failsafe.with(retryPolicy)
.onFailedAttempt(this::handleException)
.get(() -> analyzeResult(tradingAPIClient.returnOpenOrders("ALL")));
return result;
}
}

View File

@ -1,189 +0,0 @@
package nl.komtek.gpi.utils;
import nl.komtek.gpi.services.GunbotProxyService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Created by Elroy on 23-6-2017.
*/
@Component
public class ScheduledTasks {
private Logger logger = LogManager.getLogger(ScheduledTasks.class);
@Autowired
private GunbotProxyService gunbotProxyService;
@Scheduled(fixedRate = Long.MAX_VALUE)
public void itWorks() {
if (gunbotProxyService.isUsingMultipleMarkets()) {
logger.info("Running in multimarket MODE!");
}
logger.info("Good job, It works!");
}
@Scheduled(fixedDelay = 2000)
public void updateTicker() {
try {
gunbotProxyService.getTickerScheduled();
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 9000)
public void updateCompleteBalances() {
try {
String market = "default";
if (gunbotProxyService.isUsingMultipleMarkets()) {
market = "BTC";
}
gunbotProxyService.getCompleteBalancesScheduled(market);
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 9200)
public void updateCompleteBalancesETH() {
try {
if (gunbotProxyService.isActiveMarket("ETH")) {
gunbotProxyService.getCompleteBalancesScheduled("ETH");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 9400)
public void updateCompleteBalancesXMR() {
try {
if (gunbotProxyService.isActiveMarket("XMR")) {
gunbotProxyService.getCompleteBalancesScheduled("XMR");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 9600)
public void updateCompleteBalancesUSDT() {
try {
if (gunbotProxyService.isActiveMarket("USDT")) {
gunbotProxyService.getCompleteBalancesScheduled("USDT");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 1500)
public void updateTradeHistory() {
try {
String market = "default";
if (gunbotProxyService.isUsingMultipleMarkets()) {
market = "BTC";
}
gunbotProxyService.getTradeHistoryScheduled(market);
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 1550)
public void updateTradeHistoryETH() {
try {
if (gunbotProxyService.isActiveMarket("ETH")) {
gunbotProxyService.getTradeHistoryScheduled("ETH");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 1600)
public void updateTradeHistoryXMR() {
try {
if (gunbotProxyService.isActiveMarket("XMR")) {
gunbotProxyService.getTradeHistoryScheduled("XMR");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 1650)
public void updateTradeHistoryUSDT() {
try {
if (gunbotProxyService.isActiveMarket("USDT")) {
gunbotProxyService.getTradeHistoryScheduled("USDT");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 2000)
public void updateOpenOrders() {
try {
String market = "default";
if (gunbotProxyService.isUsingMultipleMarkets()) {
market = "BTC";
}
gunbotProxyService.getOpenOrdersScheduled(market);
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 2050)
public void updateOpenOrdersETH() {
try {
if (gunbotProxyService.isActiveMarket("ETH")) {
gunbotProxyService.getOpenOrdersScheduled("ETH");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 2100)
public void updateOpenOrdersXMR() {
try {
if (gunbotProxyService.isActiveMarket("XMR")) {
gunbotProxyService.getOpenOrdersScheduled("XMR");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 2150)
public void updateOpenOrdersUSDT() {
try {
if (gunbotProxyService.isActiveMarket("USDT")) {
gunbotProxyService.getOpenOrdersScheduled("USDT");
}
} catch (Exception e) {
logger.error(e);
}
}
@Scheduled(fixedDelay = 10000)
public void updateBalances() {
try {
String market = "default";
if (gunbotProxyService.isUsingMultipleMarkets()) {
market = "BTC";
}
gunbotProxyService.getBalancesScheduled(market);
} catch (Exception e) {
logger.error(e);
}
}
}

View File

@ -0,0 +1,21 @@
package nl.komtek.gpi.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
/**
* Created by Elroy on 10-7-2017.
*/
@Component
public class Util {
@Autowired
private Environment environment;
public String getEnvProperty(String key) {
String value = StringUtils.trimAllWhitespace(environment.getProperty(key));
return StringUtils.replace(value, "\"", "");
}
}