Skip to content

Commit fc41bbf

Browse files
Merge pull request #1 from anthonycorbacho/feat/update-qol
QOL update
2 parents ff17bbc + e129446 commit fc41bbf

5 files changed

Lines changed: 83 additions & 23 deletions

File tree

.github/workflows/test.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Scheduler Unit Test
2+
on:
3+
pull_request:
4+
types: [ opened, synchronize ]
5+
paths:
6+
- "*.go"
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
- name: Install Go
14+
uses: actions/setup-go@v4
15+
with:
16+
go-version: 1.20.x
17+
- name: Run Test
18+
run: go test -v -race -p=6 -cpu=1,4 ./...

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
11
## Scheduler
22

3-
This is a simple implementation of a FiFo Scheduler.
3+
**Scheduler** is a simple implementation of a FiFo Scheduler.
4+
5+
Get it:
6+
```
7+
go get -u github.com/anthonycorbacho/scheduler
8+
```
9+
10+
### Quick start
11+
It is very easy to get started, simply create an instance of the FiFoScheduler and start adding tasks.
12+
13+
```go
14+
package main
15+
16+
import (
17+
"fmt"
18+
19+
"github.com/anthonycorbacho/scheduler"
20+
)
21+
22+
func main() {
23+
s := NewFIFOScheduler()
24+
defer s.Stop()
25+
26+
jobCreator := func(i int) Job {
27+
return func(ctx context.Context) {
28+
fmt.Printf("got a task %d\n", i)
29+
}
30+
}
31+
32+
var jobs []Job
33+
for i := 0; i < 100; i++ {
34+
jobs = append(jobs, jobCreator(i))
35+
}
36+
37+
for _, j := range jobs {
38+
_ = s.Schedule(j)
39+
}
40+
}
41+
```
42+

fifoscheduler.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,26 @@ import (
66
"sync"
77
)
88

9-
// fifo represent as FIFO scheduler.
10-
type fifo struct {
11-
mu sync.Mutex
12-
resume chan struct{}
13-
scheduled int
14-
finished int
15-
pendings []Job
16-
ctx context.Context
17-
cancel context.CancelFunc
9+
// Verify interface compliance
10+
var _ Scheduler = (*Fifo)(nil)
11+
12+
// Fifo represent as FIFO scheduler.
13+
type Fifo struct {
14+
mu sync.Mutex
15+
resume chan struct{}
16+
scheduled int
17+
finished int
18+
pendings []Job
19+
ctx context.Context
20+
cancel context.CancelFunc
1821
finishCond *sync.Cond
1922
donec chan struct{}
2023
}
2124

2225
// NewFIFOScheduler returns a Scheduler that schedules jobs in FIFO
2326
// order sequentially
24-
func NewFIFOScheduler() Scheduler {
25-
f := &fifo{
27+
func NewFIFOScheduler() *Fifo {
28+
f := &Fifo{
2629
resume: make(chan struct{}, 1),
2730
donec: make(chan struct{}, 1),
2831
}
@@ -32,8 +35,8 @@ func NewFIFOScheduler() Scheduler {
3235
return f
3336
}
3437

35-
// Schedule schedules a job that will be ran in FIFO order sequentially.
36-
func (f *fifo) Schedule(j Job) error {
38+
// Schedule schedules a job that will be run in FIFO order sequentially.
39+
func (f *Fifo) Schedule(j Job) error {
3740
f.mu.Lock()
3841
defer f.mu.Unlock()
3942

@@ -51,25 +54,25 @@ func (f *fifo) Schedule(j Job) error {
5154
return nil
5255
}
5356

54-
func (f *fifo) Pending() int {
57+
func (f *Fifo) Pending() int {
5558
f.mu.Lock()
5659
defer f.mu.Unlock()
5760
return len(f.pendings)
5861
}
5962

60-
func (f *fifo) Scheduled() int {
63+
func (f *Fifo) Scheduled() int {
6164
f.mu.Lock()
6265
defer f.mu.Unlock()
6366
return f.scheduled
6467
}
6568

66-
func (f *fifo) Finished() int {
69+
func (f *Fifo) Finished() int {
6770
f.finishCond.L.Lock()
6871
defer f.finishCond.L.Unlock()
6972
return f.finished
7073
}
7174

72-
func (f *fifo) WaitFinish(n int) {
75+
func (f *Fifo) WaitFinish(n int) {
7376
f.finishCond.L.Lock()
7477
for f.finished < n || len(f.pendings) != 0 {
7578
f.finishCond.Wait()
@@ -78,15 +81,15 @@ func (f *fifo) WaitFinish(n int) {
7881
}
7982

8083
// Stop stops the scheduler and cancels all pending jobs.
81-
func (f *fifo) Stop() {
84+
func (f *Fifo) Stop() {
8285
f.mu.Lock()
8386
f.cancel()
8487
f.cancel = nil
8588
f.mu.Unlock()
8689
<-f.donec
8790
}
8891

89-
func (f *fifo) run() {
92+
func (f *Fifo) run() {
9093
// TODO: recover from job panic?
9194
defer func() {
9295
close(f.donec)
@@ -125,4 +128,4 @@ func (f *fifo) run() {
125128
f.finishCond.L.Unlock()
126129
}
127130
}
128-
}
131+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/anthonycorbacho/scheduler
22

3-
go 1.15
3+
go 1.20

scheduler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ type Scheduler interface {
2424

2525
// Stop stops the scheduler.
2626
Stop()
27-
}
27+
}

0 commit comments

Comments
 (0)