Skip to content

Commit

Permalink
add logging and logic to resolve taken ports.
Browse files Browse the repository at this point in the history
  • Loading branch information
wildone committed Jul 12, 2024
1 parent 986965d commit 5d7330b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
32 changes: 24 additions & 8 deletions electron/app/main/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,14 +409,14 @@ export class Service extends EventEmitter<ServiceEvent> {
if (this.#serviceport && this.#serviceport > 0) {
this.#getOpenPort().then((port) => {
this.#serviceport = isNaN(port) ? -2 : port
this.#log(`service ${this.#id} resolved port ${this.#serviceport}.`)
this.#logWrite("into",`service ${this.#id} resolved port ${this.#serviceport}.`)
})
}

if (this.#serviceportconsole && this.#serviceportconsole > 0) {
this.#getOpenConsolePort().then((port) => {
this.#serviceportconsole = isNaN(port) ? -2 : port
this.#log(
this.#logWrite("into",
`service ${this.#id} resolved console port ${
this.#serviceportconsole
}.`
Expand All @@ -427,7 +427,7 @@ export class Service extends EventEmitter<ServiceEvent> {
if (this.#serviceportsecondary && this.#serviceportsecondary > 0) {
this.#getOpenSecondaryPort().then((port) => {
this.#serviceportsecondary = isNaN(port) ? -2 : port
this.#log(
this.#logWrite("into",
`service ${this.#id} resolved secondary port ${
this.#serviceportsecondary
}.`
Expand All @@ -438,7 +438,7 @@ export class Service extends EventEmitter<ServiceEvent> {
if (this.#serviceportdebug && this.#serviceportdebug > 0) {
this.#getOpenDebugPort().then((port) => {
this.#serviceportdebug = isNaN(port) ? -2 : port
this.#log(
this.#logWrite("into",
`service ${this.#id} resolved debug port ${this.#serviceportdebug}.`
)
})
Expand Down Expand Up @@ -1766,34 +1766,50 @@ export class Service extends EventEmitter<ServiceEvent> {

// get an open port
async #getOpenPort(): Promise<number> {
this.#logWrite("into",`service ${this.#id} getting open port ${this.#serviceport}`)
const port = await this.#serviceManager.getOpenPort(
this.#serviceport,
this.#servicehost
this.#servicehost,
this.#id,
"port"
)
this.#logWrite("into",`service ${this.#id} got open port ${port}`)
return parseInt(port + "")
}
// get an open port
async #getOpenConsolePort(): Promise<number> {
this.#logWrite("into",`service ${this.#id} getting open console port ${this.#serviceportconsole}`)
const port = await this.#serviceManager.getOpenPort(
this.#serviceportconsole,
this.#servicehost
this.#servicehost,
this.#id,
"console"
)
this.#logWrite("into",`service ${this.#id} got open console port ${port}`)
return parseInt(port + "")
}
// get an open port
async #getOpenSecondaryPort(): Promise<number> {
this.#logWrite("into",`service ${this.#id} getting open secondary port ${this.#serviceportsecondary}`)
const port = await this.#serviceManager.getOpenPort(
this.#serviceportsecondary,
this.#servicehost
this.#servicehost,
this.#id,
"secondary"
)
this.#logWrite("into",`service ${this.#id} got open secondary port ${port}`)
return parseInt(port + "")
}
// get an open port
async #getOpenDebugPort(): Promise<number> {
this.#logWrite("into",`service ${this.#id} getting open debug port ${this.#serviceportdebug}`)
const port = await this.#serviceManager.getOpenPort(
this.#serviceportdebug,
this.#servicehost
this.#servicehost,
this.#id,
"debug"
)
this.#logWrite("into",`service ${this.#id} got open debug port ${port}`)
return parseInt(port + "")
}

Expand Down
69 changes: 66 additions & 3 deletions electron/app/main/ServiceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ interface ServiceManagerEvents {
sendGlobalEnv: (globalenv: { [key: string]: string }) => void
}

export interface ReservedPort {
port: number //what is requested
serviceId: string
type: string
status: string,
requested: number // what was assigned
}
class ServiceManager {
#id = "servicemanager"
#serviceConfigFile = "service.json"
Expand All @@ -38,6 +45,7 @@ class ServiceManager {
#stderrLogFile: string
#globalenv: { [key: string]: string } = {}
#abortController: AbortController
#servicePorts: { [key: string]: ReservedPort } = {}
constructor(
logsDir: string,
logger: Logger,
Expand Down Expand Up @@ -408,8 +416,63 @@ class ServiceManager {
}) as Service
}

async getOpenPort(port = 0, host = "localhost") {
return await getPortFree(port, host)
async #addServicePort(port: any, serviceId: string = "", type: string = "", status: string = "", requested: any) {
const reservedPort: ReservedPort = {
port: port,
serviceId: serviceId,
type: type,
status: status,
requested: requested
}
this.#servicePorts[port + ""] = reservedPort
this.#log(`addServicePort ${JSON.stringify(this.#servicePorts, null, 2)}`)
}

async #updateServicePort(port: any, serviceId: string = "", type: string = "", status: string = "") {
if (this.#servicePorts[port + ""]) {
this.#servicePorts[port + ""].status = status
}
this.#log(`updateServicePort ${JSON.stringify(this.#servicePorts, null, 2)}`)
}

async #isServicePortReserved(port: any) {
//check if #servicePorts has port
return this.#servicePorts[port + ""] ? true : false
}

async #findNotReservedServicePort(port: any) {
if (await this.#isServicePortReserved(port)) {
return await this.#findNotReservedServicePort(port + 1)
}
return port
}

async getOpenPort(port = 0, host = "localhost", serviceId = "", type = "") {
this.#log(`getOpenPort ${port} ${host} ${serviceId} ${type}`)
var freeReservedPort: any = port
if (freeReservedPort > 0) {
if (this.#servicePorts[port + ""] ? true : false) {
//const nextPort = await this.#findNotReservedServicePort(port)
freeReservedPort = freeReservedPort + 1
for (let i = 0; i < 10; i++) {
if (freeReservedPort > 65535) {
freeReservedPort = 0
break
}
if (this.#servicePorts[freeReservedPort + ""] ? true : false) {
freeReservedPort++
} else {
break
}
}
const reservedPort: ReservedPort = this.#servicePorts[port + ""]
this.#log(`port ${port} is reserved by ${reservedPort.serviceId} as ${reservedPort.type} port, trying next port ${freeReservedPort}.`)
}
}
await this.#addServicePort(freeReservedPort, serviceId, type, "checking", port)
freeReservedPort = await getPortFree(freeReservedPort, host)
await this.#updateServicePort(freeReservedPort, serviceId, type, "available")
return freeReservedPort
}

#sortServices(reverse = false) {
Expand Down Expand Up @@ -577,7 +640,7 @@ class ServiceManager {
}
})
}

}

export { type ServiceManagerEvents, ServiceManager }

0 comments on commit 5d7330b

Please sign in to comment.