cargo_hammerwork/
lib.rs

1//! # Cargo Hammerwork CLI
2//!
3//! A comprehensive command-line interface for managing Hammerwork job queues.
4//!
5//! This crate provides the `cargo hammerwork` CLI tool for database migrations,
6//! job management, worker control, queue operations, and monitoring of Hammerwork
7//! job processing systems.
8//!
9//! ## Installation
10//!
11//! ```bash
12//! cargo install cargo-hammerwork --features postgres
13//! # or
14//! cargo install cargo-hammerwork --features mysql
15//! ```
16//!
17//! ## Basic Usage
18//!
19//! The CLI can be invoked either as a cargo subcommand or directly:
20//!
21//! ```bash
22//! # As cargo subcommand
23//! cargo hammerwork migration status --database-url postgresql://localhost/mydb
24//!
25//! # Direct invocation
26//! cargo-hammerwork migration status --database-url postgresql://localhost/mydb
27//! ```
28//!
29//! ## Common Commands
30//!
31//! ### Database Migrations
32//!
33//! ```bash
34//! # Run all pending migrations
35//! cargo hammerwork migration run --database-url postgresql://localhost/mydb
36//!
37//! # Check migration status
38//! cargo hammerwork migration status --database-url postgresql://localhost/mydb
39//!
40//! # Rollback last migration
41//! cargo hammerwork migration rollback --database-url postgresql://localhost/mydb
42//! ```
43//!
44//! ### Job Management
45//!
46//! ```bash
47//! # List jobs in a queue
48//! cargo hammerwork job list --database-url postgresql://localhost/mydb --queue default
49//!
50//! # Enqueue a new job
51//! cargo hammerwork job enqueue --database-url postgresql://localhost/mydb \
52//!   --queue email --payload '{"to": "user@example.com"}'
53//!
54//! # Retry failed jobs
55//! cargo hammerwork job retry --database-url postgresql://localhost/mydb --all-failed
56//!
57//! # Cancel a specific job
58//! cargo hammerwork job cancel --database-url postgresql://localhost/mydb <job-id>
59//! ```
60//!
61//! ### Queue Operations
62//!
63//! ```bash
64//! # List all queues with statistics
65//! cargo hammerwork queue list --database-url postgresql://localhost/mydb
66//!
67//! # Clear dead jobs from a queue
68//! cargo hammerwork queue clear --database-url postgresql://localhost/mydb \
69//!   --queue default --dead
70//!
71//! # Show queue statistics
72//! cargo hammerwork queue stats --database-url postgresql://localhost/mydb \
73//!   --queue default
74//! ```
75//!
76//! ### Worker Management
77//!
78//! ```bash
79//! # Start a worker (requires configuration file)
80//! cargo hammerwork worker start --database-url postgresql://localhost/mydb \
81//!   --queue default --handler ./my_handler
82//!
83//! # Show worker status
84//! cargo hammerwork worker status --database-url postgresql://localhost/mydb
85//!
86//! # Stop all workers gracefully
87//! cargo hammerwork worker stop --database-url postgresql://localhost/mydb --all
88//! ```
89//!
90//! ### Monitoring
91//!
92//! ```bash
93//! # Show real-time dashboard
94//! cargo hammerwork monitor dashboard --database-url postgresql://localhost/mydb
95//!
96//! # Display queue metrics
97//! cargo hammerwork monitor metrics --database-url postgresql://localhost/mydb
98//!
99//! # Check system health
100//! cargo hammerwork monitor health --database-url postgresql://localhost/mydb
101//! ```
102//!
103//! ## Configuration
104//!
105//! The CLI supports configuration files to avoid repetitive arguments:
106//!
107//! ```bash
108//! # Create default config
109//! cargo hammerwork config init
110//!
111//! # Show current config
112//! cargo hammerwork config show
113//!
114//! # Set database URL in config
115//! cargo hammerwork config set database_url postgresql://localhost/mydb
116//! ```
117//!
118//! Configuration files are stored at:
119//! - Linux/Mac: `~/.config/hammerwork/config.toml`
120//! - Windows: `%APPDATA%\hammerwork\config.toml`
121//!
122//! ## Environment Variables
123//!
124//! The CLI respects these environment variables:
125//!
126//! - `DATABASE_URL` - Default database connection URL
127//! - `HAMMERWORK_CONFIG` - Path to config file
128//! - `HAMMERWORK_LOG` - Log level (error, warn, info, debug, trace)
129//!
130//! ## Examples
131//!
132//! ### Complete Workflow Example
133//!
134//! ```bash
135//! # 1. Initialize configuration
136//! cargo hammerwork config init
137//! cargo hammerwork config set database_url postgresql://localhost/hammerwork
138//!
139//! # 2. Run migrations to set up database
140//! cargo hammerwork migration run
141//!
142//! # 3. Enqueue some jobs
143//! cargo hammerwork job enqueue --queue email \
144//!   --payload '{"to": "user@example.com", "subject": "Welcome!"}'
145//!
146//! cargo hammerwork job enqueue --queue email \
147//!   --payload '{"to": "admin@example.com", "subject": "New user"}' \
148//!   --priority high
149//!
150//! # 4. Check queue status
151//! cargo hammerwork queue list
152//!
153//! # 5. Process jobs (in another terminal)
154//! cargo hammerwork worker start --queue email
155//!
156//! # 6. Monitor progress
157//! cargo hammerwork monitor dashboard
158//! ```
159//!
160//! ### Batch Operations Example
161//!
162//! ```bash
163//! # Enqueue multiple jobs at once
164//! cargo hammerwork batch enqueue --queue data-processing \
165//!   --payloads '[
166//!     {"file": "data1.csv"},
167//!     {"file": "data2.csv"},
168//!     {"file": "data3.csv"}
169//!   ]'
170//!
171//! # Cancel all jobs in a batch
172//! cargo hammerwork batch cancel <batch-id>
173//!
174//! # Show batch status
175//! cargo hammerwork batch status <batch-id>
176//! ```
177//!
178//! ### Cron Scheduling Example
179//!
180//! ```bash
181//! # Create a recurring job
182//! cargo hammerwork cron create --queue reports \
183//!   --schedule "0 9 * * MON-FRI" \
184//!   --timezone "America/New_York" \
185//!   --payload '{"type": "daily_report"}'
186//!
187//! # List cron jobs
188//! cargo hammerwork cron list
189//!
190//! # Disable a cron job
191//! cargo hammerwork cron disable <job-id>
192//! ```
193//!
194//! ## Feature Flags
195//!
196//! - `postgres` - PostgreSQL support (recommended)
197//! - `mysql` - MySQL support
198//! - `completions` - Shell completion generation
199//!
200//! ## Error Handling
201//!
202//! The CLI provides detailed error messages and non-zero exit codes on failure:
203//!
204//! - Exit code 0: Success
205//! - Exit code 1: General error
206//! - Exit code 2: Configuration error
207//! - Exit code 3: Database connection error
208//! - Exit code 4: Invalid arguments
209//!
210//! ## Verbose and Quiet Modes
211//!
212//! Control output verbosity with global flags:
213//!
214//! ```bash
215//! # Verbose mode - show debug information
216//! cargo hammerwork -v migration run
217//!
218//! # Quiet mode - suppress all but errors
219//! cargo hammerwork -q job list
220//! ```
221
222pub mod commands;
223pub mod config;
224pub mod utils;
225
226pub use commands::*;
227pub use config::*;
228pub use utils::*;