From 138fbc12545ff982f69d263864c9f98bf837d727 Mon Sep 17 00:00:00 2001 From: skynetcap <100323448+skynetcap@users.noreply.github.com> Date: Wed, 27 Dec 2023 11:56:21 -0800 Subject: [PATCH] Optimize scheduler and rearrange the try/catch block The number of threads for the scheduled executor service has been increased to 2 for performance improvement. The try/catch block in the scheduling method has been moved outside, so the trading account is created before scheduling tasks, avoiding unnecessary exception handling inside the schedule method. Additionally, added a new schedule method to handle open book markets. --- .../obcranker/ObCrankerApplication.java | 44 +++++++++++++++---- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/main/java/markets/arcana/obcranker/ObCrankerApplication.java b/src/main/java/markets/arcana/obcranker/ObCrankerApplication.java index 6babd61..b2e47dc 100644 --- a/src/main/java/markets/arcana/obcranker/ObCrankerApplication.java +++ b/src/main/java/markets/arcana/obcranker/ObCrankerApplication.java @@ -2,6 +2,7 @@ import com.google.common.io.Resources; import com.mmorrell.openbook.manager.OpenBookManager; +import com.mmorrell.openbook.model.OpenBookMarket; import lombok.extern.slf4j.Slf4j; import org.p2p.solanaj.core.Account; import org.p2p.solanaj.core.PublicKey; @@ -22,7 +23,7 @@ @Slf4j public class ObCrankerApplication { - private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); + private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2); public static void main(String[] args) { SpringApplication.run(ObCrankerApplication.class, args); @@ -33,18 +34,20 @@ public void crankEventHeapLoop() throws InterruptedException, IOException { OpenBookManager manager = new OpenBookManager(new RpcClient("https://mainnet.helius-rpc" + ".com/?api-key=a778b653-bdd6-41bc-8cda-0c7377faf1dd")); + Account tradingAccount = null; + try { + tradingAccount = Account.fromJson( + Resources.toString(Resources.getResource("mikeDBaJgkicqhZcoYDBB4dRwZFFJCThtWCYD7A9FAH.json"), Charset.defaultCharset())); + } catch (IOException e) { + e.printStackTrace(); + } + Account finalTradingAccount = tradingAccount; + scheduler.scheduleAtFixedRate(() -> { // SOL/USDC PublicKey marketId = PublicKey.valueOf("C3YPL3kYCSYKsmHcHrPWx1632GUXGqi2yMXJbfeCc57q"); - Account tradingAccount = null; - try { - tradingAccount = Account.fromJson( - Resources.toString(Resources.getResource("mikeDBaJgkicqhZcoYDBB4dRwZFFJCThtWCYD7A9FAH.json"), Charset.defaultCharset())); - } catch (IOException e) { - e.printStackTrace(); - } Optional transactionId = manager.consumeEvents( - tradingAccount, + finalTradingAccount, marketId, 8 ); @@ -56,5 +59,28 @@ public void crankEventHeapLoop() throws InterruptedException, IOException { } }, 0, 5, TimeUnit.SECONDS); + + scheduler.scheduleAtFixedRate(() -> { + manager.cacheMarkets(); + for (OpenBookMarket market : manager.getOpenBookMarkets()) { + Optional transactionId = manager.consumeEvents( + finalTradingAccount, + market.getMarketId(), + 8 + ); + + if (transactionId.isPresent()) { + log.info("Cranked events [{}]: {}", market.getName(), transactionId.get()); + } else { + log.info("No events found to consume."); + } + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + log.error("Error consuming: {}", e.getMessage()); + } + } + + }, 0, 60, TimeUnit.SECONDS); } } \ No newline at end of file