mirror of
https://git.asonix.dog/asonix/pict-rs.git
synced 2024-11-28 20:41:00 +00:00
Try adding store healthcheck
This commit is contained in:
parent
345aa7f3d6
commit
7274538b70
4 changed files with 49 additions and 2 deletions
|
@ -1060,8 +1060,12 @@ async fn identifier<R: FullRepo, S: Store>(
|
|||
})))
|
||||
}
|
||||
|
||||
async fn healthz<R: FullRepo>(repo: web::Data<R>) -> Result<HttpResponse, Error> {
|
||||
async fn healthz<R: FullRepo, S: Store>(
|
||||
repo: web::Data<R>,
|
||||
store: web::Data<S>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
repo.health_check().await?;
|
||||
store.health_check().await?;
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
|
@ -1104,7 +1108,7 @@ fn configure_endpoints<R: FullRepo + 'static, S: Store + 'static>(
|
|||
.app_data(web::Data::new(repo))
|
||||
.app_data(web::Data::new(store))
|
||||
.app_data(web::Data::new(client))
|
||||
.route("/healthz", web::get().to(healthz::<R>))
|
||||
.route("/healthz", web::get().to(healthz::<R, S>))
|
||||
.service(
|
||||
web::scope("/image")
|
||||
.service(
|
||||
|
|
10
src/store.rs
10
src/store.rs
|
@ -77,6 +77,8 @@ pub(crate) trait Store: Clone + Debug {
|
|||
type Identifier: Identifier + 'static;
|
||||
type Stream: Stream<Item = std::io::Result<Bytes>> + Unpin + 'static;
|
||||
|
||||
async fn health_check(&self) -> Result<(), StoreError>;
|
||||
|
||||
async fn save_async_read<Reader>(&self, reader: Reader) -> Result<Self::Identifier, StoreError>
|
||||
where
|
||||
Reader: AsyncRead + Unpin + 'static;
|
||||
|
@ -115,6 +117,10 @@ where
|
|||
type Identifier = T::Identifier;
|
||||
type Stream = T::Stream;
|
||||
|
||||
async fn health_check(&self) -> Result<(), StoreError> {
|
||||
T::health_check(self).await
|
||||
}
|
||||
|
||||
async fn save_async_read<Reader>(&self, reader: Reader) -> Result<Self::Identifier, StoreError>
|
||||
where
|
||||
Reader: AsyncRead + Unpin + 'static,
|
||||
|
@ -170,6 +176,10 @@ where
|
|||
type Identifier = T::Identifier;
|
||||
type Stream = T::Stream;
|
||||
|
||||
async fn health_check(&self) -> Result<(), StoreError> {
|
||||
T::health_check(self).await
|
||||
}
|
||||
|
||||
async fn save_async_read<Reader>(&self, reader: Reader) -> Result<Self::Identifier, StoreError>
|
||||
where
|
||||
Reader: AsyncRead + Unpin + 'static,
|
||||
|
|
|
@ -54,6 +54,14 @@ impl Store for FileStore {
|
|||
type Identifier = FileId;
|
||||
type Stream = Pin<Box<dyn Stream<Item = std::io::Result<Bytes>>>>;
|
||||
|
||||
async fn health_check(&self) -> Result<(), StoreError> {
|
||||
tokio::fs::metadata(&self.root_dir)
|
||||
.await
|
||||
.map_err(FileError::from)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(reader))]
|
||||
async fn save_async_read<Reader>(
|
||||
&self,
|
||||
|
@ -157,6 +165,10 @@ impl FileStore {
|
|||
pub(crate) async fn build(root_dir: PathBuf, repo: Repo) -> Result<Self, StoreError> {
|
||||
let path_gen = init_generator(&repo).await?;
|
||||
|
||||
tokio::fs::create_dir_all(&root_dir)
|
||||
.await
|
||||
.map_err(FileError::from)?;
|
||||
|
||||
Ok(FileStore {
|
||||
root_dir,
|
||||
path_gen,
|
||||
|
|
|
@ -169,6 +169,21 @@ impl Store for ObjectStore {
|
|||
type Identifier = ObjectId;
|
||||
type Stream = Pin<Box<dyn Stream<Item = std::io::Result<Bytes>>>>;
|
||||
|
||||
async fn health_check(&self) -> Result<(), StoreError> {
|
||||
let response = self
|
||||
.head_bucket_request()
|
||||
.await?
|
||||
.send()
|
||||
.await
|
||||
.map_err(ObjectError::from)?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(status_error(response).await);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn save_async_read<Reader>(&self, reader: Reader) -> Result<Self::Identifier, StoreError>
|
||||
where
|
||||
Reader: AsyncRead + Unpin + 'static,
|
||||
|
@ -438,6 +453,12 @@ impl ObjectStore {
|
|||
})
|
||||
}
|
||||
|
||||
async fn head_bucket_request(&self) -> Result<ClientRequest, StoreError> {
|
||||
let action = self.bucket.head_bucket(Some(&self.credentials));
|
||||
|
||||
Ok(self.build_request(action))
|
||||
}
|
||||
|
||||
async fn put_object_request(&self) -> Result<(ClientRequest, ObjectId), StoreError> {
|
||||
let path = self.next_file().await?;
|
||||
|
||||
|
|
Loading…
Reference in a new issue