-
-
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.
* Rename event::receives to event::callback, add Event enum * Update stream settings with new subject format * Update prefix for jobs * Enable listener * Update stream name to FARMHAND_EVENTS * Make stream name a constant
- Loading branch information
1 parent
93faf64
commit 63f6270
Showing
10 changed files
with
159 additions
and
31 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pub mod receivers; | ||
pub mod callback; | ||
pub mod subscribers; |
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,49 @@ | ||
//! This is a program for simply logging the stream of events going to farmhand nats | ||
//! It intentionally does not do anything else | ||
use anyhow::Result; | ||
use farmhand::workers::{self, events::EVENT_PREFIX}; | ||
use futures::StreamExt; | ||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
// Initialize tracing subscriber | ||
tracing_subscriber::registry() | ||
.with( | ||
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into()), | ||
) | ||
.with(tracing_subscriber::fmt::layer()) | ||
.init(); | ||
// Create the NATS client | ||
tracing::debug!("Connecting to NATS server"); | ||
let nats_client = workers::create_nats_client().await?; | ||
// Setup the Jetstream queue | ||
let jq_name = std::env::args() | ||
.nth(1) | ||
.unwrap_or_else(|| "FARMHAND_JOBS".to_string()); | ||
let queue = workers::Queue::connect(jq_name, nats_client) | ||
.await | ||
.expect("Failed to create worker queue"); | ||
|
||
// Get all events from the stream | ||
let subject = format!("{}.>", EVENT_PREFIX); // All farmhand events | ||
let runner_name = "farmhand_listener_1".to_string(); | ||
tracing::info!("Listening for events {} on {}", subject, runner_name); | ||
// Create the consumer to listen for events | ||
let consumer = queue.create_consumer(Some(runner_name), subject).await?; | ||
loop { | ||
let mut jobs = consumer.fetch().max_messages(20).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; | ||
}; | ||
tracing::info!("{:?}", job); | ||
} | ||
|
||
// 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
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,28 @@ | ||
use super::runner::chat::ChatMessagePayload; | ||
|
||
/// Represents events we send and receive from NATS | ||
/// Primarily used to get the appropriate subject name for an event | ||
pub enum Event { | ||
ChatMessage(ChatMessagePayload), | ||
} | ||
|
||
pub const EVENT_PREFIX: &str = "farmhand"; | ||
pub const PRIMARY_STREAM: &str = "FARMHAND_EVENTS"; | ||
|
||
impl Event { | ||
pub fn get_subject(&self) -> String { | ||
match self { | ||
// twitch.events.{broadcaster_name}.chat_message | ||
Event::ChatMessage(payload) => format!( | ||
"{}.twitch.events.{}.chat_message", | ||
EVENT_PREFIX, payload.broadcaster_user_name | ||
), | ||
} | ||
} | ||
} | ||
|
||
impl From<ChatMessagePayload> for Event { | ||
fn from(payload: ChatMessagePayload) -> Self { | ||
Event::ChatMessage(payload) | ||
} | ||
} |
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,4 @@ | ||
pub mod events; | ||
pub mod runner; | ||
|
||
pub use runner::Queue; | ||
|