Skip to content

Commit

Permalink
Merge pull request #11 from skatkov/too-much-to-do
Browse files Browse the repository at this point in the history
Too much to do example
  • Loading branch information
acidjazz authored Feb 14, 2025
2 parents fb0bb00 + 64389d3 commit 89e6c59
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions examples/too-much-to-do/main.go
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
}
}

0 comments on commit 89e6c59

Please sign in to comment.