From 64389d3fc187e6289adbd0b7b0e015178d29ca60 Mon Sep 17 00:00:00 2001 From: "Stanislav (Stas) Katkov" Date: Wed, 4 Dec 2024 00:12:26 +0100 Subject: [PATCH] Too much to do example --- examples/too-much-to-do/main.go | 74 +++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 examples/too-much-to-do/main.go diff --git a/examples/too-much-to-do/main.go b/examples/too-much-to-do/main.go new file mode 100644 index 0000000..a91d6e9 --- /dev/null +++ b/examples/too-much-to-do/main.go @@ -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 + } +}