Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#208 Add support for running up command x number of times #214

Merged
merged 4 commits into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions bin/common/executor/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,39 @@ export class ExecutionInterface {
* @param runtime {Runtime}
*/
async handle(execute, runtime) {
if (runtime.times != null) {
await this.#run(execute, runtime);
return;
}

await this._execute(execute, runtime);
}

/**
* Run command multiple times
* @param execute {Execute}
* @param runtime {Runtime}
* @returns {Promise<void>}
*/
async #run(execute, runtime) {
let count = 1;
let completed = false;

while (!completed) {
await this._execute(execute, runtime);

if (runtime.times !== 0 && count === runtime.times) {
completed = true;
} else {
await new Promise(resolve => setTimeout(resolve, runtime.interval));
}

if (runtime.times > 0) {
count++;
}
}
}

/**
* @return {Promise<ExecutionLog>}
*/
Expand Down
18 changes: 18 additions & 0 deletions bin/common/models/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export class Args {
*/
down;

/**
* Option for running environment
* @type {string}
*/
run;

/**
* Starts one or more groups of executions
* @type {boolean|string|string[]}
Expand All @@ -23,6 +29,18 @@ export class Args {
*/
downGroup;

/**
* Number of times to run
* @type {number|null}
*/
times;

/**
* Interval between each run
* @type {number|null}
*/
interval;

/**
* Option (optional) included with start for starting environment cleanly
* @type {boolean}
Expand Down
44 changes: 42 additions & 2 deletions bin/execution/executor/executor-yargs-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default new class {
}
})
.command({
command: 'up-group [name]',
command: 'up-group <name>',
desc: 'Run group of actions',
builder: (yargs) => {
yargs
Expand Down Expand Up @@ -86,6 +86,46 @@ export default new class {
this.#execute(segment, project, argv).catch(console.error);
}
})
.command({
command: 'run <name>',
desc: 'Run action continuously',
builder: (yargs) => {
yargs
.positional('name', {
describe: 'Name of the action',
type: 'string'
})
.option('times', {
alias: 't',
describe: 'Number of times to run action (0 - infinite)',
type: 'number',
default: 0,
coerce: (value) => {
if (value < 0) {
throw new Error('Option --times, -t must be positive value');
}
return value;
}
})
.option('interval', {
alias: 'i',
describe: 'Interval between runs (seconds)',
type: 'number',
demandOption: true,
coerce: (value) => {
if (value < 0) {
throw new Error('Option --interval, -i must be positive value');
}
return value;
}
});

customOptionsYargsCreator.addToYargs(yargs, segment.actions)
},
handler: (argv) => {
this.#execute(segment, project, argv).catch(console.error);
}
})
.command({
command: 'down [name]',
desc: 'Take down actions',
Expand Down Expand Up @@ -119,7 +159,7 @@ export default new class {
}
})
.command({
command: 'down-group [name]',
command: 'down-group <name>',
desc: 'Take down one or more group of actions',
builder: (yargs) => {
yargs
Expand Down
16 changes: 15 additions & 1 deletion bin/execution/executor/runtime-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default new class {
* @returns {Runtime}
*/
getRuntime(args) {
const up = this.#hasValue(args, 'up');
const up = this.#hasValue(args, 'up') || this.#hasValue(args, 'run');
const down = this.#hasValue(args, 'down');
const upGroup = this.#hasValue(args, 'up-group');
const downGroup = this.#hasValue(args, 'down-group');
Expand All @@ -20,6 +20,8 @@ export default new class {
return {
up: up || upGroup,
down: down || downGroup,
times: args.times,
interval: args.interval,
include: {
executions: up || down ? this.#getVariables(args['name']) : [],
groups: upGroup || downGroup ? this.#getVariables(args[`name`]) : []
Expand Down Expand Up @@ -64,6 +66,18 @@ export class Runtime {
*/
down;

/**
* Times to run the actions
* @type {number | null}
*/
times;

/**
* Interval between each run
* @type {number | null}
*/
interval;

/**
* List of groups and executions to be included in starting or stopping
* @type { { executions: string[], groups: string[] } | null }
Expand Down