Skip to content

Commit

Permalink
Merge pull request #1 from mrsoftware/feature/events
Browse files Browse the repository at this point in the history
refactor and add event system
  • Loading branch information
mrsoftware authored Sep 21, 2024
2 parents c881be4 + ee9c9dd commit 07dc79d
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 100 deletions.
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
func IgnoreError[T any](provider Provider[T]) Provider[T] {
return func(ctx context.Context, app *App[T]) error {
if err := provider(ctx, app); err != nil {
app.Logger().Errorf("privider error ignored: %s", err.Error())
app.Log(ErrorLogLevel, "provider error ignored: %s", err)
}

return nil
Expand Down
131 changes: 131 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package vabastegi

import "time"

// Event is what Vabastegi event look like.
type Event interface {
event() // it's private to prevent outside implementation.
}

// EventHandlers is list of EventHandler.
type EventHandlers []EventHandler

// Publish passed event using event handlers.
func (e EventHandlers) Publish(event Event) {
for _, handler := range e {
handler.OnEvent(event)
}
}

// EventHandler used if you need to handle the events.
type EventHandler interface {
OnEvent(event Event)
}

func (p *OnBuildsExecuting) event() {}
func (p *OnBuildsExecuted) event() {}
func (p *OnBuildExecuting) event() {}
func (p *OnBuildExecuted) event() {}
func (p *OnShutdownExecuting) event() {}
func (p *OnShutdownExecuted) event() {}
func (p *OnApplicationShutdownExecuting) event() {}
func (p *OnApplicationShutdownExecuted) event() {}
func (p *OnLog) event() {}

// OnBuildsExecuting is emitted before a Builds is executed.
type OnBuildsExecuting struct {
// BuildAt is the time build happened.
BuildAt time.Time
}

// OnBuildsExecuted is emitted after a Builds has been executed.
type OnBuildsExecuted struct {
// Runtime specifies how long it took to run this hook.
Runtime time.Duration

// Err is non-nil if the hook failed to execute.
Err error
}

// OnBuildExecuting is emitted before a Build is executed.
type OnBuildExecuting struct {
// BuildAt is the time build happened.
BuildAt time.Time

// ProviderName is the name of the function that will be executed.
ProviderName string

// CallerPath is the path of provider if from.
CallerPath string
}

// OnBuildExecuted is emitted after a Provider has been executed.
type OnBuildExecuted struct {
// ProviderName is the name of the function that was executed.
ProviderName string

// CallerPath is the path of provider if from.
CallerPath string

// Runtime specifies how long it took to run this hook.
Runtime time.Duration

// Err is non-nil if the hook failed to execute.
Err error
}

// OnShutdownExecuting is emitted before a Shutdown is executed.
type OnShutdownExecuting struct {
// ShutdownAt is the time shutdown happened.
ShutdownAt time.Time

// ProviderName is the name of the function that will be executed.
ProviderName string

// CallerPath is the path of provider if from.
CallerPath string
}

// OnShutdownExecuted is emitted after a Shutdown has been executed.
type OnShutdownExecuted struct {
// ProviderName is the name of the function that was executed.
ProviderName string

// CallerPath is the path of provider if from.
CallerPath string

// Runtime specifies how long it took to run this hook.
Runtime time.Duration

// Err is non-nil if the hook failed to execute.
Err error
}

// OnApplicationShutdownExecuting is emitted before the application Shutdown is executed.
type OnApplicationShutdownExecuting struct {
// ShutdownAt is the time shutdown happened.
ShutdownAt time.Time

// Reason is the reason for shutdown the application.
Reason string
}

// OnApplicationShutdownExecuted is emitted after the application Shutdown has been executed.
type OnApplicationShutdownExecuted struct {
// Reason is the reason for shutdown the application.
Reason string

// Runtime specifies how long it took to run this hook.
Runtime time.Duration

// Err is non-nil if the hook failed to execute.
Err error
}

// OnLog is used if a log event is sent.
type OnLog struct {
LogAt time.Time
Level logLevel
Message string
Args []interface{}
}
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ module github.com/mrsoftware/vabastegi

go 1.18.0

require github.com/mattn/go-runewidth v0.0.15

require github.com/rivo/uniseg v0.2.0 // indirect
require github.com/mrsoftware/errors v0.1.0
22 changes: 18 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
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/mrsoftware/errors v0.1.0 h1:5MSHsrsqlBMPbNzwobVt39IpgsVee7LuSe+n8aQWPsI=
github.com/mrsoftware/errors v0.1.0/go.mod h1:iHqx83gamUM9jhiV/rWZuVZe54NVqtqkIDnvZHywSM8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
58 changes: 29 additions & 29 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,46 +37,46 @@ const (
ErrorLogLevel
)

var _ Logger = &IOLogger{}
var _ EventHandler = &EventLogger{}

// Logger is what Vabastegi use as logger.
type Logger interface {
Debugf(message string, args ...interface{})
Infof(message string, args ...interface{})
Errorf(message string, args ...interface{})
}

// NewIOLogger create new instance of IOLogger.
func NewIOLogger(writer io.Writer, level logLevel) Logger {
return &IOLogger{writer: writer, level: level}
}

// IOLogger is a Logger that write to passed io.Writer.
type IOLogger struct {
// EventLogger is use event of vabastegi and log them.
type EventLogger struct {
writer io.Writer
level logLevel
}

func (i *IOLogger) IsEnable(level logLevel) bool {
return level >= i.level
// NewEventLogger create new instance of EventLogger.
func NewEventLogger(writer io.Writer, level logLevel) *EventLogger {
return &EventLogger{writer: writer, level: level}
}

func (i *IOLogger) Debugf(message string, args ...interface{}) {
i.log(DebugLogLevel, message, args...)
func (l *EventLogger) IsEnable(level logLevel) bool {
return level >= l.level
}

func (i *IOLogger) Infof(message string, args ...interface{}) {
i.log(InfoLogLevel, message, args...)
}
func (l *EventLogger) log(level logLevel, message string, args ...interface{}) {
if !l.IsEnable(level) {
return
}

func (i *IOLogger) Errorf(message string, args ...interface{}) {
i.log(ErrorLogLevel, message, args...)
fmt.Fprintf(l.writer, "[Vabastegi] ["+level.String()+"] "+message+"\n", args...)
}

func (i *IOLogger) log(level logLevel, message string, args ...interface{}) {
if !i.IsEnable(level) {
return
func (l *EventLogger) OnEvent(event Event) {
switch e := event.(type) {
case *OnBuildExecuted:
if e.Err != nil {
l.log(ErrorLogLevel, e.ProviderName+" ✕")

return
}

l.log(InfoLogLevel, e.ProviderName+" ✓")
case *OnShutdownExecuting:
l.log(InfoLogLevel, "Shutting Down %s", e.ProviderName)
case *OnApplicationShutdownExecuting:
l.log(InfoLogLevel, "Shutting Down Application: %s", e.Reason)
case *OnLog:
l.log(e.Level, e.Message, e.Args...)
}

fmt.Fprintf(i.writer, "[Vabastegi] ["+level.String()+"] "+message+"\n", args...)
}
16 changes: 8 additions & 8 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@ package vabastegi

// Options of Vabastegi.
type Options struct {
Logger Logger
GracefulShutdown bool
AppName string
EventHandlers EventHandlers
}

// Option of Vabastegi.
type Option func(options *Options)

// WithLogger provide logger for Vabastegi.
func WithLogger(logger Logger) Option {
return func(options *Options) {
options.Logger = logger
}
}

// WithGraceFullShutdown used if you need gracefully shutdown for your application.
func WithGraceFullShutdown(active bool) Option {
return func(options *Options) {
Expand All @@ -30,3 +23,10 @@ func WithAppName(appName string) Option {
options.AppName = appName
}
}

// WithEventHandlers register event handlers for vabastegi events.
func WithEventHandlers(handlers ...EventHandler) Option {
return func(options *Options) {
options.EventHandlers = append(options.EventHandlers, handlers...)
}
}
Loading

0 comments on commit 07dc79d

Please sign in to comment.