-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
142 additions
and
140 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,125 @@ | ||
import { Transport, TransportConfig } from '../services/transport' | ||
import Debug from 'debug' | ||
import { Action } from '../services/actions' | ||
import { InternalMetric } from '../services/metrics' | ||
import { EventEmitter2 } from 'eventemitter2' | ||
// import { Transport, TransportConfig } from '../services/transport' | ||
// import Debug from 'debug' | ||
// import { Action } from '../services/actions' | ||
// import { InternalMetric } from '../services/metrics' | ||
// import { EventEmitter2 } from 'eventemitter2' | ||
|
||
class SerializedAction { | ||
action_name: string // tslint:disable-line | ||
action_type: string // tslint:disable-line | ||
opts: Object | null | undefined | ||
arity: number | ||
} | ||
// class SerializedAction { | ||
// action_name: string // tslint:disable-line | ||
// action_type: string // tslint:disable-line | ||
// opts: Object | null | undefined | ||
// arity: number | ||
// } | ||
|
||
export class ProcessMetadata { | ||
axm_actions: SerializedAction[] // tslint:disable-line | ||
axm_monitor: Object // tslint:disable-line | ||
axm_options: Object // tslint:disable-line | ||
axm_dynamic?: Object // tslint:disable-line | ||
interpreter?: string | ||
versionning?: Object | ||
} | ||
// export class ProcessMetadata { | ||
// axm_actions: SerializedAction[] // tslint:disable-line | ||
// axm_monitor: Object // tslint:disable-line | ||
// axm_options: Object // tslint:disable-line | ||
// axm_dynamic?: Object // tslint:disable-line | ||
// interpreter?: string | ||
// versionning?: Object | ||
// } | ||
|
||
export class WebsocketTransport extends EventEmitter2 implements Transport { | ||
// export class WebsocketTransport extends EventEmitter2 implements Transport { | ||
|
||
private config: TransportConfig | ||
private agent: any | ||
private process: ProcessMetadata | ||
private initiated: Boolean = false // tslint:disable-line | ||
private logger: Function = Debug('axm:transport:websocket') | ||
// private config: TransportConfig | ||
// private agent: any | ||
// private process: ProcessMetadata | ||
// private initiated: Boolean = false // tslint:disable-line | ||
// private logger: Function = Debug('axm:transport:websocket') | ||
|
||
init (config: TransportConfig): Transport { | ||
if (this.initiated === true) { | ||
console.error(`Trying to re-init the transport, please avoid`) | ||
return this | ||
} | ||
this.initiated = true | ||
const AgentNode = require('@pm2/agent-node') | ||
this.logger('Init new transport service') | ||
this.config = config | ||
this.process = { | ||
axm_actions: [], | ||
axm_options: {}, | ||
axm_monitor: {} | ||
} | ||
this.agent = new AgentNode(this.config, this.process) | ||
if (this.agent instanceof Error) { | ||
throw this.agent | ||
} | ||
this.agent.sendLogs = config.sendLogs || false | ||
this.agent.start() | ||
this.agent.transport.on('**', (data) => { | ||
this.logger(`Received reverse message from websocket transport`) | ||
this.emit('data', data) | ||
}) | ||
this.logger('Agent launched') | ||
return this | ||
} | ||
// init (config: TransportConfig): Transport { | ||
// if (this.initiated === true) { | ||
// console.error(`Trying to re-init the transport, please avoid`) | ||
// return this | ||
// } | ||
// this.initiated = true | ||
// const AgentNode = require('@pm2/agent-node') | ||
// this.logger('Init new transport service') | ||
// this.config = config | ||
// this.process = { | ||
// axm_actions: [], | ||
// axm_options: {}, | ||
// axm_monitor: {} | ||
// } | ||
// this.agent = new AgentNode(this.config, this.process) | ||
// if (this.agent instanceof Error) { | ||
// throw this.agent | ||
// } | ||
// this.agent.sendLogs = config.sendLogs || false | ||
// this.agent.start() | ||
// this.agent.transport.on('**', (data) => { | ||
// this.logger(`Received reverse message from websocket transport`) | ||
// this.emit('data', data) | ||
// }) | ||
// this.logger('Agent launched') | ||
// return this | ||
// } | ||
|
||
setMetrics (metrics: InternalMetric[]) { | ||
return this.process.axm_monitor = metrics.reduce((object, metric: InternalMetric) => { | ||
if (typeof metric.name !== 'string') return object | ||
object[metric.name] = { | ||
unit: metric.unit, | ||
type: metric.id, | ||
value: metric.value | ||
} | ||
if (metric.historic == false) | ||
object[metric.name] = false | ||
return object | ||
}, {}) | ||
} | ||
// setMetrics (metrics: InternalMetric[]) { | ||
// return this.process.axm_monitor = metrics.reduce((object, metric: InternalMetric) => { | ||
// if (typeof metric.name !== 'string') return object | ||
// object[metric.name] = { | ||
// unit: metric.unit, | ||
// type: metric.id, | ||
// value: metric.value | ||
// } | ||
// if (metric.historic == false) | ||
// object[metric.name] = false | ||
// return object | ||
// }, {}) | ||
// } | ||
|
||
addAction (action: Action) { | ||
this.logger(`Add action: ${action.name}:${action.type}`) | ||
const serializedAction: SerializedAction = { | ||
action_name: action.name, | ||
action_type: action.type, | ||
arity: action.arity, | ||
opts: action.opts | ||
} | ||
this.process.axm_actions.push(serializedAction) | ||
} | ||
// addAction (action: Action) { | ||
// this.logger(`Add action: ${action.name}:${action.type}`) | ||
// const serializedAction: SerializedAction = { | ||
// action_name: action.name, | ||
// action_type: action.type, | ||
// arity: action.arity, | ||
// opts: action.opts | ||
// } | ||
// this.process.axm_actions.push(serializedAction) | ||
// } | ||
|
||
setOptions (options) { | ||
this.logger(`Set options: [${Object.keys(options).join(',')}]`) | ||
return this.process.axm_options = Object.assign(this.process.axm_options, options) | ||
} | ||
// setOptions (options) { | ||
// this.logger(`Set options: [${Object.keys(options).join(',')}]`) | ||
// return this.process.axm_options = Object.assign(this.process.axm_options, options) | ||
// } | ||
|
||
private getFormattedPayload (channel: string, payload: any) { | ||
// Reformat for backend | ||
switch (channel) { | ||
case 'axm:reply': | ||
return { data: payload } | ||
case 'process:exception': | ||
return { data: payload } | ||
case 'human:event': { | ||
const name = payload.__name | ||
payload.__name = undefined | ||
return { name, data: payload } | ||
} | ||
} | ||
return payload | ||
} | ||
// private getFormattedPayload (channel: string, payload: any) { | ||
// // Reformat for backend | ||
// switch (channel) { | ||
// case 'axm:reply': | ||
// return { data: payload } | ||
// case 'process:exception': | ||
// return { data: payload } | ||
// case 'human:event': { | ||
// const name = payload.__name | ||
// payload.__name = undefined | ||
// return { name, data: payload } | ||
// } | ||
// } | ||
// return payload | ||
// } | ||
|
||
send (channel: string, payload: Object) { | ||
return this.agent.send(channel, this.getFormattedPayload(channel, payload)) ? 0 : -1 | ||
} | ||
// send (channel: string, payload: Object) { | ||
// return this.agent.send(channel, this.getFormattedPayload(channel, payload)) ? 0 : -1 | ||
// } | ||
|
||
destroy () { | ||
this.agent.transport.disconnect() | ||
this.logger('destroy') | ||
} | ||
// destroy () { | ||
// this.agent.transport.disconnect() | ||
// this.logger('destroy') | ||
// } | ||
|
||
removeListener () { | ||
return this.agent.transport.removeListener.apply(this, arguments) | ||
} | ||
// removeListener () { | ||
// return this.agent.transport.removeListener.apply(this, arguments) | ||
// } | ||
|
||
removeAllListeners () { | ||
return this.agent.transport.removeAllListeners.apply(this, arguments) | ||
} | ||
// removeAllListeners () { | ||
// return this.agent.transport.removeAllListeners.apply(this, arguments) | ||
// } | ||
|
||
on () { | ||
return this.agent.transport.on.apply(this, arguments) | ||
} | ||
} | ||
// on () { | ||
// return this.agent.transport.on.apply(this, arguments) | ||
// } | ||
// } |
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