Skip to content

Commit

Permalink
feat: stop / start satellite (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker authored Sep 29, 2023
1 parent 08fcab1 commit a2e6146
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 9 deletions.
22 changes: 22 additions & 0 deletions src/frontend/src/lib/api/ic.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,25 @@ export const canisterStatus = async ({

return { cycles, status: toStatus(status), memory_size, canisterId, idle_cycles_burned_per_day };
};

export const canisterStart = async ({
canisterId,
identity
}: {
canisterId: Principal;
identity: Identity;
}): Promise<void> => {
const actor: ICActor = await getICActor(identity);
return actor.start_canister({ canister_id: canisterId });
};

export const canisterStop = async ({
canisterId,
identity
}: {
canisterId: Principal;
identity: Identity;
}): Promise<void> => {
const actor: ICActor = await getICActor(identity);
return actor.stop_canister({ canister_id: canisterId });
};
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@
let disableInfiniteScroll = false;
const onIntersect = async () => {
console.log('here');
if (!$authSignedInStore) {
// It would be unexpected
toasts.error({
text: $i18n.errors.no_identity,
});
return;
}
Expand Down
53 changes: 53 additions & 0 deletions src/frontend/src/lib/components/satellites/SatelliteStart.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<script lang="ts">
import { i18n } from '$lib/stores/i18n.store';
import Confirmation from '$lib/components/core/Confirmation.svelte';
import type { Satellite } from '$declarations/mission_control/mission_control.did';
import { authSignedInStore, authStore } from '$lib/stores/auth.store';
import { toasts } from '$lib/stores/toasts.store';
import { busy } from '$lib/stores/busy.store';
import { canisterStart } from '$lib/api/ic.api';
import { emit } from '$lib/utils/events.utils';
export let satellite: Satellite;
let visible = false;
const start = async () => {
if (!$authSignedInStore) {
toasts.error({
text: $i18n.errors.no_identity
});
return;
}
busy.start();
try {
await canisterStart({ canisterId: satellite.satellite_id, identity: $authStore.identity! });
emit({ message: 'junoRestartCycles', detail: { canisterId: satellite.satellite_id } });
emit({ message: 'junoReloadVersions' });
close();
toasts.success($i18n.satellites.start_success);
} catch (err: unknown) {
toasts.error({
text: $i18n.errors.satellite_start,
detail: err
});
}
busy.stop();
};
const close = () => (visible = false);
</script>

<button on:click={() => (visible = true)}>{$i18n.core.start}</button>

<Confirmation bind:visible on:junoYes={start} on:junoNo={close}>
<svelte:fragment slot="title">{$i18n.satellites.start_tile}</svelte:fragment>

<p>{$i18n.satellites.start_info}</p>
</Confirmation>
56 changes: 56 additions & 0 deletions src/frontend/src/lib/components/satellites/SatelliteStop.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<script lang="ts">
import { i18n } from '$lib/stores/i18n.store';
import Confirmation from '$lib/components/core/Confirmation.svelte';
import { busy } from '$lib/stores/busy.store';
import { toasts } from '$lib/stores/toasts.store';
import { canisterStop } from '$lib/api/ic.api';
import type { Satellite } from '$declarations/mission_control/mission_control.did';
import { authSignedInStore, authStore } from '$lib/stores/auth.store';
import { emit } from '$lib/utils/events.utils';
export let satellite: Satellite;
let visible = false;
const stop = async () => {
if (!$authSignedInStore) {
toasts.error({
text: $i18n.errors.no_identity
});
return;
}
busy.start();
try {
await canisterStop({ canisterId: satellite.satellite_id, identity: $authStore.identity! });
emit({ message: 'junoRestartCycles', detail: { canisterId: satellite.satellite_id } });
close();
toasts.success($i18n.satellites.stop_success);
} catch (err: unknown) {
toasts.error({
text: $i18n.errors.satellite_stop,
detail: err
});
}
busy.stop();
};
const close = () => (visible = false);
</script>

<button on:click={() => (visible = true)}>{$i18n.core.stop}</button>

<Confirmation bind:visible on:junoYes={stop} on:junoNo={close}>
<svelte:fragment slot="title">{$i18n.satellites.stop_title}</svelte:fragment>

<p>{$i18n.satellites.stop_explanation}</p>

<p>{$i18n.satellites.stop_error}</p>

<p>{$i18n.satellites.stop_info}</p>
</Confirmation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script lang="ts">
import type { Canister, CanisterStatus, CanisterSyncStatus } from '$lib/types/canister';
import CanisterStart from '$lib/components/satellites/SatelliteStart.svelte';
import CanisterStop from '$lib/components/satellites/SatelliteStop.svelte';
import { fade } from 'svelte/transition';
import type { Satellite } from '$declarations/mission_control/mission_control.did';
export let satellite: Satellite;
const onSyncCanister = (syncCanister: Canister) => {
if (syncCanister.id !== satellite.satellite_id.toText()) {
return;
}
canister = syncCanister;
};
let canister: Canister | undefined = undefined;
let status: CanisterStatus | undefined = undefined;
let sync: CanisterSyncStatus | undefined = undefined;
$: status = canister?.data?.status;
$: sync = canister?.sync;
</script>

<svelte:window on:junoSyncCanister={({ detail: { canister } }) => onSyncCanister(canister)} />

{#if status === 'stopped' && sync === 'synced'}
<div in:fade><CanisterStart {satellite} /></div>
{:else if status === 'running' && sync === 'synced'}
<div in:fade><CanisterStop {satellite} /></div>
{/if}

<style lang="scss">
div {
display: inline-block;
}
</style>
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<script lang="ts">
import type { Satellite } from '$declarations/mission_control/mission_control.did';
import TopUp from '$lib/components/canister/TopUp.svelte';
import CanisterStopStart from "$lib/components/satellites/SatelliteStopStart.svelte";
export let satellite: Satellite;
let detail = { satellite };
</script>

<TopUp type="topup_satellite" {detail} />

<CanisterStopStart {satellite} />
17 changes: 15 additions & 2 deletions src/frontend/src/lib/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"done": "Done",
"from": "From",
"to": "To",
"export": "Export"
"export": "Export",
"start": "Start",
"stop": "Stop"
},
"canisters": {
"top_up": "Top-up",
Expand Down Expand Up @@ -80,7 +82,15 @@
"create_satellite_price": "Starting a new satellite requires <strong>{0}</strong> ICP. Your current mission control balance is {1} ICP and you have {2} credits remaining.",
"loading_satellites": "Loading your satellites",
"overview": "Overview",
"id": "Satellite ID"
"id": "Satellite ID",
"stop_title": "Stop satellite",
"stop_info": "Do you want to stop your satellite?",
"stop_explanation": "Please note that a stopped satellite continues to consume cycles for the memory it occupies, including any remaining outstanding responses, until the cycle balance reaches 0. After that, it will be deleted.",
"stop_error": "Additionally, be aware that stopping the satellite may result in error messages being displayed in the console.",
"stop_success": "Satellite stopped.",
"start_tile": "Restart satellite",
"start_info": "Do you want to resume your satellite?",
"start_success": "Satellite resumed."
},
"mission_control": {
"title": "Mission Control",
Expand Down Expand Up @@ -194,6 +204,7 @@
"profile_placeholder": "An optional hint about the controller"
},
"errors": {
"no_identity": "Unexpected error. No identity provided.",
"no_mission_control": "Mission control center is not initialized.",
"cli_missing_params": "Missing URL parameters. Either the redirection URL or principal is not provided.",
"cli_missing_selection": "No mission control or satellite(s) selected.",
Expand All @@ -203,6 +214,8 @@
"satellite_no_found": "Satellite not found. Return to <a href=\"/\">start</a> to find your satellites.",
"satellite_name_update": "Unexpected error(s) while trying to rename your satellite.",
"satellite_missing_name": "A name must be provided.",
"satellite_stop": "Unexpected error(s) while trying to stop the satellite",
"satellite_start": "Unexpected error(s) while trying to start the satellite",
"ledger_balance_credits": "Unexpected error(s) while getting your mission control balance and credits.",
"load_credits": "Cannot load your current credits status.",
"hosting_missing_domain_name": "A domain name must be provided.",
Expand Down
17 changes: 15 additions & 2 deletions src/frontend/src/lib/i18n/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"done": "Done",
"from": "From",
"to": "To",
"export": "Export"
"export": "Export",
"start": "Start",
"stop": "Stop"
},
"canisters": {
"top_up": "Ricarica",
Expand Down Expand Up @@ -80,7 +82,15 @@
"create_satellite_price": "Avviare un nuovo satellite richiede <strong>{0}</strong> ICP. Il tuo attuale saldo mission control è di {1} ICP e hai {2} crediti rimanenti.",
"loading_satellites": "Caricando i tuoi satelliti",
"overview": "Panoramica",
"id": "Satellite ID"
"id": "Satellite ID",
"stop_title": "Stop satellite",
"stop_info": "Do you want to stop your satellite?",
"stop_explanation": "Please note that a stopped satellite continues to consume cycles for the memory it occupies, including any remaining outstanding responses, until the cycle balance reaches 0. After that, it will be deleted.",
"stop_error": "Additionally, be aware that stopping the satellite may result in error messages being displayed in the console.",
"stop_success": "Satellite stopped.",
"start_tile": "Restart satellite",
"start_info": "Do you want to resume your satellite?",
"start_success": "Satellite resumed."
},
"mission_control": {
"title": "Mission Control",
Expand Down Expand Up @@ -194,6 +204,7 @@
"profile_placeholder": "Un suggerimento opzionale sul controller"
},
"errors": {
"no_identity": "Unexpected error. No identity provided.",
"no_mission_control": "Mission control center non è inizializzato",
"cli_missing_params": "Parametri URL mancanti. Oppure l'URL di reindirizzamento, o il principal non sono stati forniti.",
"cli_missing_selection": "Nessun mission control o satellite/i selezionato.",
Expand All @@ -203,6 +214,8 @@
"satellite_no_found": "Satellite non trovato.Torna all'<a href=\"/\">inizio</a> per trovare i tuoi satelliti.",
"satellite_name_update": "Unexpected error(s) while trying to rename your satellite.",
"satellite_missing_name": "A name must be provided.",
"satellite_stop": "Unexpected error(s) while trying to stop the satellite",
"satellite_start": "Unexpected error(s) while trying to start the satellite",
"ledger_balance_credits": "Errore/i imprevisto/i durante il caricamento del saldo del tuo mission control e dei crediti.",
"load_credits": "Cannot load your current credits status.",
"hosting_missing_domain_name": "E' obbligatorio scegliere un nome a dominio",
Expand Down
17 changes: 15 additions & 2 deletions src/frontend/src/lib/i18n/zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"done": "完成",
"from": "",
"to": "",
"export": "Export"
"export": "Export",
"start": "Start",
"stop": "Stop"
},
"canisters": {
"top_up": "充值",
Expand Down Expand Up @@ -80,7 +82,15 @@
"create_satellite_price": "为了创建新的 satellite 需要 <strong>{0}</strong> ICP. 你当前 mission control 余额是 {1} ICP 而且你还剩 {2} 积分.",
"loading_satellites": "加载你的satellites",
"overview": "概要",
"id": "Satellite ID"
"id": "Satellite ID",
"stop_title": "Stop satellite",
"stop_info": "Do you want to stop your satellite?",
"stop_explanation": "Please note that a stopped satellite continues to consume cycles for the memory it occupies, including any remaining outstanding responses, until the cycle balance reaches 0. After that, it will be deleted.",
"stop_error": "Additionally, be aware that stopping the satellite may result in error messages being displayed in the console.",
"stop_success": "Satellite stopped.",
"start_tile": "Restart satellite",
"start_info": "Do you want to resume your satellite?",
"start_success": "Satellite resumed."
},
"mission_control": {
"title": "Mission Control",
Expand Down Expand Up @@ -194,6 +204,7 @@
"profile_placeholder": "可选关于控制者提示"
},
"errors": {
"no_identity": "Unexpected error. No identity provided.",
"no_mission_control": "Mission 控制中心没有初始化.",
"cli_missing_params": "URL参数缺失,重定向URL或者 principal没有提供.",
"cli_missing_selection": "没选择 mission control 或 satellite(s) .",
Expand All @@ -203,6 +214,8 @@
"satellite_no_found": "Satellite没找到,返回 <a href=\"/\">开始</a>去找你的 satellites.",
"satellite_name_update": "试图修改你的 satellite 名字出现异常.",
"satellite_missing_name": "必须提供一个名字.",
"satellite_stop": "Unexpected error(s) while trying to stop the satellite",
"satellite_start": "Unexpected error(s) while trying to start the satellite",
"ledger_balance_credits": "获取你的 mission control 余额和积分时出现未知错误.",
"load_credits": "Cannot load your current credits status.",
"hosting_missing_domain_name": "必须提供一个域名.",
Expand Down
13 changes: 13 additions & 0 deletions src/frontend/src/lib/types/i18n.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ interface I18nCore {
from: string;
to: string;
export: string;
start: string;
stop: string;
}

interface I18nCanisters {
Expand Down Expand Up @@ -87,6 +89,14 @@ interface I18nSatellites {
loading_satellites: string;
overview: string;
id: string;
stop_title: string;
stop_info: string;
stop_explanation: string;
stop_error: string;
stop_success: string;
start_tile: string;
start_info: string;
start_success: string;
}

interface I18nMission_control {
Expand Down Expand Up @@ -209,6 +219,7 @@ interface I18nCli {
}

interface I18nErrors {
no_identity: string;
no_mission_control: string;
cli_missing_params: string;
cli_missing_selection: string;
Expand All @@ -218,6 +229,8 @@ interface I18nErrors {
satellite_no_found: string;
satellite_name_update: string;
satellite_missing_name: string;
satellite_stop: string;
satellite_start: string;
ledger_balance_credits: string;
load_credits: string;
hosting_missing_domain_name: string;
Expand Down

0 comments on commit a2e6146

Please sign in to comment.