Skip to content

Commit

Permalink
proper shutdowns for hub and service
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed Sep 29, 2024
1 parent 3012d8f commit 85dc6b4
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 26 deletions.
4 changes: 2 additions & 2 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestor-client",
"version": "0.2.0",
"version": "0.2.1",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
Expand Down
8 changes: 4 additions & 4 deletions client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export interface ToolboxResponse {

export class NestorClient {

logger?: Logger|null
format: ToolsFormat
browser?: Bonjour.Browser
hubs: Hub[] = []
private logger?: Logger|null
private format: ToolsFormat
private browser?: Bonjour.Browser
private hubs: Hub[] = []

constructor(opts?: NestorClientOptions) {

Expand Down
4 changes: 2 additions & 2 deletions hub/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion hub/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestor-hub",
"version": "0.2.0",
"version": "0.2.1",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
Expand Down
33 changes: 24 additions & 9 deletions hub/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,31 @@ app.use(function(req, res) {
res.status(404).send({url: `"${req.originalUrl}" not found`})
})


// shutdown
let advertise: Bonjour.Service|null = null
process.on('SIGINT', async () => {

// log
console.log('Shutting down Nestor hub')

// first stop advertising
const promise = new Promise<void>((resolve) => {
advertise?.stop(() => resolve())
})
await promise

// done
process.exit(0)

})

// publish
const publish = (baseName: string, port: number, index: number) => {

const name = index == 0 ? baseName : `${baseName} (${index})`

const ad = Bonjour().publish({
advertise = Bonjour().publish({
name: name,
type: 'nestor',
subtypes: [ 'hub' ],
Expand All @@ -88,31 +108,26 @@ const publish = (baseName: string, port: number, index: number) => {
}
})

ad.on('error', (error) => {
advertise.on('error', (error) => {
if (error.message === 'Service name is already in use on the network') {
publish(baseName, port, index + 1)
} else {
throw error
}
})

ad.on('up', () => {
advertise.on('up', () => {
console.log(`Hub published as "${name}" on network`)
})

ad.start()
advertise.start()

}

// help
export const startHub = (name: string, port: number) => {

app.listen(port, () => {

console.log(`Nestor Hub is listening at http://localhost:${port}`)

publish(name, port, 0)

})

}
1 change: 1 addition & 0 deletions hub/src/services/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class ServiceDirectory {
}

async add(name: string, host: string, port: number, path: string): Promise<void> {
if (!path) return
let service = this.services.find(s => s.name === name)
if (service) {
service.host = host
Expand Down
4 changes: 2 additions & 2 deletions service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestor-service",
"version": "0.2.0",
"version": "0.2.1",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
Expand Down
42 changes: 38 additions & 4 deletions service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ export interface NestorServiceOptions {
autostart?: boolean
}

interface Registration {
host: string
port: number
}

export class NestorService {

name: string
port: number
path: string
advertise?: Bonjour.Service
private name: string
private port: number
private path: string
private registrations: Registration[]
private advertise?: Bonjour.Service

constructor(name: string, port: number, path: string, opts?: NestorServiceOptions) {

// init
this.registrations = []

// save
this.name = name
this.port = port
Expand All @@ -23,6 +32,13 @@ export class NestorService {
if (opts?.autostart !== false) {
this.start()
}

// proper shutdown
process.on('SIGINT', async () => {
await this.shutdown()
process.exit(0)
})

}

start(): void {
Expand All @@ -39,6 +55,7 @@ export class NestorService {
}
})
this.advertise.start()

}

stop(): void {
Expand All @@ -51,6 +68,7 @@ export class NestorService {
const body = { name: this.name, port: this.port, path: this.path }
try {
await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) })
this.registrations.push({ host, port })
} catch (err) {
console.error(`Error while registering service at ${url}`, err)
throw err
Expand All @@ -64,11 +82,27 @@ export class NestorService {
const body = { name: this.name }
try {
await fetch(url, { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) })
this.registrations = this.registrations.filter(r => r.host !== host || r.port !== port)
} catch (err) {
console.error(`Error while unregistering service at ${url}`, err)
throw err
}

}

async shutdown() {

// log
console.log('Shutting down Nestor service')

// first stop advertising
const promise = new Promise<void>((resolve) => {
this.advertise?.stop(() => resolve())
})
await promise

// then unregister
this.registrations.forEach(async r => await this.unregister(r.host, r.port))
}

}

0 comments on commit 85dc6b4

Please sign in to comment.