0

Using nodegit, how do I cancel a long running clone operation? Our repo is like 3GB, the user may want to abort it because it's taking too much time.

Can I just reject the promise? Like so?

var cloneRepository = NodeGit.Clone(cloneURL, localPath, cloneOptions);
...
if (abortCondition)
   cloneRepository.reject();
dstj
  • 4,800
  • 2
  • 39
  • 61

2 Answers2

0

Got our answer with a post on gitter.im.

Basically, we can't reject the promise. We need to spawn a child process using child_process.fork() and kill it to abort the clone.

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

...
childProcess.kill();
dstj
  • 4,800
  • 2
  • 39
  • 61
0

(adding to dstj)

If you don't want to create a complete new module file, you can use 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
                }
            }
        });
CiriousJoker
  • 552
  • 1
  • 7
  • 18