Skip to content

Commit

Permalink
fix(shared): jolokia instance is created every time getJolokia() is i…
Browse files Browse the repository at this point in the history
…nvoked

Basically, lazy-init methods that return Promise should not use async/await as otherwise we would be
screwed up with subtle concurrency issues.
  • Loading branch information
tadayosi committed Oct 18, 2023
1 parent ebad82b commit 406b4e5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/hawtio/src/plugins/rbac/rbac-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RBACService implements IRBACService {
this.aclMBean = undefined
}

async getACLMBean(): Promise<string> {
getACLMBean(): Promise<string> {
if (this.aclMBean) {
return this.aclMBean
}
Expand Down
40 changes: 22 additions & 18 deletions packages/hawtio/src/plugins/shared/jolokia-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,28 +127,28 @@ class JolokiaService implements IJolokiaService {
return this.jolokiaUrl
}

async getJolokia(): Promise<Jolokia> {
getJolokia(): Promise<Jolokia> {
if (this.jolokia) {
return this.jolokia
}

// Initialising Jolokia instance

const jolokia = await this.createJolokia()
// Checking versions
jolokia.version(
onVersionSuccessAndError(
version => {
log.info('Jolokia version:', { client: jolokia.CLIENT_VERSION, agent: version.agent })
},
error => log.error('Failed to fetch Jolokia version:', error),
),
)
// Start Jolokia
const updateRate = this.loadUpdateRate()
jolokia.start(updateRate)
log.info('Jolokia started with update rate =', updateRate)
return jolokia
this.jolokia = this.createJolokia(jolokia => {
// Checking versions
jolokia.version(
onVersionSuccessAndError(
version => {
log.info('Jolokia version:', { client: jolokia.CLIENT_VERSION, agent: version.agent })
},
error => log.error('Failed to fetch Jolokia version:', error),
),
)
// Start Jolokia
const updateRate = this.loadUpdateRate()
jolokia.start(updateRate)
log.info('Jolokia started with update rate =', updateRate)
})
return this.jolokia
}

private async initJolokiaUrl(): Promise<string | null> {
Expand Down Expand Up @@ -212,7 +212,7 @@ class JolokiaService implements IJolokiaService {
})
}

private async createJolokia(): Promise<Jolokia> {
private async createJolokia(postCreate?: (jolokia: Jolokia) => void): Promise<Jolokia> {
const jolokiaUrl = await this.getJolokiaUrl()
if (!jolokiaUrl) {
log.debug('Use dummy Jolokia')
Expand All @@ -237,6 +237,10 @@ class JolokiaService implements IJolokiaService {
// let's check if we can call faster jolokia.list()
await this.checkListOptimisation(jolokia)

// Run any post-create processing that should be done before the resolved
// Jolokia is returned
postCreate?.(jolokia)

return jolokia
}

Expand Down
2 changes: 1 addition & 1 deletion packages/hawtio/src/plugins/shared/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Workspace implements IWorkspace {
eventService.refresh()
}

async getTree(): Promise<MBeanTree> {
getTree(): Promise<MBeanTree> {
if (this.tree) {
return this.tree
}
Expand Down

0 comments on commit 406b4e5

Please sign in to comment.