Skip to content

Commit 94eae0c

Browse files
Merge pull request #90 from bluecadet/fix/pm2-no-daemon
Connect to pm2 in `no-daemon-mode`
2 parents 8639c4e + 46865e3 commit 94eae0c

File tree

9 files changed

+133
-70
lines changed

9 files changed

+133
-70
lines changed

.changeset/famous-ears-beam.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@bluecadet/launchpad-monitor": minor
3+
"@bluecadet/launchpad-utils": patch
4+
---
5+
6+
connect to pm2 using `no-daemon-mode`, and route console method calls to the parent logger

packages/content/test/config.js

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,62 +12,69 @@ const config = {
1212
},
1313
"sources": [
1414
{
15-
"id": "spaceships",
15+
"id": "dummyjson",
1616
"type": "json",
1717
"files": {
18-
"spaceships.json": "https://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1&tags=spaceship",
19-
"rockets.json": "https://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1&tags=rocket"
20-
},
21-
"strip": "services/feeds/",
22-
"imageTransforms": [
23-
{"scale": 0.75}
24-
]
25-
},
26-
{
27-
"id": "astronauts",
28-
"files": {
29-
"astronauts.json": "https://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1&tags=astronauts"
30-
},
31-
"strip": "services/feeds/"
32-
},
33-
{
34-
"id": "strapiTest",
35-
"type": "strapi",
36-
"version": 3,
37-
"baseUrl": "http:localhost:1337/",
38-
"queries": ["poems"],
39-
"limit": 100,
40-
"maxNumPages": -1,
41-
"pageNumZeroPad": 0,
42-
},
43-
{
44-
"id": "contentfulTest",
45-
"type": "contentful",
46-
"space": "qc670l0h4zjf",
47-
"locale": "en-US",
48-
"filename": "content.json",
49-
"protocol": "https",
50-
"searchParams": {
51-
"limit": 1000,
52-
"include": 10
53-
},
54-
"contentTypes": [],
55-
"imageParams": {
56-
"f": "face",
57-
"fit": "crop",
58-
"w": 512,
59-
"h": 512
18+
"photos.json": "https://dummyjson.com/products"
6019
}
6120
},
62-
{
63-
"id": "airtableTest",
64-
"type": "airtable",
65-
"baseId": "apppsX7HeYHO7rMjd",
66-
"defaultView": "Grid view",
67-
"tables": ["data"],
68-
"keyValueTables": ["settings"],
69-
"appendLocalAttachmentPaths": true,
70-
}
21+
// {
22+
// "id": "spaceships",
23+
// "type": "json",
24+
// "files": {
25+
// "spaceships.json": "https://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1&tags=spaceship",
26+
// "rockets.json": "https://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1&tags=rocket"
27+
// },
28+
// "strip": "services/feeds/",
29+
// "imageTransforms": [
30+
// {"scale": 0.75}
31+
// ]
32+
// },
33+
// {
34+
// "id": "astronauts",
35+
// "files": {
36+
// "astronauts.json": "https://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1&tags=astronauts"
37+
// },
38+
// "strip": "services/feeds/"
39+
// },
40+
// {
41+
// "id": "strapiTest",
42+
// "type": "strapi",
43+
// "version": 3,
44+
// "baseUrl": "http:localhost:1337/",
45+
// "queries": ["poems"],
46+
// "limit": 100,
47+
// "maxNumPages": -1,
48+
// "pageNumZeroPad": 0,
49+
// },
50+
// {
51+
// "id": "contentfulTest",
52+
// "type": "contentful",
53+
// "space": "qc670l0h4zjf",
54+
// "locale": "en-US",
55+
// "filename": "content.json",
56+
// "protocol": "https",
57+
// "searchParams": {
58+
// "limit": 1000,
59+
// "include": 10
60+
// },
61+
// "contentTypes": [],
62+
// "imageParams": {
63+
// "f": "face",
64+
// "fit": "crop",
65+
// "w": 512,
66+
// "h": 512
67+
// }
68+
// },
69+
// {
70+
// "id": "airtableTest",
71+
// "type": "airtable",
72+
// "baseId": "apppsX7HeYHO7rMjd",
73+
// "defaultView": "Grid view",
74+
// "tables": ["data"],
75+
// "keyValueTables": ["settings"],
76+
// "appendLocalAttachmentPaths": true,
77+
// }
7178
]
7279
};
7380

packages/launchpad/docs/logging.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ See: https://github.com/winstonjs/winston#creating-your-own-logger for all avail
4646
| <a name="module_log-manager.LogOptions+fileOptions">`fileOptions`</a> | <code>LogFileOptions</code>| <code>new LogFileOptions(fileOptions)</code> | Options for individual files and streams. |
4747
| <a name="module_log-manager.LogOptions+level">`level`</a> | <code>string</code>| <code>'info'</code> | The maximum log level to display in all default logs. |
4848
| <a name="module_log-manager.LogOptions+format">`format`</a> | <code>winston.Logform.Format</code>| <code>LogOptions.DEFAULT\_LOG\_FORMAT</code> | The format for how each line is logged. |
49+
| <a name="module_log-manager.LogOptions+overrideConsole">`overrideConsole`</a> | <code>boolean</code>| <code>true</code> | Route all console logs to the log manager. This helps<br>ensure that logs are routed to files and rotated properly.<br><br>This will also freeze the console object, so it can't be<br>modified further during runtime.<br><br>All console logs will be prefixed with `(console)`. |
4950

5051

5152
### LogFileOptions

packages/launchpad/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { LaunchpadCore } from './lib/launchpad-core.js';
44
import { launch as launchContent } from '@bluecadet/launchpad-content';
55
import { launch as launchMonitor, LaunchpadMonitor } from '@bluecadet/launchpad-monitor';
66
import { launch as launchScaffold } from '@bluecadet/launchpad-scaffold';
7-
import { launchFromCli, onExit } from '@bluecadet/launchpad-utils';
7+
import { launchFromCli } from '@bluecadet/launchpad-utils';
88

99
export default LaunchpadCore;
1010

@@ -45,10 +45,6 @@ launchFromCli(import.meta, {
4545
}).then(async config => {
4646
const launchpad = new LaunchpadCore(config);
4747

48-
onExit(async () => {
49-
await launchpad.shutdown();
50-
});
51-
5248
switch (config.startupCommand) {
5349
case StartupCommands.SCAFFOLD: {
5450
await launchScaffold(config);

packages/launchpad/lib/launchpad-core.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import autoBind from 'auto-bind';
66

77
import LaunchpadOptions from './launchpad-options.js';
8-
import { LogManager, Logger } from '@bluecadet/launchpad-utils';
8+
import { LogManager, Logger, onExit } from '@bluecadet/launchpad-utils';
99
import LaunchpadContent from '@bluecadet/launchpad-content';
1010
import LaunchpadMonitor from '@bluecadet/launchpad-monitor';
1111
import CommandCenter, { Command } from './command-center.js';
@@ -55,6 +55,10 @@ export class LaunchpadCore {
5555
this._commands.add(new Command({name: 'update-content', callback: this._runUpdateContent}));
5656

5757
this._commands.addCommandHooks(this._config.hooks);
58+
59+
if (this._config.shutdownOnExit) {
60+
onExit(this.shutdown);
61+
}
5862
}
5963

6064
/**

packages/launchpad/lib/launchpad-options.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@ export class LaunchpadOptions {
1818
commands = new CommandOptions(),
1919
hooks = new CommandHooks(),
2020
logging = new LogOptions(),
21+
shutdownOnExit = true,
2122
...rest
2223
} = {}) {
2324

25+
/**
26+
* Will listen for exit events
27+
* @type {boolean}
28+
* @default true
29+
*/
30+
this.shutdownOnExit = true;
31+
2432
/**
2533
* @type {ContentOptions}
2634
*/

packages/launchpad/test/test.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import LaunchpadCore from '@bluecadet/launchpad';
2-
import { ConfigManager, launchFromCli } from '@bluecadet/launchpad-utils';
2+
import { ConfigManager, launchFromCli, onExit } from '@bluecadet/launchpad-utils';
33

44
const getConfig = async (paths = ['user-config.js', 'config.js']) => {
55
return ConfigManager.importJsConfig(paths, import.meta);
@@ -17,17 +17,21 @@ launchFromCli(import.meta, {
1717

1818
await launchpad.startup();
1919

20-
const appNames = launchpad._monitor.getAllAppNames();
21-
await Promise.all([...appNames, 'fake-app'].map(async (appName) => {
22-
const isRunning = await launchpad._monitor.isRunning(appName);
23-
console.debug(`App '${appName}' is running: ${isRunning}`);
24-
}));
20+
// const appNames = launchpad._monitor.getAllAppNames();
21+
// await Promise.all([...appNames, 'fake-app'].map(async (appName) => {
22+
// const isRunning = await launchpad._monitor.isRunning(appName);
23+
// console.debug(`App '${appName}' is running: ${isRunning}`);
24+
// }));
2525

26-
await wait(10);
27-
await launchpad.updateContent();
26+
// onExit(async () => {
27+
// await launchpad.shutdown();
28+
// });
2829

29-
await wait(10);
30-
await launchpad.shutdown();
30+
// await wait(10);
31+
// await launchpad.updateContent();
32+
33+
// await wait(10);
34+
// await launchpad.shutdown();
3135

3236
}).catch(err => {
3337
if (err) {

packages/monitor/lib/launchpad-monitor.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ export class LaunchpadMonitor {
121121
}
122122

123123
this._logger.info('Connecting to PM2');
124-
await this._promisify(pm2.connect, pm2);
125-
124+
await this._promisify(pm2.connect, pm2, true);
126125
await this._connectPm2Bus();
127126
}
128127

packages/utils/lib/log-manager.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export class LogOptions {
3838
fileOptions = new LogFileOptions(),
3939
level = 'info',
4040
format = LogOptions.DEFAULT_LOG_FORMAT,
41+
overrideConsole = true,
4142
...rest
4243
} = {}) {
4344
/**
@@ -68,6 +69,20 @@ export class LogOptions {
6869
*/
6970
this.format = format;
7071

72+
/**
73+
* Route all console logs to the log manager. This helps
74+
* ensure that logs are routed to files and rotated properly.
75+
*
76+
* This will also freeze the console object, so it can't be
77+
* modified further during runtime.
78+
*
79+
* All console logs will be prefixed with `(console)`.
80+
*
81+
* @type {boolean}
82+
* @default true
83+
*/
84+
this.overrideConsole = true;
85+
7186
Object.assign(this, rest);
7287
}
7388
}
@@ -180,6 +195,10 @@ class LogManager {
180195
new winston.transports.DailyRotateFile({ ...this._config.fileOptions, filename: this.getFilePath('launchpad-error', false), level: 'error'}),
181196
],
182197
});
198+
199+
if (this._config.overrideConsole) {
200+
this.overrideConsoleMethods();
201+
}
183202
}
184203

185204
/**
@@ -209,6 +228,25 @@ class LogManager {
209228
}
210229
return output;
211230
}
231+
232+
233+
/**
234+
* Overrides console methods to use the parent logger instead
235+
* @private
236+
*/
237+
overrideConsoleMethods() {
238+
// Override console methods
239+
const logger = this.getLogger('console');
240+
console.log = logger.info.bind(logger);
241+
console.info = logger.info.bind(logger);
242+
console.warn = logger.warn.bind(logger);
243+
console.error = logger.error.bind(logger);
244+
console.debug = logger.debug.bind(logger);
245+
246+
// PM2 will try to override the console methods with it's own logger
247+
// so we're freezing console here to prevent that from happening
248+
Object.freeze(console);
249+
}
212250

213251
}
214252

0 commit comments

Comments
 (0)