<commit_before>/**
 * Server application
 * @module arpen/app/server
 */
const debug = require('debug')('arpen:app');
const path = require('path');
const App = require('./base');
const WError = require('verror').WError;

/**
 * Server application class
 * @extends module:arpen/app/base~App
 */
class Server extends App {
    /**
     * Create app
     * @param {string} basePath             Base path
     * @param {string[]} argv               Arguments
     */
    constructor(basePath, argv) {
        super(basePath, argv);

        this._started = new Set();
    }

    /**
     * Initialize the app
     * @param {...string} names                         Server names
     * @return {Promise}
     */
    init(...names) {
        return super.init()
            .then(() => {
                return this._initLogger();
            })
            .then(() => {
                let config = this.get('config');
                let servers = new Map();
                this.registerInstance(servers, 'servers');

                for (let name of names) {
                    let params = config.get(`servers.${name}`);
                    if (!params)
                        throw new Error(`Server ${name} not found in config`);

                    let server = this.get(params.class);
                    if (!server)
                        throw new Error(`Service ${params.class} not found when initializing server ${name}`);
                    servers.set(name, server);
                }

                return names.reduce(
                    (prev, name) => {
                        return prev.then(() => {
                            let server = servers.get(name);
                            if (typeof server.init !== 'function')
                                return;

                            let result = server.init(name);
                            if (result === null || typeof result !== 'object' || typeof result.then !== 'function')
                                throw new Error(`Server '${name}' init() did not return a Promise`);
                            return result;
                        });
                    },
                    Promise.resolve()
                );
            });
    }

    /**
     * Start the app
     * @param {...*} names                               Server names
     * @return {Promise}
     */
    start(...names) {
        return super.start(...names)
            .then(() => {
                let config = this.get('config');
                let servers = this.get('servers');

                return names.reduce(
                    (prev, name) => {
                        return prev.then(() => {
                            this._started.add(name);
                            let server = servers.get(name);
                            if (typeof server.start !== 'function')
                                return;

                            let result = server.start(name);
                            if (result === null || typeof result !== 'object' || typeof result.then !== 'function')
                                throw new Error(`Server '${name}' start() did not return a Promise`);
                            return result;
                        });
                    },
                    Promise.resolve()
                );
            })
            .then(() => {
                let config = this.get('config');
                if (config.get('user')) {
                    process.setuid(config.get('user.uid'));
                    process.setgid(config.get('user.gid'));
                }
                this._running = true;
            });
    }

    /**
     * Stop the app
     * @param {...*} names                               Server names
     * @return {Promise}
     */
    stop(...names) {
        return Promise.resolve()
            .then(() => {
                let servers = this.get('servers');
                return names.reverse().reduce(
                    (prev, name) => {
                        return prev.then(() => {
                            let server = servers.get(name);
                            if (typeof server.stop !== 'function')
                                return;

                            let result = server.stop(name);
                            if (result === null || typeof result !== 'object' || typeof result.then !== 'function')
                                throw new Error(`Server '${name}' stop() did not return a Promise`);
                            return result;
                        });
                    },
                    Promise.resolve()
                )
            })
            .then(() => {
                return super.stop(...names);
            })
    }

    /**
     * Handle process signal
     * @param {string} signal                           Signal as SIGNAME
     */
    onSignal(signal) {
        let names = Array.from(this._started);
        this._started.clear();
        this.stop(names)<commit_msg>Check if server exists before using<commit_after>/**
 * Server application
 * @module arpen/app/server
 */
const debug = require('debug')('arpen:app');
const path = require('path');
const App = require('./base');
const WError = require('verror').WError;

/**
 * Server application class
 * @extends module:arpen/app/base~App
 */
class Server extends App {
    /**
     * Create app
     * @param {string} basePath             Base path
     * @param {string[]} argv               Arguments
     */
    constructor(basePath, argv) {
        super(basePath, argv);

        this._started = new Set();
    }

    /**
     * Initialize the app
     * @param {...string} names                         Server names
     * @return {Promise}
     */
    init(...names) {
        return super.init()
            .then(() => {
                return this._initLogger();
            })
            .then(() => {
                let config = this.get('config');
                let servers = new Map();
                this.registerInstance(servers, 'servers');

                for (let name of names) {
                    let params = config.get(`servers.${name}`);
                    if (!params)
                        throw new Error(`Server ${name} not found in config`);

                    let server = this.get(params.class);
                    if (!server)
                        throw new Error(`Service ${params.class} not found when initializing server ${name}`);
                    servers.set(name, server);
                }

                return names.reduce(
                    (prev, name) => {
                        return prev.then(() => {
                            let server = servers.get(name);
                            if (!server || typeof server.init !== 'function')
                                return;

                            let result = server.init(name);
                            if (result === null || typeof result !== 'object' || typeof result.then !== 'function')
                                throw new Error(`Server '${name}' init() did not return a Promise`);
                            return result;
                        });
                    },
                    Promise.resolve()
                );
            });
    }

    /**
     * Start the app
     * @param {...*} names                               Server names
     * @return {Promise}
     */
    start(...names) {
        return super.start(...names)
            .then(() => {
                let config = this.get('config');
                let servers = this.get('servers');

                return names.reduce(
                    (prev, name) => {
                        return prev.then(() => {
                            let server = servers.get(name);
                            if (!server || typeof server.start !== 'function')
                                return;

                            this._started.add(name);
                            let result = server.start(name);
                            if (result === null || typeof result !== 'object' || typeof result.then !== 'function')
                                throw new Error(`Server '${name}' start() did not return a Promise`);
                            return result;
                        });
                    },
                    Promise.resolve()
                );
            })
            .then(() => {
                let config = this.get('config');
                if (config.get('user')) {
                    process.setuid(config.get('user.uid'));
                    process.setgid(config.get('user.gid'));
                }
                this._running = true;
            });
    }

    /**
     * Stop the app
     * @param {...*} names                               Server names
     * @return {Promise}
     */
    stop(...names) {
        return Promise.resolve()
            .then(() => {
                let servers = this.get('servers');
                return names.reverse().reduce(
                    (prev, name) => {
                        return prev.then(() => {
                            let server = servers.get(name);
                            if (!server || typeof server.stop !== 'function')
                                return;

                            let result = server.stop(name);
                            if (result === null || typeof result !== 'object' || typeof result.then !== 'function')
                                throw new Error(`Server '${name}' stop() did not return a Promise`);
                            return result;
                        });
                    },
                    Promise.resolve()
                )
            })
            .then(() => {
                return super.stop(...names);
            })
    }

    /**
     * Handle process signal
     * @param {string} signal                           Signal as SIGNAME
     */
    onSignal(signal) {
        let names = Array.from(this._started);
        this._started.clear();
        this.stop(names)
{'fraction_non_alphanumeric': 0.12674384090234492, 'fraction_numerical': 0.0, 'mean_word_length': 1.0151515151515151, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '64687', 'n_tokens_mistral': 2354, 'n_tokens_neox': 2234, 'n_words': 579}