<commit_before>/*
 * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
 */

var jobsApi = module.exports;

var redis = require("redis")
	, kue = require('kue')
	, assert = require('assert')
	, config = process.mainModule.exports.config
	, logutils = require('../../utils/log.utils')
	, util = require('util')
    , redisPub = require('./redisPub')
    , commonUtils = require('../../utils/common.utils')
    , eventEmitter = require('events').EventEmitter
    , async = require('async')
    , discServ = require('./discoveryservice.api')
    , UUID = require('uuid-js')
    , jobUtils = require('../../common/jobs.utils')
	, messages = require('../../common/messages');

try {
    computeNode = require('../api/vrouternode.jobs.api');
} catch(e) {
    computeNode = {};
}
if (!module.parent) {
	logutils.logger.warn(util.format(messages.warn.invalid_mod_call,
		module.filename));
	process.exit(1);
}

var jobListenerReadyQ = {};
var jobListenerReadyQEvent = new eventEmitter();

jobsApi.kue = kue;

jobsApi.storeQ = {};

kue.redis.createClient = function ()
{
    var server_port = (config.redis_server_port) ?
        config.redis_server_port : global.DFLT_REDIS_SERVER_PORT;
    var server_ip = (config.redis_server_ip) ?
        config.redis_server_ip : global.DFLT_REDIS_SERVER_IP;
    var client = redis.createClient(server_port, server_ip);
    var uiDB = commonUtils.getWebUIRedisDBIndex();
    client.select(uiDB);
    return client;
}

kue.redis.createClient();
commonUtils.createRedisClient(function(client) {
    jobsApi.redisClient = client;
    jobsApi.jobs = kue.createQueue();
    jobsApi.jobs.promote();
    jobListenerReadyQEvent.emit('kueReady');
});

/* kue UI listening port */
var kuePort = config.kue.ui_port || 3002;
//kue.app.listen(kuePort);
//logutils.logger.info("KUE Jobs Infra UI listening on", kuePort);

/* Jobs are of below states:
 - `failed` the job has failed
 - `complete` the job has completed
 - `promotion` the job (when delayed) is now queued
 - `progress` the job's progress ranging from 0-100
 */
var JOB_PRIORITY_LOW = 10;
var JOB_PRIORITY_NORMAL = 0;
var JOB_PRIORITY_MEDIUM = -5;
var JOB_PRIORITY_HIGH = -10;
var JOB_PRIORITY_CRITICAL = -15;

var jobCheckState = ['delayed'];
function checkKueJobPriority(jobPriority)
{
	return ((jobPriority === 'low') ||
		(jobPriority === 'normal') ||
		(jobPriority === 'high') ||
		(jobPriority === 'critical'));
}

function getKueJobPriorityByValue (priority)
{
	switch (priority) {
		case JOB_PRIORITY_LOW:
			return 'low';
		case JOB_PRIORITY_NORMAL:
			return 'normal';
		case JOB_PRIORITY_MEDIUM:
			return 'medium';
		case JOB_PRIORITY_HIGH:
			return 'high';
		case JOB_PRIORITY_CRITICAL:
			return 'critical';
		default:
			return null;
	}
}

function getKueJobExist (jobStr, callback)
{
    var oldJobStr = jobStr, oldCallback = callback;
    if (typeof oldJobStr === 'undefined' || typeof oldCallback === 'undefined') {
        return function (newJobStr, newCallback) {
            redisPub.redisPubClient.zcard(kueJobStr, function(err, data) {
                /* zcard returns the sorted set cardinality (number of elements) 
                   of the sorted set stored at key
                 */
                if (err) {
                    newCallback(err);
                } else {
                    newCallback(null, data);
                }
            });
        }
    } else {
        redisPub.redisPubClient.zcard(jobStr, function (err, data) {
            if (err) {
                callback(err);
            } else {
                callback(null, data);
            }
        });
    }
}

function doJobExist (jobName, callback)
{
    /* Check wheather job is already created or not */
    var len = jobCheckState.length;
    var kueJobStrArr = [];
    var jobExists = false;
    var count = 0;
    
    for (var i = 0; i < len; i++) {
        kueJobStrArr[i] = 'q:jobs:' + jobName + ':' + jobCheckState[i];
    }
    async.map(kueJobStrArr, getKueJobExist, function(err, resultArr) {
        if (resultArr) {
            count = resultArr.length;
            for (i = 0; i < count; i++) {
                if (resultArr[i]) {
                    /* Job Exists */
                    jobExists = true;
                    break;
                } else {
                    jobExists = false;
                }
            }
        }
        callback(err, jobExists);
    });
}

/* Function createJob:
 @jobName    : name of the job
 @jobTitle   : title of the job, useful for kue UI
 @jobPriority: priority of job
 @delayInMS  : delay to execute the job once submitted.
 @runCount   : how many times the job should be executed once
 done, if 0, then it runs infinite time
 */
function createJob (jobName, jobTitle, jobPriority, delayInMS, runCount, taskData)
{
    taskData['genBy'] = global.service.MIDDLEWARE;
    doJobExist(jobName, function(err, jobExists) {
        if (true == jobExists) {
            runCount = 1;
            /* Create a Job with runCount = 1, so only one time run */
            jobsApi.kue.Job.rangeByType(jobName, 'delayed', 0,
                                        global.MAX_INT_VALUE, 'desc',
                                        function (err, selectedJobs) {
                selectedJobs.forEach(function (job) {
                    taskData =
                        jobUtils.updateJobDataRequiresField(job.data, taskData);
                    var reqBy = job.data.taskData.reqBy;
                    if ((global.REQ_AT_SYS_INIT == reqBy) &&
                        (global.REQ_AT_SYS_INIT != taskData.reqBy)) {
                        /* Stored JOB is Requested at System INIT */
                        job.data.taskData = taskData;
                        checkAndRequeueJobs(job);
                        return;
                    }
                });<commit_msg>Handle the error gracefully when rangeByType returns null. Closes-Bug: #1417050<commit_after>/*
 * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
 */

var jobsApi = module.exports;

var redis = require("redis")
	, kue = require('kue')
	, assert = require('assert')
	, config = process.mainModule.exports.config
	, logutils = require('../../utils/log.utils')
	, util = require('util')
    , redisPub = require('./redisPub')
    , commonUtils = require('../../utils/common.utils')
    , eventEmitter = require('events').EventEmitter
    , async = require('async')
    , discServ = require('./discoveryservice.api')
    , UUID = require('uuid-js')
    , jobUtils = require('../../common/jobs.utils')
	, messages = require('../../common/messages');

try {
    computeNode = require('../api/vrouternode.jobs.api');
} catch(e) {
    computeNode = {};
}
if (!module.parent) {
	logutils.logger.warn(util.format(messages.warn.invalid_mod_call,
		module.filename));
	process.exit(1);
}

var jobListenerReadyQ = {};
var jobListenerReadyQEvent = new eventEmitter();

jobsApi.kue = kue;

jobsApi.storeQ = {};

kue.redis.createClient = function ()
{
    var server_port = (config.redis_server_port) ?
        config.redis_server_port : global.DFLT_REDIS_SERVER_PORT;
    var server_ip = (config.redis_server_ip) ?
        config.redis_server_ip : global.DFLT_REDIS_SERVER_IP;
    var client = redis.createClient(server_port, server_ip);
    var uiDB = commonUtils.getWebUIRedisDBIndex();
    client.select(uiDB);
    return client;
}

kue.redis.createClient();
commonUtils.createRedisClient(function(client) {
    jobsApi.redisClient = client;
    jobsApi.jobs = kue.createQueue();
    jobsApi.jobs.promote();
    jobListenerReadyQEvent.emit('kueReady');
});

/* kue UI listening port */
var kuePort = config.kue.ui_port || 3002;
//kue.app.listen(kuePort);
//logutils.logger.info("KUE Jobs Infra UI listening on", kuePort);

/* Jobs are of below states:
 - `failed` the job has failed
 - `complete` the job has completed
 - `promotion` the job (when delayed) is now queued
 - `progress` the job's progress ranging from 0-100
 */
var JOB_PRIORITY_LOW = 10;
var JOB_PRIORITY_NORMAL = 0;
var JOB_PRIORITY_MEDIUM = -5;
var JOB_PRIORITY_HIGH = -10;
var JOB_PRIORITY_CRITICAL = -15;

var jobCheckState = ['delayed'];
function checkKueJobPriority(jobPriority)
{
	return ((jobPriority === 'low') ||
		(jobPriority === 'normal') ||
		(jobPriority === 'high') ||
		(jobPriority === 'critical'));
}

function getKueJobPriorityByValue (priority)
{
	switch (priority) {
		case JOB_PRIORITY_LOW:
			return 'low';
		case JOB_PRIORITY_NORMAL:
			return 'normal';
		case JOB_PRIORITY_MEDIUM:
			return 'medium';
		case JOB_PRIORITY_HIGH:
			return 'high';
		case JOB_PRIORITY_CRITICAL:
			return 'critical';
		default:
			return null;
	}
}

function getKueJobExist (jobStr, callback)
{
    var oldJobStr = jobStr, oldCallback = callback;
    if (typeof oldJobStr === 'undefined' || typeof oldCallback === 'undefined') {
        return function (newJobStr, newCallback) {
            redisPub.redisPubClient.zcard(kueJobStr, function(err, data) {
                /* zcard returns the sorted set cardinality (number of elements) 
                   of the sorted set stored at key
                 */
                if (err) {
                    newCallback(err);
                } else {
                    newCallback(null, data);
                }
            });
        }
    } else {
        redisPub.redisPubClient.zcard(jobStr, function (err, data) {
            if (err) {
                callback(err);
            } else {
                callback(null, data);
            }
        });
    }
}

function doJobExist (jobName, callback)
{
    /* Check wheather job is already created or not */
    var len = jobCheckState.length;
    var kueJobStrArr = [];
    var jobExists = false;
    var count = 0;
    
    for (var i = 0; i < len; i++) {
        kueJobStrArr[i] = 'q:jobs:' + jobName + ':' + jobCheckState[i];
    }
    async.map(kueJobStrArr, getKueJobExist, function(err, resultArr) {
        if (resultArr) {
            count = resultArr.length;
            for (i = 0; i < count; i++) {
                if (resultArr[i]) {
                    /* Job Exists */
                    jobExists = true;
                    break;
                } else {
                    jobExists = false;
                }
            }
        }
        callback(err, jobExists);
    });
}

/* Function createJob:
 @jobName    : name of the job
 @jobTitle   : title of the job, useful for kue UI
 @jobPriority: priority of job
 @delayInMS  : delay to execute the job once submitted.
 @runCount   : how many times the job should be executed once
 done, if 0, then it runs infinite time
 */
function createJob (jobName, jobTitle, jobPriority, delayInMS, runCount, taskData)
{
    taskData['genBy'] = global.service.MIDDLEWARE;
    doJobExist(jobName, function(err, jobExists) {
        if (true == jobExists) {
            runCount = 1;
            /* Create a Job with runCount = 1, so only one time run */
            jobsApi.kue.Job.rangeByType(jobName, 'delayed', 0,
                                        global.MAX_INT_VALUE, 'desc',
                                        function (err, selectedJobs) {
              if ((null == err) && (null != selectedJobs)) {
                selectedJobs.forEach(function (job) {
                    taskData =
                        jobUtils.updateJobDataRequiresField(job.data, taskData);
                    var reqBy = job.data.taskData.reqBy;
                    if ((global.REQ_AT_SYS_INIT == reqBy) &&
                        (global.REQ_AT_SYS_INIT != taskData.reqBy)) {
                        /* Stored JOB is Requested at System INIT */
                        job.data.taskData = taskData;
                        checkAndRequeueJobs(job);
                        return;
                    }
                });
              }
{'fraction_non_alphanumeric': 0.12187896741430385, 'fraction_numerical': 0.0053322048243757934, 'mean_word_length': 2.200433369447454, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3852266', 'n_tokens_mistral': 3768, 'n_tokens_neox': 3600, 'n_words': 848}