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.
- β‘ Priority-based job scheduling - Jobs are executed based on priority (higher values = higher priority)
- π Resource monitoring - Real-time monitoring of CPU, memory, and IO usage
- π― Resource constraints - Define maximum resource limits for jobs
- π Concurrent execution - Configurable worker pool using goroutines
- π Job cancellation - Cancel pending or running jobs
- π Real-time dashboard - Live view of system metrics and job status
- π» CLI interface - Easy-to-use command-line interface
git clone https://github.com/BaseMax/go-smart-queue.git
cd go-smart-queue
make buildOr using Go directly:
go build -o go-smart-queue# Build the application
make build
# Run tests
make test
# Run the example
make example
# Clean build artifacts
make clean
# See all available commands
make helpAdd a new job to the queue with specified constraints:
./go-smart-queue add --name "my-job" --priority 10 --max-cpu 80 --max-memory 1024 --sleep 5Options:
--name, -n: Job name (required)--priority, -p: Job priority (default: 5, higher = higher priority)--max-cpu: Maximum CPU percentage (default: 80.0)--max-memory: Maximum memory in MB (default: 1024.0)--max-io: Maximum IO ops per second (default: 1000)--sleep, -s: Sleep duration in seconds for demo (default: 5)--command, -c: Custom command to execute (optional)
List all jobs (queued, running, and completed):
./go-smart-queue listShow system status and metrics:
./go-smart-queue statusShow status of a specific job:
./go-smart-queue status --job job-1Cancel a pending or running job:
./go-smart-queue cancel job-1Display a real-time dashboard with live system metrics and job status:
./go-smart-queue dashboardOptions:
--refresh, -r: Refresh interval in seconds (default: 2)
- Job - Represents a task with priority, constraints, and execution context
- ResourceMonitor - Monitors system resources (CPU, memory, IO) in real-time
- Scheduler - Manages job queue and worker pool
- Worker Pool - Executes jobs concurrently using goroutines
- Jobs are added to a priority queue
- Resource monitor continuously tracks system metrics
- Scheduler dispatches jobs to workers when:
- Workers are available
- System resources meet job constraints
- Workers execute jobs concurrently using goroutines
- Job status is tracked throughout its lifecycle
Jobs are only executed when system resources are below their defined constraints:
- CPU: Job won't run if current CPU usage >= MaxCPUPercent
- Memory: Job won't run if current memory usage >= MaxMemoryMB
- IO: Job won't run if current IO ops/sec >= MaxIOOpsPerSec
./go-smart-queue add --name "critical-backup" --priority 100 --max-cpu 90 --sleep 10./go-smart-queue add --name "email-task" --priority 5 --max-cpu 50 --max-memory 512 --sleep 3# Add several jobs with different priorities
./go-smart-queue add --name "job1" --priority 10 --sleep 5
./go-smart-queue add --name "job2" --priority 20 --sleep 5
./go-smart-queue add --name "job3" --priority 5 --sleep 5
# View the dashboard
./go-smart-queue dashboardSee the examples/ directory for a complete example of using the scheduler programmatically:
cd examples
./run-example.shThe example demonstrates:
- Creating multiple jobs with different priorities
- Real-time monitoring of job execution
- Resource-based throttling
- Priority-based scheduling
Run the test suite:
go test -v
# Or using make
make test
# With coverage report
make test-coverageThe scheduler is initialized with:
- Max Workers: 4 (configurable in main.go)
- Resource Update Interval: 1 second
- Job Check Interval: 500 milliseconds
.
βββ main.go # CLI interface and main entry point
βββ job.go # Job structure and lifecycle
βββ scheduler.go # Scheduler and priority queue
βββ monitor.go # Resource monitoring
βββ scheduler_test.go # Scheduler tests
βββ monitor_test.go # Monitor tests
βββ go.mod # Go module definition
βββ README.md # This file
- github.com/spf13/cobra - CLI framework
- github.com/shirou/gopsutil - System metrics
MIT License - see LICENSE file for details
Contributions are welcome! Please feel free to submit a Pull Request.
BaseMax