forked from coracle/caravel
Avoid duplicate syncs
This commit is contained in:
@@ -520,6 +520,7 @@ async fn create_relay(
|
||||
blossom_enabled: payload.blossom_enabled.unwrap_or(0),
|
||||
livekit_enabled: payload.livekit_enabled.unwrap_or(0),
|
||||
push_enabled: payload.push_enabled.unwrap_or(1),
|
||||
synced: 0,
|
||||
};
|
||||
|
||||
relay = match state.api.prepare_relay(relay) {
|
||||
@@ -902,6 +903,7 @@ mod tests {
|
||||
blossom_enabled: 0,
|
||||
livekit_enabled: 0,
|
||||
push_enabled: 1,
|
||||
synced: 0,
|
||||
};
|
||||
repo.create_relay(&relay).await.expect("create relay");
|
||||
}
|
||||
|
||||
+13
-9
@@ -54,20 +54,24 @@ impl Infra {
|
||||
let activity = self.repo.list_activity(&since).await?;
|
||||
|
||||
for a in activity {
|
||||
let sync_type = match a.activity_type.as_str() {
|
||||
"create_relay" => Some(true),
|
||||
"update_relay" | "deactivate_relay" => Some(false),
|
||||
_ => None,
|
||||
};
|
||||
let needs_sync = matches!(
|
||||
a.activity_type.as_str(),
|
||||
"create_relay" | "update_relay" | "deactivate_relay"
|
||||
);
|
||||
|
||||
if let Some(is_new) = sync_type {
|
||||
if needs_sync {
|
||||
let Some(relay) = self.repo.get_relay(&a.resource_id).await? else {
|
||||
continue;
|
||||
};
|
||||
|
||||
if let Err(e) = self.sync_relay(&relay, is_new).await {
|
||||
tracing::warn!(relay = %relay.id, error = %e, "relay sync failed");
|
||||
self.repo.fail_relay_sync(&relay, e.to_string()).await?;
|
||||
let is_new = relay.synced == 0;
|
||||
|
||||
match self.sync_relay(&relay, is_new).await {
|
||||
Ok(()) => self.repo.mark_relay_synced(&relay.id).await?,
|
||||
Err(e) => {
|
||||
tracing::warn!(relay = %relay.id, error = %e, "relay sync failed");
|
||||
self.repo.fail_relay_sync(&relay, e.to_string()).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,6 +48,8 @@ pub struct Relay {
|
||||
pub blossom_enabled: i64,
|
||||
pub livekit_enabled: i64,
|
||||
pub push_enabled: i64,
|
||||
#[serde(skip_serializing)]
|
||||
pub synced: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, sqlx::FromRow)]
|
||||
|
||||
+30
-22
@@ -162,13 +162,13 @@ impl Repo {
|
||||
|
||||
pub async fn list_relays(&self) -> Result<Vec<Relay>> {
|
||||
let rows = sqlx::query_as::<_, Relay>(
|
||||
"SELECT id, tenant, schema, subdomain, plan, status, sync_error,
|
||||
info_name, info_icon, info_description,
|
||||
policy_public_join, policy_strip_signatures,
|
||||
groups_enabled, management_enabled, blossom_enabled,
|
||||
livekit_enabled, push_enabled
|
||||
FROM relay
|
||||
ORDER BY id",
|
||||
"SELECT id, tenant, schema, subdomain, plan, status, sync_error,
|
||||
info_name, info_icon, info_description,
|
||||
policy_public_join, policy_strip_signatures,
|
||||
groups_enabled, management_enabled, blossom_enabled,
|
||||
livekit_enabled, push_enabled, synced
|
||||
FROM relay
|
||||
ORDER BY id",
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await?;
|
||||
@@ -177,14 +177,14 @@ impl Repo {
|
||||
|
||||
pub async fn list_relays_for_tenant(&self, tenant_id: &str) -> Result<Vec<Relay>> {
|
||||
let rows = sqlx::query_as::<_, Relay>(
|
||||
"SELECT id, tenant, schema, subdomain, plan, status, sync_error,
|
||||
info_name, info_icon, info_description,
|
||||
policy_public_join, policy_strip_signatures,
|
||||
groups_enabled, management_enabled, blossom_enabled,
|
||||
livekit_enabled, push_enabled
|
||||
FROM relay
|
||||
WHERE tenant = ?
|
||||
ORDER BY id",
|
||||
"SELECT id, tenant, schema, subdomain, plan, status, sync_error,
|
||||
info_name, info_icon, info_description,
|
||||
policy_public_join, policy_strip_signatures,
|
||||
groups_enabled, management_enabled, blossom_enabled,
|
||||
livekit_enabled, push_enabled, synced
|
||||
FROM relay
|
||||
WHERE tenant = ?
|
||||
ORDER BY id",
|
||||
)
|
||||
.bind(tenant_id)
|
||||
.fetch_all(&self.pool)
|
||||
@@ -194,13 +194,13 @@ impl Repo {
|
||||
|
||||
pub async fn get_relay(&self, id: &str) -> Result<Option<Relay>> {
|
||||
let row = sqlx::query_as::<_, Relay>(
|
||||
"SELECT id, tenant, schema, subdomain, plan, status, sync_error,
|
||||
info_name, info_icon, info_description,
|
||||
policy_public_join, policy_strip_signatures,
|
||||
groups_enabled, management_enabled, blossom_enabled,
|
||||
livekit_enabled, push_enabled
|
||||
FROM relay
|
||||
WHERE id = ?",
|
||||
"SELECT id, tenant, schema, subdomain, plan, status, sync_error,
|
||||
info_name, info_icon, info_description,
|
||||
policy_public_join, policy_strip_signatures,
|
||||
groups_enabled, management_enabled, blossom_enabled,
|
||||
livekit_enabled, push_enabled, synced
|
||||
FROM relay
|
||||
WHERE id = ?",
|
||||
)
|
||||
.bind(id)
|
||||
.fetch_optional(&self.pool)
|
||||
@@ -312,6 +312,14 @@ impl Repo {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn mark_relay_synced(&self, relay_id: &str) -> Result<()> {
|
||||
sqlx::query("UPDATE relay SET synced = 1, status = 'active', sync_error = '' WHERE id = ?")
|
||||
.bind(relay_id)
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn create_invoice(
|
||||
&self,
|
||||
invoice: &Invoice,
|
||||
|
||||
Reference in New Issue
Block a user