Categorygithub.com/plexsysio/taskmanager
modulepackage
0.0.0-20211220123746-de5ebdd49ae2
Repository: https://github.com/plexsysio/taskmanager.git
Documentation: pkg.go.dev

# README

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())
   }
}

# Functions

New creates a new taskmanager instance.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author

# Structs

TaskManager implements the public API for taskmanager.
TaskStatus is the status saved for the task.
WorkerInfo shows information on the worker.

# Interfaces

No description provided by the author
RestartableTask interface can be implemented to restart tasks on failures.
Task defines the interface to be implemented by users to enqueue tasks to the TaskManager.
TaskWithProgress defines additional functions to be implemented in order to query progress using the built-in taskmanager status.

# Type aliases

WorkerStatus is helper type for restricting the string values of worker status to known constants.