1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

refactored shellEnv to always timeout after 5s, and allow retry

Signed-off-by: Jim Ehrismann <jehrismann@mirantis.com>
This commit is contained in:
Jim Ehrismann 2021-06-09 12:43:19 -04:00
parent 9e3a05e087
commit d35109199d
3 changed files with 23 additions and 14 deletions

View File

@ -138,7 +138,7 @@ export abstract class ShellSession {
}
protected async getShellEnv() {
const env = clearKubeconfigEnvVars(JSON.parse(JSON.stringify(await shellEnv(undefined, 5_000))));
const env = clearKubeconfigEnvVars(JSON.parse(JSON.stringify(await shellEnv())));
const pathStr = [...this.getPathEntries(), await this.kubectlBinDirP, process.env.PATH].join(path.delimiter);
const shell = UserStore.getInstance().resolvedShell;

View File

@ -36,7 +36,7 @@ export async function shellSync() {
const { shell } = os.userInfo();
let envVars = {};
envVars = await shellEnv(shell, 5_000);
envVars = await shellEnv(shell);
const env: Env = JSON.parse(JSON.stringify(envVars));

View File

@ -28,23 +28,32 @@ export interface EnvironmentVariables {
let shellSyncFailed = false;
export async function shellEnv(shell?: string, timeout?: number) : Promise<EnvironmentVariables> {
/**
* Attempts to get the shell environment per the user's existing startup scripts.
* If the environment can't be retrieved after 5 seconds an error message is logged.
* Subsequent calls after such a timeout simply log an error message without trying
* to get the environment, unless forceRetry is true.
* @param shell the shell to get the environment from
* @param forceRetry if true will always try to get the environment, otherwise if
* a previous call to this function failed then this call will fail too.
* @returns object containing the shell's environment variables. An empty object is
* returned if the call fails.
*/
export async function shellEnv(shell?: string, forceRetry = false) : Promise<EnvironmentVariables> {
let envVars = {};
timeout ??= 0;
if (forceRetry) {
shellSyncFailed = false;
}
if (!shellSyncFailed) {
try {
if (timeout > 0 ) {
envVars = await Promise.race([
shellEnvironment(shell),
new Promise((_resolve, reject) => setTimeout(() => {
reject(new Error("Resolving shell environment is taking very long. Please review your shell configuration."));
}, timeout))
]);
} else {
envVars = await shellEnvironment(shell);
}
envVars = await Promise.race([
shellEnvironment(shell),
new Promise((_resolve, reject) => setTimeout(() => {
reject(new Error("Resolving shell environment is taking very long. Please review your shell configuration."));
}, 5_000))
]);
} catch (error) {
logger.error(`shellEnv: ${error}`);
shellSyncFailed = true;