Skip to content

Commit cb8a3d7

Browse files
authored
Merge pull request #1 from BaseMax/copilot/design-go-job-scheduler
Add Go job scheduler with resource monitoring
2 parents d8da5f0 + 038083a commit cb8a3d7

15 files changed

Lines changed: 1911 additions & 1 deletion

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Binaries
2+
go-smart-queue
3+
example
4+
debug_example
5+
*.exe
6+
*.dll
7+
*.so
8+
*.dylib
9+
10+
# Test binary
11+
*.test
12+
13+
# Output of the go coverage tool
14+
*.out
15+
coverage.html
16+
17+
# Dependency directories
18+
vendor/
19+
20+
# IDE files
21+
.vscode/
22+
.idea/
23+
*.swp
24+
*.swo
25+
*~
26+
27+
# OS files
28+
.DS_Store
29+
Thumbs.db

Makefile

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Makefile for go-smart-queue
2+
3+
.PHONY: all build test clean install example help
4+
5+
# Default target
6+
all: build
7+
8+
# Build the main CLI application
9+
build:
10+
@echo "Building go-smart-queue..."
11+
@go build -o go-smart-queue .
12+
13+
# Run tests
14+
test:
15+
@echo "Running tests..."
16+
@go test -v .
17+
18+
# Run tests with coverage
19+
test-coverage:
20+
@echo "Running tests with coverage..."
21+
@go test -v -cover -coverprofile=coverage.out .
22+
@go tool cover -html=coverage.out -o coverage.html
23+
@echo "Coverage report generated: coverage.html"
24+
25+
# Clean build artifacts
26+
clean:
27+
@echo "Cleaning..."
28+
@rm -f go-smart-queue
29+
@rm -f coverage.out coverage.html
30+
@rm -f examples/example-run
31+
32+
# Install dependencies
33+
deps:
34+
@echo "Installing dependencies..."
35+
@go mod download
36+
@go mod tidy
37+
38+
# Run the example
39+
example:
40+
@echo "Running example..."
41+
@cd examples && ./run-example.sh
42+
43+
# Install the CLI globally
44+
install: build
45+
@echo "Installing go-smart-queue..."
46+
@go install .
47+
48+
# Show help
49+
help:
50+
@echo "Available targets:"
51+
@echo " build - Build the CLI application"
52+
@echo " test - Run tests"
53+
@echo " test-coverage - Run tests with coverage report"
54+
@echo " clean - Clean build artifacts"
55+
@echo " deps - Install dependencies"
56+
@echo " example - Run the example program"
57+
@echo " install - Install CLI globally"
58+
@echo " help - Show this help message"

README.md

Lines changed: 226 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,227 @@
11
# 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+

examples/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Examples
2+
3+
This directory contains example programs demonstrating the use of go-smart-queue.
4+
5+
## Running the Example
6+
7+
The example demonstrates using the job scheduler programmatically with multiple jobs of different priorities.
8+
9+
### Build and Run
10+
11+
```bash
12+
# From the examples directory
13+
go run example.go -I .. -tags example
14+
15+
# Or build from the root directory
16+
cd ..
17+
go run ./examples/example.go ./job.go ./monitor.go ./scheduler.go
18+
```
19+
20+
Alternatively, you can copy the relevant source files:
21+
22+
```bash
23+
cp ../job.go ../monitor.go ../scheduler.go .
24+
go run *.go
25+
```
26+
27+
## Demo Script
28+
29+
The `demo.sh` script demonstrates the CLI functionality:
30+
31+
```bash
32+
./demo.sh
33+
```
34+
35+
Note: The demo script requires building the main CLI application first from the root directory.
36+
37+
## Example Output
38+
39+
The example will:
40+
1. Create 5 jobs with different priorities
41+
2. Monitor their execution in real-time
42+
3. Display system metrics (CPU, memory, IO)
43+
4. Show final results with job completion status and duration
44+
45+
You should see output similar to:
46+
47+
```
48+
Starting job scheduler example...
49+
50+
Added job: Data Processing (ID: job-1, Priority: 20)
51+
Added job: Email Notifications (ID: job-2, Priority: 15)
52+
...
53+
54+
Jobs added to queue. Monitoring execution...
55+
56+
→ Started: Processing data
57+
→ Started: Generating reports
58+
[20:11:20] Stats: Queued=3, Active=2, Completed=0 | CPU=0.0%, Mem=1519MB
59+
...
60+
61+
All jobs completed!
62+
```

0 commit comments

Comments
 (0)