Remove asset_status thread
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
@@ -1,14 +1,9 @@
|
||||
use crate::{
|
||||
config::{Config, ALPACA_ASSET_API_URL},
|
||||
config::Config,
|
||||
database, threads,
|
||||
types::{
|
||||
alpaca::api::incoming::{self, asset::Status},
|
||||
Asset,
|
||||
},
|
||||
types::{alpaca::api::incoming, Asset},
|
||||
};
|
||||
use axum::{extract::Path, Extension, Json};
|
||||
use backoff::{future::retry, ExponentialBackoff};
|
||||
use core::panic;
|
||||
use http::StatusCode;
|
||||
use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
@@ -38,9 +33,9 @@ pub struct AddAssetRequest {
|
||||
|
||||
pub async fn add(
|
||||
Extension(app_config): Extension<Arc<Config>>,
|
||||
Extension(asset_status_sender): Extension<mpsc::Sender<threads::data::asset_status::Message>>,
|
||||
Extension(data_sender): Extension<mpsc::Sender<threads::data::Message>>,
|
||||
Json(request): Json<AddAssetRequest>,
|
||||
) -> Result<(StatusCode, Json<Asset>), StatusCode> {
|
||||
) -> Result<StatusCode, StatusCode> {
|
||||
if database::assets::select_where_symbol(&app_config.clickhouse_client, &request.symbol)
|
||||
.await
|
||||
.is_some()
|
||||
@@ -48,66 +43,38 @@ pub async fn add(
|
||||
return Err(StatusCode::CONFLICT);
|
||||
}
|
||||
|
||||
let asset = retry(ExponentialBackoff::default(), || async {
|
||||
app_config.alpaca_rate_limit.until_ready().await;
|
||||
app_config
|
||||
.alpaca_client
|
||||
.get(&format!("{}/{}", ALPACA_ASSET_API_URL, request.symbol))
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()
|
||||
.map_err(|e| match e.status() {
|
||||
Some(reqwest::StatusCode::NOT_FOUND) => backoff::Error::Permanent(e),
|
||||
_ => e.into(),
|
||||
})?
|
||||
.json::<incoming::asset::Asset>()
|
||||
.await
|
||||
.map_err(backoff::Error::Permanent)
|
||||
})
|
||||
.await
|
||||
.map_err(|e| match e.status() {
|
||||
Some(reqwest::StatusCode::NOT_FOUND) => StatusCode::NOT_FOUND,
|
||||
_ => panic!("Unexpected error: {}.", e),
|
||||
})?;
|
||||
|
||||
if asset.status != Status::Active || !asset.tradable || !asset.fractionable {
|
||||
let asset = incoming::asset::get_by_symbol(&app_config, &request.symbol).await?;
|
||||
if !asset.tradable || !asset.fractionable {
|
||||
return Err(StatusCode::FORBIDDEN);
|
||||
}
|
||||
|
||||
let asset = Asset::from(asset);
|
||||
|
||||
let (asset_status_message, asset_status_response) = threads::data::asset_status::Message::new(
|
||||
threads::data::asset_status::Action::Add,
|
||||
vec![asset.clone()],
|
||||
let (data_message, data_response) = threads::data::Message::new(
|
||||
threads::data::Action::Add,
|
||||
vec![(asset.symbol, asset.class)],
|
||||
);
|
||||
|
||||
asset_status_sender
|
||||
.send(asset_status_message)
|
||||
.await
|
||||
.unwrap();
|
||||
asset_status_response.await.unwrap();
|
||||
data_sender.send(data_message).await.unwrap();
|
||||
data_response.await.unwrap();
|
||||
|
||||
Ok((StatusCode::CREATED, Json(asset)))
|
||||
Ok(StatusCode::CREATED)
|
||||
}
|
||||
|
||||
pub async fn delete(
|
||||
Extension(app_config): Extension<Arc<Config>>,
|
||||
Extension(asset_status_sender): Extension<mpsc::Sender<threads::data::asset_status::Message>>,
|
||||
Extension(data_sender): Extension<mpsc::Sender<threads::data::Message>>,
|
||||
Path(symbol): Path<String>,
|
||||
) -> Result<StatusCode, StatusCode> {
|
||||
let asset = database::assets::select_where_symbol(&app_config.clickhouse_client, &symbol)
|
||||
.await
|
||||
.ok_or(StatusCode::NOT_FOUND)?;
|
||||
|
||||
let (asset_status_message, asset_status_response) = threads::data::asset_status::Message::new(
|
||||
threads::data::asset_status::Action::Remove,
|
||||
vec![asset],
|
||||
let (asset_status_message, asset_status_response) = threads::data::Message::new(
|
||||
threads::data::Action::Remove,
|
||||
vec![(asset.symbol, asset.class)],
|
||||
);
|
||||
|
||||
asset_status_sender
|
||||
.send(asset_status_message)
|
||||
.await
|
||||
.unwrap();
|
||||
data_sender.send(asset_status_message).await.unwrap();
|
||||
asset_status_response.await.unwrap();
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
|
@@ -10,10 +10,7 @@ use log::info;
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
use tokio::{net::TcpListener, sync::mpsc};
|
||||
|
||||
pub async fn run(
|
||||
app_config: Arc<Config>,
|
||||
asset_status_sender: mpsc::Sender<threads::data::asset_status::Message>,
|
||||
) {
|
||||
pub async fn run(app_config: Arc<Config>, data_sender: mpsc::Sender<threads::data::Message>) {
|
||||
let app = Router::new()
|
||||
.route("/health", get(health::get))
|
||||
.route("/assets", get(assets::get))
|
||||
@@ -21,7 +18,7 @@ pub async fn run(
|
||||
.route("/assets", post(assets::add))
|
||||
.route("/assets/:symbol", delete(assets::delete))
|
||||
.layer(Extension(app_config))
|
||||
.layer(Extension(asset_status_sender));
|
||||
.layer(Extension(data_sender));
|
||||
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], 7878));
|
||||
let listener = TcpListener::bind(addr).await.unwrap();
|
||||
|
Reference in New Issue
Block a user