Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
release-notes: false
---

- Back off `FileHelper` file-watch retries when watching fails repeatedly.
24 changes: 9 additions & 15 deletions projects/start-sdk/lib/mainFn/Daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as T from '@start9labs/start-core/types'
import { asError } from '@start9labs/start-core/util/asError'
import { logErrorOnce } from '@start9labs/start-core/util/logErrorOnce'
import { Drop } from '../util'
import { delay } from '../util/delay'
import { SubContainer } from '../util/SubContainer'
import { CommandController } from './CommandController'
import { DaemonCommandType } from './Daemons'
Expand All @@ -14,12 +15,13 @@ const MAX_TIMEOUT_MS = 30000
* A managed long-running process wrapper around {@link CommandController}.
*
* When started, the daemon automatically restarts its underlying command on
* failure with exponential backoff (up to 30 seconds). While the daemon is
* running, it holds a use-token (`subcontainer.hold()`) on its SubContainer
* so the container is not destroyed out from under it. There are two ways to
* bring it down: {@link stop} releases the hold but leaves the SubContainer
* intact so a later {@link start} can resume in it (a restartable pause);
* {@link term} additionally destroys the SubContainer (genuine teardown).
* failure, waiting one second longer after each failure (up to 30 seconds).
* While the daemon is running, it holds a use-token (`subcontainer.hold()`)
* on its SubContainer so the container is not destroyed out from under it.
* There are two ways to bring it down: {@link stop} releases the hold but
* leaves the SubContainer intact so a later {@link start} can resume in it
* (a restartable pause); {@link term} additionally destroys the SubContainer
* (genuine teardown).
*
* @typeParam Manifest - The service manifest type
* @typeParam C - The subcontainer type, or `null` for JS-only daemons
Expand Down Expand Up @@ -178,15 +180,7 @@ export class Daemon<
if (!signal.aborted) logErrorOnce(e)
}
if (signal.aborted) break
await new Promise<void>(resolve => {
const wake = () => {
clearTimeout(timer)
signal.removeEventListener('abort', wake)
resolve()
}
const timer = setTimeout(wake, timeoutCounter)
signal.addEventListener('abort', wake)
})
await delay(timeoutCounter, { signal })
timeoutCounter += TIMEOUT_INCREMENT_MS
timeoutCounter = Math.min(MAX_TIMEOUT_MS, timeoutCounter)
}
Expand Down
1 change: 1 addition & 0 deletions projects/start-sdk/lib/test/Daemons.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const bareEager = (destroyFs: () => Promise<null>) =>
destroyRequested: false,
holdCount: 0,
teardown: null,
releaseContextCleanup: () => {},
fileMounts: new FileMounts(),
leaderExited: true,
guid: 'guid',
Expand Down
70 changes: 70 additions & 0 deletions projects/start-sdk/lib/test/delay.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { delay, ExponentialBackoff } from '../util/delay'

const MAX_TIMEOUT_MS = 2 ** 31 - 1

describe('delay', () => {
beforeEach(() => jest.useFakeTimers())
afterEach(() => jest.useRealTimers())

const settledBy = async (pending: Promise<void>, ms: number) => {
let settled = false
pending.then(() => (settled = true))
await jest.advanceTimersByTimeAsync(ms)
return settled
}

it('resolves at once when already aborted', async () => {
const ctrl = new AbortController()
ctrl.abort()
const pending = delay(Infinity, { signal: ctrl.signal })
expect(await settledBy(pending, 0)).toBe(true)
expect(jest.getTimerCount()).toBe(0)
})

it('resolves on abort and clears its timer', async () => {
const ctrl = new AbortController()
const pending = delay(MAX_TIMEOUT_MS + 1_000, { signal: ctrl.signal })
expect(await settledBy(pending, MAX_TIMEOUT_MS)).toBe(false)
ctrl.abort()
expect(await settledBy(pending, 0)).toBe(true)
expect(jest.getTimerCount()).toBe(0)
})

it('resolves an infinite delay on abort', async () => {
const ctrl = new AbortController()
const pending = delay(Infinity, { signal: ctrl.signal })
expect(await settledBy(pending, MAX_TIMEOUT_MS)).toBe(false)
ctrl.abort()
expect(await settledBy(pending, 0)).toBe(true)
})
})

describe('delay timer ref', () => {
afterEach(() => jest.restoreAllMocks())

it('holds the event loop unless ref is false', async () => {
const timers = jest.spyOn(globalThis, 'setTimeout')
const ctrl = new AbortController()
const pending = [
delay(60_000, { signal: ctrl.signal }),
delay(60_000, { signal: ctrl.signal, ref: false }),
]
const refs = timers.mock.results.map(r =>
(r.value as NodeJS.Timeout).hasRef(),
)
ctrl.abort()
await Promise.all(pending)
expect(refs).toEqual([true, false])
})
})

describe('ExponentialBackoff', () => {
it('doubles up to the cap and restarts after reset', () => {
const backoff = new ExponentialBackoff(100, 500)
expect([1, 2, 3, 4, 5].map(() => backoff.next())).toEqual([
100, 200, 400, 500, 500,
])
backoff.reset()
expect(backoff.next()).toBe(100)
})
})
Loading