Skip to content

Commit

Permalink
fix: prevent duplicate execution chrisleekr#450, chrisleekr#451
Browse files Browse the repository at this point in the history
  • Loading branch information
habibalkhabbaz committed Jul 31, 2022
1 parent 1bd376a commit d5a4103
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
34 changes: 32 additions & 2 deletions app/cronjob/__tests__/trailingTrade.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/* eslint-disable max-classes-per-file */
/* eslint-disable global-require */
const { logger } = require('../../helpers');
const { logger, cache } = require('../../helpers');

describe('trailingTrade', () => {
let mockCacheHget;
let mockCacheHset;
let mockCacheHdel;

let mockLoggerInfo;
let mockSlackSendMessage;
let mockConfigGet;
Expand Down Expand Up @@ -35,6 +39,10 @@ describe('trailingTrade', () => {
mockLoggerInfo = jest.fn();
mockSlackSendMessage = jest.fn().mockResolvedValue(true);

mockCacheHget = jest.fn().mockResolvedValue(null);
mockCacheHset = jest.fn().mockResolvedValue(true);
mockCacheHdel = jest.fn().mockResolvedValue(true);

logger.info = mockLoggerInfo;
jest.mock('../../helpers', () => ({
logger: {
Expand All @@ -44,7 +52,8 @@ describe('trailingTrade', () => {
debug: jest.fn(),
child: jest.fn()
},
slack: { sendMessage: mockSlackSendMessage }
slack: { sendMessage: mockSlackSendMessage },
cache: { hget: mockCacheHget, hset: mockCacheHset, hdel: mockCacheHdel }
}));

mockErrorHandlerWrapper = jest
Expand Down Expand Up @@ -420,6 +429,27 @@ describe('trailingTrade', () => {
);
});
});

describe('when there is existing running executeTrailingTrade for a symbol', () => {
beforeEach(async () => {
mockCacheHget = jest.fn().mockResolvedValue('true');

const { execute: trailingTradeExecute } = require('../trailingTrade');
await trailingTradeExecute(logger, 'LTCUSDT');
});

it(`does not trigger cache.hset`, () => {
expect(mockCacheHset).not.toHaveBeenCalled();
});

it(`does not trigger getAccountInfo`, () => {
expect(mockGetAccountInfo).not.toHaveBeenCalled();
});

it(`does not trigger isSymbolLocked`, () => {
expect(mockIsSymbolLocked).not.toHaveBeenCalled();
});
});
});

describe('when symbol is locked', () => {
Expand Down
10 changes: 10 additions & 0 deletions app/cronjob/trailingTrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ const {
saveDataToCache
} = require('./trailingTrade/steps');
const { errorHandlerWrapper } = require('../error-handler');
const { cache } = require('../helpers');

const execute = async (rawLogger, symbol) => {
const logger = rawLogger.child({ jobName: 'trailingTrade' });

await errorHandlerWrapper(logger, 'Trailing Trade', async () => {
if ((await cache.hget(`execute-trailing-trade`, symbol)) === 'true') {
// do nothing, there is another execution task running
return;
}

await cache.hset(`execute-trailing-trade`, symbol, true);

// Retrieve account info from cache
const accountInfo = await getAccountInfo(logger);

Expand Down Expand Up @@ -156,6 +164,8 @@ const execute = async (rawLogger, symbol) => {
logger.info({ symbol }, '⏹ TrailingTrade: Finish process (Debug)...');

logger.info({ symbol, data }, 'TrailingTrade: Finish process...');

await cache.hdel(`execute-trailing-trade`, symbol);
});
};

Expand Down

0 comments on commit d5a4103

Please sign in to comment.