diff options
author | Wolfy-J <[email protected]> | 2020-10-26 21:02:56 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2020-10-26 21:02:56 +0300 |
commit | 2d3349eee632e7357ed1eb6905444194a28a4ec0 (patch) | |
tree | 74e15277ffe8c11a24e3d3b1a30edfd9597ef984 | |
parent | 91cf918b30938129609323ded53e190385e019a6 (diff) |
- working on new cmd and logger setup
-rwxr-xr-x | composer.json | 7 | ||||
-rwxr-xr-x | go.mod | 1 | ||||
-rw-r--r--[-rwxr-xr-x] | plugins/app/app.go (renamed from plugins/factory/app.go) | 18 | ||||
-rw-r--r--[-rwxr-xr-x] | plugins/app/config.go (renamed from plugins/factory/config.go) | 2 | ||||
-rw-r--r--[-rwxr-xr-x] | plugins/app/tests/.rr.yaml (renamed from plugins/factory/tests/.rr.yaml) | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | plugins/app/tests/factory_test.go (renamed from plugins/factory/tests/factory_test.go) | 4 | ||||
-rw-r--r--[-rwxr-xr-x] | plugins/app/tests/hello.php (renamed from plugins/factory/tests/hello.php) | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | plugins/app/tests/plugin_1.go (renamed from plugins/factory/tests/plugin_1.go) | 10 | ||||
-rw-r--r--[-rwxr-xr-x] | plugins/app/tests/plugin_2.go (renamed from plugins/factory/tests/plugin_2.go) | 10 | ||||
-rw-r--r-- | plugins/logger/config.go | 9 | ||||
-rw-r--r-- | plugins/logger/zap_logger.go | 111 | ||||
-rwxr-xr-x | src/Diactoros/ServerRequestFactory.php | 26 | ||||
-rwxr-xr-x | src/Diactoros/StreamFactory.php | 57 | ||||
-rwxr-xr-x | src/Diactoros/UploadedFileFactory.php | 36 | ||||
-rwxr-xr-x | src/Exception/RoadRunnerException.php | 2 | ||||
-rwxr-xr-x | src/Exceptions/RoadRunnerException.php | 18 | ||||
-rw-r--r--[-rwxr-xr-x] | src/Http/HttpClient.php (renamed from src/HttpClient.php) | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | src/Http/PSR7Client.php (renamed from src/PSR7Client.php) | 2 | ||||
-rw-r--r-- | src/Logger/.empty | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | src/Metrics/Metrics.php (renamed from src/Metrics.php) | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | src/Metrics/MetricsInterface.php (renamed from src/MetricsInterface.php) | 0 | ||||
-rw-r--r-- | src/WorkerInterface.php | 0 |
22 files changed, 150 insertions, 163 deletions
diff --git a/composer.json b/composer.json index 283eaab1..aef75b08 100755 --- a/composer.json +++ b/composer.json @@ -18,15 +18,14 @@ "ext-json": "*", "ext-curl": "*", "spiral/goridge": "^2.4.2", - "psr/http-factory": "^1.0", - "psr/http-message": "^1.0", - "symfony/console": "^2.5.0 || ^3.0.0 || ^4.0.0 || ^5.0.0", - "laminas/laminas-diactoros": "^1.3 || ^2.0" + "symfony/console": "^2.5.0 || ^3.0.0 || ^4.0.0 || ^5.0.0" }, "config": { "vendor-dir": "vendor_php" }, "require-dev": { + "psr/http-factory": "^1.0", + "psr/http-message": "^1.0", "phpstan/phpstan": "~0.12" }, "scripts": { @@ -15,6 +15,7 @@ require ( github.com/stretchr/testify v1.6.1 github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a go.uber.org/multierr v1.6.0 + go.uber.org/zap v1.16.0 golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect diff --git a/plugins/factory/app.go b/plugins/app/app.go index 4951e3df..950d7791 100755..100644 --- a/plugins/factory/app.go +++ b/plugins/app/app.go @@ -1,4 +1,4 @@ -package factory +package app import ( "context" @@ -19,9 +19,9 @@ const ServiceName = "app" type Env map[string]string -// AppFactory creates workers for the application. -type AppFactory interface { - NewCmdFactory(env Env) (func() *exec.Cmd, error) +// WorkerFactory creates workers for the application. +type WorkerFactory interface { + CmdFactory(env Env) (func() *exec.Cmd, error) NewWorker(ctx context.Context, env Env) (roadrunner.WorkerBase, error) NewWorkerPool(ctx context.Context, opt roadrunner.Config, env Env) (roadrunner.Pool, error) } @@ -68,7 +68,8 @@ func (app *App) Stop() error { return app.factory.Close(context.Background()) } -func (app *App) NewCmdFactory(env Env) (func() *exec.Cmd, error) { +// CmdFactory provides worker command factory assocated with given context. +func (app *App) CmdFactory(env Env) (func() *exec.Cmd, error) { var cmdArgs []string // create command according to the config @@ -93,8 +94,9 @@ func (app *App) NewCmdFactory(env Env) (func() *exec.Cmd, error) { }, nil } +// NewWorker issues new standalone worker. func (app *App) NewWorker(ctx context.Context, env Env) (roadrunner.WorkerBase, error) { - spawnCmd, err := app.NewCmdFactory(env) + spawnCmd, err := app.CmdFactory(env) if err != nil { return nil, err } @@ -102,8 +104,9 @@ func (app *App) NewWorker(ctx context.Context, env Env) (roadrunner.WorkerBase, return app.factory.SpawnWorkerWithContext(ctx, spawnCmd()) } +// NewWorkerPool issues new worker pool. func (app *App) NewWorkerPool(ctx context.Context, opt roadrunner.Config, env Env) (roadrunner.Pool, error) { - spawnCmd, err := app.NewCmdFactory(env) + spawnCmd, err := app.CmdFactory(env) if err != nil { return nil, err } @@ -124,6 +127,7 @@ func (app *App) NewWorkerPool(ctx context.Context, opt roadrunner.Config, env En return p, nil } +// creates relay and worker factory. func (app *App) initFactory() (roadrunner.Factory, error) { if app.cfg.Relay == "" || app.cfg.Relay == "pipes" { return roadrunner.NewPipeFactory(), nil diff --git a/plugins/factory/config.go b/plugins/app/config.go index b2d1d0ad..eaa54e2d 100755..100644 --- a/plugins/factory/config.go +++ b/plugins/app/config.go @@ -1,4 +1,4 @@ -package factory +package app import "time" diff --git a/plugins/factory/tests/.rr.yaml b/plugins/app/tests/.rr.yaml index 171f51dc..171f51dc 100755..100644 --- a/plugins/factory/tests/.rr.yaml +++ b/plugins/app/tests/.rr.yaml diff --git a/plugins/factory/tests/factory_test.go b/plugins/app/tests/factory_test.go index 6c264fd6..7c885797 100755..100644 --- a/plugins/factory/tests/factory_test.go +++ b/plugins/app/tests/factory_test.go @@ -7,8 +7,8 @@ import ( "time" "github.com/spiral/endure" + "github.com/spiral/roadrunner/v2/plugins/app" "github.com/spiral/roadrunner/v2/plugins/config" - "github.com/spiral/roadrunner/v2/plugins/factory" "github.com/stretchr/testify/assert" ) @@ -26,7 +26,7 @@ func TestFactory(t *testing.T) { t.Fatal(err) } - err = container.Register(&factory.App{}) + err = container.Register(&app.App{}) if err != nil { t.Fatal(err) } diff --git a/plugins/factory/tests/hello.php b/plugins/app/tests/hello.php index bf9e82cc..bf9e82cc 100755..100644 --- a/plugins/factory/tests/hello.php +++ b/plugins/app/tests/hello.php diff --git a/plugins/factory/tests/plugin_1.go b/plugins/app/tests/plugin_1.go index 9011bb00..7259ea9d 100755..100644 --- a/plugins/factory/tests/plugin_1.go +++ b/plugins/app/tests/plugin_1.go @@ -4,16 +4,16 @@ import ( "errors" "fmt" + "github.com/spiral/roadrunner/v2/plugins/app" "github.com/spiral/roadrunner/v2/plugins/config" - "github.com/spiral/roadrunner/v2/plugins/factory" ) type Foo struct { configProvider config.Provider - spawner factory.AppFactory + spawner app.WorkerFactory } -func (f *Foo) Init(p config.Provider, spw factory.AppFactory) error { +func (f *Foo) Init(p config.Provider, spw app.WorkerFactory) error { f.configProvider = p f.spawner = spw return nil @@ -22,14 +22,14 @@ func (f *Foo) Init(p config.Provider, spw factory.AppFactory) error { func (f *Foo) Serve() chan error { errCh := make(chan error, 1) - r := &factory.Config{} + r := &app.Config{} err := f.configProvider.UnmarshalKey("app", r) if err != nil { errCh <- err return errCh } - cmd, err := f.spawner.NewCmdFactory(nil) + cmd, err := f.spawner.CmdFactory(nil) if err != nil { errCh <- err return errCh diff --git a/plugins/factory/tests/plugin_2.go b/plugins/app/tests/plugin_2.go index 9f401bec..86da4eed 100755..100644 --- a/plugins/factory/tests/plugin_2.go +++ b/plugins/app/tests/plugin_2.go @@ -7,16 +7,16 @@ import ( "time" "github.com/spiral/roadrunner/v2" + "github.com/spiral/roadrunner/v2/plugins/app" "github.com/spiral/roadrunner/v2/plugins/config" - "github.com/spiral/roadrunner/v2/plugins/factory" ) type Foo2 struct { configProvider config.Provider - wf factory.AppFactory + wf app.WorkerFactory } -func (f *Foo2) Init(p config.Provider, workerFactory factory.AppFactory) error { +func (f *Foo2) Init(p config.Provider, workerFactory app.WorkerFactory) error { f.configProvider = p f.wf = workerFactory return nil @@ -25,14 +25,14 @@ func (f *Foo2) Init(p config.Provider, workerFactory factory.AppFactory) error { func (f *Foo2) Serve() chan error { errCh := make(chan error, 1) - r := &factory.Config{} + r := &app.Config{} err := f.configProvider.UnmarshalKey("app", r) if err != nil { errCh <- err return errCh } - cmd, err := f.wf.NewCmdFactory(nil) + cmd, err := f.wf.CmdFactory(nil) if err != nil { errCh <- err return errCh diff --git a/plugins/logger/config.go b/plugins/logger/config.go new file mode 100644 index 00000000..12badade --- /dev/null +++ b/plugins/logger/config.go @@ -0,0 +1,9 @@ +package logger + +type Config struct { + Squash bool + Channels map[string]LoggerConfig +} + +type LoggerConfig struct { +} diff --git a/plugins/logger/zap_logger.go b/plugins/logger/zap_logger.go new file mode 100644 index 00000000..747cbf77 --- /dev/null +++ b/plugins/logger/zap_logger.go @@ -0,0 +1,111 @@ +package logger + +import ( + "github.com/fatih/color" + "github.com/spiral/endure" + "github.com/spiral/roadrunner/v2/plugins/config" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "strings" + "time" +) + +// ServiceName declares service name. +const ServiceName = "logs" + +type LogFactory interface { + // GlobalLogger returns global log instance. + GlobalLogger() *zap.Logger + + // NamedLogger returns logger dedicated to the specific channel. Similar to Named() but also reads the core params. + NamedLogger(name string) *zap.Logger +} + +// ZapLogger manages zap logger. +type ZapLogger struct { + base *zap.Logger + cfg Config +} + +func (z *ZapLogger) Init(cfg config.Provider) (err error) { + err = cfg.UnmarshalKey(ServiceName, &z.cfg) + if err != nil { + return err + } + + if z.base == nil { + cfg := zap.Config{ + Level: zap.NewAtomicLevelAt(zap.DebugLevel), + Encoding: "console", + EncoderConfig: zapcore.EncoderConfig{ + MessageKey: "message", + LevelKey: "level", + TimeKey: "time", + NameKey: "name", + EncodeName: func(s string, enc zapcore.PrimitiveArrayEncoder) { + if len(s) < 12 { + s = s + strings.Repeat(" ", 12-len(s)) + } + + enc.AppendString(color.HiGreenString(s)) + }, + EncodeLevel: func(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) { + switch level { + case zapcore.DebugLevel: + enc.AppendString(color.HiWhiteString(level.CapitalString())) + case zapcore.InfoLevel: + enc.AppendString(color.HiCyanString(level.CapitalString())) + case zapcore.WarnLevel: + enc.AppendString(color.HiYellowString(level.CapitalString())) + case zapcore.ErrorLevel, zapcore.DPanicLevel: + enc.AppendString(color.HiRedString(level.CapitalString())) + case zapcore.PanicLevel, zapcore.FatalLevel: + enc.AppendString(color.HiMagentaString(level.CapitalString())) + } + }, + EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString(t.UTC().Format("2006/01/02 15:04:05")) + }, + EncodeCaller: zapcore.ShortCallerEncoder, + }, + OutputPaths: []string{"stderr"}, + ErrorOutputPaths: []string{"stderr"}, + } + + z.base, err = cfg.Build() + if err != nil { + return err + } + } + + return nil +} + +// GlobalLogger returns global log instance. +func (z *ZapLogger) GlobalLogger() *zap.Logger { + return z.base +} + +// NamedLogger returns logger dedicated to the specific channel. Similar to Named() but also reads the core params. +func (z *ZapLogger) NamedLogger(name string) *zap.Logger { + // todo: automatically configure + return z.base.Named(name) +} + +// Provides declares factory methods. +func (z *ZapLogger) Provides() []interface{} { + return []interface{}{ + z.DefaultLogger, + z.AllocateLogger, + } +} + +// AllocateLogger allocates logger for the service. +func (z *ZapLogger) AllocateLogger(n endure.Named) (*zap.Logger, error) { + return z.NamedLogger(n.Name()), nil +} + +// DefaultLogger returns default logger. +func (z *ZapLogger) DefaultLogger() (*zap.Logger, error) { + return z.GlobalLogger(), nil +} diff --git a/src/Diactoros/ServerRequestFactory.php b/src/Diactoros/ServerRequestFactory.php deleted file mode 100755 index 3fcf8e29..00000000 --- a/src/Diactoros/ServerRequestFactory.php +++ /dev/null @@ -1,26 +0,0 @@ -<?php - -/** - * High-performance PHP process supervisor and load balancer written in Go - * - * @author Wolfy-J - */ -declare(strict_types=1); - -namespace Spiral\RoadRunner\Diactoros; - -use Psr\Http\Message\ServerRequestFactoryInterface; -use Psr\Http\Message\ServerRequestInterface; -use Zend\Diactoros\ServerRequest; - -final class ServerRequestFactory implements ServerRequestFactoryInterface -{ - /** - * @inheritdoc - */ - public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface - { - $uploadedFiles = []; - return new ServerRequest($serverParams, $uploadedFiles, $uri, $method); - } -} diff --git a/src/Diactoros/StreamFactory.php b/src/Diactoros/StreamFactory.php deleted file mode 100755 index cc0a5306..00000000 --- a/src/Diactoros/StreamFactory.php +++ /dev/null @@ -1,57 +0,0 @@ -<?php - -/** - * High-performance PHP process supervisor and load balancer written in Go - * - * @author Wolfy-J - */ -declare(strict_types=1); - -namespace Spiral\RoadRunner\Diactoros; - -use RuntimeException; -use Psr\Http\Message\StreamFactoryInterface; -use Psr\Http\Message\StreamInterface; -use Zend\Diactoros\Stream; - -final class StreamFactory implements StreamFactoryInterface -{ - /** - * @inheritdoc - * @throws RuntimeException - */ - public function createStream(string $content = ''): StreamInterface - { - $resource = fopen('php://temp', 'rb+'); - - if (! \is_resource($resource)) { - throw new RuntimeException('Cannot create stream'); - } - - fwrite($resource, $content); - rewind($resource); - return $this->createStreamFromResource($resource); - } - - /** - * @inheritdoc - */ - public function createStreamFromFile(string $file, string $mode = 'rb'): StreamInterface - { - $resource = fopen($file, $mode); - - if (! \is_resource($resource)) { - throw new RuntimeException('Cannot create stream'); - } - - return $this->createStreamFromResource($resource); - } - - /** - * @inheritdoc - */ - public function createStreamFromResource($resource): StreamInterface - { - return new Stream($resource); - } -} diff --git a/src/Diactoros/UploadedFileFactory.php b/src/Diactoros/UploadedFileFactory.php deleted file mode 100755 index 45773287..00000000 --- a/src/Diactoros/UploadedFileFactory.php +++ /dev/null @@ -1,36 +0,0 @@ -<?php - -/** - * High-performance PHP process supervisor and load balancer written in Go - * - * @author Wolfy-J - */ -declare(strict_types=1); - -namespace Spiral\RoadRunner\Diactoros; - -use Psr\Http\Message\StreamInterface; -use Psr\Http\Message\UploadedFileFactoryInterface; -use Psr\Http\Message\UploadedFileInterface; -use Zend\Diactoros\UploadedFile; - -final class UploadedFileFactory implements UploadedFileFactoryInterface -{ - /** - * @inheritdoc - */ - public function createUploadedFile( - StreamInterface $stream, - int $size = null, - int $error = \UPLOAD_ERR_OK, - string $clientFilename = null, - string $clientMediaType = null - ): UploadedFileInterface { - if ($size === null) { - $size = (int) $stream->getSize(); - } - - /** @var resource $stream */ - return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); - } -} diff --git a/src/Exception/RoadRunnerException.php b/src/Exception/RoadRunnerException.php index f83c3dd4..cd657502 100755 --- a/src/Exception/RoadRunnerException.php +++ b/src/Exception/RoadRunnerException.php @@ -9,6 +9,6 @@ declare(strict_types=1); namespace Spiral\RoadRunner\Exception; -class RoadRunnerException extends \Spiral\RoadRunner\Exceptions\RoadRunnerException +class RoadRunnerException extends \RuntimeException { } diff --git a/src/Exceptions/RoadRunnerException.php b/src/Exceptions/RoadRunnerException.php deleted file mode 100755 index 43967893..00000000 --- a/src/Exceptions/RoadRunnerException.php +++ /dev/null @@ -1,18 +0,0 @@ -<?php - -/** - * Spiral Framework. - * - * @license MIT - * @author Anton Titov (Wolfy-J) - */ -declare(strict_types=1); - -namespace Spiral\RoadRunner\Exceptions; - -/** - * @deprecated use \Spiral\RoadRunner\Exception\RoadRunnerException instead - */ -class RoadRunnerException extends \RuntimeException -{ -} diff --git a/src/HttpClient.php b/src/Http/HttpClient.php index 4ca152c8..4ca152c8 100755..100644 --- a/src/HttpClient.php +++ b/src/Http/HttpClient.php diff --git a/src/PSR7Client.php b/src/Http/PSR7Client.php index 777dd891..94a9457b 100755..100644 --- a/src/PSR7Client.php +++ b/src/Http/PSR7Client.php @@ -12,7 +12,7 @@ namespace Spiral\RoadRunner; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestFactoryInterface; use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Message\StreamFactoryInterface; +use Psr\Http\Message\StreamF actoryInterface; use Psr\Http\Message\UploadedFileFactoryInterface; use Psr\Http\Message\UploadedFileInterface; diff --git a/src/Logger/.empty b/src/Logger/.empty new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/src/Logger/.empty diff --git a/src/Metrics.php b/src/Metrics/Metrics.php index d6b6e1da..d6b6e1da 100755..100644 --- a/src/Metrics.php +++ b/src/Metrics/Metrics.php diff --git a/src/MetricsInterface.php b/src/Metrics/MetricsInterface.php index ec2009b0..ec2009b0 100755..100644 --- a/src/MetricsInterface.php +++ b/src/Metrics/MetricsInterface.php diff --git a/src/WorkerInterface.php b/src/WorkerInterface.php new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/src/WorkerInterface.php |