V1 of laucher
This commit is contained in:
+18
-4
@@ -220,16 +220,25 @@ pub async fn hello(dhp: &Dhp<'_>) -> Result<(String, String), SyncError> {
|
||||
}
|
||||
|
||||
/// Push this PC's edits; then, when `pull`, apply everything new on the DHP.
|
||||
/// Postgres must be running and the local Engine API stopped.
|
||||
pub async fn run(pg: &Pg, dhp: &Dhp<'_>, install_id: &str, pull: bool) -> Result<Report, SyncError> {
|
||||
/// Postgres must be running and the local Engine API stopped. `version` is reported
|
||||
/// so the DHP's install list can say which version each PC is on.
|
||||
pub async fn run(pg: &Pg, dhp: &Dhp<'_>, install_id: &str, version: &str, pull: bool) -> Result<Report, SyncError> {
|
||||
let mut report = Report::default();
|
||||
// Say which DHP database these edits were made against. A PC carried over from another
|
||||
// one (a test copy, say) would otherwise apply its edits, deletions included, here.
|
||||
let joined = blocking(pg, joined_db_id).await?.unwrap_or_default();
|
||||
loop {
|
||||
let items = blocking(pg, |pg| query(pg, &format!("SELECT sync.outbox({PUSH_BATCH})"))).await?;
|
||||
let count = items.as_array().map_or(0, Vec::len);
|
||||
if count == 0 {
|
||||
break;
|
||||
}
|
||||
let answer = call(dhp, "/v1/sync/push", Some(&json!({ "install_id": install_id, "changes": items }))).await?;
|
||||
let answer = call(
|
||||
dhp,
|
||||
"/v1/sync/push",
|
||||
Some(&json!({ "install_id": install_id, "version": version, "db_id": joined, "changes": items })),
|
||||
)
|
||||
.await?;
|
||||
let recorded =
|
||||
blocking(pg, move |pg| query(pg, &format!("SELECT sync.pushed({}, {})", jsonb(&items), jsonb(&answer)))).await?;
|
||||
let accepted = recorded.get("accepted").and_then(Value::as_u64).unwrap_or(0);
|
||||
@@ -247,7 +256,12 @@ pub async fn run(pg: &Pg, dhp: &Dhp<'_>, install_id: &str, pull: bool) -> Result
|
||||
let mut cursor = json!({ "since": since });
|
||||
let mut changes: Vec<Value> = Vec::new();
|
||||
let db_id = loop {
|
||||
let page = call(dhp, "/v1/sync/pull", Some(&json!({ "cursor": cursor, "limit": PULL_PAGE }))).await?;
|
||||
let page = call(
|
||||
dhp,
|
||||
"/v1/sync/pull",
|
||||
Some(&json!({ "cursor": cursor, "limit": PULL_PAGE, "install_id": install_id, "version": version })),
|
||||
)
|
||||
.await?;
|
||||
if let Some(list) = page.get("changes").and_then(Value::as_array) {
|
||||
changes.extend(list.iter().cloned());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user