Skip to content

Commit c5511f0

Browse files
committed
add possibility to run functions
1 parent 58277a1 commit c5511f0

5 files changed

Lines changed: 73 additions & 12 deletions

File tree

lib/executor.circle.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable sonarjs/cognitive-complexity */
2-
import { toArray, isFunction, isAsyncFunction, shuffleArrMutable } from 'sat-utils';
2+
import { toArray, isFunction, isAsyncFunction, shuffleArrMutable, isString } from 'sat-utils';
33
import { buildCommandExecutor } from './command.executor.builder';
44
import { sleep } from './helpers';
55
import { logger } from './logger';
@@ -64,7 +64,14 @@ async function circleExecutor(runOptions, commandsArray): Promise<{ retriable: s
6464
if (shuffle) {
6565
shuffleArrMutable(commands);
6666
}
67-
const result = await executeCommandAsync(commands.splice(0, 1)[0], runIndex).catch(error => logger.error(error));
67+
let result;
68+
69+
const commandToExecute = commands.splice(0, 1)[0];
70+
if (isString(commandToExecute)) {
71+
result = await executeCommandAsync(commandToExecute, runIndex).catch(error => logger.error(error));
72+
} else if (isFunction(commandToExecute) || isAsyncFunction(commandToExecute)) {
73+
result = await commandToExecute(runIndex).catch(error => logger.error(error));
74+
}
6875

6976
if (result) {
7077
retriable.push(result);

lib/executor.intime.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { shuffleArrMutable } from 'sat-utils';
1+
import { shuffleArrMutable, isString, isFunction, isAsyncFunction } from 'sat-utils';
22
import { buildCommandExecutor } from './command.executor.builder';
33
import { sleep } from './helpers';
44
import { logger } from './logger';
@@ -55,12 +55,17 @@ async function intimeExecutor(runOptions, commandsArray): Promise<{ retriable: s
5555
if (shuffle) {
5656
shuffleArrMutable(commands);
5757
}
58-
const commadData = commands.splice(0, 1)[0] as { cmd: string; attemptsCount: number };
59-
const executionIndex = commadData.attemptsCount--;
58+
let result;
59+
const commadData = commands.splice(0, 1)[0] as { cmd; attemptsCount: number };
6060

61-
const result = await executeCommandAsync(commadData.cmd, attemptsCount - executionIndex).catch(error =>
62-
logger.error(error),
63-
);
61+
const executionIndex = commadData.attemptsCount--;
62+
if (isString(commadData.cmd)) {
63+
result = await executeCommandAsync(commadData.cmd, attemptsCount - executionIndex).catch(error =>
64+
logger.error(error),
65+
);
66+
} else if (isFunction(commadData.cmd) || isAsyncFunction(commadData.cmd)) {
67+
result = await commadData.cmd(attemptsCount - executionIndex).catch(error => logger.error(error));
68+
}
6469

6570
if (result) {
6671
logIntimeCommand(commadData);

lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export type TBuildOpts = {
3535
};
3636

3737
export type TRunner = {
38-
(commands: string[]): Promise<{ notRetriable: string[]; retriable: string[] }>;
38+
(commands: string[] | Array<(index: number) => any>): Promise<{ notRetriable: string[]; retriable: string[] }>;
3939
};
4040

4141
const getReruner = ({

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "process-rerun",
3-
"version": "0.3.1",
3+
"version": "0.4.0",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/potapovDim/protractor-rerun.git"

specs/index.spec.ts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { sleep } from 'sat-utils';
12
import { expect } from 'assertior';
23
import { getReruner } from '../lib';
34

@@ -41,7 +42,7 @@ describe('index ', () => {
4142
maxThreads: 4,
4243
attemptsCount: 1,
4344
longestProcessTime: 10,
44-
processResultAnalyzer: () => null,
45+
processResultAnalyzer: () => false,
4546
pollTime: 10,
4647
logLevel: 'MUTE',
4748
});
@@ -73,7 +74,7 @@ describe('index ', () => {
7374
];
7475
let cmdTest = null;
7576
let stackTraceTest = null;
76-
let notRetriableTest = null;
77+
let notRetriableTest: any = null;
7778
const processResultAnalyzer = (cmd, stackTrace, notRetriable) => {
7879
cmdTest = cmd;
7980
stackTraceTest = stackTrace;
@@ -112,4 +113,52 @@ describe('index ', () => {
112113
expect(result.notRetriable).toDeepEqual([]);
113114
expect(result.retriable).toDeepEqual([]);
114115
});
116+
117+
it('[p] successExitCode', async function () {
118+
const commands = [
119+
async () => {
120+
await sleep(500);
121+
},
122+
async () => {
123+
await sleep(500);
124+
},
125+
];
126+
const processResultAnalyzer = cmd => cmd;
127+
const runner = getReruner({
128+
maxThreads: 4,
129+
attemptsCount: 1,
130+
longestProcessTime: 10 * 1000,
131+
processResultAnalyzer,
132+
successExitCode: 100,
133+
pollTime: 10,
134+
logLevel: 'MUTE',
135+
});
136+
const result = await runner(commands);
137+
expect(result.notRetriable).toDeepEqual([]);
138+
expect(result.retriable).toDeepEqual([]);
139+
});
140+
141+
it('[p] successExitCode', async function () {
142+
const commands = [
143+
async function test() {
144+
await sleep(500);
145+
},
146+
async () => {
147+
await sleep(500);
148+
},
149+
];
150+
const processResultAnalyzer = cmd => cmd;
151+
const runner = getReruner({
152+
maxThreads: 4,
153+
attemptsCount: 1,
154+
longestProcessTime: 10 * 1000,
155+
processResultAnalyzer,
156+
successExitCode: 100,
157+
pollTime: 10,
158+
logLevel: 'MUTE',
159+
});
160+
const result = await runner(commands);
161+
expect(result.notRetriable).toDeepEqual([]);
162+
expect(result.retriable).toDeepEqual([]);
163+
});
115164
});

0 commit comments

Comments
 (0)