-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e42995c
commit 6fec373
Showing
14 changed files
with
225 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,63 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"syscall" | ||
|
||
"github.com/lancer-kit/armory/log" | ||
"github.com/lancer-kit/armory/natsx" | ||
"github.com/lancer-kit/sender/app/api" | ||
"github.com/lancer-kit/sender/app/asyncapi" | ||
"github.com/lancer-kit/sender/config" | ||
"github.com/lancer-kit/uwe/v2" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type App struct { | ||
cfg *config.Cfg | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
wg *sync.WaitGroup | ||
logger *logrus.Entry | ||
workers WorkerList | ||
errors map[string]chan error | ||
cfg *config.Cfg | ||
logger *logrus.Entry | ||
} | ||
|
||
func New(cfg *config.Cfg) *App { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
logger := log.Default.WithField("app", config.ServiceName) | ||
workers := WorkerList{ | ||
config.WorkerAPIServer: api.New(ctx, cfg, logger), | ||
config.WorkerAsyncAPIEmail: asyncapi.NewEmail(ctx, cfg, logger), | ||
config.WorkerAsyncAPISms: asyncapi.NewSms(ctx, cfg, logger), | ||
} | ||
return &App{ | ||
cfg: cfg, | ||
workers: workers, | ||
logger: logger, | ||
cancel: cancel, | ||
ctx: ctx, | ||
wg: new(sync.WaitGroup), | ||
} | ||
a := new(App) | ||
a.cfg = cfg | ||
a.logger = log.Default.WithField("app", config.ServiceName) | ||
return a | ||
} | ||
|
||
func (a *App) Run() { | ||
a.errors = make(map[string]chan error) | ||
for name := range a.workers { | ||
a.errors[name] = make(chan error) | ||
} | ||
|
||
natsx.SetConfig(a.cfg.NATS) | ||
|
||
a.workers.RunAll(a.cfg.Workers, a.errors) | ||
|
||
go a.checkWorkerErrors(a.errors) | ||
|
||
a.GracefulStop() | ||
} | ||
|
||
func (a *App) GracefulStop() { | ||
signalChan := make(chan os.Signal, 1) | ||
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM) | ||
|
||
<-signalChan | ||
|
||
a.logger.Logger.Debugln("Begin graceful shutdown...") | ||
a.cancel() | ||
a.wg.Wait() | ||
a.logger.Logger.Debugln("Service successfully stopped") | ||
} | ||
|
||
func (a App) Context() context.Context { | ||
return a.ctx | ||
} | ||
|
||
func (a App) Logger() *logrus.Entry { | ||
return a.logger | ||
} | ||
|
||
func (a *App) WAdd(delta int) { | ||
a.wg.Add(delta) | ||
} | ||
|
||
func (a *App) WDone() { | ||
a.wg.Done() | ||
} | ||
|
||
func (a *App) checkWorkerErrors(errors map[string]chan error) { | ||
for name, err := range errors { | ||
go a.logErrors(name, err) | ||
chief := uwe.NewChief() | ||
workers := a.workers() | ||
for _, name := range a.cfg.Workers { | ||
chief.AddWorker(uwe.WorkerName(name), workers[uwe.WorkerName(name)]) | ||
} | ||
chief.UseDefaultRecover() | ||
chief.SetEventHandler(a.eventHandler) | ||
chief.Run() | ||
} | ||
|
||
func (a *App) workers() map[uwe.WorkerName]uwe.Worker { | ||
return map[uwe.WorkerName]uwe.Worker{ | ||
config.WorkerAPIServer: api.New( | ||
a.cfg, | ||
a.logger.WithField("worker", config.WorkerAPIServer), | ||
), | ||
config.WorkerAsyncAPIEmail: asyncapi.NewEmail( | ||
a.cfg, | ||
a.logger.WithField("worker", config.WorkerAsyncAPIEmail), | ||
), | ||
config.WorkerAsyncAPISms: asyncapi.NewSms( | ||
a.cfg, | ||
a.logger.WithField("worker", config.WorkerAsyncAPISms), | ||
), | ||
} | ||
<-a.Context().Done() | ||
} | ||
|
||
func (a *App) logErrors(name string, errStream chan error) { | ||
for err := range errStream { | ||
if err != nil { | ||
a.Logger().WithError(err). | ||
WithField("worker", name). | ||
Errorln("error from worker") | ||
} | ||
func (a *App) eventHandler(event uwe.Event) { | ||
logger := a.logger.WithField("level", event.Level).WithField("worker", event.Worker) | ||
if err := event.ToError(); err != nil { | ||
logger.WithError(err).Error("error in chief") | ||
return | ||
} | ||
|
||
logger.WithField("message", event.Message).Info("chief log") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.