krun

package module
v0.0.0-...-4f1a9d4 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2023 License: MIT Imports: 3 Imported by: 1

README

Krun - QueueRun

Build Krun

Krun is a simple worker queue, which provides an easy way to manage and execute jobs concurrently. It can wait for all jobs to finish, and get the results from the executed jobs. The package can be found at github.com/falmar/krun.

Installation

To install the Krun package, simply run:

go get -u github.com/falmar/krun

Usage

Here's a basic example of how to use the Krun package:

package main

import (
	"context"
	"fmt"
	"os"
	"time"

	"github.com/falmar/krun"
)

func main() {
	queue := krun.New(&krun.Config{
		Size:      5, // number of workers
		WaitSleep: time.Microsecond,
	})

	job := func(ctx context.Context) (interface{}, error) {
		time.Sleep(time.Millisecond * 100)
		return "Hello, Krun!", nil
	}

	ctx := context.Background()
	resChan := queue.Run(ctx, job)

	res := <-resChan
	if res.Error != nil {
		fmt.Println("Error:", res.Error)
		os.Exit(1)
	}

	queue.Wait(ctx)

	fmt.Println("Result:", res.Data.(string))
}

License

Krun is released under the MIT License. See LICENSE for more information.

TODO:

  • unit test
  • go releaser

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Size      int
	WaitSleep time.Duration
}

type Job

type Job func(ctx context.Context) (interface{}, error)

type Krun

type Krun interface {
	Run(ctx context.Context, f Job) <-chan *Result
	Wait(ctx context.Context)
	Size() int
}

func New

func New(cfg *Config) Krun

type Result

type Result struct {
	Data  interface{}
	Error error
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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