Skip to content

Commit

Permalink
TrRoutingProcessManager: Add log files and debug configuration options
Browse files Browse the repository at this point in the history
This adds the following configuration for the trRouting processes, this
needs to be added to the `config.js` file used in the project.

```
trRouting: {
    batch: {
        debug: true,
        logFiles: { maxFileSizeKb: 1000, nbLogFiles: 1 }
    },
    route: {
        debug: true,
        logFiles: { maxFileSizeKb: 2000, nbLogFiles: 3 }
    }
}
```

This will respectively affect the process used for the batch routing
tasks and the process used in the main instance. This configuration is
optional.
  • Loading branch information
tahini committed Sep 25, 2024
1 parent 7613eed commit 406414f
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 8 deletions.
13 changes: 13 additions & 0 deletions examples/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ module.exports = {

// Enable caching of connections for all scenarios in trRouting. Will use more memory
trRoutingCacheAllScenarios: false,
// Extra options for running trRouting
//trRouting: {
// // Options for batch process
// batch: {
// debug: false,
// logFiles: { maxFileSizeKb: 3000, nbLogFiles: 3 }
// },
// // Options for the instance global calculator
// route: {
// debug: false,
// logFiles: { maxFileSizeKb: 1000, nbLogFiles: 1 }
// }
//},

mapDefaultCenter: {
lat: 45.5092960,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const startTrRoutingProcess = async (
attemptRestart = false,
threadCount = 1,
{
debug = false,
debug = undefined,
cacheDirectoryPath,
// Flag to enable the trRouting connection cache for all scenario
cacheAllScenarios = false,
Expand Down Expand Up @@ -101,11 +101,17 @@ const start = async (parameters: StartParameters) => {
const cacheAllScenarios =
config.trRoutingCacheAllScenarios === undefined ? false : config.trRoutingCacheAllScenarios;

// Get trRouting process configuration
const trRoutingConfig = config.trRouting?.route;
const debugFromParamOrConfig = parameters.debug !== undefined ? parameters.debug : trRoutingConfig?.debug || false;
const logFilesFromParamOrConfig =
parameters.logFiles !== undefined ? parameters.logFiles : trRoutingConfig?.logFiles;

const params: Parameters<typeof startTrRoutingProcess>[3] = {
debug: parameters.debug,
debug: debugFromParamOrConfig,
cacheDirectoryPath: parameters.cacheDirectoryPath,
cacheAllScenarios: cacheAllScenarios,
logFiles: parameters.logFiles
logFiles: logFilesFromParamOrConfig
};

// TODO Check why we need this await, should not be useful before returning
Expand All @@ -130,11 +136,17 @@ const restart = async (
const cacheAllScenarios =
config.trRoutingCacheAllScenarios === undefined ? false : config.trRoutingCacheAllScenarios;

// Get trRouting process configuration
const trRoutingConfig = config.trRouting?.route;
const debugFromParamOrConfig = parameters.debug !== undefined ? parameters.debug : trRoutingConfig?.debug || false;
const logFilesFromParamOrConfig =
parameters.logFiles !== undefined ? parameters.logFiles : trRoutingConfig?.logFiles;

const params: Parameters<typeof startTrRoutingProcess>[3] = {
debug: parameters.debug,
debug: debugFromParamOrConfig,
cacheDirectoryPath: parameters.cacheDirectoryPath,
cacheAllScenarios: cacheAllScenarios,
logFiles: parameters.logFiles
logFiles: logFilesFromParamOrConfig
};

if (parameters.doNotStartIfStopped && !(await ProcessManager.isServiceRunning(serviceName))) {
Expand Down Expand Up @@ -175,7 +187,7 @@ const startBatch = async function (
{
port = Preferences.get('trRouting.batchPortStart', 14000),
cacheDirectoryPath = undefined,
debug = false,
debug = undefined,
logFiles = undefined
}: StartParameters = {}
) {
Expand All @@ -189,7 +201,16 @@ const startBatch = async function (
numberOfCpus = maxThreadCount;
}

const params = { cacheDirectoryPath: cacheDirectoryPath, debug, logFiles };
// Get trRouting process configuration
const batchTrRoutingConfig = config.trRouting?.batch;
const debugFromParamOrConfig = debug !== undefined ? debug : batchTrRoutingConfig?.debug || false;
const logFilesFromParamOrConfig = logFiles !== undefined ? logFiles : batchTrRoutingConfig?.logFiles;

const params = {
cacheDirectoryPath: cacheDirectoryPath,
debug: debugFromParamOrConfig,
logFiles: logFilesFromParamOrConfig
};

await startTrRoutingProcess(port, false, numberOfCpus, params);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ describe('TrRouting Process Manager: start', () => {
beforeEach(function () {
// Reset TR_ROUTING_PATH environment variable
delete process.env.TR_ROUTING_PATH;
// Reset trRoutin in the configuration
delete config.trRouting;
});

test('start process from default path', async () => {
Expand Down Expand Up @@ -149,13 +151,75 @@ describe('TrRouting Process Manager: start', () => {
logFiles
});
});
test('With debug and logFiles in the config', async () => {
const logFiles = { maxFileSizeKb: 1500, nbLogFiles: 5 };
config.trRouting = {
route: {
debug: true,
logFiles
}
}
const status = await TrRoutingProcessManager.start({});
expect(status).toEqual({
status: 'started',
action: 'start',
service: 'trRouting',
name: 'trRouting4000'
});
expect(startProcessMock).toHaveBeenCalledTimes(1);
expect(startProcessMock).toHaveBeenCalledWith({
serviceName: 'trRouting4000',
tagName: 'trRouting',
command: 'trRouting',
commandArgs: ['--port=4000', `--osrmPort=${walkingOsrmMode.getHostPort().port}`, `--osrmHost=${walkingOsrmMode.getHostPort().host}`, '--debug=1', `--cachePath=${directoryManager.projectDirectory}/cache/test`, '--threads=2'],
waitString: 'ready.',
useShell: false,
cwd: undefined,
attemptRestart: false,
logFiles
});
});
test('With debug and logFiles in the config, both in params and config', async () => {
// Values in parameters should override the ones in the configuration
const logFilesInConfig = { maxFileSizeKb: 1500, nbLogFiles: 5 };
const logFilesInParams = { maxFileSizeKb: 2000, nbLogFiles: 3 };
const debugInConfig = true;
const debugInParams = false;
config.trRouting = {
route: {
debug: debugInConfig,
logFiles: logFilesInConfig
}
}
const status = await TrRoutingProcessManager.start({ logFiles: logFilesInParams, debug: debugInParams });
expect(status).toEqual({
status: 'started',
action: 'start',
service: 'trRouting',
name: 'trRouting4000'
});
expect(startProcessMock).toHaveBeenCalledTimes(1);
expect(startProcessMock).toHaveBeenCalledWith({
serviceName: 'trRouting4000',
tagName: 'trRouting',
command: 'trRouting',
commandArgs: ['--port=4000', `--osrmPort=${walkingOsrmMode.getHostPort().port}`, `--osrmHost=${walkingOsrmMode.getHostPort().host}`, '--debug=0', `--cachePath=${directoryManager.projectDirectory}/cache/test`, '--threads=2'],
waitString: 'ready.',
useShell: false,
cwd: undefined,
attemptRestart: false,
logFiles: logFilesInParams
});
});
});

describe('TrRouting Process Manager: restart', () => {

beforeEach(function () {
// Reset TR_ROUTING_PATH environment variable
delete process.env.TR_ROUTING_PATH;
// Reset trRoutin in the configuration
delete config.trRouting;
});

test('from default path', async () => {
Expand Down Expand Up @@ -302,13 +366,75 @@ describe('TrRouting Process Manager: restart', () => {
attemptRestart: true
});
});
test('With debug and logFiles in the config', async () => {
const logFiles = { maxFileSizeKb: 1500, nbLogFiles: 5 };
config.trRouting = {
route: {
debug: true,
logFiles
}
}
const status = await TrRoutingProcessManager.restart({});
expect(status).toEqual({
status: 'started',
action: 'start',
service: 'trRouting',
name: 'trRouting4000'
});
expect(startProcessMock).toHaveBeenCalledTimes(1);
expect(startProcessMock).toHaveBeenCalledWith({
serviceName: 'trRouting4000',
tagName: 'trRouting',
command: 'trRouting',
commandArgs: ['--port=4000', `--osrmPort=${walkingOsrmMode.getHostPort().port}`, `--osrmHost=${walkingOsrmMode.getHostPort().host}`, '--debug=1', `--cachePath=${directoryManager.projectDirectory}/cache/test`, '--threads=2'],
waitString: 'ready.',
useShell: false,
cwd: undefined,
attemptRestart: true,
logFiles
});
});
test('With debug and logFiles in the config, both in params and config', async () => {
// Values in parameters should override the ones in the configuration
const logFilesInConfig = { maxFileSizeKb: 1500, nbLogFiles: 5 };
const logFilesInParams = { maxFileSizeKb: 2000, nbLogFiles: 3 };
const debugInConfig = true;
const debugInParams = false;
config.trRouting = {
route: {
debug: debugInConfig,
logFiles: logFilesInConfig
}
}
const status = await TrRoutingProcessManager.restart({ logFiles: logFilesInParams, debug: debugInParams });
expect(status).toEqual({
status: 'started',
action: 'start',
service: 'trRouting',
name: 'trRouting4000'
});
expect(startProcessMock).toHaveBeenCalledTimes(1);
expect(startProcessMock).toHaveBeenCalledWith({
serviceName: 'trRouting4000',
tagName: 'trRouting',
command: 'trRouting',
commandArgs: ['--port=4000', `--osrmPort=${walkingOsrmMode.getHostPort().port}`, `--osrmHost=${walkingOsrmMode.getHostPort().host}`, '--debug=0', `--cachePath=${directoryManager.projectDirectory}/cache/test`, '--threads=2'],
waitString: 'ready.',
useShell: false,
cwd: undefined,
attemptRestart: true,
logFiles: logFilesInParams
});
});
});

describe('TrRouting Process Manager: startBatch', () => {

beforeEach(function () {
// Reset TR_ROUTING_PATH environment variable
delete process.env.TR_ROUTING_PATH;
// Reset trRoutin in the configuration
delete config.trRouting;
});

test('start batch process with 1 cpu', async () => {
Expand Down Expand Up @@ -415,5 +541,63 @@ describe('TrRouting Process Manager: startBatch', () => {
logFiles
});
});
test('With debug and logFiles in the config', async () => {
const logFiles = { maxFileSizeKb: 1500, nbLogFiles: 5 };
config.trRouting = {
batch: {
debug: true,
logFiles
}
}
const status = await TrRoutingProcessManager.startBatch(4);
expect(status).toEqual({
status: 'started',
service: 'trRoutingBatch',
port: 14000
});
expect(startProcessMock).toHaveBeenCalledTimes(1);
expect(startProcessMock).toHaveBeenCalledWith({
serviceName: 'trRouting14000',
tagName: 'trRouting',
command: 'trRouting',
commandArgs: ['--port=14000', `--osrmPort=${walkingOsrmMode.getHostPort().port}`, `--osrmHost=${walkingOsrmMode.getHostPort().host}`, '--debug=1', `--cachePath=${directoryManager.projectDirectory}/cache/test`, '--threads=4'],
waitString: 'ready.',
useShell: false,
cwd: undefined,
attemptRestart: false,
logFiles
});
});
test('With debug and logFiles in the config, both in params and config', async () => {
// Values in parameters should override the ones in the configuration
const logFilesInConfig = { maxFileSizeKb: 1500, nbLogFiles: 5 };
const logFilesInParams = { maxFileSizeKb: 2000, nbLogFiles: 3 };
const debugInConfig = true;
const debugInParams = false;
config.trRouting = {
batch: {
debug: debugInConfig,
logFiles: logFilesInConfig
}
}
const status = await TrRoutingProcessManager.startBatch(4, { logFiles: logFilesInParams, debug: debugInParams });
expect(status).toEqual({
status: 'started',
service: 'trRoutingBatch',
port: 14000
});
expect(startProcessMock).toHaveBeenCalledTimes(1);
expect(startProcessMock).toHaveBeenCalledWith({
serviceName: 'trRouting14000',
tagName: 'trRouting',
command: 'trRouting',
commandArgs: ['--port=14000', `--osrmPort=${walkingOsrmMode.getHostPort().port}`, `--osrmHost=${walkingOsrmMode.getHostPort().host}`, '--debug=0', `--cachePath=${directoryManager.projectDirectory}/cache/test`, '--threads=4'],
waitString: 'ready.',
useShell: false,
cwd: undefined,
attemptRestart: false,
logFiles: logFilesInParams
});
});
});

5 changes: 4 additions & 1 deletion packages/transition-frontend/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ if (!process.env.NODE_ENV) {
}

const configuration = require('chaire-lib-backend/lib/config/server.config');
const config = configuration.default ? configuration.default : configuration;
// Extract from the config all options that we should not send to the frontend.
// The `{ ...config }` will be sent to the frontend
// TODO This won't be necessary once we have frontend and backend configuration separated
const { trRoutingCacheAllScenarios, trRouting, ...config } = configuration.default ? configuration.default : configuration;

// Public directory from which files are served
const publicDirectory = path.join(__dirname, '..', '..', 'public');
Expand Down

0 comments on commit 406414f

Please sign in to comment.