<commit_before>import http from 'http';
import cluster from 'cluster';
import Router from 'node-simple-router';

import Config from './Config';
import routes from './routes';

const _config = new Config();

class S3Server {
    /**
     * This represents our S3 connector.
     * @constructor
     * @param {Worker} [worker=null] - Track the worker when using cluster
     */
    constructor(worker) {
        this.worker = worker;
        this.router = new Router({
            'list_dir': true,
            logging: true,
            'use_nsr_session': false,
        });
        http.globalAgent.keepAlive = true;

        process.on('SIGINT', this.cleanUp.bind(this));
        process.on('SIGHUP', this.cleanUp.bind(this));
        process.on('SIGQUIT', this.cleanUp.bind(this));
        process.on('SIGTERM', this.cleanUp.bind(this));
        process.on('SIGPIPE', () => {});
    }

    /**
     * This starts the http server.
     */
    startup() {
        // Todo: http.globalAgent.maxSockets, http.globalAgent.maxFreeSockets

        this.server = http.createServer((req, res) => {
            Promise.resolve(this.router(req, res)).catch((err) => {
                this.server.close();
                res.statusCode = 500;
                res.setHeader('content-type', 'text/plain');
                res.end(`There was a problem. ${err}`);
            });
        });
        this.server.on('listening', () => {
            const addr = this.server.address() || {
                address: '0.0.0.0',
                port: _config.port,
            };
            this.router.log(`Serving web content at ${addr.address}${addr.port}`
                    + ` - PID: ${process.pid}`);
        });
        this.server.listen(_config.port);
        routes(this.router);
    }

    /**
     * This exits the running process properly.
     */
    cleanUp() {
        console.log("Server shutting down...");
        this.server.close();
        process.exit(0);
    }
}

export default function main() {
    if (_config.clustering && cluster.isMaster) {
        for (let n = 0; n < _config.clusters; n++) {
            cluster.fork();
        }
        cluster.on('disconnect', worker => {
            console.log(`worker ${worker.id} disconnected. Restarting.`);
            cluster.fork();
        });
    } else if (cluster.isWorker) {
        const server = new S3Server(cluster.worker);
        server.startup();
    } else {
        const server = new S3Server;
        server.startup();
    }
}<commit_msg>fix(server): do not clusterize in memory<commit_after>import http from 'http';
import cluster from 'cluster';
import Router from 'node-simple-router';

import Config from './Config';
import routes from './routes';

const _config = new Config();

class S3Server {
    /**
     * This represents our S3 connector.
     * @constructor
     * @param {Worker} [worker=null] - Track the worker when using cluster
     */
    constructor(worker) {
        this.worker = worker;
        this.router = new Router({
            'list_dir': true,
            logging: true,
            'use_nsr_session': false,
        });
        http.globalAgent.keepAlive = true;

        process.on('SIGINT', this.cleanUp.bind(this));
        process.on('SIGHUP', this.cleanUp.bind(this));
        process.on('SIGQUIT', this.cleanUp.bind(this));
        process.on('SIGTERM', this.cleanUp.bind(this));
        process.on('SIGPIPE', () => {});
    }

    /**
     * This starts the http server.
     */
    startup() {
        // Todo: http.globalAgent.maxSockets, http.globalAgent.maxFreeSockets

        this.server = http.createServer((req, res) => {
            Promise.resolve(this.router(req, res)).catch((err) => {
                this.server.close();
                res.statusCode = 500;
                res.setHeader('content-type', 'text/plain');
                res.end(`There was a problem. ${err}`);
            });
        });
        this.server.on('listening', () => {
            const addr = this.server.address() || {
                address: '0.0.0.0',
                port: _config.port,
            };
            this.router.log(`Serving web content at ${addr.address}${addr.port}`
                    + ` - PID: ${process.pid}`);
        });
        this.server.listen(_config.port);
        routes(this.router);
    }

    /**
     * This exits the running process properly.
     */
    cleanUp() {
        console.log("Server shutting down...");
        this.server.close();
        process.exit(0);
    }
}

export default function main() {
    if (_config.clustering && cluster.isMaster && !process.env.S3BACKEND) {
        for (let n = 0; n < _config.clusters; n++) {
            cluster.fork();
        }
        cluster.on('disconnect', worker => {
            console.log(`worker ${worker.id} disconnected. Restarting.`);
            cluster.fork();
        });
    } else if (cluster.isWorker) {
        const server = new S3Server(cluster.worker);
        server.startup();
    } else {
        const server = new S3Server;
        server.startup();
    }
}
{'fraction_non_alphanumeric': 0.152272280149518, 'fraction_numerical': 0.005311823726145977, 'mean_word_length': 1.8952164009111618, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4762220', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1474, 'n_words': 312}