-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate to polling watcher (remove chokidar) (#2202)
- Loading branch information
1 parent
8524f86
commit 77f60e2
Showing
20 changed files
with
216 additions
and
492 deletions.
There are no files selected for viewing
Binary file removed
BIN
-372 KB
.yarn/cache/@eslint-community-eslint-utils-npm-4.4.0-d1791bd5a3-7e559c4ce5.zip
Binary file not shown.
Binary file removed
BIN
-438 KB
.yarn/cache/@eslint-community-regexpp-npm-4.11.1-37bbb67aaa-fbcc1cb65e.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-411 KB
.yarn/cache/@vue-server-renderer-npm-3.5.12-3ad333df56-d9f25f165c.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// | ||
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Gardener contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
'use strict' | ||
|
||
const fs = require('fs/promises') | ||
const { globalLogger: logger } = require('@gardener-dashboard/logger') | ||
const PollingWatcher = require('../lib/PollingWatcher') | ||
|
||
const actualNextTick = jest.requireActual('process').nextTick | ||
const flushPromises = () => new Promise(actualNextTick) | ||
|
||
describe('polling-watcher', () => { | ||
beforeAll(() => { | ||
jest.useFakeTimers() | ||
}) | ||
|
||
afterAll(() => { | ||
jest.useRealTimers() | ||
}) | ||
|
||
describe('#run', () => { | ||
const filename = '/path/to/file' | ||
const content = 'content' | ||
const interval = 10 | ||
let ac | ||
let fn | ||
|
||
const createWatcher = (signal = ac.signal) => { | ||
return new PollingWatcher([filename], { | ||
interval, | ||
signal, | ||
}) | ||
} | ||
|
||
const advanceTimersByOneInterval = async () => { | ||
jest.advanceTimersByTime(interval + 1) | ||
await flushPromises() | ||
} | ||
|
||
beforeEach(() => { | ||
ac = new AbortController() | ||
fn = jest.fn() | ||
jest.spyOn(fs, 'readFile').mockResolvedValue(content) | ||
logger.info.mockClear() | ||
logger.error.mockClear() | ||
}) | ||
|
||
afterEach(() => { | ||
fs.readFile.mockRestore() | ||
ac.abort() | ||
}) | ||
|
||
it('should read the file content and call the callback function', async () => { | ||
createWatcher().run(fn) | ||
expect(fn).toHaveBeenCalledTimes(0) | ||
await advanceTimersByOneInterval() | ||
expect(fn).toHaveBeenCalledTimes(1) | ||
expect(fn.mock.calls[0]).toEqual([filename, content]) | ||
expect(logger.info).toHaveBeenCalledTimes(1) | ||
expect(logger.info.mock.calls[0]).toEqual(['[kube-config] updated content of file `%s`', filename]) | ||
}) | ||
|
||
it('should fail to read file content and not call the callback function', async () => { | ||
const error = new Error('no such file') | ||
fs.readFile.mockRejectedValueOnce(error) | ||
createWatcher().run(fn) | ||
expect(fn).toHaveBeenCalledTimes(0) | ||
await advanceTimersByOneInterval() | ||
expect(fn).toHaveBeenCalledTimes(0) | ||
expect(logger.error).toHaveBeenCalledTimes(1) | ||
expect(logger.error.mock.calls[0]).toEqual(['[kube-config] failed to stat file `%s`: %s', filename, error.message]) | ||
}) | ||
|
||
it('should do nothing if abortet before run', async () => { | ||
const watcher = createWatcher() | ||
ac.abort() | ||
watcher.run(fn) | ||
await advanceTimersByOneInterval() | ||
expect(fn).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
it('should do nothing if already abortet', async () => { | ||
ac.abort() | ||
const watcher = createWatcher() | ||
watcher.run(fn) | ||
await advanceTimersByOneInterval() | ||
expect(fn).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
it('should run a watcher without abort signal', async () => { | ||
const watcher = createWatcher(null) | ||
ac.abort() | ||
watcher.run(fn) | ||
await advanceTimersByOneInterval() | ||
expect(fn).toHaveBeenCalledTimes(1) | ||
watcher.abort() | ||
}) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
'use strict' | ||
|
||
const fs = require('fs/promises') | ||
const EventEmitter = require('events') | ||
const { globalLogger: logger } = require('@gardener-dashboard/logger') | ||
|
||
class PollingWatcher extends EventEmitter { | ||
#paths | ||
#interval | ||
#intervalId | ||
#state = 'initial' | ||
|
||
constructor (paths, options) { | ||
super() | ||
this.#paths = paths | ||
this.#interval = options?.interval ?? 300_000 | ||
process.nextTick(() => { | ||
this.#state = 'ready' | ||
logger.debug('[kube-config] watcher ready') | ||
this.emit('ready') | ||
}) | ||
const signal = options?.signal | ||
if (signal instanceof AbortSignal) { | ||
if (!signal.aborted) { | ||
signal.addEventListener('abort', () => this.abort(), { once: true }) | ||
} else { | ||
this.#state = 'aborted' | ||
} | ||
} | ||
} | ||
|
||
run (fn) { | ||
if (['initial', 'ready'].includes(this.#state)) { | ||
const tryReadFile = async path => { | ||
try { | ||
// eslint-disable-next-line security/detect-non-literal-fs-filename -- path is not user input | ||
const value = await fs.readFile(path, 'utf8') | ||
logger.info('[kube-config] updated content of file `%s`', path) | ||
fn(path, value) | ||
} catch (err) { | ||
logger.error('[kube-config] failed to stat file `%s`: %s', path, err.message) | ||
} | ||
} | ||
this.#state = 'running' | ||
this.#intervalId = setInterval(() => { | ||
for (const path of this.#paths) { | ||
tryReadFile(path) | ||
} | ||
}, this.#interval) | ||
} | ||
} | ||
|
||
abort () { | ||
this.#state = 'aborted' | ||
clearInterval(this.#intervalId) | ||
} | ||
} | ||
|
||
module.exports = PollingWatcher |
Oops, something went wrong.