MCPcopy
hub / github.com/inkeep/open-knowledge / updateProcessLockPort

Function updateProcessLockPort

packages/server/src/process-lock.ts:179–223  ·  view source on GitHub ↗
(opts: {
  lockName: LockName;
  lockDir: string;
  port: number;
})

Source from the content-addressed store, hash-verified

177}
178
179export function updateProcessLockPort(opts: {
180 lockName: LockName;
181 lockDir: string;
182 port: number;
183}): void {
184 const { lockName, lockDir, port } = opts;
185 const logPrefix = `[${lockName}-lock]`;
186 const lockPath = lockFilePath(lockDir, lockName);
187
188 if (!existsSync(lockPath)) {
189 console.warn(`${logPrefix} Lock file missing at ${lockPath} during port update — skipping`);
190 return;
191 }
192
193 let existing: ProcessLockMetadata;
194 try {
195 const parsed = JSON.parse(readFileSync(lockPath, 'utf-8'));
196 if (
197 !parsed ||
198 typeof parsed !== 'object' ||
199 !isValidLockPid((parsed as { pid?: unknown }).pid)
200 ) {
201 console.warn(`${logPrefix} Corrupt lock at ${lockPath} during port update — skipping`);
202 return;
203 }
204 existing = parsed as ProcessLockMetadata;
205 } catch {
206 console.warn(`${logPrefix} Unreadable lock at ${lockPath} during port update — skipping`);
207 return;
208 }
209 if (existing.pid !== process.pid) return;
210 if (typeof existing.hostname === 'string' && existing.hostname !== hostname()) return;
211
212 existing.port = port;
213 try {
214 writeFileSync(lockPath, JSON.stringify(existing, null, 2), {
215 encoding: 'utf-8',
216 mode: 0o600,
217 });
218 } catch (err) {
219 console.warn(
220 `${logPrefix} Failed to update port in ${lockPath}: ${err instanceof Error ? err.message : String(err)}`,
221 );
222 }
223}
224
225export function readProcessLock(opts: {
226 lockName: LockName;

Callers 4

updateServerLockPortFunction · 0.90
updateUiLockPortFunction · 0.90
buildHandleFunction · 0.85

Calls 4

isValidLockPidFunction · 0.90
lockFilePathFunction · 0.85
parseMethod · 0.80
warnMethod · 0.65

Tested by

no test coverage detected