"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("@pnpm/logger");
const server_1 = require("@pnpm/server");
const store_path_1 = require("@pnpm/store-path");
const delay_1 = require("delay");
const path = require("path");
const processExists = require("process-exists");
const killcb = require("tree-kill");
const util_1 = require("util");
const createStoreController_1 = require("../../createStoreController");
const serverConnectionInfoDir_1 = require("../../serverConnectionInfoDir");
const kill = util_1.promisify(killcb);
exports.default = async (opts) => {
    const store = await store_path_1.default(opts.prefix, opts.store);
    const connectionInfoDir = serverConnectionInfoDir_1.default(store);
    const serverJson = await createStoreController_1.tryLoadServerJson({
        serverJsonPath: path.join(connectionInfoDir, 'server.json'),
        shouldRetryOnNoent: false,
    });
    if (serverJson === null) {
        logger_1.storeLogger.info(`Nothing to stop. No server is running for the store at ${store}`);
        return;
    }
    const storeController = await server_1.connectStoreController(serverJson.connectionOptions);
    await storeController.stop();
    if (await serverGracefullyStops(serverJson.pid)) {
        logger_1.storeLogger.info('Server gracefully stopped');
        return;
    }
    logger_1.storeLogger.warn('Graceful shutdown failed');
    await kill(serverJson.pid, 'SIGINT');
    logger_1.storeLogger.info('Server process terminated');
};
async function serverGracefullyStops(pid) {
    if (!await processExists(pid))
        return true;
    await delay_1.default(5000);
    return !await processExists(pid);
}
