1- use std:: path:: Path ;
1+ use std:: { path:: Path , time :: Duration } ;
22
33use anyhow:: { anyhow, Context , Result } ;
44use app:: App ;
@@ -16,6 +16,7 @@ use rocket::{
1616use rocket_apitoken:: ApiToken ;
1717use rocket_vsock_listener:: VsockListener ;
1818use supervisor_client:: SupervisorClient ;
19+ use tracing:: { error, info} ;
1920
2021mod app;
2122mod config;
@@ -102,6 +103,22 @@ async fn run_host_api(app: App, figment: Figment) -> Result<()> {
102103 Ok ( ( ) )
103104}
104105
106+ async fn auto_restart_task ( app : App ) {
107+ if !app. config . cvm . auto_restart . enabled {
108+ info ! ( "Auto restart CVMs is disabled" ) ;
109+ return ;
110+ }
111+ let mut interval =
112+ tokio:: time:: interval ( Duration :: from_secs ( app. config . cvm . auto_restart . interval ) ) ;
113+ loop {
114+ info ! ( "Checking for exited VMs" ) ;
115+ if let Err ( err) = app. try_restart_exited_vms ( ) . await {
116+ error ! ( "Failed to restart exited VMs: {err:?}" ) ;
117+ }
118+ interval. tick ( ) . await ;
119+ }
120+ }
121+
105122#[ rocket:: main]
106123async fn main ( ) -> Result < ( ) > {
107124 {
@@ -130,6 +147,7 @@ async fn main() -> Result<()> {
130147 } ;
131148 let state = app:: App :: new ( config, supervisor) ;
132149 state. reload_vms ( ) . await . context ( "Failed to reload VMs" ) ?;
150+ tokio:: spawn ( auto_restart_task ( state. clone ( ) ) ) ;
133151
134152 tokio:: select! {
135153 result = run_external_api( state. clone( ) , figment. clone( ) , api_auth) => {
0 commit comments