asyncjob

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 4 Imported by: 0

README ΒΆ

Go GitHub go.mod Go version Go Report Card codecov License

Overview

AsyncJob is an asynchronous job manager with light code, clear and speed. I hope so ! 😬

Features

  • AsyncJob is a simple asynchronous job manager.
  • Full code coverage
  • Async queue
  • Define the number of asynchronous tasks (default: runtime.NumCPU())
  • Handling of managed and unmanaged errors
  • Provide a simple ETA
  • Full code description
Usage
package main

import (
	"github.com/lab210-dev/async-job"
	"log"
)

func main() {
	// Create a new AsyncJob
	asj := asyncjob.New[string]()

	// Set the number of asynchronous tasks (default: runtime.NumCPU())
	asj.SetWorkers(2)

	// Listen to the progress status
	asj.OnProgress(func(progress asyncjob.Progress) {
            log.Printf("Progress: %s\n", progress.String())
	})

	// Run all jobs 
	err := asj.Run(func(job asyncjob.Job[string]) error {
            // receive the job in job data function
            // if err return or panic, the job will be marked as failed and all progress will be canceled
            return nil
	}, []string{"Hello", "World"})

	// if a job returns an error, it stops the process
	if err != nil {
            log.Fatal(err)
	}
}

πŸ’‘ For better performance

Using a modulo to reduce the eta display (fast example)

package main

import (
	"github.com/lab210-dev/async-job"
	"log"
	"time"
)

func main() {
	// create slice of jobs
	var list []time.Duration
	for i := 1; i <= 100; i++ {
		list = append(list, time.Duration(1)*time.Millisecond)
	}
	err := asyncjob.New[time.Duration]().
		SetWorkers(2).
		OnProgress(func(progress asyncjob.Progress) {
			// Eta will be displayed every 10 jobs
			if progress.Current()%10 != 0 {
				return
			}
			// print the eta
		    log.Printf("Progress: %s\n", progress.String())
		}).
		Run(func(job asyncjob.Job[time.Duration]) error {
			// slow down the job
			time.Sleep(job.Data())
			return nil
		}, list)
	
	// if a job returns an error, it stops the process
	if err != nil {
		log.Fatal(err)
	}
}

🀝 Contributions

Contributors to the package are encouraged to help improve the code.

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type AsyncJob ΒΆ

type AsyncJob[T any] struct {
	// contains filtered or unexported fields
}

AsyncJob is a representation of AsyncJob processus

func New ΒΆ

func New[T any]() *AsyncJob[T]

New allows you to retrieve a new instance of AsyncJob

func (*AsyncJob[T]) GetWorkers ΒΆ

func (aj *AsyncJob[T]) GetWorkers() int

GetWorkers allows you to retrieve the number of workers

func (*AsyncJob[T]) OnProgress ΒΆ

func (aj *AsyncJob[T]) OnProgress(onProgressFunc func(progress Progress)) *AsyncJob[T]

OnProgress allows you set callback function for ETA

func (*AsyncJob[T]) Run ΒΆ

func (aj *AsyncJob[T]) Run(listener func(job Job[T]) error, data []T) (err error)

Run allows you to start the process

func (*AsyncJob[T]) SetWorkers ΒΆ

func (aj *AsyncJob[T]) SetWorkers(workers int) *AsyncJob[T]

SetWorkers allows you to set the number of asynchronous jobs

type Job ΒΆ

type Job[T any] struct {
	// contains filtered or unexported fields
}

Job is a representation of a unitary work

func (Job[T]) Data ΒΆ

func (j Job[T]) Data() T

Data is the value of a job

func (Job[T]) Index ΒΆ

func (j Job[T]) Index() int

Index the index of the original slice

type Progress ΒΆ

type Progress struct {
	// contains filtered or unexported fields
}

Progress is a representation of an asynchronous jobs.

func (Progress) Current ΒΆ

func (p Progress) Current() int

Current is the index of a current job

func (Progress) EstimateTimeLeft ΒΆ

func (p Progress) EstimateTimeLeft() time.Duration

EstimateTimeLeft is the estimated time left to finish all jobs

func (Progress) String ΒΆ

func (p Progress) String() string

String return content of Progress as string

func (Progress) Total ΒΆ

func (p Progress) Total() int

Total is the sum of all jobs

Jump to

Keyboard shortcuts

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