(
session: IKernelSession,
kernelConnectionMetadata: KernelConnectionMetadata,
workspaceMemento: Memento
)
| 18 | const CACHE_EXPIRY_IN_MS = 1000 * 60 * 60 * 24 * 2; |
| 19 | |
| 20 | export async function getKernelInfo( |
| 21 | session: IKernelSession, |
| 22 | kernelConnectionMetadata: KernelConnectionMetadata, |
| 23 | workspaceMemento: Memento |
| 24 | ) { |
| 25 | const promises: Promise< |
| 26 | KernelMessage.IReplyErrorContent | KernelMessage.IReplyAbortContent | KernelMessage.IInfoReply | undefined |
| 27 | >[] = []; |
| 28 | |
| 29 | const defaultResponse: KernelMessage.IInfoReply = { |
| 30 | banner: '', |
| 31 | help_links: [], |
| 32 | implementation: '', |
| 33 | implementation_version: '', |
| 34 | language_info: { name: '', version: '' }, |
| 35 | protocol_version: '', |
| 36 | status: 'ok' |
| 37 | }; |
| 38 | if (!session.kernel) { |
| 39 | return; |
| 40 | } |
| 41 | if (session.kernel?.info) { |
| 42 | promises.push(session.kernel.info); |
| 43 | session.kernel.info |
| 44 | .then((content) => cacheKernelInfo(workspaceMemento, kernelConnectionMetadata, content)) |
| 45 | .catch(noop); |
| 46 | } |
| 47 | // KernelMessage.IReplyErrorContent | KernelMessage.IReplyAbortContent | KernelMessage.IInfoReply | undefined |
| 48 | |
| 49 | // Even if we might have a cached response to the kernel info, make this request. |
| 50 | // We rely on this being sent always, to detect whether cells completed execution or the like |
| 51 | // after reloading VS Code. |
| 52 | const kernelInfoPromise = session.kernel.requestKernelInfo().then((item) => item?.content); |
| 53 | promises.push(kernelInfoPromise); |
| 54 | kernelInfoPromise |
| 55 | .then((content) => |
| 56 | cacheKernelInfo(workspaceMemento, kernelConnectionMetadata, content as KernelMessage.IInfoReply | undefined) |
| 57 | ) |
| 58 | .catch(noop); |
| 59 | // If this doesn't complete in 5 seconds for remote kernels, assume the kernel is busy & provide some default content. |
| 60 | if (kernelConnectionMetadata.kind === 'connectToLiveRemoteKernel') { |
| 61 | const cachedInfo = getCacheKernelInfo(workspaceMemento, kernelConnectionMetadata); |
| 62 | if (cachedInfo) { |
| 63 | promises.push(Promise.resolve(cachedInfo)); |
| 64 | } else { |
| 65 | promises.push(sleep(5_000).then(() => defaultResponse)); |
| 66 | } |
| 67 | } |
| 68 | const content = await Promise.race(promises); |
| 69 | if (content === defaultResponse) { |
| 70 | logger.warn('Failed to Kernel info in a timely manner, defaulting to empty info!'); |
| 71 | } else { |
| 72 | logger.trace('Got Kernel info'); |
| 73 | } |
| 74 | return content; |
| 75 | } |
| 76 | |
| 77 | async function cacheKernelInfo( |
no test coverage detected