<commit_before>'use strict';

var forever = require('forever');
var monitor = require('forever-monitor');

var nodemailer = require('nodemailer');
var sendmailTransport = require('nodemailer-sendmail-transport');

var transporter = nodemailer.createTransport(sendmailTransport({
    path: 'sendmail',
    args: [ '-t', '-i' ],
}));

/**
 * Envoie un mail à l'admin du serveur
 */
var sendMail = function (subject, text, recipient, sender, done) {

    done = done || function () {};

    var mailOptions = {
        to: recipient,
        subject: subject,
        text: text,
    };

    if (sender) {
        mailOptions.from = sender;
    }

    // envoi du mail a l'admin
    transporter.sendMail(mailOptions, function (err) {
        if (err) { console.log(err); }
        done();
    });
};

var NodeServerRunner = function (serverFile, logFile, adminMail, senderMail, uid, maxRestartCount, minTimeBetweenCrashes) {
    this.serverFile = serverFile;
    this.logFile = logFile;
    this.adminMail = adminMail;
    this.senderMail = senderMail;
    this.uid = uid;
    this.maxRestartCount = maxRestartCount || 5;
    this.minTimeBetweenCrashes = minTimeBetweenCrashes || 6000;
};

NodeServerRunner.prototype = {

    constructor: NodeServerRunner,

    serverFile: null,
    logFile: null,
    adminMail: null,
    maxRestartCount: null,
    minTimeBetweenCrashes: null,
    loopRestartCount: 0,
    lastCrashTime: null,

    _sendInfo: function (subject, text, done) {
        // Tag en début de mail
        var tag = this.uid ? '[' + this.uid + '] ' : '';
        // Infos du serveur
        var serverInfos = '\n\n- uid : ' + this.uid + '\n- fichier : ' + this.serverFile + '\n- logs : ' + this.logFile;

        sendMail(tag + subject, text + serverInfos, this.adminMail, this.senderMail, done);
    },

    start: function () {

        console.log('Lancement du serveur ' + this.serverFile + '. Les logs seront enregistrés dans ' + this.logFile);
        console.log('Le serveur se relancera s\'il crash ' + this.maxRestartCount + ' fois (avec moins de ' + this.minTimeBetweenCrashes + 'ms d\'intervale)');
        console.log('Les mails d\'erreur seront envoyés à ' + this.adminMail + '.');

        var child = new (monitor.Monitor)(this.serverFile, {
            silent: true,
            uid: this.uid,
            args: [],
            errFile: this.logFile, // logs
            killTree: true,
        });



        forever.startServer(child);
<commit_msg>Ajoute les logs au fichier existant au lieu de l'écraser<commit_after>'use strict';

var forever = require('forever');
var monitor = require('forever-monitor');

var nodemailer = require('nodemailer');
var sendmailTransport = require('nodemailer-sendmail-transport');

var transporter = nodemailer.createTransport(sendmailTransport({
    path: 'sendmail',
    args: [ '-t', '-i' ],
}));

/**
 * Envoie un mail à l'admin du serveur
 */
var sendMail = function (subject, text, recipient, sender, done) {

    done = done || function () {};

    var mailOptions = {
        to: recipient,
        subject: subject,
        text: text,
    };

    if (sender) {
        mailOptions.from = sender;
    }

    // envoi du mail a l'admin
    transporter.sendMail(mailOptions, function (err) {
        if (err) { console.log(err); }
        done();
    });
};

var NodeServerRunner = function (serverFile, logFile, adminMail, senderMail, uid, maxRestartCount, minTimeBetweenCrashes) {
    this.serverFile = serverFile;
    this.logFile = logFile;
    this.adminMail = adminMail;
    this.senderMail = senderMail;
    this.uid = uid;
    this.maxRestartCount = maxRestartCount || 5;
    this.minTimeBetweenCrashes = minTimeBetweenCrashes || 6000;
};

NodeServerRunner.prototype = {

    constructor: NodeServerRunner,

    serverFile: null,
    logFile: null,
    adminMail: null,
    maxRestartCount: null,
    minTimeBetweenCrashes: null,
    loopRestartCount: 0,
    lastCrashTime: null,

    _sendInfo: function (subject, text, done) {
        // Tag en début de mail
        var tag = this.uid ? '[' + this.uid + '] ' : '';
        // Infos du serveur
        var serverInfos = '\n\n- uid : ' + this.uid + '\n- fichier : ' + this.serverFile + '\n- logs : ' + this.logFile;

        sendMail(tag + subject, text + serverInfos, this.adminMail, this.senderMail, done);
    },

    start: function () {

        console.log('Lancement du serveur ' + this.serverFile + '. Les logs seront enregistrés dans ' + this.logFile);
        console.log('Le serveur se relancera s\'il crash ' + this.maxRestartCount + ' fois (avec moins de ' + this.minTimeBetweenCrashes + 'ms d\'intervale)');
        console.log('Les mails d\'erreur seront envoyés à ' + this.adminMail + '.');

        var child = new (monitor.Monitor)(this.serverFile, {
            append: true,
            silent: true,
            uid: this.uid,
            args: [],
            errFile: this.logFile, // logs
            killTree: true,
        });



        forever.startServer(child);

{'fraction_non_alphanumeric': 0.13124875522804222, 'fraction_numerical': 0.0023899621589324837, 'mean_word_length': 2.665693430656934, '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': '150448', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1560, 'n_words': 391}