놓친 프로세스 찾기

놓친 프로세스 찾기

프로세스 연결을 잃은 뒤 작업에 다시 연결해 봐요. 새 명령을 시작하면 효과가 중복될 수 있으므로, 먼저 이미 시작한 프로세스를 찾으세요.

출처: 문서

본문

TypeScript 프로세스 핸들로 열린 연결은 출력을 소비하는 동안 임시 단절로부터 복구돼요. 같은 프로세스로 다시 연결하고 마지막으로 전달된 청크 이후부터 이어서, 복구 에피소드당 30초 예산으로 진행해요. 명령을 다시 시작하거나 터미널 입력을 재생하지는 않아요. 원시 스트림은 명시적 재연결이 필요해요. 자동 복구가 멈추거나, 연결을 닫거나, 애플리케이션이 다시 시작될 때 이 가이드를 사용하세요.

현재 샌드박스 핸들과 프로세스를 시작할 때 애플리케이션이 지정한 세션 태그가 필요해요. 세션 태그는 검색 가능한 메타데이터이지 프로세스의 리소스 이름이 아니에요.

실행 중인 세션 찾기

세션과 실행 상태로 필터링해 프로세스를 나열해요. 예시는 페이지 매김을 따라요. 여러 프로세스가 일치하면 임의의 결과에 붙지 말고 저장된 이름이나 메타데이터로 의도한 리소스를 선택하세요.

빈 목록은 일치하는 실행 중인 프로세스가 없음을 뜻해요. 원래 명령이 시작되지 않았음을 증명하지는 않아요. 종료되었을 수도 있어요.

핵심 코드:

return sandbox.processes
  .all({ filter: `session=${session},state=running` })
  .collect();

완전한 TypeScript 예시: reconnect/list.ts

import type { Sandbox } from '@docker/sandboxes';

export async function findSession(sandbox: Sandbox, session: string) {
  return sandbox.processes
    .all({ filter: `session=${session},state=running` })
    .collect();
}

그 출력에 다시 연결

선택한 프로세스 핸들에 연결하고 출력 이벤트를 소비해요. 예시는 그 핸들이 보고한 마지막 시퀀스부터 이어가요.

다른 시스템으로 전달할 때는 그 시스템이 실제로 처리한 마지막 청크에 대한 자체 커서를 유지하세요. 프로세스가 보고한 최신 시퀀스는 그 커서보다 앞설 수 있어요. 저장된 위치에서 재생해야 할 때는 명시적 출력 재개를 사용하세요.

작업이 끝나면 연결을 닫아요. 연결이 다시 끊어지면 프로세스 이름을 보관해요. 재연결을 두 번째 프로세스 시작으로 대체하지 마세요.

핵심 코드:

const connection = await process.connect({
  resumeFrom: process.lastStreamSequence,
});
let lastSequence = BigInt(process.lastStreamSequence ?? '0');
try {
  for await (const event of connection) {
    if (event.type === 'chunk')
      write(
        event.data ?? new Uint8Array(),
        (lastSequence = BigInt(event.streamSequence ?? '0')),
      );
  }
  return lastSequence;
} finally {
  await connection.close();
}

완전한 TypeScript 예시: reconnect/attach.ts

import type { Process } from '@docker/sandboxes';

export async function rejoinProcess(
  process: Process,
  write: (bytes: Uint8Array, sequence: bigint) => void,
) {
  const connection = await process.connect({
    resumeFrom: process.lastStreamSequence,
  });
  let lastSequence = BigInt(process.lastStreamSequence ?? '0');
  try {
    for await (const event of connection) {
      if (event.type === 'chunk')
        write(
          event.data ?? new Uint8Array(),
          (lastSequence = BigInt(event.streamSequence ?? '0')),
        );
    }
    return lastSequence;
  } finally {
    await connection.close();
  }
}

더 알아보기 (Learn more)