Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/compute/compute-servers-table.ts
Views: 687
/*1Compute servers in a specific project.2*/34import { Table } from "@cocalc/frontend/app-framework/Table";5import { redux } from "@cocalc/frontend/app-framework";6import { isValidUUID } from "@cocalc/util/misc";7import { computeServersEnabled } from "./config";8import { delay } from "awaiting";910const PREFIX = "compute-server-";11function projectIdToName(project_id) {12return `${PREFIX}${project_id}`;13}14function nameToProjectId(name) {15return name.slice(PREFIX.length);16}1718// Create and register compute servers table for a given project,19// which gets automatically synchronized with the database, when20// changes occur.21class ComputeServersTable extends Table {22constructor(project_id: string) {23if (!isValidUUID(project_id)) {24throw Error(`project_id must be a valid uuid but is ${project_id}`);25}26super(projectIdToName(project_id), redux);27this.query = this.query.bind(this);28this._change = this._change.bind(this);29}3031options() {32return [];33}3435query() {36return {37compute_servers: [38{39project_id: nameToProjectId(this.name),40id: null,41project_specific_id: null,42account_id: null,43title: null,44color: null,45cost_per_hour: null,46deleted: null,47error: null,48state: null,49state_changed: null,50idle_timeout: null,51automatic_shutdown: null,52autorestart: null,53cloud: null,54configuration: null,55provisioned_configuration: null,56data: null,57avatar_image_tiny: null,58last_edited: null,59purchase_id: null,60detailed_state: null,61position: null,62template: null,63},64],65};66}6768_change(table) {69const actions = redux.getProjectActions(nameToProjectId(this.name));70// Using {compute_servers:table.get()} does NOT work. The id keys are integers,71// which leads to problems when converting after an update.72actions.setState({ compute_servers: table.get()?.toJS() });73}74}7576const tables: { [project_id: string]: ComputeServersTable } = {};77export async function init(project_id: string) {78let enabled = computeServersEnabled();79while (enabled === null) {80// customize hasn't been loaded yet, so we don't know.81await delay(1000);82enabled = computeServersEnabled();83}8485if (!enabled) {86// no need -- would just waste resources87return;88}89if (tables[project_id] != null) {90return;91}92tables[project_id] = new ComputeServersTable(project_id);93}9495export function close(project_id: string) {96if (tables[project_id] == null) {97return;98}99tables[project_id].close();100delete tables[project_id];101}102103104