1
+ use anyhow:: Context ;
1
2
use mtma_environment_types:: { EnvironmentError , Environmentish } ;
2
- use mtma_migrator_types:: migrator:: MovementMigrator ;
3
+ use mtma_migrator_types:: migrator:: {
4
+ movement_migrator:: { LiveMigrator , Runner } ,
5
+ MovementMigrator ,
6
+ } ;
3
7
use std:: fmt:: Debug ;
8
+ use std:: path:: PathBuf ;
4
9
5
10
/// Errors thrown when using the [BoxEnvironment].
6
11
#[ derive( Debug , thiserror:: Error ) ]
@@ -15,16 +20,55 @@ impl From<BoxEnvironmentError> for EnvironmentError {
15
20
}
16
21
}
17
22
18
- pub struct BoxEnvironment { }
23
+ pub struct BoxEnvironment {
24
+ /// The rest api url of the box environment.
25
+ pub rest_api_url : String ,
26
+ /// The db dir of the box environment.
27
+ pub db_dir : PathBuf ,
28
+ /// Whether to isolate the box environment by snapshotting the movement runner and where to store the snapshot.
29
+ pub snapshot_dir : Option < PathBuf > ,
30
+ }
19
31
20
32
impl BoxEnvironment {
21
- pub fn new ( ) -> Self {
22
- Self { }
33
+ pub fn new ( rest_api_url : String , db_dir : PathBuf , snapshot_dir : Option < PathBuf > ) -> Self {
34
+ Self { rest_api_url , db_dir , snapshot_dir }
23
35
}
24
36
}
25
37
26
38
impl Environmentish for BoxEnvironment {
27
39
async fn build_movement_migrator ( & self ) -> Result < MovementMigrator , EnvironmentError > {
28
- Err ( BoxEnvironmentError :: Internal ( anyhow:: anyhow!( "not implemented" ) . into ( ) ) . into ( ) )
40
+ // construct the live migrator
41
+ let movement_migrator = MovementMigrator :: new ( Runner :: Live ( LiveMigrator :: new (
42
+ self . rest_api_url . clone ( ) ,
43
+ self . db_dir . clone ( ) ,
44
+ ) ) ) ;
45
+
46
+ // make sure the live migrator is ready
47
+ let movement_migrator_for_task = movement_migrator. clone ( ) ;
48
+ let migrator_task = kestrel:: task ( async move { movement_migrator_for_task. run ( ) . await } ) ;
49
+
50
+ // wait for the rest api to be ready
51
+ let _rest_api_url = movement_migrator
52
+ . wait_for_rest_api_url ( tokio:: time:: Duration :: from_secs ( 300 ) )
53
+ . await
54
+ . context ( "failed to wait for the rest api to be ready" )
55
+ . map_err ( |e| BoxEnvironmentError :: Internal ( e. into ( ) ) ) ?;
56
+
57
+ // end the migrator task
58
+ kestrel:: end!( migrator_task)
59
+ . context ( "failed to end the migrator task" )
60
+ . map_err ( |e| BoxEnvironmentError :: Internal ( e. into ( ) ) ) ?;
61
+
62
+ // if we're snapshotting the movement_migrator should be a snapshot of the live migrator
63
+ let movement_migrator = match & self . snapshot_dir {
64
+ Some ( snapshot_dir) => movement_migrator
65
+ . snapshot ( snapshot_dir. clone ( ) )
66
+ . await
67
+ . context ( "failed to snapshot the movement migrator" )
68
+ . map_err ( |e| BoxEnvironmentError :: Internal ( e. into ( ) ) ) ?,
69
+ None => movement_migrator,
70
+ } ;
71
+
72
+ Ok ( movement_migrator)
29
73
}
30
74
}
0 commit comments