Skip to content

Commit

Permalink
feature: wait InfluxDB to become available
Browse files Browse the repository at this point in the history
On server/plugin start wait for Influx connection to become
available.

Previously plugin startup would fail if InfluxDB is not available
when plugin starts and never retry. If SK server would start more
quickly than Influx on server bootup the plugin would never start
writing data.

Fixes #52.
  • Loading branch information
tkurki committed Nov 22, 2024
1 parent 95663e4 commit 34bdc0a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/influx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,28 @@ export class SKInflux {
})
}

async init() {
const bucketId = await ensureBucketExists(this.influx, this.org, this.bucket)
try {
await this.ensureV1MappingExists(bucketId)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
this.logging.error('Could not verify or create v1 database mapping, history api will not work')
this.logging.error(err)
}
init() {
let retryCount = 0
return new Promise((resolve) => {
const retryInit = () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this._init(resolve).catch((e: any) => {
this.logging.debug(e.message)
this.logging.debug(`${e.message} retry ${retryCount++}`)
setTimeout(() => {
retryInit()
}, 10 * 1000)
})
}
retryInit()
})
}

async _init(onSuccess: (v: unknown) => void) {
const bucketId = await ensureBucketExists(this.influx, this.org, this.bucket)
await this.ensureV1MappingExists(bucketId)
onSuccess(undefined)
}
async ensureV1MappingExists(bucketId: string | undefined) {
if (!bucketId) {
throw new Error('No bucketid')
Expand Down
2 changes: 2 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ export default function InfluxPluginFactory(app: App): Plugin & InfluxPlugin {
onStop.push(() => clearInterval(interval))
}

app.setPluginStatus(`Connecting ${skInfluxes.map((sk) => sk.url)}`)
return Promise.all(skInfluxes.map((skInflux) => skInflux.init())).then(() => {
app.setPluginStatus('Connected')
const onDelta = (_delta: Delta) => {
const delta = _delta as ValuesDelta
const now = Date.now()
Expand Down

0 comments on commit 34bdc0a

Please sign in to comment.