-
Notifications
You must be signed in to change notification settings - Fork 1
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 #11 from skatkov/too-much-to-do
Too much to do example
- Loading branch information
Showing
1 changed file
with
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/fumeapp/taskin" | ||
) | ||
|
||
type Library struct { | ||
Name string | ||
} | ||
|
||
func main() { | ||
libraries := generateLibraries(20) | ||
runners := NewLibraryDownloader(libraries) | ||
|
||
runners.Run() | ||
} | ||
|
||
func generateLibraries(count int) []Library { | ||
libraries := make([]Library, count) | ||
for i := 0; i < count; i++ { | ||
libraries[i] = Library{ | ||
Name: fmt.Sprintf("Library%d", i+1), | ||
} | ||
} | ||
return libraries | ||
} | ||
|
||
func NewLibraryDownloader(libraries []Library) taskin.Runners { | ||
var tasks []taskin.Task | ||
|
||
for _, library := range libraries { | ||
tasks = append(tasks, createTasksForLibrary(library)) | ||
} | ||
|
||
if len(tasks) == 0 { | ||
fmt.Println("No tasks to run") | ||
return nil | ||
} | ||
|
||
return taskin.New(tasks, taskin.Defaults) | ||
} | ||
|
||
func createTasksForLibrary(library Library) taskin.Task { | ||
return taskin.Task{ | ||
Title: fmt.Sprintf("Process %s", library.Name), | ||
Tasks: taskin.Tasks{ | ||
{ | ||
Title: "Download", | ||
Task: createSimulatedTask(500, 2000), | ||
}, | ||
{ | ||
Title: "Unarchive", | ||
Task: createSimulatedTask(200, 1000), | ||
}, | ||
{ | ||
Title: "Process", | ||
Task: createSimulatedTask(300, 1500), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func createSimulatedTask(minDuration, maxDuration int) func(*taskin.Task) error { | ||
return func(_ *taskin.Task) error { | ||
duration := time.Duration(rand.Intn(maxDuration-minDuration)+minDuration) * time.Millisecond | ||
time.Sleep(duration) | ||
|
||
return nil | ||
} | ||
} |