|
1 | 1 | # go-smart-queue |
2 | | -A local job queue that schedules work based on system resources. |
| 2 | + |
| 3 | +A smart Go job scheduler that runs tasks while monitoring CPU, memory, and IO usage. Queue jobs with constraints and priorities, using Go routines, channels, and OS metrics to throttle execution. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- ⚡ **Priority-based job scheduling** - Jobs are executed based on priority (higher values = higher priority) |
| 8 | +- 📊 **Resource monitoring** - Real-time monitoring of CPU, memory, and IO usage |
| 9 | +- 🎯 **Resource constraints** - Define maximum resource limits for jobs |
| 10 | +- 🔄 **Concurrent execution** - Configurable worker pool using goroutines |
| 11 | +- 🛑 **Job cancellation** - Cancel pending or running jobs |
| 12 | +- 📈 **Real-time dashboard** - Live view of system metrics and job status |
| 13 | +- 💻 **CLI interface** - Easy-to-use command-line interface |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +```bash |
| 18 | +git clone https://github.com/BaseMax/go-smart-queue.git |
| 19 | +cd go-smart-queue |
| 20 | +make build |
| 21 | +``` |
| 22 | + |
| 23 | +Or using Go directly: |
| 24 | + |
| 25 | +```bash |
| 26 | +go build -o go-smart-queue |
| 27 | +``` |
| 28 | + |
| 29 | +### Quick Start with Make |
| 30 | + |
| 31 | +```bash |
| 32 | +# Build the application |
| 33 | +make build |
| 34 | + |
| 35 | +# Run tests |
| 36 | +make test |
| 37 | + |
| 38 | +# Run the example |
| 39 | +make example |
| 40 | + |
| 41 | +# Clean build artifacts |
| 42 | +make clean |
| 43 | + |
| 44 | +# See all available commands |
| 45 | +make help |
| 46 | +``` |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +### Add a Job |
| 51 | + |
| 52 | +Add a new job to the queue with specified constraints: |
| 53 | + |
| 54 | +```bash |
| 55 | +./go-smart-queue add --name "my-job" --priority 10 --max-cpu 80 --max-memory 1024 --sleep 5 |
| 56 | +``` |
| 57 | + |
| 58 | +Options: |
| 59 | +- `--name, -n`: Job name (required) |
| 60 | +- `--priority, -p`: Job priority (default: 5, higher = higher priority) |
| 61 | +- `--max-cpu`: Maximum CPU percentage (default: 80.0) |
| 62 | +- `--max-memory`: Maximum memory in MB (default: 1024.0) |
| 63 | +- `--max-io`: Maximum IO ops per second (default: 1000) |
| 64 | +- `--sleep, -s`: Sleep duration in seconds for demo (default: 5) |
| 65 | +- `--command, -c`: Custom command to execute (optional) |
| 66 | + |
| 67 | +### List Jobs |
| 68 | + |
| 69 | +List all jobs (queued, running, and completed): |
| 70 | + |
| 71 | +```bash |
| 72 | +./go-smart-queue list |
| 73 | +``` |
| 74 | + |
| 75 | +### View Status |
| 76 | + |
| 77 | +Show system status and metrics: |
| 78 | + |
| 79 | +```bash |
| 80 | +./go-smart-queue status |
| 81 | +``` |
| 82 | + |
| 83 | +Show status of a specific job: |
| 84 | + |
| 85 | +```bash |
| 86 | +./go-smart-queue status --job job-1 |
| 87 | +``` |
| 88 | + |
| 89 | +### Cancel a Job |
| 90 | + |
| 91 | +Cancel a pending or running job: |
| 92 | + |
| 93 | +```bash |
| 94 | +./go-smart-queue cancel job-1 |
| 95 | +``` |
| 96 | + |
| 97 | +### Real-time Dashboard |
| 98 | + |
| 99 | +Display a real-time dashboard with live system metrics and job status: |
| 100 | + |
| 101 | +```bash |
| 102 | +./go-smart-queue dashboard |
| 103 | +``` |
| 104 | + |
| 105 | +Options: |
| 106 | +- `--refresh, -r`: Refresh interval in seconds (default: 2) |
| 107 | + |
| 108 | +## Architecture |
| 109 | + |
| 110 | +### Core Components |
| 111 | + |
| 112 | +1. **Job** - Represents a task with priority, constraints, and execution context |
| 113 | +2. **ResourceMonitor** - Monitors system resources (CPU, memory, IO) in real-time |
| 114 | +3. **Scheduler** - Manages job queue and worker pool |
| 115 | +4. **Worker Pool** - Executes jobs concurrently using goroutines |
| 116 | + |
| 117 | +### How It Works |
| 118 | + |
| 119 | +1. Jobs are added to a priority queue |
| 120 | +2. Resource monitor continuously tracks system metrics |
| 121 | +3. Scheduler dispatches jobs to workers when: |
| 122 | + - Workers are available |
| 123 | + - System resources meet job constraints |
| 124 | +4. Workers execute jobs concurrently using goroutines |
| 125 | +5. Job status is tracked throughout its lifecycle |
| 126 | + |
| 127 | +### Resource Throttling |
| 128 | + |
| 129 | +Jobs are only executed when system resources are below their defined constraints: |
| 130 | +- **CPU**: Job won't run if current CPU usage >= MaxCPUPercent |
| 131 | +- **Memory**: Job won't run if current memory usage >= MaxMemoryMB |
| 132 | +- **IO**: Job won't run if current IO ops/sec >= MaxIOOpsPerSec |
| 133 | + |
| 134 | +## Examples |
| 135 | + |
| 136 | +### High Priority Job |
| 137 | + |
| 138 | +```bash |
| 139 | +./go-smart-queue add --name "critical-backup" --priority 100 --max-cpu 90 --sleep 10 |
| 140 | +``` |
| 141 | + |
| 142 | +### Low Resource Job |
| 143 | + |
| 144 | +```bash |
| 145 | +./go-smart-queue add --name "email-task" --priority 5 --max-cpu 50 --max-memory 512 --sleep 3 |
| 146 | +``` |
| 147 | + |
| 148 | +### Multiple Jobs |
| 149 | + |
| 150 | +```bash |
| 151 | +# Add several jobs with different priorities |
| 152 | +./go-smart-queue add --name "job1" --priority 10 --sleep 5 |
| 153 | +./go-smart-queue add --name "job2" --priority 20 --sleep 5 |
| 154 | +./go-smart-queue add --name "job3" --priority 5 --sleep 5 |
| 155 | + |
| 156 | +# View the dashboard |
| 157 | +./go-smart-queue dashboard |
| 158 | +``` |
| 159 | + |
| 160 | +### Programmatic Usage |
| 161 | + |
| 162 | +See the `examples/` directory for a complete example of using the scheduler programmatically: |
| 163 | + |
| 164 | +```bash |
| 165 | +cd examples |
| 166 | +./run-example.sh |
| 167 | +``` |
| 168 | + |
| 169 | +The example demonstrates: |
| 170 | +- Creating multiple jobs with different priorities |
| 171 | +- Real-time monitoring of job execution |
| 172 | +- Resource-based throttling |
| 173 | +- Priority-based scheduling |
| 174 | + |
| 175 | +## Testing |
| 176 | + |
| 177 | +Run the test suite: |
| 178 | + |
| 179 | +```bash |
| 180 | +go test -v |
| 181 | +# Or using make |
| 182 | +make test |
| 183 | + |
| 184 | +# With coverage report |
| 185 | +make test-coverage |
| 186 | +``` |
| 187 | + |
| 188 | +## Configuration |
| 189 | + |
| 190 | +The scheduler is initialized with: |
| 191 | +- **Max Workers**: 4 (configurable in main.go) |
| 192 | +- **Resource Update Interval**: 1 second |
| 193 | +- **Job Check Interval**: 500 milliseconds |
| 194 | + |
| 195 | +## Development |
| 196 | + |
| 197 | +### Project Structure |
| 198 | + |
| 199 | +``` |
| 200 | +. |
| 201 | +├── main.go # CLI interface and main entry point |
| 202 | +├── job.go # Job structure and lifecycle |
| 203 | +├── scheduler.go # Scheduler and priority queue |
| 204 | +├── monitor.go # Resource monitoring |
| 205 | +├── scheduler_test.go # Scheduler tests |
| 206 | +├── monitor_test.go # Monitor tests |
| 207 | +├── go.mod # Go module definition |
| 208 | +└── README.md # This file |
| 209 | +``` |
| 210 | + |
| 211 | +### Dependencies |
| 212 | + |
| 213 | +- [github.com/spf13/cobra](https://github.com/spf13/cobra) - CLI framework |
| 214 | +- [github.com/shirou/gopsutil](https://github.com/shirou/gopsutil) - System metrics |
| 215 | + |
| 216 | +## License |
| 217 | + |
| 218 | +MIT License - see LICENSE file for details |
| 219 | + |
| 220 | +## Contributing |
| 221 | + |
| 222 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 223 | + |
| 224 | +## Author |
| 225 | + |
| 226 | +BaseMax |
| 227 | + |
0 commit comments