Как отменить операцию клонирования nodegit?

#nodegit

#nodegit

Вопрос:

Как отменить длительную операцию клонирования с помощью nodegit? Наш репозиторий размером 3 ГБ, пользователь может захотеть прервать его, потому что это занимает слишком много времени.

Могу ли я просто отклонить обещание? Вот так?

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

Ответ №1:

Получили наш ответ с сообщением о gitter.im .

В принципе, мы не можем отклонить обещание. Нам нужно создать дочерний процесс с помощью child_process.fork() и убить его, чтобы прервать клонирование.

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

...
childProcess.kill();
  

Ответ №2:

(добавление в 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
                }
            }
        });