spider-pigs / funcrunner

A small golang library that runs a set of functions safely and returns simple stats.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

funcrunner

Build Status Go Report Card GoDoc

A small golang library that runs a set of functions safely (without panics) and returns simple stats.

Install

import "github.com/spider-pigs/funcrunner"

Usage

package main

import (
	"context"
	"errors"
	"fmt"
	"time"

	"github.com/spider-pigs/funcrunner"
)

func main() {
	// create functions that return an error and takes context as param
	ctx := context.Background()
	f1 := func(ctx context.Context) error {
		return nil
	}
	f2 := func(ctx context.Context) error {
		return errors.New("I can't take it anymore")
	}

	// create the runner
	runner := funcrunner.Runner{}
	runner.FuncDone = func(f funcrunner.Func, duration time.Duration, err error) {
		fmt.Printf("function %s has completed, took %s.\n", f, duration)
	}

	// Start the runner
	duration := runner.RunFunc(ctx, f1, f2)
	fmt.Printf("runner has completed, took %s..\n", duration)
}

About

A small golang library that runs a set of functions safely and returns simple stats.

License:MIT License


Languages

Language:Go 100.0%