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:
parent
9e3a05e087
commit
d35109199d
@ -138,7 +138,7 @@ export abstract class ShellSession {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected async getShellEnv() {
|
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 pathStr = [...this.getPathEntries(), await this.kubectlBinDirP, process.env.PATH].join(path.delimiter);
|
||||||
const shell = UserStore.getInstance().resolvedShell;
|
const shell = UserStore.getInstance().resolvedShell;
|
||||||
|
|
||||||
|
|||||||
@ -36,7 +36,7 @@ export async function shellSync() {
|
|||||||
const { shell } = os.userInfo();
|
const { shell } = os.userInfo();
|
||||||
let envVars = {};
|
let envVars = {};
|
||||||
|
|
||||||
envVars = await shellEnv(shell, 5_000);
|
envVars = await shellEnv(shell);
|
||||||
|
|
||||||
const env: Env = JSON.parse(JSON.stringify(envVars));
|
const env: Env = JSON.parse(JSON.stringify(envVars));
|
||||||
|
|
||||||
|
|||||||
@ -28,23 +28,32 @@ export interface EnvironmentVariables {
|
|||||||
|
|
||||||
let shellSyncFailed = false;
|
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 = {};
|
let envVars = {};
|
||||||
|
|
||||||
timeout ??= 0;
|
if (forceRetry) {
|
||||||
|
shellSyncFailed = false;
|
||||||
|
}
|
||||||
|
|
||||||
if (!shellSyncFailed) {
|
if (!shellSyncFailed) {
|
||||||
try {
|
try {
|
||||||
if (timeout > 0 ) {
|
envVars = await Promise.race([
|
||||||
envVars = await Promise.race([
|
shellEnvironment(shell),
|
||||||
shellEnvironment(shell),
|
new Promise((_resolve, reject) => setTimeout(() => {
|
||||||
new Promise((_resolve, reject) => setTimeout(() => {
|
reject(new Error("Resolving shell environment is taking very long. Please review your shell configuration."));
|
||||||
reject(new Error("Resolving shell environment is taking very long. Please review your shell configuration."));
|
}, 5_000))
|
||||||
}, timeout))
|
]);
|
||||||
]);
|
|
||||||
} else {
|
|
||||||
envVars = await shellEnvironment(shell);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`shellEnv: ${error}`);
|
logger.error(`shellEnv: ${error}`);
|
||||||
shellSyncFailed = true;
|
shellSyncFailed = true;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user