-
Notifications
You must be signed in to change notification settings - Fork 9
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 #44 from Venafi/scheduler
Scheduler
- Loading branch information
Showing
4 changed files
with
299 additions
and
99 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
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,95 @@ | ||
package pki | ||
|
||
import ( | ||
"log" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
type backgroundTask struct { | ||
name string | ||
f func() | ||
workers int64 | ||
currentWorkers int64 | ||
interval time.Duration | ||
lastRun time.Time | ||
} | ||
|
||
type taskStorageStruct struct { | ||
tasks []backgroundTask | ||
sync.RWMutex | ||
} | ||
|
||
func (task *backgroundTask) cancel() { | ||
|
||
} | ||
|
||
func (s *taskStorageStruct) getTasksNames() []string { | ||
s.RLock() | ||
defer s.RUnlock() | ||
l := make([]string, len(s.tasks)) | ||
for i := range s.tasks { | ||
l[i] = s.tasks[i].name | ||
} | ||
return l | ||
} | ||
|
||
func (s *taskStorageStruct) register(name string, f func(), count int, interval time.Duration) { | ||
s.Lock() | ||
defer s.Unlock() | ||
task := backgroundTask{name: name, f: f, workers: int64(count), interval: interval} | ||
for i := range s.tasks { | ||
if s.tasks[i].name == task.name { | ||
log.Printf("duplicated task %v", name) | ||
return | ||
} | ||
} | ||
s.tasks = append(s.tasks, task) | ||
} | ||
|
||
func (s *taskStorageStruct) del(taskName string) { | ||
s.Lock() | ||
defer s.Unlock() | ||
for i := range s.tasks { | ||
if s.tasks[i].name == taskName { | ||
s.tasks[i].cancel() | ||
s.tasks = append(s.tasks[:i], s.tasks[i+1:]...) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (s *taskStorageStruct) init() { | ||
go s.loop() | ||
} | ||
|
||
func (s *taskStorageStruct) loop() { | ||
for { | ||
s.RLock() | ||
for i := range s.tasks { | ||
if s.tasks[i].currentWorkers >= s.tasks[i].workers { | ||
continue | ||
} | ||
if time.Since(s.tasks[i].lastRun) < s.tasks[i].interval { | ||
continue | ||
} | ||
currentTask := &s.tasks[i] | ||
atomic.AddInt64(¤tTask.currentWorkers, 1) | ||
go func(counter *int64) { | ||
defer func(counter *int64) { | ||
r := recover() | ||
if r != nil { | ||
log.Printf("job failed. recover: %v\n", r) | ||
//todo: better log | ||
} | ||
atomic.AddInt64(counter, -1) | ||
}(counter) | ||
currentTask.f() | ||
}(¤tTask.currentWorkers) | ||
currentTask.lastRun = time.Now() | ||
} | ||
s.RUnlock() | ||
time.Sleep(time.Second) | ||
} | ||
} |
Oops, something went wrong.