Skip to content

Commit 8d33063

Browse files
committed
chore: snapshotting transition.
1 parent 471acc6 commit 8d33063

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
lines changed

environments/core/box/src/environment/config.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::BoxEnvironment;
22
use clap::Parser;
33
use serde::{Deserialize, Serialize};
44
use std::fmt::Debug;
5+
use std::path::PathBuf;
56

67
/// Errors thrown when working with the [Config].
78
#[derive(Debug, thiserror::Error)]
@@ -16,13 +17,24 @@ pub enum EnvironmentConfigError {
1617
/// This is the frontend for the core API.
1718
#[derive(Parser, Debug, Serialize, Deserialize, Clone, Default)]
1819
#[clap(help_expected = true)]
19-
pub struct Config {}
20+
pub struct Config {
21+
/// The rest api url of the box environment.
22+
pub rest_api_url: String,
23+
/// The db dir of the box environment.
24+
pub db_dir: PathBuf,
25+
/// Whether to isolate the box environment by snapshotting the movement runner and where to store the snapshot.
26+
pub snapshot_dir: Option<PathBuf>,
27+
}
2028

2129
impl Config {
2230
/// Builds the [BoxEnvironment] struct from the config.
2331
///
2432
/// Note: preserving faillibility here because we may add debug path configuration to the [BoxEnvironment] soon.
2533
pub fn build(&self) -> Result<BoxEnvironment, EnvironmentConfigError> {
26-
Ok(BoxEnvironment::new())
34+
Ok(BoxEnvironment::new(
35+
self.rest_api_url.clone(),
36+
self.db_dir.clone(),
37+
self.snapshot_dir.clone(),
38+
))
2739
}
2840
}

environments/core/box/src/environment/environment.rs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
use anyhow::Context;
12
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+
};
37
use std::fmt::Debug;
8+
use std::path::PathBuf;
49

510
/// Errors thrown when using the [BoxEnvironment].
611
#[derive(Debug, thiserror::Error)]
@@ -15,16 +20,55 @@ impl From<BoxEnvironmentError> for EnvironmentError {
1520
}
1621
}
1722

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+
}
1931

2032
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 }
2335
}
2436
}
2537

2638
impl Environmentish for BoxEnvironment {
2739
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)
2973
}
3074
}

migration/cli/migrate-node/docs/cli/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,13 @@ Options:
140140
**Selection (2/4):** `environment-box`
141141
The config for the [BoxEnvironment]
142142

143-
Usage: --environment-box.*
143+
Usage: --environment-box.* <REST_API_URL> <DB_DIR> [SNAPSHOT_DIR]
144+
145+
Arguments:
146+
<REST_API_URL> The rest api url of the box environment
147+
<DB_DIR> The db dir of the box environment
148+
[SNAPSHOT_DIR] Whether to isolate the box environment by snapshotting the movement runner and where to store the
149+
snapshot
144150

145151
Options:
146152
-h, --help Print help (see more with '--help')

0 commit comments

Comments
 (0)