Skip to content

Commit

Permalink
feat: start implementing free wisdoms route nad daily rotator worker
Browse files Browse the repository at this point in the history
  • Loading branch information
nikola-bozin-org committed May 5, 2024
1 parent 94b0a9c commit 1046b53
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 7 deletions.
15 changes: 14 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["server", "worker"]
members = ["server", "worker_wisdoms_checker", "worker_wisdoms_daily_rotator"]

resolver = "2"

Expand Down
16 changes: 12 additions & 4 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/bin/bash

# Build worker
cd worker
# Build worker worker_wisdoms_checker
cd worker_wisdoms_checker
cargo build --release
cd ..

# Build worker worker_wisdoms_daily_rotator
cd worker_wisdoms_daily_rotator
cargo build --release
cd ..

Expand All @@ -10,8 +15,11 @@ cd server
cargo build --release
cd ..

# Start worker
./target/release/worker &
# Start worker worker_wisdoms_checker
./target/release/worker_wisdoms_checker &

# Start worker worker_wisdoms_daily_rotator
./target/release/worker_wisdoms_daily_rotator &

# Start server
./target/release/wisdomia-monolithic
4 changes: 4 additions & 0 deletions server/src/routes/wisdoms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ fn _routes() -> Router {
Router::new().route("/", get(get_wisdoms))
}

async fn get_free_wisdoms(Extension(state): Extension<Arc<AppState>>) -> impl IntoResponse {
(StatusCode::OK)
}

async fn get_wisdoms(Extension(state): Extension<Arc<AppState>>) -> impl IntoResponse {
tracing_fast_dev::tfd().info("GET_WISDOM", "FUNCTION");
match _get_wisdoms(&state.db).await {
Expand Down
2 changes: 1 addition & 1 deletion worker/Cargo.toml → worker_wisdoms_checker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "worker"
name = "worker_wisdoms_checker"
version = "0.1.0"
edition = "2021"

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub async fn worker_thread(pool: PgPool) {
});

loop {
dbg!("Checking wisdoms...");
tokio::time::sleep(Duration::from_secs(5)).await;

//Its one dot (.) because we are running the worker it from the root.
Expand Down
13 changes: 13 additions & 0 deletions worker_wisdoms_daily_rotator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "worker_wisdoms_daily_rotator"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = { version = "1.35.1", features = ["full"] }
serde = { version = "1.0.194", features = ["derive"] }
serde_json = "1.0.112"
dotenv = "0.15.0"
sqlx = { version = "0.7.4", features = ["postgres","runtime-tokio-rustls"] }
base64="0.22.1"
teloxide = { version = "0.12", features = ["macros"] }
10 changes: 10 additions & 0 deletions worker_wisdoms_daily_rotator/src/daily_rotator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::time::Duration;

use sqlx::PgPool;

pub async fn worker_thread(_pool: PgPool) {
loop {
dbg!("Rotating...");
tokio::time::sleep(Duration::from_secs(5)).await;
}
}
27 changes: 27 additions & 0 deletions worker_wisdoms_daily_rotator/src/db_connect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pub type Result<T> = core::result::Result<T, Error>;

use sqlx::postgres::PgPoolOptions;

pub type Database = sqlx::Pool<sqlx::Postgres>;

#[derive(Debug)]
pub enum Error {
FailedConnectingToDatabase { error: String },
}

impl From<sqlx::Error> for Error {
fn from(value: sqlx::Error) -> Self {
Self::FailedConnectingToDatabase {
error: value.to_string(),
}
}
}

pub async fn connect(db_url: &str) -> Result<Database> {
let pool: Database = PgPoolOptions::new()
// TODO: Max connections! Dynamic?
.max_connections(5)
.connect(db_url)
.await?;
Ok(pool)
}
19 changes: 19 additions & 0 deletions worker_wisdoms_daily_rotator/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
mod daily_rotator;
mod db_connect;

use std::env;

use daily_rotator::worker_thread;
use db_connect::connect;

#[tokio::main]
async fn main() {
let _ = dotenv::dotenv();

let database_url = env::var("DATABASE_URL")
.unwrap_or_else(|_| panic!("Missing required environment variable: {}", "DATABASE_URL"));

let db = connect(database_url.as_str()).await.unwrap();

tokio::spawn(worker_thread(db)).await.unwrap();
}

0 comments on commit 1046b53

Please sign in to comment.