-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from vaskoz/day10
Day10
- Loading branch information
Showing
6 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package day10 | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// Scheduler represents a job scheduler. | ||
type Scheduler struct { | ||
queue chan job | ||
wg sync.WaitGroup | ||
startTime time.Time | ||
} | ||
|
||
type job struct { | ||
f func() | ||
n uint | ||
jobStartTime time.Time | ||
} | ||
|
||
// NewScheduler returns a new instance of a running job scheduler. | ||
func NewScheduler() *Scheduler { | ||
s := &Scheduler{queue: make(chan job), startTime: time.Now()} | ||
s.wg.Add(1) | ||
go func() { | ||
for j := range s.queue { | ||
s.wg.Add(1) | ||
go func(j job) { | ||
ms, _ := time.ParseDuration(fmt.Sprintf("%dms", j.n)) | ||
time.Sleep(ms) | ||
log.Printf("starting job at %dms", convertNanoToMillis(time.Since(s.startTime).Nanoseconds())) | ||
j.f() | ||
log.Printf("completed job at %dms", convertNanoToMillis(time.Since(s.startTime).Nanoseconds())) | ||
s.wg.Done() | ||
}(j) | ||
} | ||
}() | ||
return s | ||
} | ||
|
||
// Schedule schedules a new job to be run. | ||
func (s *Scheduler) Schedule(f func(), millis uint) { | ||
current := convertNanoToMillis(time.Since(s.startTime).Nanoseconds()) | ||
log.Printf("Received at %dms scheduled for %dms", current, current+int64(millis)) | ||
s.queue <- job{f, millis, time.Now()} | ||
} | ||
|
||
// Shutdown prevents new jobs from being scheduled and allows the scheduler to terminate. | ||
func (s *Scheduler) Shutdown() { | ||
close(s.queue) | ||
s.wg.Done() | ||
s.wg.Wait() | ||
log.Printf("Scheduler shutdown complete at %dms", convertNanoToMillis(time.Since(s.startTime).Nanoseconds())) | ||
} | ||
|
||
func convertNanoToMillis(duration int64) int64 { | ||
return duration / int64(1000000) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package day10 | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestScheduler(t *testing.T) { | ||
t.Parallel() | ||
sched := NewScheduler() | ||
sched.Schedule(func() { fmt.Println("hi there") }, 10) | ||
sched.Schedule(func() { fmt.Println("hello there") }, 20) | ||
sched.Schedule(func() { fmt.Println("so long") }, 30) | ||
sched.Schedule(func() { time.Sleep(1 * time.Second); fmt.Println("i fell asleep") }, 1) | ||
sched.Shutdown() | ||
} | ||
|
||
func TestSchedulerParallel(t *testing.T) { | ||
t.Parallel() | ||
sched := NewScheduler() | ||
sched.Schedule(func() { fmt.Println("hi there") }, 10) | ||
sched.Schedule(func() { fmt.Println("hello there") }, 20) | ||
sched.Schedule(func() { fmt.Println("so long") }, 30) | ||
sched.Schedule(func() { time.Sleep(2 * time.Second); fmt.Println("i fell asleep again") }, 1) | ||
time.Sleep(2 * time.Second) | ||
sched.Schedule(func() { fmt.Println("later jobs") }, 1) | ||
sched.Schedule(func() { fmt.Println("later still") }, 10) | ||
sched.Shutdown() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters