-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Queue with NATS-based system (#56)
* Add binaries up/down, add initial queue modules * Allow dead code to Queue struct * Fix subjects, make queue name uppercase * Initialize job runner * Add new nats-based runner system * Post twitch channel chat messages to nats * Remove query macro
- Loading branch information
1 parent
e7910f8
commit 93faf64
Showing
21 changed files
with
829 additions
and
27 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use anyhow::Result; | ||
use farmhand::{db, workers}; | ||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
tracing_subscriber::registry() | ||
.with( | ||
tracing_subscriber::EnvFilter::try_from_default_env() | ||
.unwrap_or_else(|_| "down=info".into()), | ||
) | ||
.with(tracing_subscriber::fmt::layer()) | ||
.init(); | ||
tracing::warn!("Deleting all data from the project, this is a destructive operation"); | ||
for i in (1..=5).rev() { | ||
tracing::warn!("Deleting all data in {} seconds...", i); | ||
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; | ||
} | ||
tracing::info!("Starting deletion process"); | ||
let db_pool = db::connect_to_database().await?; | ||
|
||
// Delete all data from the database | ||
tracing::debug!("Deleting all data from the database"); | ||
db::delete_all_data(&db_pool).await?; | ||
|
||
tracing::info!("Successfully deleted all data from the database"); | ||
|
||
// Delete all streams | ||
tracing::debug!("Deleting all streams"); | ||
let nats_client = workers::create_nats_client().await?; | ||
let jq_name = "FARMHAND_JOBS".to_string(); | ||
workers::Queue::delete(jq_name, nats_client).await?; | ||
|
||
tracing::info!("Successfully deleted all streams"); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,55 @@ | ||
pub fn main() { | ||
tracing::info!("Hello from the job runner!"); | ||
use anyhow::Result; | ||
use async_nats::jetstream::AckKind; | ||
use farmhand::workers::{self, runner::process_message}; | ||
use futures::StreamExt; | ||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
tracing_subscriber::registry() | ||
.with( | ||
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into()), | ||
) | ||
.with(tracing_subscriber::fmt::layer()) | ||
.init(); | ||
// Connect to the stream | ||
tracing::debug!("Connecting to NATS server"); | ||
let nats_client = workers::create_nats_client().await?; | ||
let jq_name = "FARMHAND_JOBS".to_string(); | ||
tracing::debug!("Connecting to queue"); | ||
let queue = workers::Queue::connect(jq_name, nats_client) | ||
.await | ||
.expect("Failed to create worker queue"); | ||
|
||
// Create a consumer for the queue | ||
let subject = "farmhand_jobs.>".to_string(); // All jobs | ||
let runner_name = "farmhand_runner_1".to_string(); | ||
tracing::info!("Listening for jobs {} on {}", subject, runner_name); | ||
let consumer = queue.create_consumer(Some(runner_name), subject).await?; | ||
|
||
// Start consuming jobs | ||
loop { | ||
let mut jobs = consumer.fetch().max_messages(3).messages().await?; | ||
|
||
while let Some(job) = jobs.next().await { | ||
// Make sure the job is good to go | ||
let Ok(job) = job else { | ||
tracing::error!("Failed to receive job"); | ||
continue; | ||
}; | ||
// Process the message itself, ack on success, nack on failure | ||
match process_message(&job.message).await { | ||
Ok(_) => job.ack().await.expect("Failed to ack job"), | ||
Err(err) => { | ||
tracing::error!("Failed to process job: {}", err); | ||
job.ack_with(AckKind::Nak(None)) | ||
.await | ||
.expect("Failed to nack job"); | ||
} | ||
} | ||
} | ||
|
||
// Optional: Add a small delay to prevent tight loops when there are no jobs | ||
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use anyhow::Result; | ||
use farmhand::{db, workers}; | ||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
tracing_subscriber::registry() | ||
.with( | ||
tracing_subscriber::EnvFilter::try_from_default_env() | ||
.unwrap_or_else(|_| "up=info".into()), | ||
) | ||
.with(tracing_subscriber::fmt::layer()) | ||
.init(); | ||
tracing::info!("Initializing project"); | ||
// Run database-related initialization tasks | ||
let (_db_handle, _nats_handle) = tokio::join!(init_project_db(), init_project_nats()); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Function for initializing project-wide database dependencies | ||
async fn init_project_db() { | ||
tracing::debug!("Starting database initialization"); | ||
|
||
// Connect to the database | ||
tracing::debug!("Connecting to database"); | ||
let db_pool = db::connect_to_database() | ||
.await | ||
.expect("Failed to connect to database"); | ||
|
||
// Run migrations so we can use the database | ||
tracing::debug!("Running migrations"); | ||
db::run_migrations(&db_pool) | ||
.await | ||
.expect("Failed to run migrations"); | ||
|
||
tracing::info!("Successfully initialized database"); | ||
} | ||
|
||
/// Function for initializing project-wide nats dependencies | ||
async fn init_project_nats() { | ||
tracing::debug!("Starting NATS initialization"); | ||
|
||
// Connect to the NATS server | ||
tracing::debug!("Connecting to NATS server"); | ||
let nats_client = workers::create_nats_client() | ||
.await | ||
.expect("Failed to connect to NATS"); | ||
|
||
// Create the job queue stream | ||
let jq_name = "FARMHAND_JOBS".to_string(); | ||
let jq_desc = Some("Farmhand job runner queue".to_string()); | ||
let jq_subjects = vec!["farmhand_jobs.>".to_string()]; | ||
workers::Queue::new(jq_name, jq_desc, jq_subjects, nats_client) | ||
.await | ||
.expect("Failed to create worker queue"); | ||
|
||
tracing::info!("Successfully initialized NATS worker queue"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod queue; | ||
|
||
pub use queue::QueueError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum QueueError { | ||
#[error("Invalid Connection: {0}")] | ||
InvalidConnection(String), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod api; | ||
pub mod db; | ||
pub mod error; | ||
pub mod prelude; | ||
pub mod storage; | ||
pub mod vod; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod runner; | ||
|
||
pub use runner::Queue; | ||
pub use runner::{create_nats_client, get_nats_url}; |
Oops, something went wrong.