forked from Keller18306/MgkeTimetableBot
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(app): graceful shutdown on SIGTERM/SIGINT via App.stop #20
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
Merged
BlindMaster24
merged 4 commits into
devin/1777024936-rate-limit
from
devin/1777025566-graceful-shutdown
Apr 27, 2026
+204
−8
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ece7ce0
feat(app): graceful shutdown on SIGTERM/SIGINT via App.stop
devin-ai-integration[bot] 62387d3
fix(parser): stop() must await active parse iteration before returning
devin-ai-integration[bot] 8ea93de
fix(app): clear stop timeout timer to avoid leaking handles
devin-ai-integration[bot] 04c25c3
chore(format): prettier on src/app.ts
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,108 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| vi.mock('../../config', () => ({ | ||
| config: { | ||
| http: { port: 0 }, | ||
| dev: false, | ||
| api: { url: '/api', rateLimit: { enabled: false } } | ||
| } | ||
| })); | ||
|
|
||
| const closeMock = vi.fn().mockResolvedValue(undefined); | ||
| vi.mock('../../src/db', () => ({ | ||
| sequelize: { close: closeMock, sync: vi.fn().mockResolvedValue(undefined) } | ||
| })); | ||
| vi.mock('../../src/db/migrator', () => ({ | ||
| runMigrations: vi.fn().mockResolvedValue([]) | ||
| })); | ||
|
|
||
| vi.mock('../../src/http', () => ({ HttpService: class {} })); | ||
| vi.mock('../../src/services/alice', () => ({ AliceApp: class {} })); | ||
| vi.mock('../../src/services/api', () => ({ Api: class {} })); | ||
| vi.mock('../../src/services/bots', () => ({ BotService: class {} })); | ||
| vi.mock('../../src/services/bots/tg', () => ({ TgBot: class {} })); | ||
| vi.mock('../../src/services/bots/viber', () => ({ ViberBot: class {} })); | ||
| vi.mock('../../src/services/bots/vk', () => ({ VkBot: class {} })); | ||
| vi.mock('../../src/services/google', () => ({ GoogleService: class {} })); | ||
| vi.mock('../../src/services/image', () => ({ ImageService: class {} })); | ||
| vi.mock('../../src/services/parser', () => ({ ParserService: class {} })); | ||
| vi.mock('../../src/services/timetable', () => ({ Timetable: class {} })); | ||
| vi.mock('../../src/services/vk_app', () => ({ VKApp: class {} })); | ||
|
|
||
| const loadApp = async () => { | ||
| const { App } = await import('../../src/app'); | ||
| return App; | ||
| }; | ||
|
|
||
| describe('App.stop', () => { | ||
| beforeEach(() => vi.clearAllMocks()); | ||
| afterEach(() => vi.restoreAllMocks()); | ||
|
|
||
| it('calls stop on services in reverse registration order and closes the DB', async () => { | ||
| const App = await loadApp(); | ||
| const app = new App([], { validate: false }); | ||
| const order: string[] = []; | ||
|
|
||
| const mkSvc = (name: string, withStop = true) => ({ | ||
| run: vi.fn(), | ||
| stop: withStop | ||
| ? vi.fn().mockImplementation(async () => { | ||
| order.push(name); | ||
| }) | ||
| : undefined | ||
| }); | ||
|
|
||
| (app as any).services.set('http', mkSvc('http')); | ||
| (app as any).services.set('parser', mkSvc('parser')); | ||
| (app as any).services.set('tg', mkSvc('tg')); | ||
| (app as any).services.set('timetable', mkSvc('timetable', false)); | ||
|
|
||
| await app.stop(); | ||
|
|
||
| expect(order).toEqual(['tg', 'parser', 'http']); | ||
| }); | ||
|
|
||
| it('continues when one service throws and still closes the DB', async () => { | ||
| const App = await loadApp(); | ||
| const app = new App([], { validate: false }); | ||
| closeMock.mockClear(); | ||
|
|
||
| (app as any).services.set('http', { | ||
| run: vi.fn(), | ||
| stop: vi.fn().mockResolvedValue(undefined) | ||
| }); | ||
| (app as any).services.set('tg', { | ||
| run: vi.fn(), | ||
| stop: vi.fn().mockRejectedValue(new Error('boom')) | ||
| }); | ||
|
|
||
| await app.stop(); | ||
|
|
||
| expect((app as any).services.get('http').stop).toHaveBeenCalled(); | ||
| expect(closeMock).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('enforces a per-service timeout and moves on to the next', async () => { | ||
| const App = await loadApp(); | ||
| const app = new App([], { validate: false }); | ||
|
|
||
| let resolveStuck: () => void = () => {}; | ||
| (app as any).services.set('stuck', { | ||
| run: vi.fn(), | ||
| stop: vi.fn().mockImplementation( | ||
| () => | ||
| new Promise<void>((resolve) => { | ||
| resolveStuck = resolve; | ||
| }) | ||
| ) | ||
| }); | ||
| const fast = { run: vi.fn(), stop: vi.fn().mockResolvedValue(undefined) }; | ||
| (app as any).services.set('fast', fast); | ||
|
|
||
| const done = app.stop({ timeoutMs: 50 }); | ||
| await done; | ||
| resolveStuck(); | ||
|
|
||
| expect(fast.stop).toHaveBeenCalled(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.