task

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2021 License: MIT Imports: 5 Imported by: 0

README

gotask

Go distributed task executor

Getting Started

Download
go get github.com/inuggets/gotask
Usage
Worker
import (
    task "github.com/inuggets/gotask"
    "github.com/inuggets/nmq"
)

type Addend struct {
    A int64
    B int64
}

// New worker
w := task.NewWorker(
    "test", // Worker name
    nmq.NewRedisPool("localhost:6379", ""), // redis pool
    &task.WorkerOptions{ // options
        Processors: 2,
    },
)

// Register 'add' task
w.AddTask(&Task{
    ID:   "add",
    Func: func(payload string) error {
        // Run task
        addend := &Addend{}
        err := json.Unmarshal([]byte(payload), addend)
        if err != nil {
            return err
        }
        log.Default().Println(fmt.Sprintf("%d + %d = %d\n", addend.A, addend.B, addend.A+addend.B))
        return nil
    },
})

// Start worker
go w.Start()
Client
import (
    task "github.com/inuggets/gotask"
    "github.com/inuggets/nmq"
)

client := task.NewClient(nmq.NewRedisPool("localhost:6379", ""), "test")
addend := &Addend{
    A: 1,
    B: 2,
}
bytes, err := json.Marshal(addend)
if err != nil {
    log.Fatal(err)
}

// Submit 'add' task
err = client.SubmitTask("add", string(bytes))
if err != nil {
    log.Fatal(err)
}

License

gotask is under the MIT license. See the LICENSE for detail.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(pool *redis.Pool, worker string) *Client

func (*Client) SubmitTask

func (c *Client) SubmitTask(taskId string, payload string) error

type Message

type Message struct {
	QueueMessage *nmq.Message `json:"-"`
	TaskId       string       `json:"taskId"`
	Payload      string       `json:"payload"`
}

type Task

type Task struct {
	ID   string
	Func func(payload string) error
}

type Worker

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

func NewWorker

func NewWorker(name string, pool *redis.Pool, opt ...*WorkerOptions) *Worker

func (*Worker) AddTask

func (w *Worker) AddTask(task *Task)

func (*Worker) Broker

func (w *Worker) Broker() nmq.Broker

func (*Worker) Start

func (w *Worker) Start()

func (*Worker) Stop

func (w *Worker) Stop()

type WorkerOptions

type WorkerOptions struct {
	Processors int
}

Jump to

Keyboard shortcuts

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