如何取消nodegit克隆操作?

时间:2016-10-11 19:27:34

标签: nodegit

使用nodegit,如何取消长时间运行的克隆操作?我们的回购就像是3GB,用户可能想要中止它,因为它花了太多时间。

我可以拒绝承诺吗?像这样?

var cloneRepository = NodeGit.Clone(cloneURL, localPath, cloneOptions);
...
if (abortCondition)
   cloneRepository.reject();

2 个答案:

答案 0 :(得分:0)

通过a post on gitter.im获得了答案。

基本上,我们不能拒绝承诺。我们需要使用child_process.fork()生成子进程并将其终止以中止克隆。

const fork = require('child_process').fork;
var childProcess = fork("clone.js", null, { silent: true});

...
childProcess.kill();

答案 1 :(得分:0)

(添加到dstj)

如果您不想创建一个完整的新模块文件,可以使用npm forkme。

 // Import for this process
 var path = require("path");
 
 var child = forkme([{
            param1,
            param2,
            param3,
            param4,//\
            param5 // \
        }],        //  -> These can't be functions. Only static variables are allowed.
            function (outer) {
                // This is running in a separate process, so modules have to be included again
                
                // Note that I couldn't get electron-settings to import here, not sure if that can be replicated on a fresh project.
                var path = require("path");
                
                // Variables can be accessed via
                console.log(outer.param1)
                
                process.send({ foo: "bar" });
                
                process.exit(-1); // Returns -1
                    });

        child.on('message', (data) => {
            console.log(data.foo);
        });

        child.on('exit', (code) => {
            if (code !== null) {  // Process wasnt killed
                if (code == 0) {  // Process worked fine
                  // do something
                } else {  // Some error happened
                  var err = new Error("crap.");
                  // do something
                }
            }
        });