Skip to main content

FuncTask

FuncTask allows execution of arbitrary functions with progress display and results.

Creating

task := ziva.NewFuncTask("Processing", func() error {
    // perform work
    time.Sleep(2 * time.Second)
    return nil
})

Configuration Options

task := ziva.NewFuncTask(
    "Data Loading",
    processFunc,
    ziva.WithSummaryFunction(func() []string {
        return []string{"Processed: 100 files"}
    }),
    ziva.WithStopOnError(true), // stop queue on error
)

Controlling the completion label

By default a completed FuncTask renders a “DONE” badge on the right.
task := ziva.NewFuncTask("Build", build).
    WithSuccessLabel("FINISHED") // custom text
Hide the badge completely when it is not needed:
task := ziva.NewFuncTask("Background setup", prepare).
    WithoutSuccessLabel()
You can achieve the same result with functional options passed to the constructor:
task := ziva.NewFuncTask(
    "Import data",
    importData,
    ziva.WithoutSuccessLabelOption(),       // disable the badge
    ziva.WithSummaryFunction(renderSummary),
)
Later you may re-enable the badge with WithSuccessLabelEnabled(true) or swap the text using WithSuccessLabel(...).

Example with Report

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()