<commit_before>// Copyright 2013 The GearmaNode Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*
 * @fileoverview This script represents class communicating as a worker with Gearman job server.
 * @author <EMAIL> (<NAME>)
 */

var util          = require('util'),
    events        = require('events'),
	winston       = require('winston'),
    ServerManager = require('./server-manager').ServerManager,
    Job           = require('./job').Job,
    protocol      = require('./protocol'),
    common        = require('./common');


/**
 * @class Worker
 * @classdesc A Worker for communicating with Gearman job servers.
 * @constructor
 * @augments events.EventEmitter
 * @mixes ServerManager
 *
 * @param options literal representing the client
 * @param {string} options.host hostname of single job server
 * @param {number} options.port port of single job server
 * @param {array} options.servers array of host,port pairs of multiple job servers
 * @param {boolean} options.withUnique flag whether a job will be grabbed with the client assigned unique ID
 */
var Worker = exports.Worker = function(options) {
    var returned;

    this._type = 'Worker';
    // call ServerManager initialization
    returned = this.initServers(options);
    if (returned instanceof Error) { return returned; }

    // VALIDATION
    if (this.hasOwnProperty('withUnique')) { // AAA!!!
        returned = common.verifyOptions({withUnique: options.withUnique}, {withUnique: [true, false]});
        if (returned instanceof Error) { return returned; }
    }

    // table of functions successfully registered on a job server
    // {'name': [func, options]}
    this.functions = {};

    events.EventEmitter.call(this);
    Worker.logger.log('info', 'worker initialized with %d job server(s)', this.jobServers.length);
};

// inheritance
util.inherits(Worker, events.EventEmitter);
// mixes ServerManager
ServerManager.mixin(Worker);

// static logger
Worker.logger = winston.loggers.get('Worker');


/**
 * Registers a function name with the job server and specifies a callback corresponding to that function.
 *
 * @method
 * @param name name of a function
 * @param {Function} callback the function to be run when a job is received,  gets {@link Job} as parameter
 * @param options literal representing additional options
 * @param {number} options.timeout timeout value, the job server will mark the job as failed and notify any listening clients
 * @param {string} options.toStringEncoding if given received payload will be converted to String with this encoding, otherwise payload turned over as Buffer
 * @returns {void} nothing
 */
Worker.prototype.addFunction = function(name, callback, options) {
    var jobServer, pattern, returned;
    if (!name) { return new Error('undefined function name'); }
    if (!(callback instanceof Function)) { return new Error('invalid callback (not a function)'); }

    // validate options
    options = options || {};
    pattern = { timeout: 'optional', withUnique: 'optional', toStringEncoding: 'optional' }
    returned = common.verifyOptions(options, pattern);
    if (returned instanceof Error) { return returned; }

    // validate encoding
    if (options.hasOwnProperty('toStringEncoding') && !Buffer.isEncoding(options.toStringEncoding)) {
        return new Error('invalid encoding: ' + options.toStringEncoding);
    }

    for (var i = 0; i < this.jobServers.length; i ++) { // TODO iterate only servers without any previous error
        jobServer = this.jobServers[i];
        jobServer.send(protocol.encodePacket(protocol.PACKET_TYPES.CAN_DO, 'ascii', [name]));
        this._preSleep(jobServer);
        this.functions[name] = [callback, options];
        Worker.logger.log('debug', 'registered function name=%s on server uid=%s', name, jobServer.getUid());<commit_msg>logging of added function: debug->info<commit_after>// Copyright 2013 The GearmaNode Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*
 * @fileoverview This script represents class communicating as a worker with Gearman job server.
 * @author <EMAIL> (<NAME>)
 */

var util          = require('util'),
    events        = require('events'),
	winston       = require('winston'),
    ServerManager = require('./server-manager').ServerManager,
    Job           = require('./job').Job,
    protocol      = require('./protocol'),
    common        = require('./common');


/**
 * @class Worker
 * @classdesc A Worker for communicating with Gearman job servers.
 * @constructor
 * @augments events.EventEmitter
 * @mixes ServerManager
 *
 * @param options literal representing the client
 * @param {string} options.host hostname of single job server
 * @param {number} options.port port of single job server
 * @param {array} options.servers array of host,port pairs of multiple job servers
 * @param {boolean} options.withUnique flag whether a job will be grabbed with the client assigned unique ID
 */
var Worker = exports.Worker = function(options) {
    var returned;

    this._type = 'Worker';
    // call ServerManager initialization
    returned = this.initServers(options);
    if (returned instanceof Error) { return returned; }

    // VALIDATION
    if (this.hasOwnProperty('withUnique')) { // AAA!!!
        returned = common.verifyOptions({withUnique: options.withUnique}, {withUnique: [true, false]});
        if (returned instanceof Error) { return returned; }
    }

    // table of functions successfully registered on a job server
    // {'name': [func, options]}
    this.functions = {};

    events.EventEmitter.call(this);
    Worker.logger.log('info', 'worker initialized with %d job server(s)', this.jobServers.length);
};

// inheritance
util.inherits(Worker, events.EventEmitter);
// mixes ServerManager
ServerManager.mixin(Worker);

// static logger
Worker.logger = winston.loggers.get('Worker');


/**
 * Registers a function name with the job server and specifies a callback corresponding to that function.
 *
 * @method
 * @param name name of a function
 * @param {Function} callback the function to be run when a job is received,  gets {@link Job} as parameter
 * @param options literal representing additional options
 * @param {number} options.timeout timeout value, the job server will mark the job as failed and notify any listening clients
 * @param {string} options.toStringEncoding if given received payload will be converted to String with this encoding, otherwise payload turned over as Buffer
 * @returns {void} nothing
 */
Worker.prototype.addFunction = function(name, callback, options) {
    var jobServer, pattern, returned;
    if (!name) { return new Error('undefined function name'); }
    if (!(callback instanceof Function)) { return new Error('invalid callback (not a function)'); }

    // validate options
    options = options || {};
    pattern = { timeout: 'optional', withUnique: 'optional', toStringEncoding: 'optional' }
    returned = common.verifyOptions(options, pattern);
    if (returned instanceof Error) { return returned; }

    // validate encoding
    if (options.hasOwnProperty('toStringEncoding') && !Buffer.isEncoding(options.toStringEncoding)) {
        return new Error('invalid encoding: ' + options.toStringEncoding);
    }

    for (var i = 0; i < this.jobServers.length; i ++) { // TODO iterate only servers without any previous error
        jobServer = this.jobServers[i];
        jobServer.send(protocol.encodePacket(protocol.PACKET_TYPES.CAN_DO, 'ascii', [name]));
        this._preSleep(jobServer);
        this.functions[name] = [callback, options];
        Worker.logger.log('info', 'registered function name=%s on server uid=%s', name, jobServer.getUid());
{'fraction_non_alphanumeric': 0.11163162705667276, 'fraction_numerical': 0.002056672760511883, 'mean_word_length': 3.68576017130621, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2325846', 'n_tokens_mistral': 2260, 'n_tokens_neox': 2213, 'n_words': 861}