Giter Club home page Giter Club logo

gotasks's Introduction

Go Report Card LICENSE

gotasks

A production inspired task parallelization library

Install

go get -u github.com/monime-lab/gotasks

Sample

package main

import (
	"context"
	"errors"
	"fmt"
	"github.com/monime-lab/gotasks"
	"github.com/monime-lab/gotries"
	"log"
	"time"
)

func saveToStore1() error {
	return nil
}

func saveToStore2() error {
	return nil
}

func getUserByID(id int) (interface{}, error) {
	return fmt.Sprintf("user-%d", id), nil
}

func main() {
	runnerExampleOne()
	runnerExampleTwo()
	schedulerExampleOne()
	schedulerExampleTwo()
}

func runnerExampleOne() {
	_, err := gotasks.NewTaskRunner( /* Options here... */).
		AddRunnableTask(func(ctx context.Context) error {
			return saveToStore1()
		}).
		AddRunnableTask(func(ctx context.Context) error {
			return saveToStore2()
		}).RunAndWaitAny(context.TODO())
	if err != nil {
		panic(err)
	}
	log.Printf("At least one of them succeeds!!!")
}

func runnerExampleTwo() {
	runner := gotasks.NewTaskRunner(
		// This is a fail fast switch useful
		// when calling runner.RunAndWaitAll()
		// The call will return on the first failure
		gotasks.WithEagerFail(true),
		// The maximum parallelism. This is a
		// concurrency rate-limiter for when
		// the number of tasks can be high.
		// At any point, there are at most `max`
		// task (goroutines) running concurrently.
		// Value < 1 means unbounded parallelism
		gotasks.WithMaxParallelism(10),
		// Syntactic sugar to WithMaxParallelism(1).
		// Useful for executing multiple tasks serially
		gotasks.WithSequentialParallelism(),
		// Default retry options for all the added tasks
		gotasks.WithRetryOptions(
			// Retry all tasks twice...
			gotries.WithMaxAttempts(2),
		),
	)
	for i := 1; i <= 5; i++ {
		func(id int) {
			runner.AddCallableTask(func(ctx context.Context) (interface{}, error) {
				return getUserByID(id)
			}, gotries.WithTaskName(fmt.Sprintf("RunnerTask-%d", i)))
		}(i)
	}
	users, err := runner.RunAndWaitAll(context.TODO())
	if err != nil {
		// The error(s) are composed using https://github.com/uber-go/multierr
		log.Fatalf("At least one failed. Error: %s", err)
	}
	log.Printf("Users: %s", users)
}

func schedulerExampleOne() {
	_ = gotasks.DefaultScheduler().Schedule(context.Background(), func(ctx context.Context) error {
		println("Printed after 1 second")
		return nil
	}, 1*time.Second)
	_ = gotasks.DefaultScheduler().Schedule(context.Background(), func(ctx context.Context) error {
		println("Printed after 2 seconds")
		return nil
	}, 2*time.Second)
	future3 := gotasks.DefaultScheduler().Schedule(context.Background(), func(ctx context.Context) error {
		println("Printed after 5 seconds")
		return errors.New("error after printing: 'Printed after 5 seconds")
	}, 5*time.Second)
	if err := future3.Wait(); err != nil {
		log.Fatal(err)
	}
}

func schedulerExampleTwo() {
	future := gotasks.DefaultScheduler().ScheduleAtFixedRate(context.Background(), func(ctx context.Context) error {
		fmt.Printf("Running at: %s\n", time.Now().Format(time.RFC3339))
		return errors.New("oops!!! What's wrong")
	}, 0, 1*time.Second)
	go func() {
		time.Sleep(10 * time.Second)
		println("Stopping the scheduled action")
		future.Cancel()
	}()
	err := future.Wait()
	log.Printf(":::::::::::::::::::: Stopped. Err: %v", err)
}

Contribute

For issues, comments, recommendation or feedback please do it here.

Contributions are highly welcome.

๐Ÿ‘

gotasks's People

Contributors

alphashaw avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.