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

feat: introduce process lock so that solo doesn't allow parallel execution of commands #54

Closed
wants to merge 1 commit into from
Closed
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
52 changes: 11 additions & 41 deletions src/commands/account.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class AccountCommand extends BaseCommand {
}
},
{
title: 'create the new account',
title: 'Create the new account',
task: async (ctx, task) => {
self.accountInfo = await self.createNewAccount(ctx)
const accountInfoCopy = { ...self.accountInfo }
Expand Down Expand Up @@ -203,23 +203,23 @@ export class AccountCommand extends BaseCommand {
}
},
{
title: 'get the account info',
title: 'Get the account info',
task: async (ctx, task) => {
ctx.treasuryAccountInfo = await self.accountManager.getTreasuryAccountKeys(ctx.config.namespace)
await self.loadNodeClient(ctx)
ctx.accountInfo = await self.buildAccountInfo(await self.getAccountInfo(ctx), ctx.config.namespace, ctx.config.privateKey)
}
},
{
title: 'update the account',
title: 'Update the account',
task: async (ctx, task) => {
if (!(await self.updateAccountInfo(ctx))) {
throw new FullstackTestingError(`An error occurred updating account ${ctx.accountInfo.accountId}`)
}
}
},
{
title: 'get the updated account info',
title: 'Get the updated account info',
task: async (ctx, task) => {
self.accountInfo = await self.buildAccountInfo(await self.getAccountInfo(ctx), ctx.config.namespace, false)
this.logger.showJSON('account info', self.accountInfo)
Expand Down Expand Up @@ -270,7 +270,7 @@ export class AccountCommand extends BaseCommand {
}
},
{
title: 'get the account info',
title: 'Get the account info',
task: async (ctx, task) => {
ctx.treasuryAccountInfo = await self.accountManager.getTreasuryAccountKeys(ctx.config.namespace)
await self.loadNodeClient(ctx)
Expand Down Expand Up @@ -312,18 +312,8 @@ export class AccountCommand extends BaseCommand {
flags.privateKey,
flags.amount
),
handler: argv => {
accountCmd.logger.debug("==== Running 'account create' ===")
accountCmd.logger.debug(argv)

accountCmd.create(argv).then(r => {
accountCmd.logger.debug("==== Finished running 'account create' ===")
if (!r) process.exit(1)
}).catch(err => {
accountCmd.logger.showUserError(err)
process.exit(1)
})
}
handler: argv => accountCmd.handleCommand(
argv, async (args) => await accountCmd.create(args))
})
.command({
command: 'update',
Expand All @@ -334,18 +324,8 @@ export class AccountCommand extends BaseCommand {
flags.privateKey,
flags.amount
),
handler: argv => {
accountCmd.logger.debug("==== Running 'account update' ===")
accountCmd.logger.debug(argv)

accountCmd.update(argv).then(r => {
accountCmd.logger.debug("==== Finished running 'account update' ===")
if (!r) process.exit(1)
}).catch(err => {
accountCmd.logger.showUserError(err)
process.exit(1)
})
}
handler: argv => accountCmd.handleCommand(
argv, async (args) => await accountCmd.update(args))
})
.command({
command: 'get',
Expand All @@ -354,18 +334,8 @@ export class AccountCommand extends BaseCommand {
flags.namespace,
flags.accountId
),
handler: argv => {
accountCmd.logger.debug("==== Running 'account get' ===")
accountCmd.logger.debug(argv)

accountCmd.get(argv).then(r => {
accountCmd.logger.debug("==== Finished running 'account get' ===")
if (!r) process.exit(1)
}).catch(err => {
accountCmd.logger.showUserError(err)
process.exit(1)
})
}
handler: argv => accountCmd.handleCommand(
argv, async (args) => await accountCmd.get(args))
})
.demandCommand(1, 'Select an account command')
}
Expand Down
35 changes: 34 additions & 1 deletion src/commands/base.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*
*/
'use strict'
import { MissingArgumentError } from '../core/errors.mjs'
import { FullstackTestingError, MissingArgumentError } from '../core/errors.mjs'
import { ConfigManager } from '../core/index.mjs'
import { ShellRunner } from '../core/shell_runner.mjs'
import * as helpers from '../core/helpers.mjs'

export class BaseCommand extends ShellRunner {
async prepareChartPath (chartDir, chartRepo, chartName) {
Expand Down Expand Up @@ -48,4 +50,35 @@ export class BaseCommand extends ShellRunner {
this.configManager = opts.configManager
this.depManager = opts.depManager
}

/**
* Handle the execution of the command
*
* It ensures process file is locked before the handleFunc is called
*
* @param argv argv of the command
* @param handleFunc async function to be invoked
* @param errHandler error handler
* @return {Promise<void>}
*/
async handleCommand (argv, handleFunc, errHandler = helpers.defaultErrorHandler) {
if (!argv) throw new MissingArgumentError('argv is required')
if (!handleFunc) throw new MissingArgumentError('handleFunc is required')

let error = null
try {
this.logger.debug(`==== Start: '${argv._.join(' ')}' ===`, { config: this.configManager.config, argv })
await ConfigManager.acquireProcessLock(this.logger)
await handleFunc(argv)
} catch (e) {
error = new FullstackTestingError(`Error occurred: ${e.message}`, e)
} finally {
await ConfigManager.releaseProcessLock(this.logger)
this.logger.debug(`==== End: '${argv._.join(' ')}' ===`, { config: this.configManager.config, argv })
}

if (error) {
return errHandler(error, this.logger)
}
}
}
55 changes: 8 additions & 47 deletions src/commands/cluster.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -223,33 +223,14 @@ export class ClusterCommand extends BaseCommand {
.command({
command: 'list',
desc: 'List all available clusters',
handler: argv => {
clusterCmd.logger.debug("==== Running 'cluster list' ===", { argv })

clusterCmd.showClusterList().then(r => {
clusterCmd.logger.debug('==== Finished running `cluster list`====')

if (!r) process.exit(1)
}).catch(err => {
clusterCmd.logger.showUserError(err)
process.exit(1)
})
}
handler: argv => clusterCmd.handleCommand(
argv, async (args) => await clusterCmd.showClusterList(args))
})
.command({
command: 'info',
desc: 'Get cluster info',
handler: argv => {
clusterCmd.logger.debug("==== Running 'cluster info' ===", { argv })
clusterCmd.getClusterInfo(argv).then(r => {
clusterCmd.logger.debug('==== Finished running `cluster info`====')

if (!r) process.exit(1)
}).catch(err => {
clusterCmd.logger.showUserError(err)
process.exit(1)
})
}
handler: argv => clusterCmd.handleCommand(
argv, async (args) => await clusterCmd.getClusterInfo(args))
})
.command({
command: 'setup',
Expand All @@ -264,18 +245,8 @@ export class ClusterCommand extends BaseCommand {
flags.deployCertManagerCrds,
flags.fstChartVersion
),
handler: argv => {
clusterCmd.logger.debug("==== Running 'cluster setup' ===", { argv })

clusterCmd.setup(argv).then(r => {
clusterCmd.logger.debug('==== Finished running `cluster setup`====')

if (!r) process.exit(1)
}).catch(err => {
clusterCmd.logger.showUserError(err)
process.exit(1)
})
}
handler: argv => clusterCmd.handleCommand(
argv, async (args) => await clusterCmd.setup(args))
})
.command({
command: 'reset',
Expand All @@ -284,18 +255,8 @@ export class ClusterCommand extends BaseCommand {
flags.clusterName,
flags.clusterSetupNamespace
),
handler: argv => {
clusterCmd.logger.debug("==== Running 'cluster reset' ===", { argv })

clusterCmd.reset(argv).then(r => {
clusterCmd.logger.debug('==== Finished running `cluster reset`====')

if (!r) process.exit(1)
}).catch(err => {
clusterCmd.logger.showUserError(err)
process.exit(1)
})
}
handler: argv => clusterCmd.handleCommand(
argv, async (args) => await clusterCmd.reset(args))
})
.demandCommand(1, 'Select a cluster command')
}
Expand Down
11 changes: 10 additions & 1 deletion src/commands/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@ function Initialize (opts) {
}

// Expose components from the command module
export { Initialize, flags }
export {
Initialize,
InitCommand,
ClusterCommand,
NetworkCommand,
NodeCommand,
RelayCommand,
AccountCommand,
flags
}
10 changes: 2 additions & 8 deletions src/commands/init.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,8 @@ export class InitCommand extends BaseCommand {
flags.fstChartVersion
)
},
handler: (argv) => {
initCmd.init(argv).then(r => {
if (!r) process.exit(1)
}).catch(err => {
initCmd.logger.showUserError(err)
process.exit(1)
})
}
handler: argv => initCmd.handleCommand(
argv, async (args) => await initCmd.init(args))
}
}
}
45 changes: 6 additions & 39 deletions src/commands/network.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -367,19 +367,8 @@ export class NetworkCommand extends BaseCommand {
flags.enablePrometheusSvcMonitor,
flags.fstChartVersion
),
handler: argv => {
networkCmd.logger.debug("==== Running 'network deploy' ===")
networkCmd.logger.debug(argv)

networkCmd.deploy(argv).then(r => {
networkCmd.logger.debug('==== Finished running `network deploy`====')

if (!r) process.exit(1)
}).catch(err => {
networkCmd.logger.showUserError(err)
process.exit(1)
})
}
handler: argv => networkCmd.handleCommand(
argv, async (args) => await networkCmd.deploy(args))
})
.command({
command: 'destroy',
Expand All @@ -389,19 +378,8 @@ export class NetworkCommand extends BaseCommand {
flags.force,
flags.deletePvcs
),
handler: argv => {
networkCmd.logger.debug("==== Running 'network destroy' ===")
networkCmd.logger.debug(argv)

networkCmd.destroy(argv).then(r => {
networkCmd.logger.debug('==== Finished running `network destroy`====')

if (!r) process.exit(1)
}).catch(err => {
networkCmd.logger.showUserError(err)
process.exit(1)
})
}
handler: argv => networkCmd.handleCommand(
argv, async (args) => await networkCmd.destroy(args))
})
.command({
command: 'refresh',
Expand All @@ -418,19 +396,8 @@ export class NetworkCommand extends BaseCommand {
flags.hederaExplorerTlsHostName,
flags.enablePrometheusSvcMonitor
),
handler: argv => {
networkCmd.logger.debug("==== Running 'chart upgrade' ===")
networkCmd.logger.debug(argv)

networkCmd.refresh(argv).then(r => {
networkCmd.logger.debug('==== Finished running `chart upgrade`====')

if (!r) process.exit(1)
}).catch(err => {
networkCmd.logger.showUserError(err)
process.exit(1)
})
}
handler: argv => networkCmd.handleCommand(
argv, async (args) => await networkCmd.refresh(args))
})
.demandCommand(1, 'Select a chart command')
}
Expand Down
Loading
Loading