diff --git a/bash/aws1.sh b/bash/aws.sh similarity index 70% rename from bash/aws1.sh rename to bash/aws.sh index 588c55f..15328b9 100644 --- a/bash/aws1.sh +++ b/bash/aws.sh @@ -3,4 +3,5 @@ rm *.log npm run trades assets=ETH,USD server aws &> trades.ALL.log & npm run commit server aws pair=ETH_USD &>> commit.log & npm run cleanDB server aws &>> cleanDB.log & -npm run round contractType=Vivaldi tenor=100C-3H debug server aws &>> vivaldi-100C-3H.log & \ No newline at end of file +npm run round contractType=Vivaldi tenor=100C-3H debug server aws &>> vivaldi-100C-3H.log & +npm run events server events=StartReset,StartPreReset,Others $1 &>> events.log & \ No newline at end of file diff --git a/bash/aws2.sh b/bash/aws2.sh deleted file mode 100644 index 5db44e1..0000000 --- a/bash/aws2.sh +++ /dev/null @@ -1,3 +0,0 @@ -npm run trigger server aws event=StartPreReset &>> preReset.log & -npm run trigger server aws event=StartReset &>> reset.log & -npm run fetchPrice server aws &>> fetchPrice.log & \ No newline at end of file diff --git a/bash/azure1.sh b/bash/azure.sh similarity index 70% rename from bash/azure1.sh rename to bash/azure.sh index de2e6c6..ffde7cd 100644 --- a/bash/azure1.sh +++ b/bash/azure.sh @@ -3,4 +3,5 @@ rm *.log npm run trades assets=ETH,USD server azure &> trades.ALL.log & npm run commit server azure pair=ETH_USD &>> commit.log & npm run cleanDB server azure &>> cleanDB.log & -npm run round contractType=Vivaldi tenor=100C-3H debug server azure &>> vivaldi-100C-3H.log & \ No newline at end of file +npm run round contractType=Vivaldi tenor=100C-3H debug server azure &>> vivaldi-100C-3H.log & +npm run events server events=StartReset,StartPreReset,Others $1 &>> events.log & \ No newline at end of file diff --git a/bash/azure2.sh b/bash/azure2.sh deleted file mode 100644 index ea19942..0000000 --- a/bash/azure2.sh +++ /dev/null @@ -1,3 +0,0 @@ -npm run trigger server azure event=StartPreReset &>> preReset.log & -npm run trigger server azure event=StartReset &>> reset.log & -npm run fetchPrice server azure &>> fetchPrice.log & \ No newline at end of file diff --git a/bash/gcp1.sh b/bash/gcp.sh similarity index 70% rename from bash/gcp1.sh rename to bash/gcp.sh index e24828f..8b47267 100644 --- a/bash/gcp1.sh +++ b/bash/gcp.sh @@ -3,4 +3,5 @@ rm *.log npm run trades assets=ETH,USD server gcp &> trades.ALL.log & npm run commit server gcp pair=ETH_USD &>> commit.log & npm run cleanDB server gcp &>> cleanDB.log & -npm run round contractType=Vivaldi tenor=100C-3H debug server gcp &>> vivaldi-100C-3H.log & \ No newline at end of file +npm run round contractType=Vivaldi tenor=100C-3H debug server gcp &>> vivaldi-100C-3H.log & +npm run events server events=StartReset,StartPreReset,Others $1 &>> events.log & \ No newline at end of file diff --git a/bash/gcp2.sh b/bash/gcp2.sh deleted file mode 100644 index c901a1a..0000000 --- a/bash/gcp2.sh +++ /dev/null @@ -1,3 +0,0 @@ -npm run trigger server gcp event=StartPreReset &>> preReset.log & -npm run trigger server gcp event=StartReset &>> reset.log & -npm run fetchPrice server gcp &>> fetchPrice.log & \ No newline at end of file diff --git a/bash/public.sh b/bash/public.sh index 71ad5fc..6543afa 100644 --- a/bash/public.sh +++ b/bash/public.sh @@ -3,4 +3,4 @@ rm *.log npm run trades assets=ETH,USD server dynamo &> trades.ALL.log & npm run prices period=1 dev dynamo &> prices.1.log & npm run prices period=60 dev dynamo &> prices.60.log & -npm run fetchEvents server dynamo $1 &>> others.log & \ No newline at end of file +npm run events server dynamo events=Others $1 &>> events.log & \ No newline at end of file diff --git a/package.json b/package.json index 16bf1b1..1fbee35 100644 --- a/package.json +++ b/package.json @@ -9,14 +9,13 @@ "tslint": "tslint -c tslint.json -p tsconfig.json src/**/*.ts{,x} --fix", "commit": "ts-node ./src/index commit", "trades": "ts-node src/index trades", - "trigger": "ts-node ./src/index trigger", "prices": "ts-node src/index prices", "node": "ts-node ./src/index node", "cleanDB": "ts-node ./src/index cleanDB", "fetchPrice": "ts-node ./src/index fetchPrice", - "fetchEvents": "ts-node ./src/index fetchEvents", "startCustodian": "ts-node ./src/index startCustodian", - "round": "ts-node ./src/index round" + "round": "ts-node ./src/index round", + "events": "ts-node ./src/index events" }, "jest": { "roots": [ diff --git a/src/common/constants.ts b/src/common/constants.ts index 129dd61..ef802c5 100644 --- a/src/common/constants.ts +++ b/src/common/constants.ts @@ -6,8 +6,13 @@ export const CLEAN_DB = 'cleanDB'; export const FETCH_PRICE = 'fetchPrice'; export const START_CUSTODIAN = 'startCustodian'; export const FETCH_EVENTS = 'fetchEvents'; +export const EVENTS = 'events'; export const ROUND = 'round'; +export const EVENTS_START_RESET = 'StartReset'; +export const EVENTS_START_PRE_RESET = 'StartPreReset'; +export const EVENTS_OTHERS = 'Others'; + // db setting export const DB_SQL_SCHEMA_PRICEFEED = 'priceFeedDB'; export const DB_SQL_TRADE = 'trades'; @@ -79,4 +84,4 @@ export const REDEEM_GAS_TH = 0.005; export const CREATE_INTERVAL = 10; // in seconds export const CREATE_GAS_TH = 0.01; export const EVENT_FETCH_BLOCK_INTERVAL = 100; -export const EVENT_FETCH_TIME_INTERVAL = 600000; +export const EVENT_FETCH_TIME_INTERVAL = 300000; diff --git a/src/common/types.ts b/src/common/types.ts index 4b91c29..2c46fdc 100644 --- a/src/common/types.ts +++ b/src/common/types.ts @@ -17,6 +17,7 @@ export interface IOption { sources: string[]; exSources: string[]; assets: string[]; + events: string[]; live: boolean; dbLive: boolean; server: boolean; diff --git a/src/index.ts b/src/index.ts index 97e8eba..c57f34c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,8 +34,8 @@ dbUtil.init(tool, option).then(() => { case CST.TRADES: new MarketDataService().startFetching(tool, option); break; - case CST.FETCH_EVENTS: - new ContractService(tool, option).fetchEvent(); + case CST.EVENTS: + new MarketDataService().startFetchingEvent(tool, option); break; case DataConstants.DB_PRICES: new MarketDataService().startAggregate(option.period); @@ -46,9 +46,6 @@ dbUtil.init(tool, option).then(() => { case CST.START_CUSTODIAN: new ContractService(tool, option).startCustodian(option); break; - case CST.TRIGGER: - new ContractService(tool, option).trigger(); - break; case CST.COMMIT: new ContractService(tool, option).commitPrice(); break; diff --git a/src/services/ContractService.test.ts b/src/services/ContractService.test.ts index ab7ef61..1c6f9d1 100644 --- a/src/services/ContractService.test.ts +++ b/src/services/ContractService.test.ts @@ -173,7 +173,7 @@ test('fetchEvent', async () => { test('startCustodian, worng type', async () => { contractService.createDuoWrappers = jest.fn(); - await contractService.startCustodian(option); + await contractService.startCustodian(option as any); expect(contractService.createDuoWrappers as jest.Mock).not.toBeCalled(); }); @@ -203,7 +203,7 @@ test('startCustodian, dualClass', async () => { } } as any) ); - await contractService1.startCustodian(option); + await contractService1.startCustodian(option as any); expect((startCustodian as jest.Mock).mock.calls).toMatchSnapshot(); }); @@ -233,7 +233,7 @@ test('startCustodian, wrong type', async () => { } } as any) ); - await contractService1.startCustodian(option); + await contractService1.startCustodian(option as any); expect(startCustodian as jest.Mock).not.toBeCalled(); }); diff --git a/src/services/MarketDataService.test.ts b/src/services/MarketDataService.test.ts index 616baa7..0ca0082 100644 --- a/src/services/MarketDataService.test.ts +++ b/src/services/MarketDataService.test.ts @@ -8,12 +8,24 @@ import osUtil from '../utils/osUtil'; import priceUtil from '../utils/priceUtil'; import util from '../utils/util'; import MarketDataService from './MarketDataService'; + jest.mock('@finbook/duo-contract-wrapper', () => ({ Web3Wrapper: jest.fn(() => ({ contractAddresses: kovan })) })); +const triggerMock = jest.fn(); +const fetchEventMock = jest.fn(); +jest.mock('../services/ContractService', () => + jest.fn().mockImplementation(() => ({ + trigger: triggerMock, + fetchEvent: fetchEventMock + })) +); + +import ContractService from '../services/ContractService'; + const marketDataService = new MarketDataService(); test('retry after long enought time', () => { const launchMock = jest.fn(); @@ -28,23 +40,13 @@ test('retry after long enought time', () => { failCount: 2, instance: undefined as any }; - marketDataService.retry( - 'tool', - { - forceREST: false, - debug: false, - live: false - } as any, - 'source', - ['asset1', 'asset2', 'asset3'] - ); + marketDataService.retry('source', launchMock); expect(marketDataService.subProcesses['source']).toMatchSnapshot(); expect((global.setTimeout as jest.Mock).mock.calls).toMatchSnapshot(); (global.setTimeout as jest.Mock).mock.calls[0][0](); - expect(launchMock.mock.calls).toMatchSnapshot(); + expect(launchMock as jest.Mock).toBeCalledTimes(1); marketDataService.launchSource = launchOriginal; }); - test('retry within short time', () => { child_process.exec = jest.fn() as any; util.getUTCNowTimestamp = jest.fn(() => 1234567890); @@ -55,22 +57,13 @@ test('retry within short time', () => { failCount: 2, instance: undefined as any }; - marketDataService.retry( - 'tool', - { - forceREST: false, - debug: false, - live: false - } as any, - 'source', - ['asset1', 'asset2', 'asset3'] - ); + marketDataService.retry('source', () => jest.fn()); expect(marketDataService.subProcesses['source']).toMatchSnapshot(); }); test('launchSource fail windows', () => { osUtil.isWindows = jest.fn(() => true); - child_process.exec = jest.fn() as any; + child_process.exec = jest.fn(() => false) as any; util.getUTCNowTimestamp = jest.fn(() => 1234567890); marketDataService.retry = jest.fn(); marketDataService.subProcesses['source'] = { @@ -86,9 +79,14 @@ test('launchSource fail windows', () => { dynamo: true, azure: true } as any); + const originalLaunchSource = marketDataService.launchSource; + marketDataService.launchSource = jest.fn(); expect(((child_process.exec as any) as jest.Mock).mock.calls).toMatchSnapshot(); expect(marketDataService.subProcesses).toMatchSnapshot(); expect((marketDataService.retry as jest.Mock).mock.calls).toMatchSnapshot(); + (marketDataService.retry as jest.Mock).mock.calls[0][1](); + expect((marketDataService.launchSource as jest.Mock).mock.calls).toMatchSnapshot(); + marketDataService.launchSource = originalLaunchSource; }); test('launchSource success windows', () => { @@ -113,12 +111,17 @@ test('launchSource success windows', () => { live: false, aws: true } as any); + const originalLaunchSource = marketDataService.launchSource; + marketDataService.launchSource = jest.fn(); expect(((child_process.exec as any) as jest.Mock).mock.calls).toMatchSnapshot(); expect(marketDataService.subProcesses).toMatchSnapshot(); expect(execOn.mock.calls).toMatchSnapshot(); execOn.mock.calls[0][1](); execOn.mock.calls[0][1](1); expect((marketDataService.retry as jest.Mock).mock.calls).toMatchSnapshot(); + (marketDataService.retry as jest.Mock).mock.calls[0][1](); + expect((marketDataService.launchSource as jest.Mock).mock.calls).toMatchSnapshot(); + marketDataService.launchSource = originalLaunchSource; }); test('launchSource forceREST windows', () => { @@ -285,6 +288,89 @@ test('launchSource live not windows', () => { expect(marketDataService.subProcesses).toMatchSnapshot(); }); +test('launchEvent ', () => { + osUtil.isWindows = jest.fn(() => false); + child_process.exec = jest.fn() as any; + util.getUTCNowTimestamp = jest.fn(() => 1234567890); + marketDataService.subProcesses['event'] = { + source: 'event', + lastFailTimestamp: 0, + failCount: 0, + instance: undefined as any + }; + marketDataService.launchEvent('tool', 'event', { + forceREST: false, + debug: false, + live: true, + event: 'event', + gcp: true + } as any); + expect(((child_process.exec as any) as jest.Mock).mock.calls).toMatchSnapshot(); + expect(marketDataService.subProcesses).toMatchSnapshot(); +}); + +test('launchEvent azure', () => { + osUtil.isWindows = jest.fn(() => false); + const on = jest.fn(); + child_process.exec = jest.fn(() => ({ + on: on + })) as any; + marketDataService.retry = jest.fn(); + util.getUTCNowTimestamp = jest.fn(() => 1234567890); + marketDataService.subProcesses['event'] = { + source: 'event', + lastFailTimestamp: 0, + failCount: 0, + instance: undefined as any + }; + marketDataService.launchEvent('tool', 'event', { + forceREST: false, + debug: false, + live: true, + event: 'event', + azure: true + } as any); + const originalLaunchEvent = marketDataService.launchEvent; + marketDataService.launchEvent = jest.fn(); + expect(((child_process.exec as any) as jest.Mock).mock.calls).toMatchSnapshot(); + expect((marketDataService.retry as jest.Mock).mock.calls).toMatchSnapshot(); + (on as jest.Mock).mock.calls[0][1](); + (on as jest.Mock).mock.calls[0][1]('code'); + expect((on as jest.Mock).mock.calls).toMatchSnapshot(); + (marketDataService.retry as jest.Mock).mock.calls[0][1](); + expect(marketDataService.subProcesses).toMatchSnapshot(); + expect((marketDataService.launchEvent as jest.Mock).mock.calls).toMatchSnapshot(); + marketDataService.launchEvent = originalLaunchEvent; +}); + +test('launchEvent with existingInstance, no windows', () => { + osUtil.isWindows = jest.fn(() => true); + child_process.exec = jest.fn(() => null) as any; + util.getUTCNowTimestamp = jest.fn(() => 1234567890); + marketDataService.retry = jest.fn(); + marketDataService.subProcesses['Others'] = { + source: 'Others', + lastFailTimestamp: 0, + failCount: 0, + instance: undefined as any + }; + marketDataService.launchEvent('tool', 'Others', { + forceREST: false, + debug: false, + live: true, + event: 'Others', + server: true, + aws: true + } as any); + const originalLaunchEvent = marketDataService.launchEvent; + marketDataService.launchEvent = jest.fn(); + expect((marketDataService.retry as jest.Mock).mock.calls).toMatchSnapshot(); + (marketDataService.retry as jest.Mock).mock.calls[0][1](); + expect((marketDataService.launchEvent as jest.Mock).mock.calls).toMatchSnapshot(); + expect(marketDataService.subProcesses).toMatchSnapshot(); + marketDataService.launchEvent = originalLaunchEvent; +}); + test('startFetching no source', async () => { util.sleep = jest.fn(); marketDataService.launchSource = jest.fn(); @@ -362,6 +448,39 @@ test('startFetching source no sourcePairs', async () => { expect(geminiApi.fetchTrades as jest.Mock).not.toBeCalled(); }); +test('startFetchingEvent no event', async () => { + util.sleep = jest.fn(); + marketDataService.launchEvent = jest.fn(); + + await marketDataService.startFetchingEvent('tool', { + event: '', + debug: false, + events: ['event1', 'event2'] + } as any); + + expect((marketDataService.launchEvent as jest.Mock).mock.calls).toMatchSnapshot(); + expect(marketDataService.subProcesses).toMatchSnapshot(); +}); + +test('startFetchingEvent with event', async () => { + util.sleep = jest.fn(); + marketDataService.launchEvent = jest.fn(); + + await marketDataService.startFetchingEvent('tool', { + event: 'event', + debug: false + } as any); + + await marketDataService.startFetchingEvent('tool', { + event: 'StartReset', + debug: false + } as any); + + expect(triggerMock).toBeCalledTimes(1); + expect(fetchEventMock).toBeCalledTimes(1); + expect((ContractService as any).mock.calls).toMatchSnapshot(); +}); + test('cleanDb', async () => { dbUtil.cleanDB = jest.fn(); dbUtil.insertHeartbeat = jest.fn(); diff --git a/src/services/MarketDataService.ts b/src/services/MarketDataService.ts index 9122329..874d898 100644 --- a/src/services/MarketDataService.ts +++ b/src/services/MarketDataService.ts @@ -2,6 +2,7 @@ import child_process from 'child_process'; import apis from '../apis'; import * as CST from '../common/constants'; import { IOption, ISubProcess } from '../common/types'; +import ContractService from '../services/ContractService'; import dbUtil from '../utils/dbUtil'; import osUtil from '../utils/osUtil'; import priceUtil from '../utils/priceUtil'; @@ -48,6 +49,27 @@ export default class MarketDataService { } } + public async startFetchingEvent(tool: string, option: IOption): Promise { + if (!option.event) + for (const event of option.events) { + option.event = event; + this.subProcesses[event] = { + source: option.event, + lastFailTimestamp: 0, + failCount: 0, + instance: undefined as any + }; + this.launchEvent(tool, event, option); + } + else { + util.logDebug('start fetching events'); + const contractService = new ContractService(tool, option); + if ([CST.EVENTS_START_PRE_RESET, CST.EVENTS_START_RESET].includes(option.event)) + contractService.trigger(); + else contractService.fetchEvent(); + } + } + public launchSource(tool: string, source: string, assets: string[], option: IOption) { util.logInfo(source); const cmd = @@ -71,28 +93,55 @@ export default class MarketDataService { if (!procInstance) { util.logError('Failed to launch ' + source); - this.retry(tool, option, source, assets); + this.retry(source, () => this.launchSource(tool, source, assets, option)); } else { util.logInfo(`[${source}]: Launched ${tool} ${assets.join(',')}`); procInstance.on('exit', code => { util.logError(`[${source}]: Exit with code ${code}`); - if (code) this.retry(tool, option, source, assets); + if (code) this.retry(source, () => this.launchSource(tool, source, assets, option)); }); } } - public retry(tool: string, option: IOption, source: string, assets: string[]) { + public launchEvent(tool: string, event: string, option: IOption) { + const cmd = + `npm run ${tool} event=${event}${option.event === CST.EVENTS_OTHERS ? ' dynamo' : ''} ${ + option.azure ? ' azure' : '' + }${option.gcp ? ' gcp' : ''}${option.aws ? ' aws' : ''}${ + option.server ? ' server' : '' + }` + + (osUtil.isWindows() ? '>>' : '&>') + + ` ${tool}.${option.event}.log`; + + const procInstance = child_process.exec( + cmd, + osUtil.isWindows() ? {} : { shell: '/bin/bash' } + ); + + this.subProcesses[option.event].instance = procInstance; + this.subProcesses[option.event].lastFailTimestamp = util.getUTCNowTimestamp(); + + if (!procInstance) { + util.logError('Failed to launch public event '); + this.retry(event, () => this.launchEvent(tool, event, option)); + } else + procInstance.on('exit', code => { + util.logError(`[${option.event}]: Exit with code ${code}`); + if (code) this.retry(event, () => this.launchEvent(tool, event, option)); + }); + } + + public retry(name: string, task: () => any) { const now: number = util.getUTCNowTimestamp(); - if (now - this.subProcesses[source].lastFailTimestamp < 30000) - this.subProcesses[source].failCount++; - else this.subProcesses[source].failCount = 1; + if (now - this.subProcesses[name].lastFailTimestamp < 30000) + this.subProcesses[name].failCount++; + else this.subProcesses[name].failCount = 1; - this.subProcesses[source].lastFailTimestamp = now; + this.subProcesses[name].lastFailTimestamp = now; - if (this.subProcesses[source].failCount < 3) - global.setTimeout(() => this.launchSource(tool, source, assets, option), 5000); - else util.logError('Retry Aborted ' + source); + if (this.subProcesses[name].failCount < 3) global.setTimeout(() => task(), 5000); + else util.logError('Retry Aborted ' + name); } public async cleanDb() { diff --git a/src/services/__snapshots__/MarketDataService.test.ts.snap b/src/services/__snapshots__/MarketDataService.test.ts.snap index f77cd52..17ccee6 100644 --- a/src/services/__snapshots__/MarketDataService.test.ts.snap +++ b/src/services/__snapshots__/MarketDataService.test.ts.snap @@ -4,6 +4,168 @@ exports[`cleanDb 1`] = `86400000`; exports[`cleanDb 2`] = `30000`; +exports[`launchEvent 1`] = ` +Array [ + Array [ + "npm run tool event=event gcp&> tool.event.log", + Object { + "shell": "/bin/bash", + }, + ], +] +`; + +exports[`launchEvent 2`] = ` +Object { + "event": Object { + "failCount": 0, + "instance": undefined, + "lastFailTimestamp": 1234567890, + "source": "event", + }, + "source": Object { + "failCount": 0, + "instance": undefined, + "lastFailTimestamp": 1234567890, + "source": "source", + }, +} +`; + +exports[`launchEvent azure 1`] = ` +Array [ + Array [ + "npm run tool event=event azure&> tool.event.log", + Object { + "shell": "/bin/bash", + }, + ], +] +`; + +exports[`launchEvent azure 2`] = `Array []`; + +exports[`launchEvent azure 3`] = ` +Array [ + Array [ + "exit", + [Function], + ], +] +`; + +exports[`launchEvent azure 4`] = ` +Object { + "event": Object { + "failCount": 0, + "instance": Object { + "on": [MockFunction] { + "calls": Array [ + Array [ + "exit", + [Function], + ], + ], + "results": Array [ + Object { + "isThrow": false, + "value": undefined, + }, + ], + }, + }, + "lastFailTimestamp": 1234567890, + "source": "event", + }, + "source": Object { + "failCount": 0, + "instance": undefined, + "lastFailTimestamp": 1234567890, + "source": "source", + }, +} +`; + +exports[`launchEvent azure 5`] = ` +Array [ + Array [ + "tool", + "event", + Object { + "azure": true, + "debug": false, + "event": "event", + "forceREST": false, + "live": true, + }, + ], +] +`; + +exports[`launchEvent with existingInstance, no windows 1`] = ` +Array [ + Array [ + "Others", + [Function], + ], +] +`; + +exports[`launchEvent with existingInstance, no windows 2`] = ` +Array [ + Array [ + "tool", + "Others", + Object { + "aws": true, + "debug": false, + "event": "Others", + "forceREST": false, + "live": true, + "server": true, + }, + ], +] +`; + +exports[`launchEvent with existingInstance, no windows 3`] = ` +Object { + "Others": Object { + "failCount": 0, + "instance": null, + "lastFailTimestamp": 1234567890, + "source": "Others", + }, + "event": Object { + "failCount": 0, + "instance": Object { + "on": [MockFunction] { + "calls": Array [ + Array [ + "exit", + [Function], + ], + ], + "results": Array [ + Object { + "isThrow": false, + "value": undefined, + }, + ], + }, + }, + "lastFailTimestamp": 1234567890, + "source": "event", + }, + "source": Object { + "failCount": 0, + "instance": undefined, + "lastFailTimestamp": 1234567890, + "source": "source", + }, +} +`; + exports[`launchSource debug not windows 1`] = ` Array [ Array [ @@ -71,18 +233,8 @@ Object { exports[`launchSource fail not windows 3`] = ` Array [ Array [ - "tool", - Object { - "debug": false, - "forceREST": false, - "live": false, - }, "source", - Array [ - "asset1", - "asset2", - "asset3", - ], + [Function], ], ] `; @@ -100,7 +252,7 @@ exports[`launchSource fail windows 2`] = ` Object { "source": Object { "failCount": 0, - "instance": undefined, + "instance": false, "lastFailTimestamp": 1234567890, "source": "source", }, @@ -108,9 +260,24 @@ Object { `; exports[`launchSource fail windows 3`] = ` +Array [ + Array [ + "source", + [Function], + ], +] +`; + +exports[`launchSource fail windows 4`] = ` Array [ Array [ "tool", + "source", + Array [ + "asset1", + "asset2", + "asset3", + ], Object { "azure": true, "debug": false, @@ -118,12 +285,6 @@ Array [ "forceREST": false, "live": false, }, - "source", - Array [ - "asset1", - "asset2", - "asset3", - ], ], ] `; @@ -303,21 +464,30 @@ Array [ `; exports[`launchSource success windows 4`] = ` +Array [ + Array [ + "source", + [Function], + ], +] +`; + +exports[`launchSource success windows 5`] = ` Array [ Array [ "tool", - Object { - "aws": true, - "debug": false, - "forceREST": false, - "live": false, - }, "source", Array [ "asset1", "asset2", "asset3", ], + Object { + "aws": true, + "debug": false, + "forceREST": false, + "live": false, + }, ], ] `; @@ -340,25 +510,6 @@ Array [ ] `; -exports[`retry after long enought time 3`] = ` -Array [ - Array [ - "tool", - "source", - Array [ - "asset1", - "asset2", - "asset3", - ], - Object { - "debug": false, - "forceREST": false, - "live": false, - }, - ], -] -`; - exports[`retry within short time 1`] = ` Object { "failCount": 3, @@ -658,3 +809,125 @@ Array [ ], ] `; + +exports[`startFetchingEvent no event 1`] = ` +Array [ + Array [ + "tool", + "event1", + Object { + "debug": false, + "event": "event2", + "events": Array [ + "event1", + "event2", + ], + }, + ], + Array [ + "tool", + "event2", + Object { + "debug": false, + "event": "event2", + "events": Array [ + "event1", + "event2", + ], + }, + ], +] +`; + +exports[`startFetchingEvent no event 2`] = ` +Object { + "Others": Object { + "failCount": 0, + "instance": null, + "lastFailTimestamp": 1234567890, + "source": "Others", + }, + "bitstamp": Object { + "failCount": 0, + "instance": undefined, + "lastFailTimestamp": 0, + "source": "bitstamp", + }, + "event": Object { + "failCount": 0, + "instance": Object { + "on": [MockFunction] { + "calls": Array [ + Array [ + "exit", + [Function], + ], + ], + "results": Array [ + Object { + "isThrow": false, + "value": undefined, + }, + ], + }, + }, + "lastFailTimestamp": 1234567890, + "source": "event", + }, + "event1": Object { + "failCount": 0, + "instance": undefined, + "lastFailTimestamp": 0, + "source": "event1", + }, + "event2": Object { + "failCount": 0, + "instance": undefined, + "lastFailTimestamp": 0, + "source": "event2", + }, + "gdax": Object { + "failCount": 0, + "instance": undefined, + "lastFailTimestamp": 0, + "source": "gdax", + }, + "gemini": Object { + "failCount": 0, + "instance": undefined, + "lastFailTimestamp": 0, + "source": "gemini", + }, + "kraken": Object { + "failCount": 0, + "instance": undefined, + "lastFailTimestamp": 0, + "source": "kraken", + }, + "source": Object { + "failCount": 0, + "instance": undefined, + "lastFailTimestamp": 1234567890, + "source": "source", + }, +} +`; + +exports[`startFetchingEvent with event 1`] = ` +Array [ + Array [ + "tool", + Object { + "debug": false, + "event": "event", + }, + ], + Array [ + "tool", + Object { + "debug": false, + "event": "StartReset", + }, + ], +] +`; diff --git a/src/utils/__snapshots__/eventUtil.test.ts.snap b/src/utils/__snapshots__/eventUtil.test.ts.snap index 6830992..8642c7a 100644 --- a/src/utils/__snapshots__/eventUtil.test.ts.snap +++ b/src/utils/__snapshots__/eventUtil.test.ts.snap @@ -158,7 +158,7 @@ exports[`fetch, force 4`] = ` Array [ Array [ [Function], - 600000, + 300000, ], ] `; @@ -179,7 +179,7 @@ exports[`fetch, no events 2`] = ` Array [ Array [ [Function], - 600000, + 300000, ], ] `; @@ -342,7 +342,7 @@ exports[`fetch, no force 4`] = ` Array [ Array [ [Function], - 600000, + 300000, ], ] `; @@ -505,7 +505,7 @@ exports[`fetch, no force, continuous call 4`] = ` Array [ Array [ [Function], - 600000, + 300000, ], ] `; diff --git a/src/utils/__snapshots__/util.test.ts.snap b/src/utils/__snapshots__/util.test.ts.snap index f906e30..fc8ef01 100644 --- a/src/utils/__snapshots__/util.test.ts.snap +++ b/src/utils/__snapshots__/util.test.ts.snap @@ -2,7 +2,7 @@ exports[`composeQuery 1`] = `"?key1=value1&key2=value2"`; -exports[`getStatusProcess EVENT 1`] = `"EVENT_GCP_PUBLIC_STARTPRERESET"`; +exports[`getStatusProcess EVENT 1`] = `""`; exports[`getStatusProcess PRICE 1`] = `"PRICE_AWS_PUBLIC_HOURLY"`; @@ -18,9 +18,9 @@ exports[`getStatusProcess commit do not useDynamo 1`] = `"FEED_AWS_PRIVATE"`; exports[`getStatusProcess commit useDynamo 1`] = `"FEED_AWS_PUBLIC"`; -exports[`getStatusProcess fetchEvents do not useDynamo 1`] = `"EVENT_AWS_PRIVATE_OTHERS"`; +exports[`getStatusProcess events do not useDynamo 1`] = `"EVENT_AWS_PRIVATE"`; -exports[`getStatusProcess fetchEvents useDynamo 1`] = `"EVENT_AWS_PUBLIC_OTHERS"`; +exports[`getStatusProcess events useDynamo 1`] = `"EVENT_AWS_PUBLIC_OTHERS"`; exports[`getStatusProcess fetchPrice do not useDynamo 1`] = `"FETCH_AWS_PRIVATE"`; @@ -30,13 +30,13 @@ exports[`getStatusProcess prices do not useDynamo 1`] = `"PRICE_AWS_PRIVATE_MINU exports[`getStatusProcess prices useDynamo 1`] = `"PRICE_AWS_PUBLIC_MINUTELY"`; -exports[`getStatusProcess trades do not useDynamo 1`] = `"TRADE_AWS_PRIVATE"`; +exports[`getStatusProcess round do not useDynamo 1`] = `"ROUND_AWS_PRIVATE"`; -exports[`getStatusProcess trades useDynamo 1`] = `"TRADE_AWS_PUBLIC"`; +exports[`getStatusProcess round useDynamo 1`] = `"ROUND_AWS_PUBLIC"`; -exports[`getStatusProcess trigger do not useDynamo 1`] = `"EVENT_AWS_PRIVATE_OTHERS"`; +exports[`getStatusProcess trades do not useDynamo 1`] = `"TRADE_AWS_PRIVATE"`; -exports[`getStatusProcess trigger useDynamo 1`] = `"EVENT_AWS_PUBLIC_OTHERS"`; +exports[`getStatusProcess trades useDynamo 1`] = `"TRADE_AWS_PUBLIC"`; exports[`log debug 1`] = ` Array [ @@ -83,6 +83,10 @@ Object { "dbLive": false, "dynamo": false, "event": "event", + "events": Array [ + "event1", + "event2", + ], "exSources": Array [ "ex1", "ex2", @@ -115,6 +119,9 @@ Object { "dbLive": false, "dynamo": false, "event": "", + "events": Array [ + "", + ], "exSources": Array [ "", ], diff --git a/src/utils/util.test.ts b/src/utils/util.test.ts index 8daabd3..a8627f7 100644 --- a/src/utils/util.test.ts +++ b/src/utils/util.test.ts @@ -60,6 +60,7 @@ test('parseOptions default', () => { 'source=', 'pair=', 'event=', + 'events=', 'provider=', 'contractType=', 'tenor=' @@ -82,6 +83,7 @@ test('parseOptions', () => { 'sources=s1,s2', 'pair=pair', 'event=event', + 'events=event1,event2', 'provider=provider', 'contractType=contractType', 'tenor=tenor' @@ -109,10 +111,10 @@ const toolToTest = [ CST.TRADES, CST.COMMIT, CST.CLEAN_DB, - CST.TRIGGER, - CST.FETCH_EVENTS, + CST.EVENTS, CST.FETCH_PRICE, - DataConstants.DB_PRICES + DataConstants.DB_PRICES, + CST.ROUND ]; const option: IOption = util.parseOptions(['npm', 'run', 'tool', 'period=1']); @@ -120,11 +122,13 @@ const option: IOption = util.parseOptions(['npm', 'run', 'tool', 'period=1']); for (const tool of toolToTest) { test(`getStatusProcess ${tool} useDynamo`, () => { option.dynamo = true; + option.event = CST.EVENTS_OTHERS; expect(util.getStatusProcess(tool, option)).toMatchSnapshot(); }); test(`getStatusProcess ${tool} do not useDynamo`, () => { option.dynamo = false; + option.event = ''; expect(util.getStatusProcess(tool, option)).toMatchSnapshot(); }); } diff --git a/src/utils/util.ts b/src/utils/util.ts index 88fbff5..5f91e06 100644 --- a/src/utils/util.ts +++ b/src/utils/util.ts @@ -1,4 +1,3 @@ -import { Constants as WrapperConstants } from '@finbook/duo-contract-wrapper'; import { Constants as DataConstants } from '@finbook/duo-market-data'; import moment from 'moment'; import * as CST from '../common/constants'; @@ -49,6 +48,7 @@ class Util { contractType: 'Beethoven', tenor: 'Perpetual', assets: [''], + events: [''], sources: [''], exSources: [''], source: '', @@ -71,6 +71,9 @@ class Util { case 'assets': option.assets = args[1].split(','); break; + case 'events': + option.events = args[1].split(','); + break; case 'source': option.source = args[1] || option.source; break; @@ -110,7 +113,7 @@ class Util { public getStatusProcess(tool: string, option: IOption) { let type = ''; const platform = option.azure ? '_AZURE' : option.gcp ? '_GCP' : '_AWS'; - const privacy = option.dynamo ? '_PUBLIC' : '_PRIVATE'; + let privacy = option.dynamo ? '_PUBLIC' : '_PRIVATE'; let source = ''; switch (tool) { @@ -118,17 +121,10 @@ class Util { type = 'TRADE'; source = option.source ? '_' + option.source.toUpperCase() : ''; break; - case CST.TRIGGER: - case CST.FETCH_EVENTS: + case CST.EVENTS: type = 'EVENT'; - source = - '_' + - ([ - WrapperConstants.EVENT_START_PRE_RESET, - WrapperConstants.EVENT_START_RESET - ].includes(option.event) - ? option.event.toUpperCase() - : 'OTHERS'); + source = option.event ? '_' + option.event.toUpperCase() : ''; + if (option.event === CST.EVENTS_OTHERS) privacy = '_PUBLIC'; break; case CST.COMMIT: type = 'FEED';