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 (*Job) Run ¶
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()
type WorkerFunc ¶
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()
}
Click to show internal directories.
Click to hide internal directories.