@@ -1169,9 +1169,9 @@ impl Conductor {
1169
1169
let ws = entities:: workspace:: ActiveModel {
1170
1170
id : ActiveValue :: Set ( workspace_id) ,
1171
1171
deleted_at : ActiveValue :: Set ( None ) ,
1172
- updated_at : ActiveValue :: Set ( None ) ,
1173
1172
name : ActiveValue :: Set ( name. clone ( ) ) ,
1174
1173
created_at : ActiveValue :: Set ( now. into ( ) ) ,
1174
+ updated_at : ActiveValue :: Set ( Some ( now. into ( ) ) ) ,
1175
1175
status : ActiveValue :: Set ( WorkspaceStatus :: New . to_string ( ) ) ,
1176
1176
repo_url : ActiveValue :: Set ( repo. url . clone ( ) ) ,
1177
1177
repo_name : ActiveValue :: Set ( repo. name . clone ( ) ) ,
@@ -2059,15 +2059,27 @@ impl Conductor {
2059
2059
. do_create_workspace ( & user, & ws, repo, & machine_type, ip, user_agent)
2060
2060
. await
2061
2061
{
2062
- let err = if let ApiError :: InternalError ( e) = e {
2063
- e
2064
- } else {
2065
- e. to_string ( )
2066
- } ;
2067
- tracing:: error!( "create workspace failed: {err}" ) ;
2068
- let _ = conductor
2069
- . update_workspace_status ( & ws, WorkspaceStatus :: Failed )
2070
- . await ;
2062
+ tracing:: error!( "create workspace failed: {e:?}" ) ;
2063
+ if let Ok ( ws) = conductor. db . get_workspace ( ws. id ) . await {
2064
+ if let Some ( usage_id) = ws. usage_id {
2065
+ let now = Utc :: now ( ) ;
2066
+ if let Ok ( txn) = conductor. db . conn . begin ( ) . await {
2067
+ let _ = conductor
2068
+ . enterprise
2069
+ . usage
2070
+ . end_usage ( & txn, usage_id, now. into ( ) )
2071
+ . await ;
2072
+ let _ = txn. commit ( ) . await ;
2073
+ }
2074
+ }
2075
+ }
2076
+ let _ = entities:: workspace:: ActiveModel {
2077
+ id : ActiveValue :: Set ( ws. id ) ,
2078
+ status : ActiveValue :: Set ( WorkspaceStatus :: Failed . to_string ( ) ) ,
2079
+ ..Default :: default ( )
2080
+ }
2081
+ . update ( & conductor. db . conn )
2082
+ . await ;
2071
2083
}
2072
2084
} ) ;
2073
2085
@@ -2189,7 +2201,7 @@ impl Conductor {
2189
2201
2190
2202
pub async fn delete_workspace (
2191
2203
& self ,
2192
- workspace : entities:: workspace:: Model ,
2204
+ workspace : & entities:: workspace:: Model ,
2193
2205
ip : Option < String > ,
2194
2206
user_agent : Option < String > ,
2195
2207
) -> Result < ( ) , ApiError > {
@@ -2235,6 +2247,7 @@ impl Conductor {
2235
2247
let update_ws = entities:: workspace:: ActiveModel {
2236
2248
id : ActiveValue :: Set ( workspace. id ) ,
2237
2249
status : ActiveValue :: Set ( WorkspaceStatus :: Deleting . to_string ( ) ) ,
2250
+ updated_at : ActiveValue :: Set ( Some ( now. into ( ) ) ) ,
2238
2251
usage_id : ActiveValue :: Set ( None ) ,
2239
2252
..Default :: default ( )
2240
2253
} ;
@@ -2332,6 +2345,7 @@ impl Conductor {
2332
2345
id : ActiveValue :: Set ( ws. id ) ,
2333
2346
status : ActiveValue :: Set ( status. to_string ( ) ) ,
2334
2347
deleted_at : ActiveValue :: Set ( Some ( now. into ( ) ) ) ,
2348
+ updated_at : ActiveValue :: Set ( Some ( now. into ( ) ) ) ,
2335
2349
..Default :: default ( )
2336
2350
}
2337
2351
. update ( & txn)
@@ -2375,6 +2389,7 @@ impl Conductor {
2375
2389
entities:: workspace:: ActiveModel {
2376
2390
id : ActiveValue :: Set ( ws. id ) ,
2377
2391
status : ActiveValue :: Set ( status. to_string ( ) ) ,
2392
+ updated_at : ActiveValue :: Set ( Some ( now. into ( ) ) ) ,
2378
2393
..Default :: default ( )
2379
2394
}
2380
2395
. update ( & self . db . conn )
@@ -3330,6 +3345,32 @@ impl Conductor {
3330
3345
. await ?;
3331
3346
Ok ( ( ) )
3332
3347
}
3348
+
3349
+ pub async fn auto_delete_inactive_workspaces_on_host (
3350
+ & self ,
3351
+ host_id : Uuid ,
3352
+ ) -> Result < ( ) , ApiError > {
3353
+ let workspaces = self
3354
+ . db
3355
+ . get_inactive_workspaces_on_host (
3356
+ host_id,
3357
+ ( Utc :: now ( ) - Duration :: from_secs ( 14 * 24 * 60 * 60 ) ) . into ( ) ,
3358
+ )
3359
+ . await ?;
3360
+ for ws in workspaces {
3361
+ if ws. compose_parent . is_none ( ) {
3362
+ tracing:: info!(
3363
+ "now delete ws {} due to inactivity, last updated at {:?}" ,
3364
+ ws. name,
3365
+ ws. updated_at
3366
+ ) ;
3367
+ if let Err ( e) = self . delete_workspace ( & ws, None , None ) . await {
3368
+ tracing:: info!( "delete inactive ws {} error: {e:?}" , ws. name) ;
3369
+ }
3370
+ }
3371
+ }
3372
+ Ok ( ( ) )
3373
+ }
3333
3374
}
3334
3375
3335
3376
pub fn encode_pkcs8_pem ( key : & KeyPair ) -> Result < String > {
0 commit comments