-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
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,47 @@ | ||
import { status } from './status' | ||
import { sleep } from './utils' | ||
|
||
export class PeriodicTask { | ||
private promise: Promise<void> | ||
private running = true | ||
private abortController: AbortController | ||
|
||
constructor(task: () => Promise<void> | void, intervalMs = 1000, minimumWaitMs = 1000) { | ||
this.abortController = new AbortController() | ||
|
||
const abortRequested = new Promise((resolve) => { | ||
this.abortController.signal.addEventListener('abort', resolve, { once: true }) | ||
}) | ||
|
||
this.promise = new Promise<void>(async (resolve, reject) => { | ||
try { | ||
status.debug('🔄', 'Periodic task starting...', { task }) | ||
while (!this.abortController.signal.aborted) { | ||
const startTimeMs = +Date.now() | ||
await task() | ||
const waitTimeMs = Math.max(intervalMs - startTimeMs, minimumWaitMs) | ||
status.debug('🔄', `Next evaluation in ${waitTimeMs / 1000}s`, { task }) | ||
await Promise.race([sleep(waitTimeMs), abortRequested]) | ||
} | ||
status.info('✅', 'Periodic task stopped by request.', { task }) | ||
resolve() | ||
} catch (error) { | ||
status.warn('⚠️', 'Error in periodic task!', { task, error }) | ||
reject(error) | ||
} finally { | ||
this.running = false | ||
} | ||
}) | ||
} | ||
|
||
public isRunning(): boolean { | ||
return this.running | ||
} | ||
|
||
public async stop(): Promise<void> { | ||
this.abortController.abort() | ||
try { | ||
await this.promise | ||
} catch {} | ||
} | ||
} |
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,24 @@ | ||
import { PeriodicTask } from '../../src/utils/periodic-task' | ||
|
||
describe('PeriodicTask', () => { | ||
describe('updates completion status', () => { | ||
it('on success', async () => { | ||
const fn = jest.fn() | ||
const task = new PeriodicTask(fn) | ||
expect(fn).toBeCalled() | ||
expect(task.isRunning()).toEqual(true) | ||
await task.stop() | ||
expect(task.isRunning()).toEqual(false) | ||
}) | ||
|
||
it('on failure', async () => { | ||
const fn = jest.fn(() => { | ||
throw new Error() | ||
}) | ||
const task = new PeriodicTask(fn) | ||
expect(fn).toBeCalled() | ||
await task.stop() | ||
expect(task.isRunning()).toEqual(false) | ||
}) | ||
}) | ||
}) |