GithubHelp home page GithubHelp logo

asabya / taskmanager Goto Github PK

View Code? Open in Web Editor NEW

This project forked from plexsysio/taskmanager

0.0 0.0 0.0 54 KB

Async Task manager

License: BSD 3-Clause "New" or "Revised" License

Go 100.00%

taskmanager's Introduction

taskmanager Go Go Reference Coverage Status

Async task manager. Tasks can easily be customized and executed asynchronously on the next available worker.

The manager keeps workers ready to multiplex tasks. The maximum no. of workers can be configured.

This package was mainly created to abstract all async functionality from the app. It provides a consistent context interface to manage routine lifecycle from a single place.

Install

taskmanager works like a regular Go module:

> go get github.com/plexsysio/taskmanager

Usage

import "github.com/plexsysio/taskmanager"

type exampleTask struct {}

func (e *exampleTask) Name() string {
   return "exampleTask"
}

func (e *exampleTask) Execute(ctx context.Context) error {
   for {
      select {
      case <-ctx.Done():
         // taskmanager stopped
         return nil
      default:
        // Do work. For long running tasks use ctx or move to next iteration
      }
   }
}

func main() {

   tm := taskmanager.New(1, 100, time.Second*15)
   t := &exampleTask{}

   sched, err := tm.Go(t)
   if err != nil {
      fmt.Println(err)
      return
   }

   // Task scheduled
   <-sched
   

Closures can also be scheduled

   fSched, err := tm.GoFunc(func(ctx context.Context) error {
      for {
         select {
         case <-ctx.Done():
            //taskmanager stopped
            return nil
         default:
            // Do work
         }
      }
   })
   if err != nil {
      fmt.Println(err)
      return
   }

   // Stop will wait for all routines to stop. Context can be passed here to
   // ensure timeout in Stop
   ctx, _ := context.WithTimeout(time.Second)
   err = tm.Stop(ctx)
   if err != nil {
      fmt.Printf("failed stopping %s\n", err.Error())
   }
}

taskmanager's People

Contributors

aloknerurkar avatar asabya 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.