多线程中的电子过程在发生错误后不会关闭

时间:2016-09-01 23:44:57

标签: javascript node.js multithreading electron nightmare

情况: 我想创建一个多线程脚本,我用CSV加载IP列表和帐户信息。 我加载数据并调用一个函数,我打开电子并与Vo一起运行我的恶梦脚本。在剧本里面,我去了一个网站,循环浏览一个链接列表,检查是否有人住在澳大利亚。 当我有错误时,例如Timeout,浏览器停止工作。 错误示例 - >

{ message: 'navigation error',
  code: -7,
  details: 'Navigation timed out after 30000 ms',
  url: 'https://facebook.com/login' }

这是我的代码

var fs = require('fs');
var csv = require('fast-csv');
var vo = require('vo');
var Nightmare = require('nightmare');

    var count = 0;
    var urls = fs.readFileSync('uniqueIds.csv').toString().split("\n");

    var arrayUrls = Object.keys(urls).map(function (key) {return urls[key]});

    var bloqNumber = 0;
    function *run(proxy, user, pass, urlsID) {
        var nightmare = new Nightmare({ 
            webPreferences: { partition: 'your-custom-partition'},
            switches:{
            'proxy-server': proxy,
            'ignore-certificate-errors': true
        }, show: true });

        yield nightmare
        .goto('https://facebook.com/login')
        .wait(".inputtext._55r1.inputtext._1kbt.inputtext._1kbt")
        .type('input[name="email"]', user)
        .type('input[name="pass"]', pass)
        .click('button[name=login]')
        .wait(29000);

        var range = urlsID * 2000;
        var rangeStart = range - 2000;
        var urlsarray = arrayUrls.slice(rangeStart, range);
        for (var i = 0; i < urlsarray.length; i++) {
                count++;
                console.log(count + " -> " + proxy);
                if (count > 150){
                    yield nightmare.end();
                }
                yield nightmare
                .goto("https://www.facebook.com/profile.php?id=" + urlsarray[i] + "&sk=about&section=living&pnref=about")
                .wait(1000);
                var seqCheck = yield nightmare.exists(".captcha_interstitial");
                var bloqCheck = yield nightmare.exists(".mvl.ptm.uiInterstitial.uiInterstitialLarge.uiBoxWhite");
                if (seqCheck == true) {
                  console.log("Seqcheck");
                    yield nightmare.wait(29000);
                }
                if (bloqCheck == true) {
                  console.log("Blocked for a week" + user + proxy);
                  bloqNumber++;
                  console.log(bloqNumber);
                if (bloqNumber > 6) {
                  yield nightmare.end();
                }
                  continue;
                }

                var location = yield nightmare.exists("._3pw9._2pi4._2ge8");
                bloqNumber = 0;
                console.log(location);
                if (location == true) {
                  var getLocation = yield nightmare.evaluate(function() {
                                  var jsonObject = new Array();
                                  var links = document.getElementsByClassName('_3pw9 _2pi4 _2ge8');
                                  var numProfiles = links.length;
                                    for(var i = 0; i< numProfiles; i++){
                                    var elem;
                                    try {
                                    elem = links[0].querySelector("._50f5._50f7 a").text;
                                      } catch (err) {
                                        var arrr = new Array('Hello', 'world');
                                        return arrr;
                                      }
                                    jsonObject.push(elem);
                                  }
                                  return jsonObject;
                                });
                 var locationString = getLocation.join(" + ");
                 console.log(locationString + " -> " + urlsarray[i]);
                   if (locationString.indexOf("Australia") !== -1 ||
                      locationString.indexOf("Queensland") !== -1 || 
                      locationString.indexOf("New South Wales") !== -1 || 
                      locationString.indexOf("Victoria") !== -1 ||
                      locationString.indexOf("Northern Territory") !== -1 || 
                      locationString.indexOf("South Australia") !== -1||
                      locationString.indexOf("Tasmania") !== -1 ||
                      locationString.indexOf("Sydney") !== -1 ||
                      locationString.indexOf("Adelaide") !== -1 ||
                      locationString.indexOf("Cairns") !== -1 ||
                      locationString.indexOf("Perth") !== -1 ||
                      locationString.indexOf("Melbourne") !== -1 ||
                      locationString.indexOf("Brisbane") !== -1 ||
                      locationString.indexOf("Bundaberg") !== -1 ||
                      locationString.indexOf("Canberra") !== -1 ||
                      locationString.indexOf("Newcastle") !== -1 ||
                      locationString.indexOf("Western Australia") !== -1 ) {
                    console.log("Im in australia");

                 var stringToPrint = urlsarray[i] + ", " + locationString + "\n";
                    fs.appendFile('pages.csv', stringToPrint.replace(/(\r\n|\n|\r)/gm,"") + "\n", function (err) {
                        console.log("a new entry");
                      });
                    }
                    } else {
                      console.log("It was false");
                    }

                }
                yield nightmare.end();
        }  

fs.createReadStream('proxies.csv')
    .pipe(csv())
    .on('data', function (data) {
        var proxy = data[0];
        var user = data[1];
        var pass = data[2];
        var urlsID = data[3];
        console.log(urlsID);
        console.log(user);
        console.log(pass);
        vo(run(proxy, user, pass, urlsID)).then(out => console.log('out', out)).catch(error => console.log(error));
    }).on('end', function (data) {
    console.log('CSV reading finished.')
});

期望的结果: 我希望每次我得到某种错误,我的线程正在关闭。

1 个答案:

答案 0 :(得分:0)

解决。只需在下面的示例中附加.catch即可。

yield nightmare
.goto('https://facebook.com/login')
.wait(".inputtext._55r1.inputtext._1kbt.inputtext._1kbt")
.type('input[name="email"]', user)
.type('input[name="pass"]', pass)
.click('button[name=login]')
.wait(29000).catch(function(err){
                  console.dir(err);
                  nightmare.end();
                });