-
-
Notifications
You must be signed in to change notification settings - Fork 328
fix: share daemon retirement and verify mixed-version cutover #3132
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
Closed
thymikee
wants to merge
2
commits into
fix/daemon-timeout-retirement
from
fix/daemon-registration-cutover
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { runCmdDetachedMonitored } from '@agent-device/host-kit/command'; | ||
| import { readProcessStartTime } from '@agent-device/host-kit/process'; | ||
| import { stopDaemonProcess } from '../../daemon-process.ts'; | ||
| import type { DaemonPaths } from '../../daemon-resolution.ts'; | ||
|
|
||
| // c237027737: server-lifecycle.ts readLockInfo/acquireDaemonLock/releaseDaemonLock. | ||
| const legacyLockProtocol = ` | ||
| function readLockInfo(lockPath) { | ||
| if (!fs.existsSync(lockPath)) return null; | ||
| try { | ||
| const parsed = JSON.parse(fs.readFileSync(lockPath, 'utf8')); | ||
| if (!Number.isInteger(parsed.pid) || parsed.pid <= 0) return null; | ||
| return parsed; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
| function acquireDaemonLock(baseDir, lockPath, lockData) { | ||
| if (!fs.existsSync(baseDir)) fs.mkdirSync(baseDir, { recursive: true }); | ||
| const payload = JSON.stringify(lockData, null, 2); | ||
| const tryWriteLock = () => { | ||
| try { | ||
| fs.writeFileSync(lockPath, payload, { flag: 'wx', mode: 0o600 }); | ||
| return true; | ||
| } catch (error) { | ||
| if (error.code === 'EEXIST') return false; | ||
| throw error; | ||
| } | ||
| }; | ||
| if (tryWriteLock()) return true; | ||
| const existing = readLockInfo(lockPath); | ||
| if (existing?.pid && existing.pid !== process.pid && | ||
| isAgentDeviceDaemonProcess(existing.pid, existing.processStartTime)) return false; | ||
| try { fs.unlinkSync(lockPath); } catch {} | ||
| return tryWriteLock(); | ||
| } | ||
| function releaseDaemonLock(lockPath) { | ||
| const existing = readLockInfo(lockPath); | ||
| if (existing && existing.pid !== process.pid) return; | ||
| try { if (fs.existsSync(lockPath)) fs.unlinkSync(lockPath); } catch {} | ||
| } | ||
| `; | ||
|
|
||
| export function spawnLegacyDaemonFixture(paths: DaemonPaths, acquisitionBarrier?: string) { | ||
| const codeDir = path.join(paths.baseDir, 'legacy'); | ||
| const entry = path.join(codeDir, 'dist', 'src', 'internal', 'daemon.js'); | ||
| fs.mkdirSync(path.dirname(entry), { recursive: true }); | ||
| fs.writeFileSync(path.join(codeDir, 'package.json'), '{"type":"module"}'); | ||
| const processUrl = new URL('../../daemon-process.ts', import.meta.url).href; | ||
| const hostProcessUrl = new URL('../../../packages/host-kit/src/process.ts', import.meta.url).href; | ||
| fs.writeFileSync( | ||
| entry, | ||
| ` | ||
| import fs from 'node:fs'; | ||
| import { isAgentDeviceDaemonProcess } from ${JSON.stringify(processUrl)}; | ||
| import { readProcessStartTime } from ${JSON.stringify(hostProcessUrl)}; | ||
| ${legacyLockProtocol} | ||
| const paths = ${JSON.stringify(paths)}; | ||
| const barrier = ${JSON.stringify(acquisitionBarrier)}; | ||
| if (barrier) { | ||
| fs.writeFileSync(barrier + '.ready-' + process.pid, 'ready'); | ||
| while (!fs.existsSync(barrier)) await new Promise(resolve => setTimeout(resolve, 10)); | ||
| } | ||
| const identity = { pid: process.pid, processStartTime: readProcessStartTime(process.pid) }; | ||
| const acquired = acquireDaemonLock(paths.baseDir, paths.lockPath, { | ||
| ...identity, version: '0.21.20', startedAt: Date.now(), | ||
| }); | ||
| fs.writeFileSync(paths.baseDir + '/legacy-disposition.json', JSON.stringify({ acquired })); | ||
| if (!acquired) process.exit(0); | ||
| fs.writeFileSync(paths.infoPath, JSON.stringify({ ...identity, port: 4210, token: 'legacy-token', version: '0.21.20' })); | ||
| process.on('SIGTERM', () => { releaseDaemonLock(paths.lockPath); process.exit(0); }); | ||
| setInterval(() => {}, 1000); | ||
| `, | ||
| ); | ||
| const child = runCmdDetachedMonitored(process.execPath, ['--experimental-strip-types', entry]); | ||
| const startTime = readProcessStartTime(child.pid); | ||
| return { | ||
| pid: child.pid, | ||
| exited: child.exited, | ||
| async stop() { | ||
| const result = await stopDaemonProcess( | ||
| { pid: child.pid, startTime }, | ||
| { mode: 'force', termTimeoutMs: 0, killTimeoutMs: 2_000 }, | ||
| ); | ||
| assert.notEqual(result.status, 'retained', JSON.stringify(result)); | ||
| await child.exited; | ||
| }, | ||
| }; | ||
| } |
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
Oops, something went wrong.
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.