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

exec command returns before command is executed #1520

Open
bodycombathuang opened this issue Jan 16, 2024 · 4 comments
Open

exec command returns before command is executed #1520

bodycombathuang opened this issue Jan 16, 2024 · 4 comments
Labels
lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.

Comments

@bodycombathuang
Copy link

async function func1() {
const command = ['tar', 'zcf', '-'];
command.push('-C', cwd);
command.push(srcPath);
const writerStream = fs.createWriteStream(srcPath+".tar")
const errStream = new WritableStreamBuffer();

await exec.exec(
    namespace,
    podName,
    containerName,
    command,
    writerStream,
    errStream,
    null,
    false,
    async ({ status }) => {
        writerStream.close();

        if (status === 'Failure' || errStream.size()) {
            throw new Error(`Error from cpFromPod - details: \n ${errStream.getContentsAsString()}`);
        }
        fs.createReadStream(`${srcPath}.tar`).pipe(tar.extract({cwd:tgtPath}));

    },
);

}
func1()
fs.rmSync(srcPath+".tar"(

I run the above code, fs.rmSync throws the tar file does not exist error, I feel the command is not executed immediately after exec finishes

@brendandburns
Copy link
Contributor

exec returns a promise to the underlying WebSocket, not to the command execution. If you want to wait for the command execution to complete, you need to wait on that callback that you passed in (the function that calls fs.createReadStream(...)

@bodycombathuang
Copy link
Author

hi @brendandburns how to wait on that callback?
https://github.com/kubernetes-client/javascript/blob/master/src/cp.ts, I can see a sample here, but I do not know how to wait on that callback, could you help me

@brendandburns
Copy link
Contributor

You need to create a Promise before you make the call to exec and then resolve that promise within the callback. Then wait for that promise to complete before you move on after the exec

Details of promises here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.
Projects
None yet
Development

No branches or pull requests

4 participants