<commit_before>/* jshint node:true */
/**
 * PUSH Notification server
 * (c) Telefonica Digital, 2012 - All rights reserved
 * License: GNU Affero V3 (see LICENSE file)
 * <NAME> <<EMAIL>>
 * <NAME> <<EMAIL>>
 */

'use strict';


var Log = require('../common/Logger.js'),
    MsgBroker = require('../common/MsgBroker.js'),
    MobileNetwork = require('../common/MobileNetwork.js'),
    http = require('http'),
    https = require('https'),
    urlparser = require('url');

function NS_UA_UDP() {
    this.closingCorrectly = false;
    this.msgBrokerReady = false;
    this.readyTimeout = undefined;
}

NS_UA_UDP.prototype = {

    checkReady: function() {
        if (this.msgBrokerReady) {
            Log.debug('NS_UDP::checkReady --> We are ready. Clearing any readyTimeout');
            clearTimeout(this.readyTimeout);
        } else {
            Log.debug('NS_UDP::checkReady --> Not ready yet. msgBrokerReady=' + this.msgBrokerReady);
        }
        return this.msgBrokerReady;
    },

    start: function() {

        Log.info('NS_UDP:start --> Starting UA-UDP server');
        var self = this;

        MsgBroker.once('ready', function() {
            Log.info('NS_UDP::start --> MsgBroker ready and connected');
            self.msgBrokerReady = true;
            self.checkReady();
        });

        MsgBroker.on('ready', this.subscribeQueues.bind(this));

        //Hack. Once we have a disconnected queue, we must subscribe again for each
        //broker.
        //This happens on RabbitMQ as follows:
        // 1) We are connected to several brokers
        // 2) We are subscribed to the same queue on those brokers
        // 3) Master fails :(
        // 4) RabbitMQ promotes the eldest slave to be the master
        // 5) RabbitMQ disconnects all clients. Not a socket disconnection, but
        //    unbinds the connection to the subscribed queue.
        //
        // Hacky solution: once we have a disconnected queue (a socket error), we
        // subscribe again to the queue.
        // It's not beautiful (we should really unsubscribe all queues first), but works.
        // This *MIGHT* require OPS job if we have a long-long socket errors with queues.
        // (we might end up with gazillions (hundreds, really) callbacks on the same
        // socket for handling messages)
        MsgBroker.on('queuedisconnected', this.subscribeQueues.bind(this));

        MsgBroker.once('closed', function() {
            self.msgBrokerReady = false;
            if (self.closingCorrectly) {
                Log.info('NS_UDP::stop --> Closed MsgBroker');
                return;
            }
            Log.critical(Log.messages.CRITICAL_MBDISCONNECTED, {
                'class': 'NS_UDP',
                'method': 'start'
            });
            self.stop();
        });

        // Subscribe to the UDP common Queue
        process.nextTick(function() {
            MsgBroker.start();
            MobileNetwork.start();
        });<commit_msg>JIRA 31003. Stopping the mongos service in the UDP machine doesn't completely stop the push service<commit_after>/* jshint node:true */
/**
 * PUSH Notification server
 * (c) Telefonica Digital, 2012 - All rights reserved
 * License: GNU Affero V3 (see LICENSE file)
 * <NAME> <<EMAIL>>
 * <NAME> <<EMAIL>>
 */

'use strict';


var Log = require('../common/Logger.js'),
    MsgBroker = require('../common/MsgBroker.js'),
    MobileNetwork = require('../common/MobileNetwork.js'),
    http = require('http'),
    https = require('https'),
    urlparser = require('url');

function NS_UA_UDP() {
    this.closingCorrectly = false;
    this.msgBrokerReady = false;
    this.mobileNetworkReady = false;
    this.readyTimeout = undefined;
}

NS_UA_UDP.prototype = {

    checkReady: function() {
        if (this.msgBrokerReady && this.mobileNetworkReady) {
            Log.debug('NS_UDP::checkReady --> We are ready. Clearing any readyTimeout');
            clearTimeout(this.readyTimeout);
        } else {
            Log.debug('NS_UDP::checkReady --> Not ready yet. msgBrokerReady=' + this.msgBrokerReady + 'mobileNetworkReady=' + this.mobileNetworkReady);
        }
        return this.msgBrokerReady && this.mobileNetworkReady;
    },

    start: function() {

        Log.info('NS_UDP:start --> Starting UA-UDP server');
        var self = this;

        MsgBroker.once('ready', function() {
            Log.info('NS_UDP::start --> MsgBroker ready and connected');
            self.msgBrokerReady = true;
            self.checkReady();
        });

        MsgBroker.on('ready', this.subscribeQueues.bind(this));

        //Hack. Once we have a disconnected queue, we must subscribe again for each
        //broker.
        //This happens on RabbitMQ as follows:
        // 1) We are connected to several brokers
        // 2) We are subscribed to the same queue on those brokers
        // 3) Master fails :(
        // 4) RabbitMQ promotes the eldest slave to be the master
        // 5) RabbitMQ disconnects all clients. Not a socket disconnection, but
        //    unbinds the connection to the subscribed queue.
        //
        // Hacky solution: once we have a disconnected queue (a socket error), we
        // subscribe again to the queue.
        // It's not beautiful (we should really unsubscribe all queues first), but works.
        // This *MIGHT* require OPS job if we have a long-long socket errors with queues.
        // (we might end up with gazillions (hundreds, really) callbacks on the same
        // socket for handling messages)
        MsgBroker.on('queuedisconnected', this.subscribeQueues.bind(this));

        MsgBroker.once('closed', function() {
            self.msgBrokerReady = false;
            if (self.closingCorrectly) {
                Log.info('NS_UDP::stop --> Closed MsgBroker');
                return;
            }
            Log.critical(Log.messages.CRITICAL_MBDISCONNECTED, {
                'class': 'NS_UDP',
                'method': 'start'
            });
            self.stop();
        });

        MobileNetwork.on('ready', function() {
            Log.info('NS_UDP::start --> MobileNetwork is ready');
            self.mobileNetworkReady = true;
            self.checkReady();
        });

        MobileNetwork.once('closed', function() {
            self.mobileNetworkReady = false;
            if (self.closingCorrectly) {
                Log.info('NS_UDP::stop --> Closed MobileNetwork (DataStore)');
                return;
            }
            Log.critical(Log.messages.CRITICAL_DBDISCONNECTED, {
                'class': 'NS_UDP',
                'method': 'start'
            });
            self.stop();
        });

        // Subscribe to the UDP common Queue
        process.nextTick(function() {
            MsgBroker.start();
            MobileNetwork.start();
        });
{'fraction_non_alphanumeric': 0.11997661844220371, 'fraction_numerical': 0.0036533684056700277, 'mean_word_length': 2.091237579042457, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1165423', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1874, 'n_words': 535}