0

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

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

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

2 回答 2

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
                }
            }
        });

于 2017-07-26T15:46:06.470 回答
0

通过 gitter.im 上的帖子得到了我们的答案。

基本上,我们不能拒绝承诺。我们需要使用child_process.fork()并杀死它来生成一个子进程以中止克隆。

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

...
childProcess.kill();
于 2016-10-12T16:19:02.567 回答