Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
reverted events handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Mar 10, 2022
1 parent 61fb3fe commit 215a8f9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
27 changes: 27 additions & 0 deletions events.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package buffalo

// TODO: TODO-v1 check if they are really need to be exported.
/* The event id should be unique across packages as the format of
"<package-name>:<additional-names>:<optional-error>" as documented. They
should not be used by another packages to keep it informational. To make
it sure, they need to be internal.
Especially for plugable conponents like servers or workers, they can have
their own event definition if they need but the buffalo runtime can emit
generalize events when e.g. the runtime calls configured worker.
*/
const (
// EvtAppStart is emitted when buffalo.App#Serve is called
EvtAppStart = "buffalo:app:start"
Expand All @@ -18,6 +27,24 @@ const (
// EvtRouteErr is emitted when there is a problem handling processing a route
EvtRouteErr = "buffalo:route:err"

// EvtServerStart is emitted when buffalo is about to start servers
EvtServerStart = "buffalo:server:start"
// EvtServerStartErr is emitted when an error occurs when starting servers
EvtServerStartErr = "buffalo:server:start:err"
// EvtServerStop is emitted when buffalo is about to stop servers
EvtServerStop = "buffalo:server:stop"
// EvtServerStopErr is emitted when an error occurs when stopping servers
EvtServerStopErr = "buffalo:server:stop:err"

// EvtWorkerStart is emitted when buffalo is about to start workers
EvtWorkerStart = "buffalo:worker:start"
// EvtWorkerStartErr is emitted when an error occurs when starting workers
EvtWorkerStartErr = "buffalo:worker:start:err"
// EvtWorkerStop is emitted when buffalo is about to stop workers
EvtWorkerStop = "buffalo:worker:stop"
// EvtWorkerStopErr is emitted when an error occurs when stopping workers
EvtWorkerStopErr = "buffalo:worker:stop:err"

// EvtFailureErr is emitted when something can't be processed at all. it is a bad thing
EvtFailureErr = "buffalo:failure:err"
)
13 changes: 10 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/gobuffalo/buffalo/servers"
"github.com/gobuffalo/buffalo/worker"
"github.com/gobuffalo/events"
"github.com/markbates/refresh/refresh/web"
)
Expand All @@ -23,14 +22,16 @@ import (
func (a *App) Serve(srvs ...servers.Server) error {
var wg sync.WaitGroup

// TODO: this information is not correct.
// FIXME: this information is not correct.
// It needs to be fixed as we support multiple servers.
a.Logger.Infof("starting application at http://%s", a.Options.Addr)

payload := events.Payload{
"app": a,
}
if err := events.EmitPayload(EvtAppStart, payload); err != nil {
// just to make sure if events work properly?
a.Logger.Error("unable to emit event. something went wrong internally")
return err
}

Expand Down Expand Up @@ -64,16 +65,19 @@ func (a *App) Serve(srvs ...servers.Server) error {
timeout := time.Duration(a.Options.TimeoutSecondShutdown) * time.Second
ctx, cfn := context.WithTimeout(context.Background(), timeout)
defer cfn()
events.EmitPayload(EvtServerStop, payload)
if err := s.Shutdown(ctx); err != nil {
events.EmitError(EvtServerStopErr, err, payload)
a.Logger.Error("shutting down server: ", err)
}
cfn()
}

if !a.WorkerOff {
a.Logger.Info("shutting down worker")
events.EmitPayload(EvtWorkerStop, payload)
if err := a.Worker.Stop(); err != nil {
events.EmitError(worker.EvtWorkerStopErr, err, payload)
events.EmitError(EvtWorkerStopErr, err, payload)
a.Logger.Error("error while shutting down worker: ", err)
}
}
Expand All @@ -84,7 +88,9 @@ func (a *App) Serve(srvs ...servers.Server) error {
wg.Add(1)
go func() {
defer wg.Done()
events.EmitPayload(EvtWorkerStart, payload)
if err := a.Worker.Start(ctx); err != nil {
events.EmitError(EvtWorkerStartErr, err, payload)
a.Stop(err)
}
}()
Expand All @@ -95,6 +101,7 @@ func (a *App) Serve(srvs ...servers.Server) error {
wg.Add(1)
go func(s servers.Server) {
defer wg.Done()
events.EmitPayload(EvtServerStart, payload)
// s.Start always returns non-nil error
a.Stop(s.Start(ctx, a))
}(s)
Expand Down
13 changes: 0 additions & 13 deletions worker/events.go

This file was deleted.

3 changes: 0 additions & 3 deletions worker/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"sync"
"time"

"github.com/gobuffalo/events"
"github.com/markbates/safe"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -61,7 +60,6 @@ func (w *Simple) Register(name string, h Handler) error {

// Start the worker
func (w *Simple) Start(ctx context.Context) error {
events.EmitPayload(EvtWorkerStart, events.Payload{"worker": w})
w.Logger.Info("starting Simple background worker")

w.ctx, w.cancel = context.WithCancel(ctx)
Expand All @@ -70,7 +68,6 @@ func (w *Simple) Start(ctx context.Context) error {

// Stop the worker
func (w *Simple) Stop() error {
events.EmitPayload(EvtWorkerStop, events.Payload{"worker": w})
w.Logger.Info("stopping Simple background worker")

w.wg.Wait()
Expand Down

0 comments on commit 215a8f9

Please sign in to comment.