multistatus

package module
v0.0.0-...-2c3aefe Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2017 License: MIT Imports: 8 Imported by: 3

README

multistatus GoDoc

Use the terminal to show the status of multiple concurrent processes in Go.

Installation

go get github.com/zikes/multistatus

Example

// Create a new WorkerSet
workerSet := multistatus.New()

// Populate the WorkerSet with Workers
for i := 0; i < 10; i++ {

  // Create and return a Worker
  worker := workerSet.Add(fmt.Sprintf("Task #%d", i))
  go func(w *multistatus.Worker) {
    // Sleep, then finish the worker
    time.Sleep(time.Second * time.Duration(rand.Intn(5)))
    worker.Done()
  }(worker)
}

// Print the WorkerSet's status until all Workers have completed
workerSet.Print(context.Background())

License

MIT

Documentation

Overview

Package multistatus will print a continuously updating block of text to the stdout to show the current status of a set of concurrent goroutines.

Usage:

import (
	"context"
	"fmt"
	"math/rand"
	"time"

	ms "github.com/zikes/multistatus"
)

func main() {
	// Create a new WorkerSet
	ws := ms.New()

	// Populate the WorkerSet with Workers
	for i := 0; i < 10; i++ {
		w := ws.Add(fmt.Sprintf("Task #%d", i))
		go func(w *ms.Worker) {
			// Sleep for 0-8 seconds, then tell the Worker it failed or completed
			time.Sleep(time.Millisecond * time.Duration(rand.Intn(8000)))
			if rand.Intn(5) == 1 {
				w.Fail()
			} else {
				w.Done()
			}
		}(w)
	}

	// Print the WorkerSet's status until all Workers have completed
	ws.Print(context.Background())
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Worker

type Worker struct {
	State WorkerState
	Name  string
	// contains filtered or unexported fields
}

Worker is used to track the status of a worker task

func (*Worker) Active

func (w *Worker) Active() bool

Active will return `true` if the Worker.State is Pending

func (*Worker) Done

func (w *Worker) Done()

Done will set the Worker.State to Completed and decrement the parent WorkerSet's sync.WaitGroup

func (*Worker) Fail

func (w *Worker) Fail()

Fail will set the Worker.State to Fail and decrement the parent WorkerSet's sync.WaitGroup

type WorkerSet

type WorkerSet struct {
	Workers []*Worker
	// contains filtered or unexported fields
}

A WorkerSet is a collection of Workers

func New

func New() *WorkerSet

New returns an empty WorkerSet

func (*WorkerSet) Add

func (w *WorkerSet) Add(s string) *Worker

Add creates and returns a new Worker, and increments the WorkerSet's sync.WaitGroup

func (*WorkerSet) Print

func (w *WorkerSet) Print(ctx context.Context)

Print initiates the WorkerSet's sync.WaitGroup.Wait() and continuously prints the status of all the Workers in its collection, cancelable via context cancelation.

If the stdout is determined to not be a terminal then it will not print until the WaitGroup has finished, and its output will be free of terminal escapes.

type WorkerState

type WorkerState int

WorkerState represent the current state of a Worker

const (
	Completed WorkerState = iota
	Failed
	Pending
)

Available Worker states

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL