FuncTask
FuncTask
allows execution of arbitrary functions with progress display and results.
Creating
Copy
Ask AI
task := ziva.NewFuncTask("Processing", func() error {
// perform work
time.Sleep(2 * time.Second)
return nil
})
Configuration Options
Copy
Ask AI
task := ziva.NewFuncTask(
"Data Loading",
processFunc,
ziva.WithSummaryFunction(func() []string {
return []string{"Processed: 100 files"}
}),
ziva.WithStopOnError(true), // stop queue on error
)
Example with Report
Copy
Ask AI
var stats struct {
Processed int
Errors int
}
task := ziva.NewFuncTask(
"File Processing",
func() error {
for i := 0; i < 100; i++ {
time.Sleep(50 * time.Millisecond)
stats.Processed++
if rand.Intn(20) == 0 {
stats.Errors++
}
}
return nil
},
ziva.WithSummaryFunction(func() []string {
return []string{
fmt.Sprintf("Processed: %d", stats.Processed),
fmt.Sprintf("Errors: %d", stats.Errors),
}
}),
)
queue := ziva.NewQueue("Batch Processing")
queue.AddTasks(task)
queue.Run()