Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jordigh committed Jun 7, 2024
1 parent c3e23ca commit 1e9049a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/server/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare module "app/server/lib/shutdown" {
export function addCleanupHandler<T>(context: T, method: (this: T) => void, timeout?: number, name?: string): void;
export function removeCleanupHandlers<T>(context: T): void;
export function cleanupOnSignals(...signalNames: string[]): void;
export function exit(optExitCode?: number): Promise<void>;
export function exit(optExitCode?: number, newEnv?: Record<string, string|number>): Promise<void>;
}

// There is a @types/bluebird, but it's not great, and breaks for some of our usages.
Expand Down
25 changes: 24 additions & 1 deletion app/server/lib/FlexServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import {buildWidgetRepository, getWidgetsInPlugins, IWidgetRepository} from 'app
import {setupLocale} from 'app/server/localization';
import axios from 'axios';
import * as cookie from 'cookie';
import EventEmitter from 'events';
import express from 'express';
import * as fse from 'fs-extra';
import * as http from 'http';
Expand Down Expand Up @@ -109,7 +110,7 @@ export interface FlexServerOptions {

const noop: express.RequestHandler = (req, res, next) => next();

export class FlexServer implements GristServer {
export class FlexServer extends EventEmitter implements GristServer {
public readonly create = create;
public tagChecker: TagChecker;
public app: express.Express;
Expand Down Expand Up @@ -186,6 +187,7 @@ export class FlexServer implements GristServer {

constructor(public port: number, public name: string = 'flexServer',
public readonly options: FlexServerOptions = {}) {
super()

Check warning on line 190 in app/server/lib/FlexServer.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.9, 18.x, :lint:python:client:common:smoke:stubs:)

Missing semicolon

Check warning on line 190 in app/server/lib/FlexServer.ts

View workflow job for this annotation

GitHub Actions / build_and_test (:lint:python:client:common:smoke:, 18.x, 3.10)

Missing semicolon

Check warning on line 190 in app/server/lib/FlexServer.ts

View workflow job for this annotation

GitHub Actions / build_and_test (:lint:python:client:common:smoke:, 18.x, 3.11)

Missing semicolon
this.app = express();
this.app.set('port', port);

Expand Down Expand Up @@ -624,6 +626,15 @@ export class FlexServer implements GristServer {
}
counter++;
});

// Cargo-culted from above
this.on('restartWithNewEnv', (newEnv) => {
log.info('Restarting with new environment variables');
if (counter === 0) {
void(shutdown.exit(0, newEnv));
}
counter++;
});
}

public addTagChecker() {
Expand Down Expand Up @@ -1883,6 +1894,10 @@ export class FlexServer implements GristServer {
const probes = new BootProbes(this.app, this, '/api', adminMiddleware);
probes.addEndpoints();

this.app.post('/api/admin/restart', requireInstallAdmin, expressWrap(async (req, resp) => {
return this.restartServer(req, resp);
}));

// Restrict this endpoint to install admins
this.app.get('/api/install/prefs', requireInstallAdmin, expressWrap(async (_req, resp) => {
const activation = await this._activations.current();
Expand Down Expand Up @@ -1939,6 +1954,14 @@ export class FlexServer implements GristServer {
}));
}

public async restartServer(req: express.Request, resp: express.Response) {
const newEnv = req.body.newEnv;
resp.on('finish', () => {
this.emit('restartWithNewEnv', newEnv);
});
return resp.status(200).send();
}

// Get the HTML template sent for document pages.
public async getDocTemplate(): Promise<DocTemplate> {
const page = await fse.readFile(path.join(getAppPathTo(this.appRoot, 'static'),
Expand Down
27 changes: 19 additions & 8 deletions app/server/lib/shutdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
* Module for managing graceful shutdown.
*/

const log = require('app/server/lib/log');
const Promise = require('bluebird');
const child_process = require("child_process");

var log = require('app/server/lib/log');
var Promise = require('bluebird');
const os = require('os');

var os = require('os');
let cleanupHandlers = [];

var cleanupHandlers = [];

var signalsHandled = {};
let signalsHandled = {};

/**
* Adds a handler that should be run on shutdown.
Expand Down Expand Up @@ -97,7 +97,7 @@ function signalExit(signal) {
* signals to the process. This should only be used for signals that normally kill the process.
* E.g. cleanupOnSignals('SIGINT', 'SIGTERM', 'SIGUSR2');
*/
function cleanupOnSignals(varSignalNames) {
function cleanupOnSignals() {
for (var i = 0; i < arguments.length; i++) {
var signal = arguments[i];
if (signalsHandled[signal]) { continue; }
Expand All @@ -110,13 +110,24 @@ exports.cleanupOnSignals = cleanupOnSignals;
/**
* Run cleanup handlers and exit the process with the given exit code (0 if omitted).
*/
function exit(optExitCode) {
function exit(optExitCode, newEnv) {
var prog = 'grist[' + process.pid + ']';
var code = optExitCode || 0;
log.info("Server %s cleaning up", prog);
return runCleanupHandlers()
.finally(function() {
log.info("Server %s exiting with code %s", prog, code);
if (newEnv !== undefined) {
process.on('exit', () => {
log.info("Server restarting with new environment");
child_process.spawn(process.argv.shift(), process.argv, {
cwd: process.cwd(),
detached : true,
stdio: "inherit",
env: {...process.env, ...newEnv}
});
});
}
process.exit(code);
});
}
Expand Down

0 comments on commit 1e9049a

Please sign in to comment.