|
| 1 | +// ***************************************************************************** |
| 2 | +// Copyright (C) 2023 TypeFox and others. |
| 3 | +// |
| 4 | +// This program and the accompanying materials are made available under the |
| 5 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 6 | +// http://www.eclipse.org/legal/epl-2.0. |
| 7 | +// |
| 8 | +// This Source Code may also be made available under the following Secondary |
| 9 | +// Licenses when the conditions for such availability set forth in the Eclipse |
| 10 | +// Public License v. 2.0 are satisfied: GNU General Public License, version 2 |
| 11 | +// with the GNU Classpath Exception which is available at |
| 12 | +// https://www.gnu.org/software/classpath/license.html. |
| 13 | +// |
| 14 | +// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 |
| 15 | +// ***************************************************************************** |
| 16 | +/*--------------------------------------------------------------------------------------------- |
| 17 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 18 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 19 | + *--------------------------------------------------------------------------------------------*/ |
| 20 | + |
| 21 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 22 | +import { CellExecution, NotebookExecutionStateService } from '../service/notebook-execution-state-service'; |
| 23 | +import { CellKind, NotebookCellExecutionState } from '../../common'; |
| 24 | +import { NotebookCellModel } from '../view-model/notebook-cell-model'; |
| 25 | +import { NotebookModel } from '../view-model/notebook-model'; |
| 26 | +import { NotebookKernelService } from './notebook-kernel-service'; |
| 27 | +import { Disposable } from '@theia/core'; |
| 28 | + |
| 29 | +export interface CellExecutionParticipant { |
| 30 | + onWillExecuteCell(executions: CellExecution[]): Promise<void>; |
| 31 | +} |
| 32 | + |
| 33 | +@injectable() |
| 34 | +export class NotebookExecutionService { |
| 35 | + |
| 36 | + @inject(NotebookExecutionStateService) |
| 37 | + protected notebookExecutionStateService: NotebookExecutionStateService; |
| 38 | + |
| 39 | + @inject(NotebookKernelService) |
| 40 | + protected notebookKernelService: NotebookKernelService; |
| 41 | + |
| 42 | + private readonly cellExecutionParticipants = new Set<CellExecutionParticipant>(); |
| 43 | + |
| 44 | + async executeNotebookCells(notebook: NotebookModel, cells: Iterable<NotebookCellModel>): Promise<void> { |
| 45 | + const cellsArr = Array.from(cells) |
| 46 | + .filter(c => c.cellKind === CellKind.Code); |
| 47 | + if (!cellsArr.length) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + console.debug(`NotebookExecutionService#executeNotebookCells ${JSON.stringify(cellsArr.map(c => c.handle))}`); |
| 52 | + |
| 53 | + // create cell executions |
| 54 | + const cellExecutions: [NotebookCellModel, CellExecution][] = []; |
| 55 | + for (const cell of cellsArr) { |
| 56 | + const cellExe = this.notebookExecutionStateService.getCellExecution(cell.uri); |
| 57 | + if (!cellExe) { |
| 58 | + cellExecutions.push([cell, this.notebookExecutionStateService.createCellExecution(notebook.uri, cell.handle)]); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + const kernel = this.notebookKernelService.getSelectedOrSuggestedKernel(notebook); |
| 63 | + |
| 64 | + if (!kernel) { |
| 65 | + // clear all pending cell executions |
| 66 | + cellExecutions.forEach(cellExe => cellExe[1].complete({})); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // filter cell executions based on selected kernel |
| 71 | + const validCellExecutions: CellExecution[] = []; |
| 72 | + for (const [cell, cellExecution] of cellExecutions) { |
| 73 | + if (!kernel.supportedLanguages.includes(cell.language)) { |
| 74 | + cellExecution.complete({}); |
| 75 | + } else { |
| 76 | + validCellExecutions.push(cellExecution); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // request execution |
| 81 | + if (validCellExecutions.length > 0) { |
| 82 | + await this.runExecutionParticipants(validCellExecutions); |
| 83 | + |
| 84 | + this.notebookKernelService.selectKernelForNotebook(kernel, notebook); |
| 85 | + await kernel.executeNotebookCellsRequest(notebook.uri, validCellExecutions.map(c => c.cellHandle)); |
| 86 | + // the connecting state can change before the kernel resolves executeNotebookCellsRequest |
| 87 | + const unconfirmed = validCellExecutions.filter(exe => exe.state === NotebookCellExecutionState.Unconfirmed); |
| 88 | + if (unconfirmed.length) { |
| 89 | + console.debug(`NotebookExecutionService#executeNotebookCells completing unconfirmed executions ${JSON.stringify(unconfirmed.map(exe => exe.cellHandle))}`); |
| 90 | + unconfirmed.forEach(exe => exe.complete({})); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + registerExecutionParticipant(participant: CellExecutionParticipant): Disposable { |
| 96 | + this.cellExecutionParticipants.add(participant); |
| 97 | + return Disposable.create(() => this.cellExecutionParticipants.delete(participant)); |
| 98 | + } |
| 99 | + |
| 100 | + private async runExecutionParticipants(executions: CellExecution[]): Promise<void> { |
| 101 | + for (const participant of this.cellExecutionParticipants) { |
| 102 | + await participant.onWillExecuteCell(executions); |
| 103 | + } |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + async cancelNotebookCellHandles(notebook: NotebookModel, cells: Iterable<number>): Promise<void> { |
| 108 | + const cellsArr = Array.from(cells); |
| 109 | + console.debug(`NotebookExecutionService#cancelNotebookCellHandles ${JSON.stringify(cellsArr)}`); |
| 110 | + const kernel = this.notebookKernelService.getSelectedOrSuggestedKernel(notebook); |
| 111 | + if (kernel) { |
| 112 | + await kernel.cancelNotebookCellExecution(notebook.uri, cellsArr); |
| 113 | + |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + async cancelNotebookCells(notebook: NotebookModel, cells: Iterable<NotebookCellModel>): Promise<void> { |
| 118 | + this.cancelNotebookCellHandles(notebook, Array.from(cells, cell => cell.handle)); |
| 119 | + } |
| 120 | +} |
0 commit comments