"use strict";

//Load dependencies
var async = require("async");
var applicationStorage = process.require("core/applicationStorage.js");
var updateModel = process.require("updates/updateModel.js");
var updateService = process.require("updates/updateService.js");
var guildService = process.require("guilds/guildService.js");
var bnetAPI = process.require("core/api/bnet.js");

/**
 * GuildUpdateProcess constructor
 * @constructor
 */
function GuildUpdateProcess() {
}

/**
 * Update a guild
 */
GuildUpdateProcess.prototype.updateGuild = function () {
    var self = this;
    var logger = applicationStorage.logger;


    async.waterfall([
        function (callback) {
            //Get next guild to update
            updateService.getNextUpdate('wp_gu', function (error, guildUpdate) {
                if (guildUpdate == null) {
                    //Guild update is empty
                    logger.info("No guild to update ... waiting 3 sec");
                    setTimeout(function () {
                        callback(true);
                    }, 3000);
                } else {
                    //CN PATCH
                    if (guildUpdate.region == 'cn') {
                        callback(true);
                    } else {
                        callback(error, guildUpdate);
                    }
                }
            });
        },
        function (guildUpdate, callback) {
            if (guildUpdate.priority <= 3) {
                updateModel.getCount("wp_cu", 3, function (error, count) {
                    if (count > 10000) {
                        logger.info("Too many characters in priority 3 ... waiting 1 min ");
                        updateModel.insert("wp_gu", guildUpdate.region, guildUpdate.realm, guildUpdate.name, guildUpdate.priority, function () {
                            setTimeout(function () {
                                callback(true);
                            }, 60000);
                        });
                    } else {
                        callback(error, guildUpdate);
                    }
                });
            } else {
                callback(null, guildUpdate)
            }
        },
        function (guildUpdate, callback) {
            //Sanitize name
            logger.info("Update guild %s-%s-%s", guildUpdate.region, guildUpdate.realm, guildUpdate.name);
            bnetAPI.getGuild(guildUpdate.region, guildUpdate.realm, guildUpdate.name, ["members"], function (error, guild) {
                if (error) {
                    if (error.statusCode == 403) {
                        logger.info("Bnet Api Deny ... waiting 1 min");
                        updateModel.insert("wp_gu", guildUpdate.region, guildUpdate.realm, guildUpdate.name, guildUpdate.priority, function () {
                            setTimeout(function () {
                                callback(true);
                            }, 60000);
                        });
                    } else {
                        callback(error);
                    }
                } else {
                    if (guild && guild.realm && guild.name) {
                        callback(error, guildUpdate.region, guild, guildUpdate.priority);
                    } else {
                        logger.warn("Bnet return empty guild skip it");
                        callback(true);
                    }
                }

            })
        },
        function (region, guild, priority, callback) {
            guildService.setMembersToUpdate(region, guild.realm, guild.name, guild.members, priority, function (error) {
                callback(error);
            });
        }
    ], function (error) {
        if (error && error !== true) {
            logger.error(error.message);
        }
        self.updateGuild();
    });
};

/**
 * Start GuildUpdateProcess
 * @param callback
 */
GuildUpdateProcess.prototype.start = function (callback) {
    applicationStorage.logger.info("Starting GuildUpdateProcess");
    this.updateGuild();
    callback();
};

module.exports = GuildUpdateProcess;
@@ -1444,9 +1444,708 @@
 ity 
-%3C
+== 0) %7B%0A                updateModel.getCount(%22wp_cu%22, 0, function (error, count) %7B%0A                    if (count %3E 10000) %7B%0A                        logger.info(%22Too many characters in priority 0 ... waiting 1 min %22);%0A                        updateModel.insert(%22wp_gu%22, guildUpdate.region, guildUpdate.realm, guildUpdate.name, guildUpdate.priority, function () %7B%0A                            setTimeout(function () %7B%0A                                callback(true);%0A                            %7D, 60000);%0A                        %7D);%0A                    %7D else %7B%0A                        callback(error, guildUpdate);%0A                    %7D%0A                %7D);%0A            %7D else if (guildUpdate.priority =
 = 3)

Fix bug on guildupdate priority 3 and 0 who can slow redis.