Created
May 30, 2021 08:07
-
-
Save hoangsetup/f0536c4f82dca9883dcc385c2fb96f49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/pm2Lib.ts b/src/pm2Lib.ts | |
index 4b8cd2f..33e315a 100644 | |
--- a/src/pm2Lib.ts | |
+++ b/src/pm2Lib.ts | |
@@ -1,10 +1,24 @@ | |
import pm2, { Proc, ProcessDescription, StartOptions } from 'pm2'; | |
import { promisify } from 'util'; | |
+import { EventEmitter } from 'events'; | |
+ | |
+export interface IProcessOutLog { | |
+ data: string; | |
+ at: number; | |
+ process: { | |
+ namespace: string; | |
+ rev: string; | |
+ name: string; | |
+ pm_id: number; | |
+ }; | |
+} | |
class Pm2Lib { | |
private readonly SCRIPT_PATH = process.env.SCRIPT_PATH; | |
private readonly MINERS = ['miner01.js', 'miner02.js']; | |
+ private bus: EventEmitter | undefined; | |
+ | |
async getProcesses(): Promise<ProcessDescription[]> { | |
const processes: ProcessDescription[] = []; | |
@@ -39,6 +53,15 @@ class Pm2Lib { | |
return promisify(pm2.stop).call(pm2, filename); | |
} | |
+ async onLogOut(onLog: (logObj: IProcessOutLog) => void) { | |
+ if (!this.bus) { | |
+ this.bus = await promisify<EventEmitter>(pm2.launchBus).call(pm2) | |
+ } | |
+ this.bus.on('log:out', (procLog: IProcessOutLog) => { | |
+ onLog(procLog); | |
+ }); | |
+ } | |
+ | |
private getStartOptions(filename: string): StartOptions { | |
const alias = filename.replace('.js', ''); | |
return { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment