Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ thiserror = "1"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
stellar-strkey = "0.0.8"
ed25519-dalek = "2"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
2 changes: 2 additions & 0 deletions worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ thiserror = "1"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
stellar-strkey = "0.0.8"
ed25519-dalek = "2"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
44 changes: 44 additions & 0 deletions worker/src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::env;
use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};

pub fn init_logging() -> Result<(), Box<dyn std::error::Error>> {
let log_level = env::var("LOG_LEVEL").unwrap_or_else(|_| "info".to_string());

let filter_str = format!("worker={}", log_level);
let env_filter =
EnvFilter::try_from_str(&filter_str).unwrap_or_else(|_| EnvFilter::new("worker=info"));

let is_production =
env::var("RUST_ENV").unwrap_or_else(|_| "development".to_string()) == "production";

if is_production {
tracing_subscriber::registry()
.with(env_filter)
.with(fmt::layer().json().with_current_span(true))
.init();
} else {
tracing_subscriber::registry()
.with(env_filter)
.with(
fmt::layer()
.pretty()
.with_target(true)
.with_thread_ids(true)
.with_file(true)
.with_line_number(true),
)
.init();
}

tracing::info!(
"Logging initialized - level: {}, mode: {}",
log_level,
if is_production {
"production"
} else {
"development"
}
);

Ok(())
}
52 changes: 49 additions & 3 deletions worker/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Contract Fox Worker - Background service for contract management
mod logging;
mod redaction;

use serde::{Deserialize, Serialize};
use tracing::{debug, error, info, instrument, warn};

#[derive(Debug, Serialize, Deserialize)]
pub struct WorkerConfig {
Expand All @@ -8,6 +12,16 @@ pub struct WorkerConfig {
pub network: String,
}

impl WorkerConfig {
pub fn log_safe(&self) -> serde_json::Value {
serde_json::json!({
"rpc_url": self.rpc_url,
"poll_interval": self.poll_interval,
"network": self.network
})
}
}

impl Default for WorkerConfig {
fn default() -> Self {
Self {
Expand All @@ -19,17 +33,49 @@ impl Default for WorkerConfig {
}

pub async fn run_worker(config: WorkerConfig) -> Result<(), Box<dyn std::error::Error>> {
println!("Starting worker with config: {:?}", config);
info!("Starting contract fox worker");
debug!(
"Worker configuration: {}",
serde_json::to_string_pretty(&config.log_safe())
.unwrap_or_else(|_| "Failed to serialize config".to_string())
);

loop {
println!("Polling for contract updates...");
tokio::time::sleep(tokio::time::Duration::from_secs(config.poll_interval)).await;
debug!("Polling for contract updates");

match tokio::time::timeout(
tokio::time::Duration::from_secs(config.poll_interval),
check_contracts(&config),
)
.await
{
Ok(result) => {
if let Err(e) = result {
error!("Error checking contracts: {}", e);
}
}
Err(_) => {
debug!("Polling timeout reached, continuing to next iteration");
}
}
}
}

async fn check_contracts(config: &WorkerConfig) -> Result<(), Box<dyn std::error::Error>> {
debug!("Checking contracts on network: {}", config.network);

tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;

Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
logging::init_logging()?;

let config = WorkerConfig::default();
info!("Worker initialized with default configuration");

run_worker(config).await
}

Expand Down
Loading