Skip to content

Commit

Permalink
fix: do orama upsert in the background (jsr-io#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato authored Mar 1, 2024
1 parent 7622f77 commit 0ece70b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 45 deletions.
10 changes: 4 additions & 6 deletions api/src/api/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,7 @@ pub async fn create_handler(mut req: Request<Body>) -> ApiResult<ApiPackage> {

let orama_client = req.data::<Option<OramaClient>>().unwrap();
if let Some(orama_client) = orama_client {
orama_client
.upsert_package(&package, &Default::default())
.await?;
orama_client.upsert_package(&package, &Default::default());
}

Ok(ApiPackage::from((package, None, Default::default())))
Expand Down Expand Up @@ -370,7 +368,7 @@ pub async fn update_handler(mut req: Request<Body>) -> ApiResult<ApiPackage> {
.update_package_runtime_compat(&scope, &package, &runtime_compat)
.await?;
if let Some(orama_client) = orama_client {
orama_client.upsert_package(&package, &meta).await?;
orama_client.upsert_package(&package, &meta);
}
Ok(ApiPackage::from((package, repo, meta)))
}
Expand All @@ -379,7 +377,7 @@ pub async fn update_handler(mut req: Request<Body>) -> ApiResult<ApiPackage> {
.update_package_is_featured(&scope, &package, is_featured)
.await?;
if let Some(orama_client) = orama_client {
orama_client.upsert_package(&package, &meta).await?;
orama_client.upsert_package(&package, &meta);
}
Ok(ApiPackage::from((package, repo, meta)))
}
Expand Down Expand Up @@ -419,7 +417,7 @@ async fn update_description(
.await?;

if let Some(orama_client) = orama_client {
orama_client.upsert_package(&package, &meta).await?;
orama_client.upsert_package(&package, &meta);
}

let npm_version_manifest_path =
Expand Down
87 changes: 48 additions & 39 deletions api/src/orama.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
// Copyright 2024 the JSR authors. All rights reserved. MIT license.
// Copyright Deno Land Inc. All Rights Reserved. Proprietary and confidential.

use std::sync::Arc;

use crate::api::ApiPackageScore;
use crate::db::Package;
use crate::db::PackageVersionMeta;
use crate::ids::PackageName;
use crate::ids::ScopeName;
use crate::util::USER_AGENT;
use tracing::error;
use tracing::instrument;
use tracing::Instrument;
use tracing::Span;

#[derive(Clone)]
pub struct OramaClient {
private_api_key: String,
index_id: String,
private_api_key: Arc<str>,
index_id: Arc<str>,
}

impl OramaClient {
pub fn new(private_api_key: String, index_id: String) -> Self {
Self {
private_api_key,
index_id,
private_api_key: private_api_key.into(),
index_id: index_id.into(),
}
}

Expand All @@ -38,47 +44,50 @@ impl OramaClient {
Ok(response)
}

#[instrument(name = "OramaClient::upsert_package", skip(self), err)]
pub async fn upsert_package(
&self,
package: &Package,
meta: &PackageVersionMeta,
) -> Result<(), anyhow::Error> {
#[instrument(name = "OramaClient::upsert_package", skip(self))]
pub fn upsert_package(&self, package: &Package, meta: &PackageVersionMeta) {
let id = format!("@{}/{}", package.scope, package.name);
let score = package
.latest_version
.as_ref()
.map(|_| ApiPackageScore::from((meta, package)).score_percentage());

let id = format!("@{}/{}", package.scope, package.name);
let res = self
.request(
&format!("/webhooks/{}/notify", self.index_id),
serde_json::json!({
"upsert": [
{
"id": id,
"scope": &package.scope,
"name": &package.name,
"description": &package.description,
"runtimeCompat": &package.runtime_compat,
"score": score,
}
]
}),
)
.await?;
let status = res.status();
if status.is_success() {
Ok(())
} else {
let response = res.text().await?;
Err(anyhow::anyhow!(
"failed to deploy changes (status {status}): {response}"
))
}
let body = serde_json::json!({
"upsert": [
{
"id": id,
"scope": &package.scope,
"name": &package.name,
"description": &package.description,
"runtimeCompat": &package.runtime_compat,
"score": score,
}
]
});
let span = Span::current();
let client = self.clone();
let path = format!("/webhooks/{}/notify", self.index_id);
tokio::spawn(
async move {
let res = match client.request(&path, body).await {
Ok(res) => res,
Err(err) => {
error!("failed to OramaClient::upsert_package: {err}");
return;
}
};
let status = res.status();
if !status.is_success() {
let response = res.text().await.unwrap_or_default();
error!(
"failed to OramaClient::upsert_package for {id} (status {status}): {response}"
);
}
}
.instrument(span),
);
}

#[instrument(name = "OramaClient::upsert_package", skip(self), err)]
#[instrument(name = "OramaClient::delete_package", skip(self), err)]
pub async fn delete_package(
&self,
scope: &ScopeName,
Expand Down

0 comments on commit 0ece70b

Please sign in to comment.