A zero-dependency async task running library that aims to be simple, easy to use and flexible.
go get github.com/svaloumas/iocast
The module utilizes Go Generics internally, enabling the flexibility to define your custom structs to use as arguments and arbitrary result types in your tasks.
func DownloadContent(ctx context.Context, args *Args) (string, error) {
contentChan := make(chan []byte)
go func() {
contentChan <- fetchContent(args.addr, args.id)
close(contentChan)
}()
select {
case content := <-contentChan:
return saveToDisk(content)
case <-ctx.Done():
return "", ctx.Err()
}
}
func main() {
numOfWorkers := runtime.NumCPU()
p := iocast.NewWorkerPool(numOfWorkers, numOfWorkers*2)
p.Start(context.Background())
defer p.Stop()
args := &Args{addr: "http://somewhere.net", id: 1}
taskFn := iocast.NewTaskFunc(context.Background(), args, DownloadContent)
t := iocast.TaskBuilder(taskFn).
MaxRetries(2).
BackOff([]time.Duration{2*time.Second, 5*time.Second}).
Build()
p.Enqueue(t)
m := t.Metadata()
log.Printf("status: %s", m.Status)
result := <-t.Wait()
}
See examples for a detailed illustration of how to run simple tasks and linked tasks as pipelines.
- Generic Task Arguments. Pass any built-in or custom type as an argument to your tasks.
- Flexible Task Results. Return any type of value from your tasks.
- Context Awareness. Optionally include a context when running tasks.
- Retry attemtps. Define the number of retry attempts for each task.
- Task Pipelines. Chain tasks to execute sequentially, with the option to pass the result of one task as the argument for the next.
- Database Interface. Use the built-in in-memory database or use custom drivers for other storage engines by implementing an one-func interface.
- Task Metadata. Retrieve metadata such as status, creation time, execution time, and elapsed time. Metadata is also stored with the task results.
- Scheduler: Schedule tasks to run at a specific timestamp.
- Retries backoff mechanism: Set the duration of the intervals between failed retry attempts.
- Scheduler: Add support for periodic tasks.
go test -v ./...