Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Commit

Permalink
wip(backend): setup bg worker with postgres client
Browse files Browse the repository at this point in the history
  • Loading branch information
evoxmusic committed Jan 2, 2024
1 parent b773de0 commit ea01923
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
5 changes: 4 additions & 1 deletion backend/src/catalog/services.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::sync::Arc;

use serde::{Deserialize, Serialize};
use tokio::sync::mpsc::Receiver;
use tokio_postgres::Client;

use crate::catalog::{execute_command, ExecValidateScriptRequest, JobResults};
use crate::yaml_config::CatalogServiceYamlConfig;
Expand All @@ -25,7 +28,7 @@ impl BackgroundWorkerTask {
}
}

pub async fn background_worker(mut rx: Receiver<BackgroundWorkerTask>) {
pub async fn background_worker(mut rx: Receiver<BackgroundWorkerTask>, pg_client: Arc<Client>) {
while let Some(task) = rx.recv().await {
let mut job_results = JobResults {
user_fields_input: task.req.payload.clone(),
Expand Down
23 changes: 21 additions & 2 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,32 @@ async fn main() {
}
};

show_loaded_config(&yaml_config);
let (client, connect) = tokio_postgres::connect(
format!(
"host={} port={} user={} password={} dbname={}",
env::var("DB_HOST").unwrap_or_else(|_| "localhost".to_string()),
env::var("DB_PORT").unwrap_or_else(|_| "5432".to_string()),
env::var("DB_USER").unwrap_or_else(|_| "postgres".to_string()),
env::var("DB_PASSWORD").unwrap_or_else(|_| "postgres".to_string()),
env::var("DB_NAME").unwrap_or_else(|_| "qovery_portal".to_string()),
).as_str(),
tokio_postgres::NoTls,
).await.unwrap_or_else(|err| {
error!("failed to connect to database: {}", err);
std::process::exit(1);
});

let client = Arc::new(client);
let bgw_client = client.clone();

let (tx, rx) = tokio::sync::mpsc::channel::<BackgroundWorkerTask>(100);

let _ = tokio::spawn(async move {
catalog::services::background_worker(rx).await;
catalog::services::background_worker(rx, bgw_client).await;
});

show_loaded_config(&yaml_config);

let app = Router::new()
.fallback(unknown_route)
.route("/", get(|| async { "OK" }))
Expand All @@ -84,6 +102,7 @@ async fn main() {
.route("/catalogs/:slug/services/:slug/execute", post(exec_catalog_service_post_validate_scripts))
.layer(Extension(yaml_config))
.layer(Extension(tx))
.layer(Extension(client))
.layer(CorsLayer::new().allow_origin(Any));
//.route("/catalog/:id", get(catalog::get_catalog_by_id))
//.route("/catalog", post(catalog::create_catalog));
Expand Down
9 changes: 9 additions & 0 deletions docker-compose-dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
postgres:
image: postgres:16-alpine
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: qovery_portal
ports:
- 5432:5432

0 comments on commit ea01923

Please sign in to comment.