<commit_before><commit_msg>module for callback based on events<commit_after>var http = require('http');
var MG = require('./my_globals').C;
var url = require('url');
var db = require('./dbrelayer');

function init(emitter, callback) {
    "use strict";
    emitter.on(MG.EVENT_NEWSTATE, function new_event(data) {
        console.log("DATA EN EVENTO")
        console.dir(data)
        if (data.state === MG.STATE_ERROR || data.state === MG.STATE_COMPLETED) {
            do_http_callback(data.task, data.result, function (error, result) {
                var st = {
                    id:data.task.id,
                    state:MG.CALLBACK_RESULT,
                    date:new Date(),
                    task:data.task,
                    err:error,
                    result:result
                };

                emitter.emit(MG.EVENT_NEWSTATE, st);

            });
        }
    });

}

function do_http_callback(task, resp_obj, callback) {
    "use strict";
    var callback_host = task.headers[MG.HEAD_RELAYER_HTTPCALLBACK];
    var cb_res;
    if (callback_host) {
        var callback_options = url.parse(callback_host);
        callback_options.method = 'POST';
        var callback_req = http.request(callback_options, function (callback_res) {
            //check callback_res status (modify state) Not interested in body
            cb_res = {callback_status:callback_res.statusCode};

                if (callback) {
                    callback(null, cb_res);
                }
        });


        callback_req.on('error', function (err) {
            //error in request
            var str_err = JSON.stringify(err);  // Too much information?????
            var cb_st = {callback_status:str_err};
            //store iff persistence policy

                if (callback) {
                    callback(cb_st, null);
                }

        });
        var str_resp_obj = JSON.stringify(resp_obj);
        callback_req.write(str_resp_obj);
        callback_req.end();
    } else {
        if (callback) {
            callback(null);
        }
    }
}

exports.init = init;
{'fraction_non_alphanumeric': 0.11375535459305093, 'fraction_numerical': 0.0, 'mean_word_length': 1.7841059602649008, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6561139', 'n_tokens_mistral': 610, 'n_tokens_neox': 579, 'n_words': 122}