From 69ee09070d8c84192f91454e882f265b7fb0a88c Mon Sep 17 00:00:00 2001 From: sercan Date: Mon, 7 Jan 2019 08:38:15 +0100 Subject: [PATCH 1/5] improved subscribeorders #687 --- lib/cli/commands/subscribeorders.ts | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/cli/commands/subscribeorders.ts b/lib/cli/commands/subscribeorders.ts index 00b569d1d..33a5cd823 100644 --- a/lib/cli/commands/subscribeorders.ts +++ b/lib/cli/commands/subscribeorders.ts @@ -15,6 +15,22 @@ export const builder = { }; export const handler = (argv: Arguments) => { + ensureConnection(argv, true); +}; + +const ensureConnection = (argv: Arguments, printError?: boolean) => { + loadXudClient(argv).getInfo(new xudrpc.GetInfoRequest(), (error: Error | null) => { + if (error) { + if (printError) console.error(`${error.name}: ${error.message}`); + setTimeout(ensureConnection.bind(undefined, argv), 3000); + } else { + console.log('Successfully connected, subscribing for orders'); + subscribeOrders(argv); + } + }); +}; + +const subscribeOrders = (argv: Arguments) => { const addedOrdersRequest = new xudrpc.SubscribeAddedOrdersRequest(); addedOrdersRequest.setExisting(argv.existing); const addedOrdersSubscription = loadXudClient(argv).subscribeAddedOrders(addedOrdersRequest); @@ -22,13 +38,28 @@ export const handler = (argv: Arguments) => { console.log(`Order added: ${JSON.stringify(order.toObject())}`); }); + // adding end, close, error events only once, + // since they'll be thrown for three of subscriptions in the corresponding cases, catching once is enough. + addedOrdersSubscription.on('end', ensureConnection.bind(undefined, argv, true)); + addedOrdersSubscription.on('close', ensureConnection.bind(undefined, argv, true)); + addedOrdersSubscription.on('error', (err: Error) => { + console.log(`Unexpected error occured: ${JSON.stringify(err)}, retrying to connect`); + ensureConnection(argv); + }); + const removedOrdersSubscription = loadXudClient(argv).subscribeRemovedOrders(new xudrpc.SubscribeRemovedOrdersRequest()); removedOrdersSubscription.on('data', (orderRemoval: xudrpc.OrderRemoval) => { console.log(`Order removed: ${JSON.stringify(orderRemoval.toObject())}`); }); + // prevent exiting and do nothing, it's already cached above. + removedOrdersSubscription.on('error', () => {}); + const swapsSubscription = loadXudClient(argv).subscribeSwaps(new xudrpc.SubscribeSwapsRequest()); swapsSubscription.on('data', (swapResult: xudrpc.SwapResult) => { console.log(`Order swapped: ${JSON.stringify(swapResult.toObject())}`); }); + + // prevent exiting and do nothing, it's already cached above. + swapsSubscription.on('error', () => {}); }; From 80f1c22cdd5595dda3d30b11cc8f28504632ac60 Mon Sep 17 00:00:00 2001 From: sercan Date: Mon, 7 Jan 2019 11:34:48 +0100 Subject: [PATCH 2/5] requested changes #687 --- lib/cli/commands/subscribeorders.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/cli/commands/subscribeorders.ts b/lib/cli/commands/subscribeorders.ts index 33a5cd823..30520b7e3 100644 --- a/lib/cli/commands/subscribeorders.ts +++ b/lib/cli/commands/subscribeorders.ts @@ -19,7 +19,7 @@ export const handler = (argv: Arguments) => { }; const ensureConnection = (argv: Arguments, printError?: boolean) => { - loadXudClient(argv).getInfo(new xudrpc.GetInfoRequest(), (error: Error | null) => { + loadXudClient(argv).waitForReady(Number.POSITIVE_INFINITY, (error: Error | null) => { if (error) { if (printError) console.error(`${error.name}: ${error.message}`); setTimeout(ensureConnection.bind(undefined, argv), 3000); @@ -40,8 +40,8 @@ const subscribeOrders = (argv: Arguments) => { // adding end, close, error events only once, // since they'll be thrown for three of subscriptions in the corresponding cases, catching once is enough. - addedOrdersSubscription.on('end', ensureConnection.bind(undefined, argv, true)); - addedOrdersSubscription.on('close', ensureConnection.bind(undefined, argv, true)); + addedOrdersSubscription.on('end', reconnect.bind(undefined, argv)); + addedOrdersSubscription.on('close', reconnect.bind(undefined, argv)); addedOrdersSubscription.on('error', (err: Error) => { console.log(`Unexpected error occured: ${JSON.stringify(err)}, retrying to connect`); ensureConnection(argv); @@ -63,3 +63,8 @@ const subscribeOrders = (argv: Arguments) => { // prevent exiting and do nothing, it's already cached above. swapsSubscription.on('error', () => {}); }; + +const reconnect = (argv: Arguments) => { + console.log('Stream is closed unexpectedly, trying to reconnect'); + ensureConnection(argv, false); +}; From 94bbb4a7b95116817f36c6b4fac57ce0edcb0dbc Mon Sep 17 00:00:00 2001 From: sercan Date: Mon, 7 Jan 2019 15:44:38 +0100 Subject: [PATCH 3/5] requested changes #687 --- lib/cli/commands/subscribeorders.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli/commands/subscribeorders.ts b/lib/cli/commands/subscribeorders.ts index 30520b7e3..a7337257d 100644 --- a/lib/cli/commands/subscribeorders.ts +++ b/lib/cli/commands/subscribeorders.ts @@ -65,6 +65,6 @@ const subscribeOrders = (argv: Arguments) => { }; const reconnect = (argv: Arguments) => { - console.log('Stream is closed unexpectedly, trying to reconnect'); + console.log('Stream has closed unexpectedly, trying to reconnect'); ensureConnection(argv, false); }; From 44880d95c937fbdb47bc9ae2112df49b127166c6 Mon Sep 17 00:00:00 2001 From: sercan Date: Tue, 8 Jan 2019 14:53:09 +0100 Subject: [PATCH 4/5] requested changes #687 --- lib/cli/commands/subscribeorders.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/cli/commands/subscribeorders.ts b/lib/cli/commands/subscribeorders.ts index a7337257d..1791c21d2 100644 --- a/lib/cli/commands/subscribeorders.ts +++ b/lib/cli/commands/subscribeorders.ts @@ -18,8 +18,11 @@ export const handler = (argv: Arguments) => { ensureConnection(argv, true); }; +let xud: any; + const ensureConnection = (argv: Arguments, printError?: boolean) => { - loadXudClient(argv).waitForReady(Number.POSITIVE_INFINITY, (error: Error | null) => { + if (!xud) xud = loadXudClient(argv); + xud.waitForReady(Number.POSITIVE_INFINITY, (error: Error | null) => { if (error) { if (printError) console.error(`${error.name}: ${error.message}`); setTimeout(ensureConnection.bind(undefined, argv), 3000); @@ -41,9 +44,8 @@ const subscribeOrders = (argv: Arguments) => { // adding end, close, error events only once, // since they'll be thrown for three of subscriptions in the corresponding cases, catching once is enough. addedOrdersSubscription.on('end', reconnect.bind(undefined, argv)); - addedOrdersSubscription.on('close', reconnect.bind(undefined, argv)); addedOrdersSubscription.on('error', (err: Error) => { - console.log(`Unexpected error occured: ${JSON.stringify(err)}, retrying to connect`); + console.log(`Unexpected error occured: ${JSON.stringify(err)}, trying to reconnect`); ensureConnection(argv); }); @@ -52,7 +54,7 @@ const subscribeOrders = (argv: Arguments) => { console.log(`Order removed: ${JSON.stringify(orderRemoval.toObject())}`); }); - // prevent exiting and do nothing, it's already cached above. + // prevent exiting and do nothing, it's already caught above. removedOrdersSubscription.on('error', () => {}); const swapsSubscription = loadXudClient(argv).subscribeSwaps(new xudrpc.SubscribeSwapsRequest()); @@ -60,11 +62,11 @@ const subscribeOrders = (argv: Arguments) => { console.log(`Order swapped: ${JSON.stringify(swapResult.toObject())}`); }); - // prevent exiting and do nothing, it's already cached above. + // prevent exiting and do nothing, it's already caught above. swapsSubscription.on('error', () => {}); }; const reconnect = (argv: Arguments) => { - console.log('Stream has closed unexpectedly, trying to reconnect'); + console.log('Stream has closed, trying to reconnect'); ensureConnection(argv, false); }; From 54266efa92d9ea68f6f58055a2a6d23df5a26983 Mon Sep 17 00:00:00 2001 From: sercan Date: Tue, 15 Jan 2019 11:30:36 +0100 Subject: [PATCH 5/5] changed variable type #687 --- lib/cli/commands/subscribeorders.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/cli/commands/subscribeorders.ts b/lib/cli/commands/subscribeorders.ts index 1791c21d2..c17b9125e 100644 --- a/lib/cli/commands/subscribeorders.ts +++ b/lib/cli/commands/subscribeorders.ts @@ -1,6 +1,7 @@ import { loadXudClient } from '../command'; import { Arguments } from 'yargs'; import * as xudrpc from '../../proto/xudrpc_pb'; +import { XudClient } from '../../proto/xudrpc_grpc_pb'; export const command = 'streamorders [existing]'; @@ -18,7 +19,7 @@ export const handler = (argv: Arguments) => { ensureConnection(argv, true); }; -let xud: any; +let xud: XudClient; const ensureConnection = (argv: Arguments, printError?: boolean) => { if (!xud) xud = loadXudClient(argv);