Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: wait for InfluxDB to become available #73

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"license": "Apache-2.0",
"devDependencies": {
"@signalk/server-api": "^2.3.0",
"@signalk/server-api": "^2.5.0",
"@types/chai": "^4.3.3",
"@types/express": "^4.17.17",
"@types/mocha": "^9.1.1",
Expand Down
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
6 changes: 4 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { SKInflux, SKInfluxConfig } from './influx'
import { EventEmitter } from 'stream'
import { getDailyLogData, registerHistoryApiRoute } from './HistoryAPI'
import { IRouter } from 'express'
import { Context, Delta, MetaDelta, Path, PathValue, SourceRef, ValuesDelta } from '@signalk/server-api'
import { Context, Delta, hasValues, MetaDelta, Path, PathValue, SourceRef, ValuesDelta } from '@signalk/server-api'

// eslint-disable-next-line @typescript-eslint/no-var-requires
const packageInfo = require('../package.json')
Expand Down Expand Up @@ -143,14 +143,16 @@ 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()
const isSelf = delta.context === selfContext
delta.updates &&
delta.updates.forEach((update) => {
update.values &&
hasValues(update) &&
update.values.forEach((pathValue) => {
skInfluxes.forEach((skInflux) =>
skInflux.handleValue(
Expand Down
Loading