Skip to content

Commit 9fd97a8

Browse files
committed
feat(sdk): rework deploy command with new scheme
1 parent 0e920dd commit 9fd97a8

File tree

9 files changed

+134
-206
lines changed

9 files changed

+134
-206
lines changed

meta-cli/src/cli/deploy.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ mod default_mode {
204204
let secrets =
205205
lade_sdk::hydrate(deploy.node.env.clone(), deploy.base_dir.to_path_buf()).await?;
206206

207+
ServerStore::set_secrets(secrets);
208+
207209
let (loader_event_tx, loader_event_rx) = mpsc::unbounded_channel();
208210

209211
let loader = LoaderActor::new(
@@ -215,8 +217,7 @@ mod default_mode {
215217
.auto_stop()
216218
.start();
217219

218-
let pusher =
219-
PushManagerBuilder::new(console.clone()).start(deploy.node.clone(), secrets);
220+
let pusher = PushManagerBuilder::new(console.clone()).start();
220221

221222
Ok(Self {
222223
deploy,
@@ -347,7 +348,7 @@ mod watch_mode {
347348

348349
let pusher = PushManagerBuilder::new(console.clone())
349350
.linear_backoff(Duration::from_secs(5), 3)
350-
.start(deploy.node.clone(), secrets);
351+
.start();
351352

352353
let actor_system = ActorSystem {
353354
console: console.clone(),

meta-cli/src/cli/serialize.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22
// SPDX-License-Identifier: MPL-2.0
33

44
use super::{Action, GenArgs};
5-
use crate::com::responses::SDKResponse;
6-
use crate::com::server::response;
75
use crate::com::store::{Command, ServerStore};
86
use crate::config::Config;
97
use crate::deploy::actors::console::ConsoleActor;
10-
use crate::deploy::actors::loader::{
11-
LoadModule, LoaderActor, LoaderEvent, PostProcessOptions, StopBehavior,
12-
};
13-
use crate::typegraph::loader::TypegraphInfos;
8+
use crate::deploy::actors::loader::{LoadModule, LoaderActor, LoaderEvent, StopBehavior};
149
use actix::prelude::*;
1510
use anyhow::{bail, Context, Result};
1611
use async_trait::async_trait;
@@ -98,16 +93,7 @@ impl Action for Serialize {
9893
while let Some(event) = event_rx.recv().await {
9994
match event {
10095
LoaderEvent::Typegraph(tg_infos) => {
101-
let tg = ServerStore::get_response(&tg_infos.path)
102-
.ok_or_else(|| -> Result<Arc<SDKResponse>> {
103-
bail!(
104-
"Invalid state, no response was sent by {:?}",
105-
&tg_infos.path
106-
)
107-
})
108-
.unwrap()
109-
.as_typegraph()?;
110-
96+
let tg = ServerStore::get_response_or_fail(&tg_infos.path)?.as_typegraph()?;
11197
loaded.push(tg)
11298
}
11399
LoaderEvent::Stopped(b) => {

meta-cli/src/codegen/deno.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ mod tests {
473473
use super::*;
474474
use crate::config::Config;
475475
use crate::deploy::actors::console::ConsoleActor;
476-
use crate::deploy::actors::loader::{LoadModule, LoaderActor, LoaderEvent, PostProcessOptions};
476+
use crate::deploy::actors::loader::{LoadModule, LoaderActor, LoaderEvent};
477477
use crate::tests::utils::ensure_venv;
478478
use actix::prelude::*;
479479

@@ -501,10 +501,10 @@ mod tests {
501501
));
502502

503503
let mut event_rx = event_rx;
504-
let tg = match event_rx.recv().await.unwrap() {
505-
LoaderEvent::Typegraph(tg) => tg,
506-
evt => bail!("unexpected loader evt: {evt:?}"),
507-
};
504+
// let tg = match event_rx.recv().await.unwrap() {
505+
// LoaderEvent::Typegraph(tg) => tg,
506+
// evt => bail!("unexpected loader evt: {evt:?}"),
507+
// };
508508

509509
// TODO:
510510
// run typegraph! thenget serialized version

meta-cli/src/com/responses.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
2+
// SPDX-License-Identifier: MPL-2.0
3+
14
use super::store::Command;
25
use crate::deploy::actors::pusher::PushResultRaw;
36
use anyhow::{bail, Result};
@@ -52,9 +55,9 @@ impl SDKResponse {
5255
}
5356

5457
pub fn as_push_result(&self) -> Result<PushResultRaw> {
55-
match &self.data {
56-
Some(value) => serde_json::from_value(value.to_owned()).map_err(|e| e.into()),
57-
None => todo!(),
58-
}
58+
self.validate()?;
59+
let response: common::graphql::Response =
60+
serde_json::from_value(self.data.clone().unwrap())?;
61+
response.data("addTypegraph")
5962
}
6063
}

meta-cli/src/com/store.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: MPL-2.0
33

44
use crate::config::Config;
5+
use anyhow::{bail, Result};
56
use common::node::BasicAuth;
67
use lazy_static::lazy_static;
78
use serde::{Deserialize, Serialize};
@@ -107,6 +108,13 @@ impl ServerStore {
107108
with_store(|s| s.sdk_responses.get(tg_path).map(|v| v.to_owned()))
108109
}
109110

111+
pub fn get_response_or_fail(tg_path: &PathBuf) -> Result<Arc<SDKResponse>> {
112+
match Self::get_response(tg_path) {
113+
Some(res) => Ok(res.to_owned()),
114+
None => bail!("Invalid state, no response was sent by {:?}", &tg_path),
115+
}
116+
}
117+
110118
pub fn get_responses() -> HashMap<PathBuf, Arc<SDKResponse>> {
111119
with_store(|s| s.sdk_responses.clone())
112120
}

meta-cli/src/deploy/actors/push_manager.rs

Lines changed: 60 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ use actix::prelude::*;
1313
use anyhow::Result;
1414
use async_trait::async_trait;
1515
use colored::Colorize;
16-
use common::typegraph::Typegraph;
1716
use tokio::sync::oneshot;
1817

19-
use common::node::Node;
18+
use crate::com::store::ServerStore;
19+
use crate::typegraph::loader::TypegraphInfos;
2020

2121
use super::console::{
2222
input::{Confirm, ConfirmHandler, Select, SelectOption},
@@ -50,10 +50,10 @@ impl PushManagerBuilder {
5050
self
5151
}
5252

53-
pub fn start(self, node: Node, secrets: HashMap<String, String>) -> Addr<PushManagerActor> {
53+
pub fn start(self) -> Addr<PushManagerActor> {
5454
PushManagerActor::create(|ctx| {
5555
let addr = ctx.address();
56-
let pusher = PusherActor::new(self.console.clone(), node, secrets, addr).start();
56+
let pusher = PusherActor::new(self.console.clone(), addr).start();
5757

5858
PushManagerActor {
5959
console: self.console,
@@ -108,10 +108,13 @@ impl PushManagerActor {
108108
}
109109
}
110110

111-
fn add_active(&mut self, tg: &Typegraph) -> bool {
111+
fn add_active(&mut self, tg_infos: &TypegraphInfos) -> bool {
112112
let console = self.console.clone();
113-
let path = tg.path.clone().unwrap();
114-
let name = tg.name().unwrap();
113+
let path = tg_infos.path.clone();
114+
let name = ServerStore::get_response_or_fail(&path)
115+
.unwrap()
116+
.typegraph_name
117+
.clone();
115118

116119
if !self.ensure_path(&name, &path) {
117120
return false;
@@ -144,10 +147,13 @@ impl PushManagerActor {
144147
}
145148
}
146149

147-
fn remove_active(&mut self, tg: &Typegraph) -> Option<CancelationStatus> {
150+
fn remove_active(&mut self, tg_infos: &TypegraphInfos) -> Option<CancelationStatus> {
148151
let console = self.console.clone();
149-
let path = tg.path.clone().unwrap();
150-
let name = tg.name().unwrap();
152+
let path = tg_infos.path.clone();
153+
let name = ServerStore::get_response_or_fail(&path)
154+
.unwrap()
155+
.typegraph_name
156+
.clone();
151157

152158
if !self.ensure_path(&name, &path) {
153159
return None;
@@ -225,28 +231,6 @@ impl PushManagerActor {
225231
}
226232
}
227233
}
228-
229-
// pub fn apply_options(&mut self, push: Push) -> Result<Push> {
230-
// let typegraph_key = push.typegraph.get_key()?;
231-
// if let Some(options) = self.one_time_push_options.remove(&typegraph_key) {
232-
// let mut push = push;
233-
// for option in options {
234-
// match option {
235-
// OneTimePushOption::ForceReset { runtime_name } => {
236-
// let mut typegraph = (*push.typegraph).clone();
237-
// EmbeddedPrismaMigrationOptionsPatch::default()
238-
// .reset_on_drift(true)
239-
// .apply(&mut typegraph, vec![runtime_name])
240-
// .unwrap();
241-
// push.typegraph = typegraph.into();
242-
// }
243-
// }
244-
// }
245-
// Ok(push)
246-
// } else {
247-
// Ok(push)
248-
// }
249-
// }
250234
}
251235

252236
impl Actor for PushManagerActor {
@@ -265,15 +249,14 @@ impl Handler<Push> for PushManagerActor {
265249
type Result = ();
266250

267251
fn handle(&mut self, push: Push, _ctx: &mut Self::Context) -> Self::Result {
268-
// if self.add_active(&push.typegraph) {
269-
// // let push = self.apply_options(push).unwrap(); // TODO error handling
270-
271-
// self.pusher.do_send(push);
272-
// } else {
273-
// let tg_name = push.typegraph.name().unwrap().cyan();
274-
// self.console
275-
// .warning(format!("Typegraph {tg_name} was not pushed."));
276-
// }
252+
if self.add_active(&push.typegraph) {
253+
self.pusher.do_send(push);
254+
} else {
255+
let response = push.typegraph.get_response_or_fail().unwrap();
256+
let tg_name = response.typegraph_name.cyan();
257+
self.console
258+
.warning(format!("Typegraph {tg_name} was not pushed."));
259+
}
277260
}
278261
}
279262

@@ -346,46 +329,47 @@ impl Handler<PushFinished> for PushManagerActor {
346329
type Result = ();
347330

348331
fn handle(&mut self, msg: PushFinished, ctx: &mut Self::Context) -> Self::Result {
349-
350332
// TODO
351333
// NOTE: https://github.com/metatypedev/metatype/commit/169e5f402ea04372a23deaf0a44fbaa45a7cf0b7
352334

353335
// let name = msg.push.typegraph.name().unwrap();
336+
let response = ServerStore::get_response_or_fail(&msg.push.typegraph.path).unwrap();
337+
let name = response.typegraph_name.clone();
338+
339+
if msg.success {
340+
self.console.info(format!(
341+
"{} Successfully pushed typegraph {name}.",
342+
"✓".green(),
343+
name = name.cyan()
344+
));
345+
}
346+
if !self.failed_push_exists {
347+
self.failed_push_exists = !msg.success;
348+
}
354349

355-
// if msg.success {
356-
// self.console.info(format!(
357-
// "{} Successfully pushed typegraph {name}.",
358-
// "✓".green(),
359-
// name = name.cyan()
360-
// ));
361-
// }
362-
// if !self.failed_push_exists {
363-
// self.failed_push_exists = !msg.success;
364-
// }
365-
366-
// let res = self.remove_active(&msg.push.typegraph);
367-
// let Some(CancelationStatus(is_cancelled)) = res else {
368-
// return;
369-
// };
370-
371-
// if let Some(follow_up) = msg.follow_up {
372-
// use PushFollowUp as F;
373-
// match follow_up {
374-
// F::ScheduleRetry => {
375-
// if !is_cancelled {
376-
// self.schedule_retry(msg.push, ctx.address());
377-
// }
378-
// }
379-
// F::Interact(interaction) => {
380-
// if !is_cancelled {
381-
// let console = self.console.clone();
382-
// Arbiter::current().spawn(async move {
383-
// Self::interact(console, interaction).await;
384-
// });
385-
// }
386-
// }
387-
// }
388-
// }
350+
let res = self.remove_active(&msg.push.typegraph);
351+
let Some(CancelationStatus(is_cancelled)) = res else {
352+
return;
353+
};
354+
355+
if let Some(follow_up) = msg.follow_up {
356+
use PushFollowUp as F;
357+
match follow_up {
358+
F::ScheduleRetry => {
359+
if !is_cancelled {
360+
self.schedule_retry(msg.push, ctx.address());
361+
}
362+
}
363+
F::Interact(interaction) => {
364+
if !is_cancelled {
365+
let console = self.console.clone();
366+
Arbiter::current().spawn(async move {
367+
Self::interact(console, interaction).await;
368+
});
369+
}
370+
}
371+
}
372+
}
389373
}
390374
}
391375

0 commit comments

Comments
 (0)