
uwe
UWE (Ubiquitous Workers Engine) is a common toolset for building and organizing your Go application, actor-like workers.
Table of Content
- Quick Start
- Documentation
2. Chief
3. Worker
4. Presets
Quick Start
Get uwe
using go get:
go get git.ooo.ua/pub/uwe/v2
Here is an example HelloWorld service with HTTP API and background worker:
package main
import (
"fmt"
"log"
"net/http"
"time"
"git.ooo.ua/pub/uwe/v2"
"git.ooo.ua/pub/uwe/v2/presets/api"
)
func main() {
apiCfg := api.Config{
Host: "0.0.0.0",
Port: 8080,
EnableCORS: false,
ApiRequestTimeout: 0,
}
chief := uwe.NewChief()
chief.AddWorker("app-server", api.NewServer(apiCfg, getRouter()))
chief.AddWorker("dummy", NewDummy())
chief.UseDefaultRecover()
chief.SetEventHandler(uwe.STDLogEventHandler())
chief.Run()
}
type dummy struct{}
func NewDummy() uwe.Worker {
return &dummy{}
}
func (d *dummy) Init() error { return nil }
func (d *dummy) Run(ctx uwe.Context) error {
ticker := time.NewTicker(time.Second)
for {
select {
case <-ticker.C:
log.Println("do something")
case <-ctx.Done():
log.Println("good bye")
return nil
}
}
}
func getRouter() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/hello/uwe", func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, "hello world")
})
log.Println("REST API router initialized")
return mux
}
Documentation
Chief
Chief is a supervisor that can be placed at the top of the go application's execution stack,
it is blocked until SIGTERM is intercepted and then it shutdown all workers gracefully.
Also, Chief
can be used as a child supervisor inside the Worker
, which is launched by Chief
at the top-level.
Worker
Worker is an interface for async workers which launches and manages by the Chief.
Init()
- method used to initialize some state of the worker that required interaction with outer context,
for example, initialize some connectors. In many cases this method is optional, so it can be implemented as empty:
func (*W) Init() error { return nil }
.Run(ctx Context) error
- starts the Worker
instance execution. The context will provide a signal
when a worker must stop through the ctx.Done()
.
Workers lifecycle:
(*) -> [New] -> [Initialized] -> [Run] -> [Stopped]
| | |
| | ↓
|-------------|------> [Failed]
Presets
This library provides some working presets to simplify the use of Chief
in projects and reduce duplicate code.
HTTP Server
api.Server
is worker by default for starting a standard HTTP server. Server requires configuration and initialized http.Handler
.
The HTTP server will work properly and will be correctly disconnected upon a signal from Supervisor (Chief).
Warning: this Server does not process SSL/TLS certificates on its own.To start an HTTPS server, look for a specific worker.
package main
import (
"fmt"
"log"
"net/http"
"git.ooo.ua/pub/uwe/v2"
)
func main() {
apiCfg := Config{
Host: "0.0.0.0",
Port: 8080,
EnableCORS: false,
ApiRequestTimeout: 0,
}
chief := uwe.NewChief()
chief.UseDefaultRecover()
chief.SetEventHandler(uwe.STDLogEventHandler())
mux := http.NewServeMux()
mux.HandleFunc("/hello/uwe", func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, "hello world")
})
chief.AddWorker("app-server", NewServer(apiCfg, mux))
chief.Run()
}
Job
presets.Job
is a primitive worker who performs an action
callback with a given period
.
package main
import (
"log"
"time"
"git.ooo.ua/pub/uwe/v2"
"git.ooo.ua/pub/uwe/v2/presets"
)
func main() {
var action = func() error {
log.Println("do something")
return nil
}
chief := uwe.NewChief()
chief.UseDefaultRecover()
chief.SetEventHandler(uwe.STDLogEventHandler())
chief.AddWorker("simple-job", presets.NewJob(time.Second, action))
chief.Run()
}
WorkerFunc
presets.WorkerFunc
is a type of worker that consist from one function. Allow to use the function as worker.
package presets
import (
"log"
"time"
"git.ooo.ua/pub/uwe/v2"
"git.ooo.ua/pub/uwe/v2/presets"
)
func main() {
var anonFuncWorker = func(ctx uwe.Context) error {
ticker := time.NewTicker(time.Second)
for {
select {
case <-ticker.C:
log.Println("do something")
case <-ctx.Done():
log.Println("good bye")
return nil
}
}
}
chief := uwe.NewChief()
chief.UseDefaultRecover()
chief.SetEventHandler(uwe.STDLogEventHandler())
chief.AddWorker("anon-func", WorkerFunc(anonFuncWorker))
chief.Run()
}
License
This library is distributed under the Apache 2.0 license.