presets

package
v2.2.3 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2021 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Job

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

Job is a primitive worker who performs an `action` callback with a given period.

func NewJob

func NewJob(period time.Duration, action func() error) *Job

NewJob create new job with given `period`.

func (*Job) Init

func (j *Job) Init() error

Init is a method to satisfy `uwe.Worker` interface.

func (*Job) Run

func (j *Job) Run(ctx uwe.Context) error

Run executes the `action` callback with the specified `period` until a stop signal is received.

Example
var action = func() error {
	// define all the processing code here
	// or move it to a method and make a call here
	log.Println("do something")
	return nil
}

// initialize new instance of Chief
chief := uwe.NewChief()
chief.UseDefaultRecover()
chief.SetEventHandler(uwe.STDLogEventHandler())

// will add workers into the pool
chief.AddWorker("simple-job", NewJob(time.Second, action))

chief.Run()
Output:

type WorkerFunc

type WorkerFunc func(ctx uwe.Context) error

WorkerFunc is a type of worker that consist from one function. Allow to use the function as worker.

func (WorkerFunc) Init

func (WorkerFunc) Init() error

Init is a method to satisfy `uwe.Worker` interface.

func (WorkerFunc) Run

func (f WorkerFunc) Run(ctx uwe.Context) error

Run executes function as worker.

Example
package main

import (
	"log"
	"time"

	"github.com/lancer-kit/uwe/v2"
)

type dummy struct {
	tickDuration time.Duration
}

func (d *dummy) doSomething(ctx uwe.Context) error {
	// initialize all required stuffs for the execution flow
	ticker := time.NewTicker(d.tickDuration)

	for {
		select {
		case <-ticker.C:
			// define all the processing code here
			// or move it to a method and make a call here
			log.Println("do something")
		case <-ctx.Done():
			// close all connections, channels and finalise state if needed
			log.Println("good bye")
			return nil
		}
	}
}

func main() {
	var anonFuncWorker = func(ctx uwe.Context) error {
		// initialize all required stuffs for the execution flow
		ticker := time.NewTicker(time.Second)
		for {
			select {
			case <-ticker.C:
				// define all the processing code here
				// or move it to a method and make a call here
				log.Println("do something")
			case <-ctx.Done():
				// close all connections, channels and finalise state if needed
				log.Println("good bye")
				return nil
			}
		}
	}
	var dummyWorker = dummy{tickDuration: time.Second}

	// initialize new instance of Chief
	chief := uwe.NewChief()
	chief.UseDefaultRecover()
	chief.SetEventHandler(uwe.STDLogEventHandler())

	// will add workers into the pool
	chief.AddWorker("anon-func", WorkerFunc(anonFuncWorker))
	chief.AddWorker("method-as-worker", WorkerFunc(dummyWorker.doSomething))

	chief.Run()
}
Output:

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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