Path: blob/master/src/packages/frontend/compute/automatic-shutdown.tsx
1503 views
/*1Configuration to automate turning server off via backend maintenance task involving many2different rules:34- idle timeout5- spend limit67*/89import {10Alert,11Button,12Card,13Checkbox,14Flex,15Modal,16Popconfirm,17Space,18Spin,19Switch,20} from "antd";21import { useState } from "react";22import Inline from "./inline";23import { IdleTimeout } from "./idle-timeout";24import { SpendLimit } from "./spend-limit";25import { HealthCheck } from "./health-check";26import { ShutdownTime } from "./shutdown-time";27import ShowError from "@cocalc/frontend/components/error";28import { Icon } from "@cocalc/frontend/components";2930export function AutomaticShutdownCard(props) {31return (32<Card33styles={{34body: props.enabled ? undefined : { display: "none" },35title: { fontSize: "15px" },36}}37title={<CardTitle {...props} />}38>39{props.children}40<ShowError41error={props.error}42setError={props.setError}43style={{ width: "100%", marginTop: "15px" }}44/>45</Card>46);47}4849function CardTitle({50icon,51title,52enabled,53setEnabled,54saving,55setSaving,56setError,57save,58hasUnsavedChanges,59savedEnabled,60confirmSave = false,61}) {62const [justSaved, setJustSaved] = useState<boolean>(false);63const doSave = async () => {64try {65setSaving(true);66setJustSaved(true);67await save();68} catch (err) {69setError(`${err}`);70} finally {71setSaving(false);72setTimeout(() => {73setJustSaved(false);74}, 1000);75}76};77let saveButton = (78<Button79type="primary"80disabled={saving || !hasUnsavedChanges || justSaved}81onClick={confirmSave ? undefined : doSave}82>83<Icon name="save" /> Save{" "}84{saving && <Spin style={{ marginLeft: "5px" }} delay={500} />}85</Button>86);87if (confirmSave) {88saveButton = (89<Popconfirm title={confirmSave} onConfirm={doSave}>90{saveButton}91</Popconfirm>92);93}94return (95<Flex style={{ alignItems: "center" }}>96<div97style={{98width: "150px",99overflow: "hidden",100textOverflow: "ellipsis",101}}102>103<Icon name={icon as any} style={{ marginRight: "10px" }} /> {title}104</div>105<div style={{ flex: 1 }} />106<Space>107<Checkbox108disabled={saving}109checked={enabled}110onChange={(e) => {111setEnabled(e.target.checked);112}}113>114Enable{enabled ? "d" : ""}115</Checkbox>116{saveButton}117</Space>118<div style={{ flex: 1 }} />119<div style={{ marginLeft: "15px", width: "105px" }}>120{savedEnabled ? (121<Alert type="success" showIcon message={"Enabled"} />122) : undefined}123</div>124</Flex>125);126}127128export function AutomaticShutdownModal({ id, project_id, close }) {129const [help, setHelp] = useState<boolean>(false);130return (131<Modal132width={900}133open134onCancel={close}135onOk={close}136cancelText="Close"137okButtonProps={{ style: { display: "none" } }}138title={139<div>140<Inline141id={id}142style={{143display: "block",144textAlign: "center",145margin: "-5px 15px 5px 0",146}}147/>148<Flex style={{ marginRight: "20px", alignItems: "center" }}>149<div style={{ fontSize: "18px", margin: "15px 0" }}>150Configure Automatic Shutdown and Health Check Strategies151</div>152<div style={{ flex: 1 }} />153<Switch154checkedChildren={"Help"}155unCheckedChildren={"Help"}156checked={help}157onChange={(val) => setHelp(val)}158/>159</Flex>160{help && (161<div style={{ fontSize: "14px", fontWeight: "normal" }}>162<Button163href="https://youtu.be/Kx_47fs_xcI"164target="_blank"165style={{ float: "right" }}166>167<Icon name="youtube" style={{ color: "red" }} />168YouTube Video169</Button>170Each strategy automatically turns this compute server off when a171condition is met. This can save you money keeping spending under172control. When the server is shutdown, a message is also sent and a173log entry is created.174</div>175)}176</div>177}178>179<IdleTimeout id={id} project_id={project_id} help={help} />180<div style={{ height: "15px" }} />181<ShutdownTime id={id} project_id={project_id} help={help} />182<div style={{ height: "15px" }} />183<SpendLimit id={id} project_id={project_id} help={help} />184<div style={{ height: "15px" }} />185<HealthCheck id={id} project_id={project_id} help={help} />186</Modal>187);188}189190191