Skip to content

Commit

Permalink
refactor(deployer): cache checking
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianbarbu committed Mar 12, 2024
1 parent 5c39f42 commit 7b31a10
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions deployer/src/deployment/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use shuttle_common::{
DEPLOYER_END_MSG_COMPLETED, DEPLOYER_END_MSG_CRASHED, DEPLOYER_END_MSG_STARTUP_ERR,
DEPLOYER_END_MSG_STOPPED, DEPLOYER_RUNTIME_START_FAILED, DEPLOYER_RUNTIME_START_RESPONSE,
},
resource::{self, ProvisionResourceRequest, ResourceInput},
resource::{self, ResourceInput, Type},
DatabaseResource, DbInput, SecretStore,
};
use shuttle_proto::{
Expand Down Expand Up @@ -414,30 +414,26 @@ async fn load(
}
}

fn log(ty: resource::Type, msg: &str) {
fn log(ty: &resource::Type, msg: &str) {
info!("[Resource][{}] {}", ty, msg);
}

/// If an old resource with matching type + config and valid data exists, return it
fn get_cached_output<T: DeserializeOwned>(
shuttle_resource: &ProvisionResourceRequest,
shuttle_resource_type: &Type,
config: &serde_json::Value,
prev_resources: &[resource::Response],
) -> Option<T> {
prev_resources
.iter()
.find(|resource| {
// TODO: verify only the config fields that are relevant for provisioning and resource caching.
// When provisioning currently we don't need any configs fields for existing Shuttle resources,
// so we can leave out the configs comparison.
resource.r#type == shuttle_resource.r#type // && resource.config == shuttle_resource.config
})
.find(|resource| resource.r#type == *shuttle_resource_type && resource.config == *config)
.and_then(|resource| {
let cached_output = resource.data.clone();
log(shuttle_resource.r#type, "Found cached output");
log(shuttle_resource_type, "Found cached output");
match serde_json::from_value::<T>(cached_output) {
Ok(output) => Some(output),
Err(_) => {
log(shuttle_resource.r#type, "Failed to validate cached output");
log(shuttle_resource_type, "Failed to validate cached output");
None
}
}
Expand Down Expand Up @@ -504,12 +500,15 @@ async fn provision(
// no config fields are used yet, but verify the format anyways
let config: DbInput = serde_json::from_value(shuttle_resource.config.clone())
.context("deserializing resource config")?;

let output = get_cached_output(&shuttle_resource, prev_resources.as_slice());
// We pass a Null config right now because this is relevant only for updating the resources
// through the provisioner, which is something we don't support currently. If there will be
// config fields that are relevant for provisioner updates on top of resources, they should
// be cached.
let output = get_cached_output(&shuttle_resource.r#type, &serde_json::Value::Null, prev_resources.as_slice());
let output = match output {
Some(o) => o,
None => {
log(shuttle_resource.r#type, "Provisioning...");
log(&shuttle_resource.r#type, "Provisioning...");
// ###
let mut req = Request::new(DatabaseRequest {
project_name: project_name.to_string(),
Expand Down

0 comments on commit 7b31a10

Please sign in to comment.