Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/print graph #50

Merged
merged 19 commits into from
Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
<p align="center">
<a href="https://pkg.go.dev/github.com/spiral/Endure?tab=doc"><img src="https://godoc.org/github.com/spiral/Endure?status.svg"></a>
<a href="https://github.com/spiral/Endure/actions"><img src="https://github.com/spiral/Endure/workflows/CI/badge.svg" alt=""></a>
<a href="https://goreportcard.com/report/github.com/spiral/Endure"><img src="https://goreportcard.com/badge/github.com/spiral/Endure"></a>
<a href="https://codecov.io/gh/spiral/Endure/"><img src="https://codecov.io/gh/spiral/Endure/branch/master/graph/badge.svg"></a>
<a href="https://codecov.io/gh/spiral/endure"><img src="https://codecov.io/gh/spiral/endure/branch/master/graph/badge.svg?token=itNaiZ6ALN"/></a>
<a href="https://discord.gg/TFeEmCs"><img src="https://img.shields.io/badge/discord-chat-magenta.svg"></a>
<a href="https://lgtm.com/projects/g/spiral/endure/alerts/"><img src="https://img.shields.io/lgtm/alerts/g/spiral/endure.svg?logo=lgtm&logoWidth=18"></a>
</p>
Expand Down
13 changes: 0 additions & 13 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ package endure
// InitMethodName is the function name for the reflection
const InitMethodName = "Init"

// ConfigureMethodName
const ConfigureMethodName = "Configure"

// CloseMethodName
const CloseMethodName = "Close"

// ServeMethodName
const ServeMethodName = "Serve"

Expand Down Expand Up @@ -37,13 +31,6 @@ type result struct {
}

type (
// used to gracefully stop and configure the plugins
graceful interface {
// Configure is used when we need to make preparation and wait for all services till Serve
Configure() error
// Close frees resources allocated by the service
Close() error
}
// this is the main service interface with should implement every plugin
Service interface {
// Serve
Expand Down
41 changes: 21 additions & 20 deletions endure.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type Endure struct {
retry bool
maxInterval time.Duration
initialInterval time.Duration
// option to visualize resulted (before init) graph
visualize bool

mutex *sync.RWMutex

Expand Down Expand Up @@ -168,6 +170,12 @@ func SetBackoffTimes(initialInterval time.Duration, maxInterval time.Duration) O
}
}

func Visualize(print bool) Options {
return func(endure *Endure) {
endure.visualize = print
}
}

// Depender depends the dependencies
// name is a name of the dependency, for example - S2
// vertex is a value -> pointer to the structure
Expand Down Expand Up @@ -212,10 +220,20 @@ func (e *Endure) Register(vertex interface{}) error {
func (e *Endure) Init() error {
const op = errors.Op("Init")
// traverse the graph
if err := e.addEdges(); err != nil {
err := e.addEdges()
if err != nil {
return errors.E(op, errors.Init, err)
}

// if failed - continue, just send warning to a user
// visualize is not critical
if e.visualize {
err = structures.Visualize(e.graph.Vertices)
if err != nil {
e.logger.Warn("failed to visualize the graph", zap.Error(err))
}
}

// we should build init list in the reverse order
sorted, err := structures.TopologicalSort(e.graph.Vertices)
if err != nil {
Expand Down Expand Up @@ -251,33 +269,16 @@ func (e *Endure) Serve() (<-chan *Result, error) {
const op = errors.Op("Serve")
e.startMainThread()

// simple check that we have at least one vertex in the graph to Serve
atLeastOne := false
// call configure
nCopy := e.runList.Head

// DEPRECATED TODO
nCopy := e.runList.Head
for nCopy != nil {
if nCopy.Vertex.IsDisabled {
nCopy = nCopy.Next
continue
}
atLeastOne = true
// deprecated
err := e.configure(nCopy)
if err != nil {
e.logger.Error("backoff failed", zap.String("vertex id", nCopy.Vertex.ID), zap.Error(err))
return nil, errors.E(op, errors.Serve, err)
}

nCopy = nCopy.Next
}

nCopy = e.runList.Head
for nCopy != nil {
if nCopy.Vertex.IsDisabled {
nCopy = nCopy.Next
continue
}
err := e.serve(nCopy)
if err != nil {
return nil, errors.E(op, errors.Serve, err)
Expand Down
3 changes: 3 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
ArgType
Init
Serve
Unsupported
Disabled

Traverse
Expand Down Expand Up @@ -73,6 +74,8 @@ func (k Kind) String() string {
return "Traverse error"
case FunctionCall:
return "Function call error"
case Unsupported:
return "Unsupported"
default:
return "UNDEF"
}
Expand Down
2 changes: 2 additions & 0 deletions examples/sample_1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Created by .ignore support plugin (hsz.mobi)
examples_bolt_db
6 changes: 4 additions & 2 deletions examples/sample_1/go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module github.com/spiral/endure/examples/db_http_logger

go 1.14
go 1.15

require (
github.com/NYTimes/gziphandler v1.1.1
github.com/gorilla/mux v1.7.4
github.com/rs/cors v1.7.0
github.com/spiral/endure v1.0.0-beta6
github.com/spiral/endure v1.0.0-beta9
go.etcd.io/bbolt v1.3.5
)

replace github.com/spiral/endure v1.0.0-beta9 => /home/valery/Projects/opensource/spiral/endure
26 changes: 20 additions & 6 deletions examples/sample_1/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,32 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
github.com/cenkalti/backoff/v4 v4.0.2 h1:JIufpQLbh4DkbQoii76ItQIUFzevQSqOLZca4eamEDs=
github.com/cenkalti/backoff/v4 v4.0.2/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg=
github.com/cenkalti/backoff/v4 v4.1.0 h1:c8LkOFQTzuO0WBM/ae5HdGQuZPfPxp7lqBRwQRm4fSc=
github.com/cenkalti/backoff/v4 v4.1.0/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/corona10/goimagehash v1.0.2 h1:pUfB0LnsJASMPGEZLj7tGY251vF+qLGqOgEP4rUs6kA=
github.com/corona10/goimagehash v1.0.2/go.mod h1:/l9umBhvcHQXVtQO1V6Gp1yD20STawkhRnnX0D1bvVI=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/goccy/go-graphviz v0.0.8 h1:hYQikvj368s8+rmfzFOZeiCXvSocGH7rfAyLTOy/7AM=
github.com/goccy/go-graphviz v0.0.8/go.mod h1:wXVsXxmyMQU6TN3zGRttjNn3h+iCAS7xQFC6TlNvLhk=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5 h1:BvoENQQU+fZ9uukda/RzCAL/191HHwJA5b13R6diVlY=
github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -25,8 +36,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/spiral/endure v1.0.0-beta6 h1:Kl8xHM/7sSCfpaBndJnJ1zaHVcEeug05OeiemauczwA=
github.com/spiral/endure v1.0.0-beta6/go.mod h1:ZqOMVUfVNlUKzhO3WpbqTqV4q4fKOJG8qCyqBYvmL6g=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand All @@ -41,10 +50,14 @@ go.uber.org/multierr v1.5.0 h1:KCa4XfM8CWFCpxXRGok+Q0SS/0XBhMDbHHGABQLvD2A=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEaUSmT1ysygQC7qYo7sG4=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.15.0 h1:ZZCA22JRF2gQE5FoNmhmrf7jeJJ2uhqDUNRYKm8dvmM=
go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc=
go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM=
go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/image v0.0.0-20200119044424-58c23975cae1/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 h1:QelT11PB4FXiDEXucrfNckHoFxwt8USGY1ajP1ZF5lM=
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
Expand All @@ -67,6 +80,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
Binary file added examples/sample_1/graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 5 additions & 4 deletions examples/sample_1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"os"
"os/signal"
"syscall"

"github.com/spiral/endure"
"github.com/spiral/endure/examples/db_http_logger/modules/db"
Expand All @@ -13,7 +14,7 @@ import (
)

func main() {
container, err := endure.NewContainer(endure.DebugLevel, endure.RetryOnFail(true))
container, err := endure.NewContainer(endure.DebugLevel, endure.RetryOnFail(true), endure.Visualize(true))
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -49,13 +50,13 @@ func main() {
}

// stop by CTRL+C
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGKILL, syscall.SIGINT)

for {
select {
case e := <-errCh:
println(e.Error.Err.Error())
println(e.Error.Error())
er := container.Stop()
if er != nil {
panic(er)
Expand Down
13 changes: 2 additions & 11 deletions examples/sample_1/modules/db/db_layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Repository interface {
func (db *DB) Init(logger logger.SuperLogger) error {
logger.SuperLogToStdOut("initializing DB")
db.logger = logger
db.path = "./examples"
db.path = "./examples_bolt_db"
bdb, err := bolt.Open(db.path, 0666, nil)
if err != nil {
return err
Expand All @@ -37,17 +37,8 @@ func (db *DB) Serve() chan error {
return errCh
}

func (db *DB) Configure() error {
db.logger.SuperLogToStdOut("configuring DB")
return nil
}

func (db *DB) Close() error {
return db.boltdb.Close()
}

func (db *DB) Stop() error {
return nil
return db.boltdb.Close()
}

func (db *DB) Name() string {
Expand Down
62 changes: 28 additions & 34 deletions examples/sample_1/modules/http/http_layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ func (h *Http) Init(db db.Repository, logger logger.SuperLogger) error {
h.client = client
h.db = db
h.logger = logger

h.logger.SuperLogToStdOut("configuring http")
r := mux.NewRouter()

c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowCredentials: true,
AllowedMethods: []string{"GET", "HEAD", "POST", "PUT", "OPTIONS"},
AllowedHeaders: []string{"*"},
})

r.Methods("POST").HandlerFunc(h.update).Path("/update")
r.Methods("POST").HandlerFunc(h.ddelete).Path("/delete")
r.Methods("GET").HandlerFunc(h.sselect).Path("/select")
r.Methods("POST").HandlerFunc(h.insert).Path("/insert")

// just as sample, we put server here
server := &http.Server{
Addr: ":8080",
Handler: c.Handler(r),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}

h.server = server

return nil
}

Expand Down Expand Up @@ -73,36 +100,6 @@ func (h *Http) Stop() error {
return nil
}

func (h *Http) Configure() error {
h.logger.SuperLogToStdOut("configuring http")
r := mux.NewRouter()

c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowCredentials: true,
AllowedMethods: []string{"GET", "HEAD", "POST", "PUT", "OPTIONS"},
AllowedHeaders: []string{"*"},
})

r.Methods("POST").HandlerFunc(h.update).Path("/update")
r.Methods("POST").HandlerFunc(h.ddelete).Path("/delete")
r.Methods("GET").HandlerFunc(h.sselect).Path("/select")
r.Methods("POST").HandlerFunc(h.insert).Path("/insert")

// just as sample, we put server here
server := &http.Server{
Addr: ":8080",
Handler: c.Handler(r),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}

h.server = server

return nil
}

func (h *Http) Depends() []interface{} {
return []interface{}{
h.AddMiddleware,
Expand All @@ -114,9 +111,6 @@ func (h *Http) AddMiddleware(m Middleware) error {
return nil
}

func (h *Http) Close() error {
return nil
}

///////////////// INFRA HANDLERS //////////////////////////////

Expand All @@ -137,7 +131,7 @@ func (h *Http) sselect(writer http.ResponseWriter, request *http.Request) {
writer.WriteHeader(http.StatusOK)

for i := 0; i < 10000; i++ {
writer.Write([]byte("TEST_GZIP_HEADERS"))
_, _ = writer.Write([]byte("TEST_GZIP_HEADERS"))
}

}
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.15

require (
github.com/cenkalti/backoff/v4 v4.1.0
github.com/goccy/go-graphviz v0.0.8
github.com/stretchr/testify v1.6.1
go.uber.org/zap v1.16.0
)
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5 // indirect
)
Loading