rustvello-cli 0.1.6

Rustvello distributed task system CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use clap::{Parser, Subcommand};
use std::sync::Arc;

use rustvello::builder::Rustvello;
use rustvello::logging::{init_logging, LogConfig};
use rustvello::prelude::*;
use rustvello_core::runner::Runner;
use rustvello_core::state_backend::StateBackendCore;
use uuid::Uuid;

#[derive(Parser)]
#[command(
    name = "rustvello",
    about = "Rustvello distributed task system CLI",
    version
)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Start a task runner that processes queued invocations
    Run {
        /// Application ID
        #[arg(short, long, default_value = "rustvello")]
        app_id: String,

        /// SQLite database path (uses in-memory if not specified)
        #[arg(short, long)]
        db_path: Option<String>,

        /// TOML configuration file
        #[arg(short, long)]
        config: Option<String>,

        /// Use in-memory backends (for development)
        #[arg(long)]
        memory: bool,

        /// Idle sleep time in milliseconds
        #[arg(long, default_value = "100")]
        idle_sleep_ms: u64,
    },

    /// Show the status of an invocation
    Status {
        /// Invocation ID to check
        invocation_id: String,

        /// SQLite database path
        #[arg(short, long)]
        db_path: Option<String>,
    },

    /// List invocations, optionally filtered by status
    List {
        /// Filter by status (REGISTERED, PENDING, RUNNING, SUCCESS, FAILED, RETRY)
        #[arg(short, long)]
        status: Option<String>,

        /// Filter by task ID (format: module.name)
        #[arg(short, long)]
        task: Option<String>,

        /// SQLite database path
        #[arg(short, long)]
        db_path: Option<String>,
    },

    /// Purge all data (broker queue, invocations, results)
    Purge {
        /// SQLite database path
        #[arg(short, long)]
        db_path: Option<String>,

        /// Skip confirmation prompt
        #[arg(short = 'y', long)]
        yes: bool,
    },

    /// Show system information
    Info,

    /// Show effective configuration
    Config {
        /// TOML configuration file
        #[arg(short, long)]
        config: Option<String>,

        /// Application ID
        #[arg(short, long, default_value = "rustvello")]
        app_id: String,
    },
}

fn parse_status(s: &str) -> Option<InvocationStatus> {
    s.parse::<InvocationStatus>().ok()
}

type SqliteBackends = (
    Arc<rustvello_sqlite::broker::SqliteBroker>,
    Arc<rustvello_sqlite::orchestrator::SqliteOrchestrator>,
    Arc<rustvello_sqlite::state_backend::SqliteStateBackend>,
);

fn make_sqlite_backends(
    db_path: &Option<String>,
) -> Result<SqliteBackends, Box<dyn std::error::Error>> {
    let db = Arc::new(match db_path {
        Some(path) => rustvello_sqlite::db::Database::open(path, "rustvello")?,
        None => rustvello_sqlite::db::Database::in_memory()?,
    });
    Ok((
        Arc::new(rustvello_sqlite::broker::SqliteBroker::new(Arc::clone(&db))),
        Arc::new(rustvello_sqlite::orchestrator::SqliteOrchestrator::new(
            Arc::clone(&db),
        )),
        Arc::new(rustvello_sqlite::state_backend::SqliteStateBackend::new(db)),
    ))
}

#[tokio::main]
async fn main() {
    // Initialize tracing with structured span fields in log output.
    // Span fields (runner_id, app_id, invocation_id, task_id) are
    // automatically included in every log line within their scope.
    init_logging(&LogConfig::default());
    let cli = Cli::parse();

    match cli.command {
        Commands::Run {
            app_id,
            db_path,
            config,
            memory,
            idle_sleep_ms,
        } => {
            println!("Starting Rustvello runner (app_id: {})", app_id);

            let mut builder = Rustvello::builder()
                .from_env()
                .app_id(&app_id)
                .auto_discover_tasks();

            if let Some(ref config_path) = config {
                builder = builder.from_file(config_path).unwrap_or_else(|e| {
                    eprintln!("Failed to load config file: {}", e);
                    std::process::exit(1);
                });
            }

            if memory {
                builder = builder.memory();
            } else if let Some(ref path) = db_path {
                builder = builder.sqlite(path, "rustvello");
            } else {
                // Default: in-memory SQLite
                builder = builder.sqlite(":memory:", "rustvello");
            }

            let app = builder.build().await.unwrap_or_else(|e| {
                eprintln!("Failed to build application: {}", e);
                std::process::exit(1);
            });

            let runner = app.into_runner().with_idle_sleep(idle_sleep_ms);

            println!(
                "Runner {} started, waiting for tasks...",
                runner.runner_id()
            );

            if let Err(e) = runner.run().await {
                eprintln!("Runner error: {}", e);
                std::process::exit(1);
            }
        }

        Commands::Status {
            invocation_id,
            db_path,
        } => {
            let (_, orchestrator, state_backend) =
                make_sqlite_backends(&db_path).unwrap_or_else(|e| {
                    eprintln!("Failed to open database: {}", e);
                    std::process::exit(1);
                });
            let inv_id = match Uuid::parse_str(&invocation_id) {
                Ok(uuid) => InvocationId::from_string(uuid.to_string()),
                Err(_) => {
                    eprintln!("Invalid invocation ID: not a valid UUID");
                    std::process::exit(1);
                }
            };

            match orchestrator.get_invocation_status(&inv_id).await {
                Ok(record) => {
                    println!("Invocation: {}", inv_id);
                    println!("Status:     {}", record.status);
                    println!("Timestamp:  {}", record.timestamp);
                    if let Some(runner) = &record.runner_id {
                        println!("Runner:     {}", runner);
                    }

                    // Show result or error if terminal
                    if record.status == InvocationStatus::Success {
                        match state_backend.get_result(&inv_id).await {
                            Ok(Some(result)) => println!("Result:     {}", result),
                            Ok(None) => {}
                            Err(e) => eprintln!("Warning: failed to fetch result: {}", e),
                        }
                    } else if record.status == InvocationStatus::Failed {
                        match state_backend.get_error(&inv_id).await {
                            Ok(Some(error)) => println!("Error:      {}", error),
                            Ok(None) => {}
                            Err(e) => eprintln!("Warning: failed to fetch error: {}", e),
                        }
                    }
                }
                Err(e) => {
                    eprintln!("Error: {}", e);
                    std::process::exit(1);
                }
            }
        }

        Commands::List {
            status,
            task,
            db_path,
        } => {
            let (_, orchestrator, _) = make_sqlite_backends(&db_path).unwrap_or_else(|e| {
                eprintln!("Failed to open database: {}", e);
                std::process::exit(1);
            });

            let task_id = task.map(|t| {
                t.parse::<TaskId>().unwrap_or_else(|e| {
                    eprintln!("Invalid task ID '{}': {}", t, e);
                    std::process::exit(1);
                })
            });

            if let Some(status_str) = status {
                let status = parse_status(&status_str).unwrap_or_else(|| {
                    eprintln!("Invalid status: {}", status_str);
                    std::process::exit(1);
                });

                match orchestrator
                    .get_invocations_by_status(status, task_id.as_ref())
                    .await
                {
                    Ok(ids) => {
                        println!("Found {} invocations with status {}:", ids.len(), status);
                        for id in &ids {
                            println!("  {}", id);
                        }
                    }
                    Err(e) => {
                        eprintln!("Error: {}", e);
                        std::process::exit(1);
                    }
                }
            } else if let Some(tid) = task_id {
                match orchestrator.get_invocations_by_task(&tid).await {
                    Ok(ids) => {
                        println!("Found {} invocations for task {}:", ids.len(), tid);
                        for id in &ids {
                            println!("  {}", id);
                        }
                    }
                    Err(e) => {
                        eprintln!("Error: {}", e);
                        std::process::exit(1);
                    }
                }
            } else {
                println!("Specify --status or --task to filter invocations");
            }
        }

        Commands::Purge { db_path, yes } => {
            if !yes {
                println!("This will delete ALL data. Use --yes to confirm.");
                std::process::exit(0);
            }

            let (broker, orchestrator, state_backend) = make_sqlite_backends(&db_path)
                .unwrap_or_else(|e| {
                    eprintln!("Failed to open database: {}", e);
                    std::process::exit(1);
                });
            if let Err(e) = broker.purge(None).await {
                eprintln!("Failed to purge broker: {}", e);
                std::process::exit(1);
            }
            if let Err(e) = orchestrator.purge().await {
                eprintln!("Failed to purge orchestrator: {}", e);
                std::process::exit(1);
            }
            if let Err(e) = state_backend.purge().await {
                eprintln!("Failed to purge state backend: {}", e);
                std::process::exit(1);
            }
            println!("All data purged.");
        }

        Commands::Info => {
            println!("Rustvello v{}", env!("CARGO_PKG_VERSION"));
            println!("Distributed task system for Rust");
            println!("Homepage: https://pynenc.org");
            println!("Repository: https://github.com/pynenc/rustvello");
        }

        Commands::Config { config, app_id } => {
            let mut builder = Rustvello::builder().from_env().app_id(&app_id);

            if let Some(ref config_path) = config {
                builder = builder.from_file(config_path).unwrap_or_else(|e| {
                    eprintln!("Failed to load config file: {}", e);
                    std::process::exit(1);
                });
            }

            let app = builder.memory().build().await.unwrap_or_else(|e| {
                eprintln!("Failed to build application: {}", e);
                std::process::exit(1);
            });

            println!("=== Effective Configuration ===");
            println!();
            println!("[app]");
            println!("  app_id                       = {:?}", app.config.app_id);
            println!(
                "  dev_mode_force_sync          = {}",
                app.config.dev_mode_force_sync
            );
            println!(
                "  max_pending_seconds          = {}",
                app.config.max_pending_seconds
            );
            println!(
                "  heartbeat_interval_seconds   = {}",
                app.config.heartbeat_interval_seconds
            );
            println!(
                "  runner_dead_after_seconds     = {}",
                app.config.runner_dead_after_seconds
            );
            println!(
                "  recovery_check_interval_sec  = {}",
                app.config.recovery_check_interval_seconds
            );
            println!(
                "  print_arguments              = {}",
                app.config.print_arguments
            );
            println!(
                "  argument_print_mode          = {:?}",
                app.config.argument_print_mode
            );
            println!(
                "  truncate_arguments_length    = {}",
                app.config.truncate_arguments_length
            );
            println!(
                "  recover_pending_cron         = {:?}",
                app.config.recover_pending_cron
            );
            println!(
                "  recover_running_cron         = {:?}",
                app.config.recover_running_cron
            );
            println!(
                "  cached_status_time_seconds   = {}",
                app.config.cached_status_time_seconds
            );
            println!(
                "  logging_level                = {:?}",
                app.config.logging_level
            );
            println!(
                "  log_format                   = {:?}",
                app.config.log_format
            );
            println!(
                "  log_use_colors               = {:?}",
                app.config.log_use_colors
            );
            println!(
                "  compact_log_context          = {}",
                app.config.compact_log_context
            );
            println!(
                "  blocking_control             = {}",
                app.config.blocking_control
            );
            println!(
                "  auto_final_inv_purge_hours   = {}",
                app.config.auto_final_invocation_purge_hours
            );
            println!(
                "  scheduler_interval_seconds   = {}",
                app.config.scheduler_interval_seconds
            );
            println!(
                "  enable_scheduler             = {}",
                app.config.enable_scheduler
            );
        }
    }
}