mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 12:11:56 +00:00
feat: add DbWatchedCallbacks abstraction, TypedDbWatch-based callbacks, and SDK watchable wrappers
- Extract DbWatchedCallbacks<K> abstraction in callbacks.rs using SyncMutex for the repeated patchdb subscribe-wait-fire-remove callback pattern - Move get_host_info and get_status callbacks to use TypedDbWatch instead of raw db.subscribe, eliminating race conditions between reading and watching - Make getStatus return Option<StatusInfo> to handle uninstalled packages - Add getStatus .const/.once/.watch/.onChange wrapper in container-runtime for legacy SystemForEmbassy adapter - Add SDK watchable wrapper classes for all callback-enabled effects: GetStatus, GetServiceManifest, GetHostInfo, GetContainerIp, GetSslCertificate
This commit is contained in:
@@ -69,7 +69,7 @@ export type Effects = {
|
||||
getStatus(options: {
|
||||
packageId?: PackageId
|
||||
callback?: () => void
|
||||
}): Promise<StatusInfo>
|
||||
}): Promise<StatusInfo | null>
|
||||
/** DEPRECATED: indicate to the host os what runstate the service is in */
|
||||
setMainStatus(options: SetMainStatus): Promise<null>
|
||||
|
||||
|
||||
112
sdk/base/lib/util/GetContainerIp.ts
Normal file
112
sdk/base/lib/util/GetContainerIp.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import { Effects } from '../Effects'
|
||||
import { PackageId } from '../osBindings'
|
||||
import { AbortedError } from './AbortedError'
|
||||
import { DropGenerator, DropPromise } from './Drop'
|
||||
|
||||
export class GetContainerIp {
|
||||
constructor(
|
||||
readonly effects: Effects,
|
||||
readonly opts: { packageId?: PackageId } = {},
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Returns the container IP. Reruns the context from which it has been called if the underlying value changes
|
||||
*/
|
||||
const() {
|
||||
return this.effects.getContainerIp({
|
||||
...this.opts,
|
||||
callback:
|
||||
this.effects.constRetry &&
|
||||
(() => this.effects.constRetry && this.effects.constRetry()),
|
||||
})
|
||||
}
|
||||
/**
|
||||
* Returns the container IP. Does nothing if the value changes
|
||||
*/
|
||||
once() {
|
||||
return this.effects.getContainerIp(this.opts)
|
||||
}
|
||||
|
||||
private async *watchGen(abort?: AbortSignal) {
|
||||
const resolveCell = { resolve: () => {} }
|
||||
this.effects.onLeaveContext(() => {
|
||||
resolveCell.resolve()
|
||||
})
|
||||
abort?.addEventListener('abort', () => resolveCell.resolve())
|
||||
while (this.effects.isInContext && !abort?.aborted) {
|
||||
let callback: () => void = () => {}
|
||||
const waitForNext = new Promise<void>((resolve) => {
|
||||
callback = resolve
|
||||
resolveCell.resolve = resolve
|
||||
})
|
||||
yield await this.effects.getContainerIp({
|
||||
...this.opts,
|
||||
callback: () => callback(),
|
||||
})
|
||||
await waitForNext
|
||||
}
|
||||
return new Promise<never>((_, rej) => rej(new AbortedError()))
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the container IP. Returns an async iterator that yields whenever the value changes
|
||||
*/
|
||||
watch(abort?: AbortSignal): AsyncGenerator<string, never, unknown> {
|
||||
const ctrl = new AbortController()
|
||||
abort?.addEventListener('abort', () => ctrl.abort())
|
||||
return DropGenerator.of(this.watchGen(ctrl.signal), () => ctrl.abort())
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the container IP. Takes a custom callback function to run whenever the value changes
|
||||
*/
|
||||
onChange(
|
||||
callback: (
|
||||
value: string,
|
||||
error?: Error,
|
||||
) => { cancel: boolean } | Promise<{ cancel: boolean }>,
|
||||
) {
|
||||
;(async () => {
|
||||
const ctrl = new AbortController()
|
||||
for await (const value of this.watch(ctrl.signal)) {
|
||||
try {
|
||||
const res = await callback(value)
|
||||
if (res.cancel) {
|
||||
ctrl.abort()
|
||||
break
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(
|
||||
'callback function threw an error @ GetContainerIp.onChange',
|
||||
e,
|
||||
)
|
||||
}
|
||||
}
|
||||
})()
|
||||
.catch((e) => callback('', e))
|
||||
.catch((e) =>
|
||||
console.error(
|
||||
'callback function threw an error @ GetContainerIp.onChange',
|
||||
e,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the container IP. Returns when the predicate is true
|
||||
*/
|
||||
waitFor(pred: (value: string) => boolean): Promise<string> {
|
||||
const ctrl = new AbortController()
|
||||
return DropPromise.of(
|
||||
Promise.resolve().then(async () => {
|
||||
for await (const next of this.watchGen(ctrl.signal)) {
|
||||
if (pred(next)) {
|
||||
return next
|
||||
}
|
||||
}
|
||||
return ''
|
||||
}),
|
||||
() => ctrl.abort(),
|
||||
)
|
||||
}
|
||||
}
|
||||
112
sdk/base/lib/util/GetHostInfo.ts
Normal file
112
sdk/base/lib/util/GetHostInfo.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import { Effects } from '../Effects'
|
||||
import { Host, HostId, PackageId } from '../osBindings'
|
||||
import { AbortedError } from './AbortedError'
|
||||
import { DropGenerator, DropPromise } from './Drop'
|
||||
|
||||
export class GetHostInfo {
|
||||
constructor(
|
||||
readonly effects: Effects,
|
||||
readonly opts: { hostId: HostId; packageId?: PackageId },
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Returns host info. Reruns the context from which it has been called if the underlying value changes
|
||||
*/
|
||||
const() {
|
||||
return this.effects.getHostInfo({
|
||||
...this.opts,
|
||||
callback:
|
||||
this.effects.constRetry &&
|
||||
(() => this.effects.constRetry && this.effects.constRetry()),
|
||||
})
|
||||
}
|
||||
/**
|
||||
* Returns host info. Does nothing if the value changes
|
||||
*/
|
||||
once() {
|
||||
return this.effects.getHostInfo(this.opts)
|
||||
}
|
||||
|
||||
private async *watchGen(abort?: AbortSignal) {
|
||||
const resolveCell = { resolve: () => {} }
|
||||
this.effects.onLeaveContext(() => {
|
||||
resolveCell.resolve()
|
||||
})
|
||||
abort?.addEventListener('abort', () => resolveCell.resolve())
|
||||
while (this.effects.isInContext && !abort?.aborted) {
|
||||
let callback: () => void = () => {}
|
||||
const waitForNext = new Promise<void>((resolve) => {
|
||||
callback = resolve
|
||||
resolveCell.resolve = resolve
|
||||
})
|
||||
yield await this.effects.getHostInfo({
|
||||
...this.opts,
|
||||
callback: () => callback(),
|
||||
})
|
||||
await waitForNext
|
||||
}
|
||||
return new Promise<never>((_, rej) => rej(new AbortedError()))
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches host info. Returns an async iterator that yields whenever the value changes
|
||||
*/
|
||||
watch(abort?: AbortSignal): AsyncGenerator<Host | null, never, unknown> {
|
||||
const ctrl = new AbortController()
|
||||
abort?.addEventListener('abort', () => ctrl.abort())
|
||||
return DropGenerator.of(this.watchGen(ctrl.signal), () => ctrl.abort())
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches host info. Takes a custom callback function to run whenever the value changes
|
||||
*/
|
||||
onChange(
|
||||
callback: (
|
||||
value: Host | null,
|
||||
error?: Error,
|
||||
) => { cancel: boolean } | Promise<{ cancel: boolean }>,
|
||||
) {
|
||||
;(async () => {
|
||||
const ctrl = new AbortController()
|
||||
for await (const value of this.watch(ctrl.signal)) {
|
||||
try {
|
||||
const res = await callback(value)
|
||||
if (res.cancel) {
|
||||
ctrl.abort()
|
||||
break
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(
|
||||
'callback function threw an error @ GetHostInfo.onChange',
|
||||
e,
|
||||
)
|
||||
}
|
||||
}
|
||||
})()
|
||||
.catch((e) => callback(null, e))
|
||||
.catch((e) =>
|
||||
console.error(
|
||||
'callback function threw an error @ GetHostInfo.onChange',
|
||||
e,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches host info. Returns when the predicate is true
|
||||
*/
|
||||
waitFor(pred: (value: Host | null) => boolean): Promise<Host | null> {
|
||||
const ctrl = new AbortController()
|
||||
return DropPromise.of(
|
||||
Promise.resolve().then(async () => {
|
||||
for await (const next of this.watchGen(ctrl.signal)) {
|
||||
if (pred(next)) {
|
||||
return next
|
||||
}
|
||||
}
|
||||
return null
|
||||
}),
|
||||
() => ctrl.abort(),
|
||||
)
|
||||
}
|
||||
}
|
||||
112
sdk/base/lib/util/GetServiceManifest.ts
Normal file
112
sdk/base/lib/util/GetServiceManifest.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import { Effects } from '../Effects'
|
||||
import { Manifest, PackageId } from '../osBindings'
|
||||
import { AbortedError } from './AbortedError'
|
||||
import { DropGenerator, DropPromise } from './Drop'
|
||||
|
||||
export class GetServiceManifest {
|
||||
constructor(
|
||||
readonly effects: Effects,
|
||||
readonly opts: { packageId: PackageId },
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Returns the service manifest. Reruns the context from which it has been called if the underlying value changes
|
||||
*/
|
||||
const() {
|
||||
return this.effects.getServiceManifest({
|
||||
...this.opts,
|
||||
callback:
|
||||
this.effects.constRetry &&
|
||||
(() => this.effects.constRetry && this.effects.constRetry()),
|
||||
})
|
||||
}
|
||||
/**
|
||||
* Returns the service manifest. Does nothing if the value changes
|
||||
*/
|
||||
once() {
|
||||
return this.effects.getServiceManifest(this.opts)
|
||||
}
|
||||
|
||||
private async *watchGen(abort?: AbortSignal) {
|
||||
const resolveCell = { resolve: () => {} }
|
||||
this.effects.onLeaveContext(() => {
|
||||
resolveCell.resolve()
|
||||
})
|
||||
abort?.addEventListener('abort', () => resolveCell.resolve())
|
||||
while (this.effects.isInContext && !abort?.aborted) {
|
||||
let callback: () => void = () => {}
|
||||
const waitForNext = new Promise<void>((resolve) => {
|
||||
callback = resolve
|
||||
resolveCell.resolve = resolve
|
||||
})
|
||||
yield await this.effects.getServiceManifest({
|
||||
...this.opts,
|
||||
callback: () => callback(),
|
||||
})
|
||||
await waitForNext
|
||||
}
|
||||
return new Promise<never>((_, rej) => rej(new AbortedError()))
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the service manifest. Returns an async iterator that yields whenever the value changes
|
||||
*/
|
||||
watch(abort?: AbortSignal): AsyncGenerator<Manifest, never, unknown> {
|
||||
const ctrl = new AbortController()
|
||||
abort?.addEventListener('abort', () => ctrl.abort())
|
||||
return DropGenerator.of(this.watchGen(ctrl.signal), () => ctrl.abort())
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the service manifest. Takes a custom callback function to run whenever the value changes
|
||||
*/
|
||||
onChange(
|
||||
callback: (
|
||||
value: Manifest | null,
|
||||
error?: Error,
|
||||
) => { cancel: boolean } | Promise<{ cancel: boolean }>,
|
||||
) {
|
||||
;(async () => {
|
||||
const ctrl = new AbortController()
|
||||
for await (const value of this.watch(ctrl.signal)) {
|
||||
try {
|
||||
const res = await callback(value)
|
||||
if (res.cancel) {
|
||||
ctrl.abort()
|
||||
break
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(
|
||||
'callback function threw an error @ GetServiceManifest.onChange',
|
||||
e,
|
||||
)
|
||||
}
|
||||
}
|
||||
})()
|
||||
.catch((e) => callback(null, e))
|
||||
.catch((e) =>
|
||||
console.error(
|
||||
'callback function threw an error @ GetServiceManifest.onChange',
|
||||
e,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the service manifest. Returns when the predicate is true
|
||||
*/
|
||||
waitFor(pred: (value: Manifest) => boolean): Promise<Manifest> {
|
||||
const ctrl = new AbortController()
|
||||
return DropPromise.of(
|
||||
Promise.resolve().then(async () => {
|
||||
for await (const next of this.watchGen(ctrl.signal)) {
|
||||
if (pred(next)) {
|
||||
return next
|
||||
}
|
||||
}
|
||||
throw new Error('context left before predicate passed')
|
||||
}),
|
||||
() => ctrl.abort(),
|
||||
)
|
||||
}
|
||||
}
|
||||
118
sdk/base/lib/util/GetSslCertificate.ts
Normal file
118
sdk/base/lib/util/GetSslCertificate.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import { Effects } from '../Effects'
|
||||
import { AbortedError } from './AbortedError'
|
||||
import { DropGenerator, DropPromise } from './Drop'
|
||||
|
||||
export class GetSslCertificate {
|
||||
constructor(
|
||||
readonly effects: Effects,
|
||||
readonly opts: {
|
||||
hostnames: string[]
|
||||
algorithm?: 'ecdsa' | 'ed25519'
|
||||
},
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Returns the SSL certificate. Reruns the context from which it has been called if the underlying value changes
|
||||
*/
|
||||
const() {
|
||||
return this.effects.getSslCertificate({
|
||||
...this.opts,
|
||||
callback:
|
||||
this.effects.constRetry &&
|
||||
(() => this.effects.constRetry && this.effects.constRetry()),
|
||||
})
|
||||
}
|
||||
/**
|
||||
* Returns the SSL certificate. Does nothing if the value changes
|
||||
*/
|
||||
once() {
|
||||
return this.effects.getSslCertificate(this.opts)
|
||||
}
|
||||
|
||||
private async *watchGen(abort?: AbortSignal) {
|
||||
const resolveCell = { resolve: () => {} }
|
||||
this.effects.onLeaveContext(() => {
|
||||
resolveCell.resolve()
|
||||
})
|
||||
abort?.addEventListener('abort', () => resolveCell.resolve())
|
||||
while (this.effects.isInContext && !abort?.aborted) {
|
||||
let callback: () => void = () => {}
|
||||
const waitForNext = new Promise<void>((resolve) => {
|
||||
callback = resolve
|
||||
resolveCell.resolve = resolve
|
||||
})
|
||||
yield await this.effects.getSslCertificate({
|
||||
...this.opts,
|
||||
callback: () => callback(),
|
||||
})
|
||||
await waitForNext
|
||||
}
|
||||
return new Promise<never>((_, rej) => rej(new AbortedError()))
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the SSL certificate. Returns an async iterator that yields whenever the value changes
|
||||
*/
|
||||
watch(
|
||||
abort?: AbortSignal,
|
||||
): AsyncGenerator<[string, string, string], never, unknown> {
|
||||
const ctrl = new AbortController()
|
||||
abort?.addEventListener('abort', () => ctrl.abort())
|
||||
return DropGenerator.of(this.watchGen(ctrl.signal), () => ctrl.abort())
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the SSL certificate. Takes a custom callback function to run whenever the value changes
|
||||
*/
|
||||
onChange(
|
||||
callback: (
|
||||
value: [string, string, string] | null,
|
||||
error?: Error,
|
||||
) => { cancel: boolean } | Promise<{ cancel: boolean }>,
|
||||
) {
|
||||
;(async () => {
|
||||
const ctrl = new AbortController()
|
||||
for await (const value of this.watch(ctrl.signal)) {
|
||||
try {
|
||||
const res = await callback(value)
|
||||
if (res.cancel) {
|
||||
ctrl.abort()
|
||||
break
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(
|
||||
'callback function threw an error @ GetSslCertificate.onChange',
|
||||
e,
|
||||
)
|
||||
}
|
||||
}
|
||||
})()
|
||||
.catch((e) => callback(null, e))
|
||||
.catch((e) =>
|
||||
console.error(
|
||||
'callback function threw an error @ GetSslCertificate.onChange',
|
||||
e,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the SSL certificate. Returns when the predicate is true
|
||||
*/
|
||||
waitFor(
|
||||
pred: (value: [string, string, string]) => boolean,
|
||||
): Promise<[string, string, string]> {
|
||||
const ctrl = new AbortController()
|
||||
return DropPromise.of(
|
||||
Promise.resolve().then(async () => {
|
||||
for await (const next of this.watchGen(ctrl.signal)) {
|
||||
if (pred(next)) {
|
||||
return next
|
||||
}
|
||||
}
|
||||
throw new Error('context left before predicate passed')
|
||||
}),
|
||||
() => ctrl.abort(),
|
||||
)
|
||||
}
|
||||
}
|
||||
116
sdk/base/lib/util/GetStatus.ts
Normal file
116
sdk/base/lib/util/GetStatus.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
import { Effects } from '../Effects'
|
||||
import { PackageId, StatusInfo } from '../osBindings'
|
||||
import { AbortedError } from './AbortedError'
|
||||
import { DropGenerator, DropPromise } from './Drop'
|
||||
|
||||
export class GetStatus {
|
||||
constructor(
|
||||
readonly effects: Effects,
|
||||
readonly opts: { packageId?: PackageId } = {},
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Returns the service status. Reruns the context from which it has been called if the underlying value changes
|
||||
*/
|
||||
const() {
|
||||
return this.effects.getStatus({
|
||||
...this.opts,
|
||||
callback:
|
||||
this.effects.constRetry &&
|
||||
(() => this.effects.constRetry && this.effects.constRetry()),
|
||||
})
|
||||
}
|
||||
/**
|
||||
* Returns the service status. Does nothing if the value changes
|
||||
*/
|
||||
once() {
|
||||
return this.effects.getStatus(this.opts)
|
||||
}
|
||||
|
||||
private async *watchGen(abort?: AbortSignal) {
|
||||
const resolveCell = { resolve: () => {} }
|
||||
this.effects.onLeaveContext(() => {
|
||||
resolveCell.resolve()
|
||||
})
|
||||
abort?.addEventListener('abort', () => resolveCell.resolve())
|
||||
while (this.effects.isInContext && !abort?.aborted) {
|
||||
let callback: () => void = () => {}
|
||||
const waitForNext = new Promise<void>((resolve) => {
|
||||
callback = resolve
|
||||
resolveCell.resolve = resolve
|
||||
})
|
||||
yield await this.effects.getStatus({
|
||||
...this.opts,
|
||||
callback: () => callback(),
|
||||
})
|
||||
await waitForNext
|
||||
}
|
||||
return new Promise<never>((_, rej) => rej(new AbortedError()))
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the service status. Returns an async iterator that yields whenever the value changes
|
||||
*/
|
||||
watch(
|
||||
abort?: AbortSignal,
|
||||
): AsyncGenerator<StatusInfo | null, never, unknown> {
|
||||
const ctrl = new AbortController()
|
||||
abort?.addEventListener('abort', () => ctrl.abort())
|
||||
return DropGenerator.of(this.watchGen(ctrl.signal), () => ctrl.abort())
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the service status. Takes a custom callback function to run whenever the value changes
|
||||
*/
|
||||
onChange(
|
||||
callback: (
|
||||
value: StatusInfo | null,
|
||||
error?: Error,
|
||||
) => { cancel: boolean } | Promise<{ cancel: boolean }>,
|
||||
) {
|
||||
;(async () => {
|
||||
const ctrl = new AbortController()
|
||||
for await (const value of this.watch(ctrl.signal)) {
|
||||
try {
|
||||
const res = await callback(value)
|
||||
if (res.cancel) {
|
||||
ctrl.abort()
|
||||
break
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(
|
||||
'callback function threw an error @ GetStatus.onChange',
|
||||
e,
|
||||
)
|
||||
}
|
||||
}
|
||||
})()
|
||||
.catch((e) => callback(null, e))
|
||||
.catch((e) =>
|
||||
console.error(
|
||||
'callback function threw an error @ GetStatus.onChange',
|
||||
e,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Watches the service status. Returns when the predicate is true
|
||||
*/
|
||||
waitFor(
|
||||
pred: (value: StatusInfo | null) => boolean,
|
||||
): Promise<StatusInfo | null> {
|
||||
const ctrl = new AbortController()
|
||||
return DropPromise.of(
|
||||
Promise.resolve().then(async () => {
|
||||
for await (const next of this.watchGen(ctrl.signal)) {
|
||||
if (pred(next)) {
|
||||
return next
|
||||
}
|
||||
}
|
||||
return null
|
||||
}),
|
||||
() => ctrl.abort(),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,12 @@ export { once } from './once'
|
||||
export { asError } from './asError'
|
||||
export * as Patterns from './patterns'
|
||||
export * from './typeHelpers'
|
||||
export { GetContainerIp } from './GetContainerIp'
|
||||
export { GetHostInfo } from './GetHostInfo'
|
||||
export { GetOutboundGateway } from './GetOutboundGateway'
|
||||
export { GetServiceManifest } from './GetServiceManifest'
|
||||
export { GetSslCertificate } from './GetSslCertificate'
|
||||
export { GetStatus } from './GetStatus'
|
||||
export { GetSystemSmtp } from './GetSystemSmtp'
|
||||
export { Graph, Vertex } from './graph'
|
||||
export { inMs } from './inMs'
|
||||
|
||||
Reference in New Issue
Block a user