executor

package
v0.0.0-...-a08b8de Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: MIT Imports: 2 Imported by: 2

Documentation

Overview

Package executor contains a task executor and its related utilities. A task executor provides a simple interface for concurrently conducting multiple small number of tasks using a single goroutine for each one. It offers a signals channel to control the executor itself and a reports channel to get reports of the tasks as they get completed. The Task and Report are both interfaces and the client can specify any kind of logic in them. But it is recommended that a Task should be something that is simple and individual and should NOT spawn further goroutines. User can control the maximum simultaneous goroutines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Executor

type Executor struct {
	ActiveWorkers int64

	Tasks   chan Task
	Reports chan Report
	// contains filtered or unexported fields
}

The Executor struct is the main executor for tasks. 'maxWorkers' represents the maximum number of simultaneous goroutines. 'ActiveWorkers' tells the number of active goroutines spawned by the Executor at given time. 'Tasks' is the channel on which the Executor receives the tasks. 'Reports' is channel on which the Executor publishes the every tasks reports. 'signals' is channel that can be used to control the executor. Right now, only the termination signal is supported which is essentially is sending '1' on this channel by the client.

func NewExecutor

func NewExecutor(maxWorkers int, signals chan int) *Executor

NewExecutor creates a new Executor. 'maxWorkers' tells the maximum number of simultaneous goroutines. 'signals' channel can be used to control the Executor.

func (*Executor) AddTask

func (executor *Executor) AddTask(task Task) bool

AddTask is used to submit a new task to the Executor is a non-blocking way. The Client can submit a new task using the Executor's tasks channel directly but that will block if the tasks channel is full. It should be considered that this method doesn't add the given task if the tasks channel is full and it is up to client to try again later.

func (*Executor) Inactive

func (executor *Executor) Inactive() bool

Inactive checks if the Executor is idle. This happens when there are no pending tasks, active workers and reports to publish.

type Report

type Report interface {
	Status() int
	String() string
}

Report interface defines a report of a Job. A report must define how the Job went using the Status() method and a the description about the report using the String().

type Task

type Task interface {
	Execute() Report
	String() string
}

Task interface defines a task for the Executor. 'Execute' method is used to start the task which must return a Report. String() gives a small description about the Task.

Jump to

Keyboard shortcuts

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