Coder Social home page Coder Social logo

async's Introduction

Async library

Async library supports following features to handle ong background tasks

  • Running multiple tasks without Wait using GoWithContext
  • Running multiple tasks with Wait and error if any using GoWithWait
  • Running multiple tasks without Context and Wait using Go

Apart from above featues it also handles

  • Timeout event while running tasks
  • Panic in tasks
  • Logs default Key Value saved in context (e.g. trace-id, taskname etc.) for better task tracking

Installation

go get -u github.com/netbookai/[email protected]

Usage

async supports following methods GoWithContext, Go, GoWithWait

Using GoWithContext

  //....
	ctx := context.Background()
	logger := log.NewLogger(zap.NewLogger())
	timeout := 20 * time.Second

	// run as goroutine with timeout and error handlers
	async.New().Add(
		"testTask1",
		func(taskCtx context.Context) error {
			return testTask1(taskCtx, logger)
		},
	).Add(
		"testTask2",
		func(taskCtx context.Context) error {
			return testTask2(taskCtx, logger)
		},
	).GoWithContext(ctx, timeout, logger)

  //...
  

Sample Output

Alt text

Using GoWithWait

	ctx := context.Background()
	logger := log.NewLogger(zap.NewLogger())
	timeout := 20 * time.Second

	// run as goroutine with timeout and error handlers
	err := async.New().Add(
		"testTask1",
		func(taskCtx context.Context) error {
			return testTask2(taskCtx, logger)
		},
	).Add(
		"testTask2",
		func(taskCtx context.Context) error {
			return testTask2(taskCtx, logger)
		},
	).GoWithWait(ctx, timeout, logger)

	//waiting for tasks to finish

	if err != nil {
		logger.Error(ctx, "error occured", "error", err)
		return
	}

	//resuming next task
	//...

Using Go

  //....
	logger := log.NewLogger(zap.NewLogger())
	timeout := 20 * time.Second

	// run as goroutine with timeout and error handlers
	
	async.New().Add(
		"testTask1",
		func() error {
			return testTask1(taskCtx, logger)
		},
	).Add(
		"testTask2",
		func(taskCtx context.Context) error {
			return testTask2(taskCtx, logger)
		},
	).Go(timeout, logger)

  //...

Support

Raise a PR, we will get back to you.

TODO

  • add retry mechanism for task based by filtering permanent vs temporary errors

Authors and acknowledgment

License

GNU GENERAL PUBLIC LICENSE

async's People

Contributors

trivishnu avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

imaskm

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.