<commit_before>/**
 * Created by AsherS on 8/24/15.
 */

var _ = require('underscore');
var logger = require('./logger/logger')(module);
var flavorDownloader = require('./FlavorDownloader');
var masterManifestGeneratorCreator = require('./MasterManifestGenerator');
var Q = require('q');
var ErrorUtils = require('./utils/error-utils');
var util = require('util');
var events = require("events");

var EntryDownloader = (function () {

    function EntryDownloader(entryId, hostname, port, applicationName, manifestTimeWindow, manifestMaxChunkCount) {
        events.EventEmitter.call(this);
        this.entryId = entryId;
        this.manifestTimeWindow = manifestTimeWindow;
        this.manifestMaxChunkCount = manifestMaxChunkCount;
        this.flavorsDownloaders = [];
        this.masterManifestGenerator = masterManifestGeneratorCreator(entryId, hostname, port, applicationName, logger);
    }

    util.inherits(EntryDownloader, events.EventEmitter);

    var onFlavorDownloaderStopped = function(flavor){
        var that = this;
        logger.info("Flavor " + flavor + " stopped - checking if any flavor is still active for entry " + that.entryId);
        if (_.every(that.flavorsDownloaders, function(f){ return !f.isEnabled; }))
        {
            logger.info("No remaining active flavor downloaders for entry " + that.entryId + " - shutting down entry downloader");
            that.emit('stopped', that.entryId);
        }
    };

    EntryDownloader.prototype.start = function () {
        var that = this;
        logger.info("Entry start requested for entry " + that.entryId);
        var persistenceFormat = require('./../common/PersistenceFormat');
        logger.info("Starting entry downloader for entryId: " + that.entryId + "\n");
        return that.masterManifestGenerator.getAllFlavors()
            .then(function(flavors) {
                that.flavorsDownloaders = _.chain(flavors)
                    .uniq(function(flavor){
                        return flavor.bandwidth;
                    })
                    .map(function (flavor) {
                        var playlistName = persistenceFormat.getManifestName();
                        var bitrate = flavor.bandwidth;
                        var destPath = persistenceFormat.getFlavorFullPath(that.entryId, bitrate);
                        logger.info("EntryId: " + that.entryId + " - creating flavor downloader: " + bitrate);
                        return new flavorDownloader(flavor.liveURL, destPath, that.entryId, bitrate, playlistName, that.manifestTimeWindow, that.manifestMaxChunkCount);
                    }).value();

                //start the download
                var promises = _.map(that.flavorsDownloaders, function (f) {
                    logger.info("EntryId: " + that.entryId + " - starting flavor downloader: " + f.flavor);
                    f.on('stopped', onFlavorDownloaderStopped.bind(that));
                    return f.start();
                });

                return Q.allSettled(promises);

            }).then(function (results) {

                var errs = ErrorUtils.aggregateErrors(results);

                if (errs.numErrors === results.length) {
                    // Fail if no flavor downloader could be started
                    throw new Error("Failed to start entry downloader for entry: " + that.entryId + ". All flavor downloaders failed\n" + errs.err);
                }

                if (errs.numErrors > 0) {
                    // Report an error (but proceed) if only some flavor downloaders could not be started
                    logger.error('Failed to start flavor downloaders for some of the flavors:\n',errs.err);
                }
            });
    };

    EntryDownloader.prototype.stop = function () {
        var that = this;
        logger.info("Entry stop requested for entry " + this.entryId);
        logger.info('stopping entry downloader for entry id: ' + this.entryId);
        var stopPromises =_.map(this.flavorsDownloaders, function (downloader) {
            return downloader.stop();
        });

        return Q.all(stopPromises).then(function(){
            logger.info('successfully stopped entry downloader for entry id: ' + that.entryId);
        }, function(err){
            logger.error('failed stopping flavor downloader for entry id: ' + that.entryId + ' and flavor ' + downloader.flavor + "\n" + err + "\n" + err.stack);
            throw err;
        });
    };

    return EntryDownloader;
})();

module.exports = EntryDownloader;<commit_msg>reuse a single stop promise for the entry downloader<commit_after>/**
 * Created by AsherS on 8/24/15.
 */

var _ = require('underscore');
var logger = require('./logger/logger')(module);
var flavorDownloader = require('./FlavorDownloader');
var masterManifestGeneratorCreator = require('./MasterManifestGenerator');
var Q = require('q');
var ErrorUtils = require('./utils/error-utils');
var util = require('util');
var events = require("events");

var EntryDownloader = (function () {

    function EntryDownloader(entryId, hostname, port, applicationName, manifestTimeWindow, manifestMaxChunkCount) {
        events.EventEmitter.call(this);
        this.entryId = entryId;
        this.manifestTimeWindow = manifestTimeWindow;
        this.manifestMaxChunkCount = manifestMaxChunkCount;
        this.flavorsDownloaders = [];
        this.masterManifestGenerator = masterManifestGeneratorCreator(entryId, hostname, port, applicationName, logger);
    }

    util.inherits(EntryDownloader, events.EventEmitter);

    var onFlavorDownloaderStopped = function(flavor){
        var that = this;
        logger.info("Flavor " + flavor + " stopped - checking if any flavor is still active for entry " + that.entryId);
        if (_.every(that.flavorsDownloaders, function(f){ return !f.isEnabled; }))
        {
            logger.info("No remaining active flavor downloaders for entry " + that.entryId + " - shutting down entry downloader");
            that.emit('stopped', that.entryId);
        }
    };

    EntryDownloader.prototype.start = function () {
        var that = this;
        logger.info("Entry start requested for entry " + that.entryId);
        var persistenceFormat = require('./../common/PersistenceFormat');
        logger.info("Starting entry downloader for entryId: " + that.entryId + "\n");
        return that.masterManifestGenerator.getAllFlavors()
            .then(function(flavors) {
                that.flavorsDownloaders = _.chain(flavors)
                    .uniq(function(flavor){
                        return flavor.bandwidth;
                    })
                    .map(function (flavor) {
                        var playlistName = persistenceFormat.getManifestName();
                        var bitrate = flavor.bandwidth;
                        var destPath = persistenceFormat.getFlavorFullPath(that.entryId, bitrate);
                        logger.info("EntryId: " + that.entryId + " - creating flavor downloader: " + bitrate);
                        return new flavorDownloader(flavor.liveURL, destPath, that.entryId, bitrate, playlistName, that.manifestTimeWindow, that.manifestMaxChunkCount);
                    }).value();

                //start the download
                var promises = _.map(that.flavorsDownloaders, function (f) {
                    logger.info("EntryId: " + that.entryId + " - starting flavor downloader: " + f.flavor);
                    f.on('stopped', onFlavorDownloaderStopped.bind(that));
                    return f.start();
                });

                return Q.allSettled(promises);

            }).then(function (results) {

                var errs = ErrorUtils.aggregateErrors(results);

                if (errs.numErrors === results.length) {
                    // Fail if no flavor downloader could be started
                    throw new Error("Failed to start entry downloader for entry: " + that.entryId + ". All flavor downloaders failed\n" + errs.err);
                }

                if (errs.numErrors > 0) {
                    // Report an error (but proceed) if only some flavor downloaders could not be started
                    logger.error('Failed to start flavor downloaders for some of the flavors:\n',errs.err);
                }
            });
    };

    EntryDownloader.prototype.stop = function () {
        var that = this;
        if (that.stopPromise)
        {
            logger.info("Entry stop already requested for entry " + this.entryId + " ignoring request");
            return that.stopPromise;
        }

        logger.info("Entry stop requested for entry " + this.entryId);
        logger.info('stopping entry downloader for entry id: ' + this.entryId);
        var stopPromises =_.map(this.flavorsDownloaders, function (downloader) {
            return downloader.stop();
        });

        that.stopPromise = Q.all(stopPromises).then(function(){
            logger.info('successfully stopped entry downloader for entry id: ' + that.entryId);
        }, function(err){
            logger.error('failed stopping flavor downloader for entry id: ' + that.entryId + ' and flavor ' + downloader.flavor + "\n" + err + "\n" + err.stack);
            throw err;
        });

        return that.stopPromise;
    };

    return EntryDownloader;
})();

module.exports = EntryDownloader;
{'fraction_non_alphanumeric': 0.11073181139321528, 'fraction_numerical': 0.0012801365478984424, 'mean_word_length': 2.2361063168795305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 69, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '733112', 'n_tokens_mistral': 2471, 'n_tokens_neox': 2373, 'n_words': 592}