From dc25d55f78c108d772154c03135039e81d5cd087 Mon Sep 17 00:00:00 2001 From: Bastian Rang Date: Fri, 7 Apr 2017 16:46:55 +0200 Subject: [PATCH] + implement laravel queue worker for webserver-blueprint + add helper functions to copy links and environment variables from one service to another + make redis-service available on production --- .../Infrastructure/Service/Service.php | 35 +++++++++++++++++++ .../Service/Services/LaravelQueueWorker.php | 17 +++++++++ app/Blueprint/Webserver/README.md | 1 + .../Webserver/WebserverBlueprint.php | 32 +++++++++++++++-- 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 app/Blueprint/Infrastructure/Service/Services/LaravelQueueWorker.php diff --git a/app/Blueprint/Infrastructure/Service/Service.php b/app/Blueprint/Infrastructure/Service/Service.php index 1905a0b..4f5336f 100644 --- a/app/Blueprint/Infrastructure/Service/Service.php +++ b/app/Blueprint/Infrastructure/Service/Service.php @@ -209,6 +209,16 @@ public function setEnvironmentVariable(string $name, string $value) { $this->environmentVariables[$name] = $value; } + /** + * @param Service $service + */ + public function setEnvironmentVariablesFrom(Service $service) { + $environmentVariables = $service->getEnvironmentVariables(); + foreach($environmentVariables as $name => $value) { + $this->setEnvironmentVariable($name, $value); + } + } + /** * @param string $name * @param string $default @@ -254,6 +264,31 @@ public function addLink(Service $service, $name = null) { $this->links[$name] = $service; } + /** + * Add internal and external links from other service + * @param Service $service + */ + public function addLinksFrom(Service $service) { + $links = $service->getLinks(); + foreach($links as $name => $link) { + if (is_numeric($name)) { + $this->addLink($link); + } else { + $this->addLink($link, $name); + } + } + + $externalLinks = $service->getExternalLinks(); + foreach($externalLinks as $name => $link) { + if (is_numeric($name)) { + $this->addExternalLink($link); + } else { + $this->addExternalLink($link, $name); + } + } + + } + /** * @return \string[] */ diff --git a/app/Blueprint/Infrastructure/Service/Services/LaravelQueueWorker.php b/app/Blueprint/Infrastructure/Service/Services/LaravelQueueWorker.php new file mode 100644 index 0000000..e5ea591 --- /dev/null +++ b/app/Blueprint/Infrastructure/Service/Services/LaravelQueueWorker.php @@ -0,0 +1,17 @@ +setName('LaravelQueueWorker'); + $this->setImage('ipunktbs/laravel-queue-worker:php7.0-v1.0'); + $this->setRestart( self::RESTART_UNLESS_STOPPED ); +// $this->setTty(true); +// $this->setKeepStdin(true); + } +} \ No newline at end of file diff --git a/app/Blueprint/Webserver/README.md b/app/Blueprint/Webserver/README.md index 18a3d80..88da16d 100644 --- a/app/Blueprint/Webserver/README.md +++ b/app/Blueprint/Webserver/README.md @@ -40,6 +40,7 @@ This blueprint creates infrastructures to support apps using php7. | Option | Defaults to | Explanation | | ------- |:-----------:| ------------ | |`php`| `7.0` | Add php fpm to the nginx. The default version `7.0` does not start an extra service. Other fpm versions are run in their own container and will be a sidekick inside the nginx service. Available Versions: `7.0`, `5.3` | +|`queues`| [] | Add Laravel Queue Worker, providing their name and connection in `name` and `connection` | |`add-redis`| false | Add a Redis server and link it to the main app, providing its name and port in `REDIS_HOST` and `REDIS_PORT` | |`add-database`| false | If set to true then a database server will be started as part of the stack and linked to the main app. Database name, user and password can be found in `DATABASE_NAME`, `DATABASE_USER` and `DATABASE_PORT` | |`database.name`| db | Sets the name of the default database created by the database container | diff --git a/app/Blueprint/Webserver/WebserverBlueprint.php b/app/Blueprint/Webserver/WebserverBlueprint.php index 7ca3ef1..534520f 100644 --- a/app/Blueprint/Webserver/WebserverBlueprint.php +++ b/app/Blueprint/Webserver/WebserverBlueprint.php @@ -9,6 +9,7 @@ use Rancherize\Blueprint\Infrastructure\Service\Service; use Rancherize\Blueprint\Infrastructure\Service\Services\AppService; use Rancherize\Blueprint\Infrastructure\Service\Services\DatabaseService; +use Rancherize\Blueprint\Infrastructure\Service\Services\LaravelQueueWorker; use Rancherize\Blueprint\Infrastructure\Service\Services\PmaService; use Rancherize\Blueprint\Infrastructure\Service\Services\RedisService; use Rancherize\Blueprint\Validation\Exceptions\ValidationFailedException; @@ -68,7 +69,6 @@ public function init(Configurable $configurable, string $environment, InputInter $initializer->init($fallbackConfigurable, 'expose-port', $port); $initializer->init($fallbackConfigurable, 'use-app-container', false); $initializer->init($fallbackConfigurable, 'mount-workdir', true); - $initializer->init($fallbackConfigurable, 'add-redis', false); $initializer->init($fallbackConfigurable, 'add-database', false); $initializer->init($fallbackConfigurable, 'database.pma.enable', false); $initializer->init($fallbackConfigurable, 'database.pma.require-login', false); @@ -89,10 +89,11 @@ public function init(Configurable $configurable, string $environment, InputInter } $initializer->init($fallbackConfigurable, 'php', "7.0"); + $initializer->init($fallbackConfigurable, 'queues', []); $initializer->init($fallbackConfigurable, 'docker.repository', 'repo/name', $projectConfigurable); $initializer->init($fallbackConfigurable, 'docker.version-prefix', '', $projectConfigurable); $initializer->init($fallbackConfigurable, 'nginx-config', '', $projectConfigurable); - + $initializer->init($fallbackConfigurable, 'add-redis', false); $initializer->init($fallbackConfigurable, 'service-name', 'Project', $projectConfigurable); $initializer->init($fallbackConfigurable, 'docker.base-image', 'busybox', $projectConfigurable); $initializer->init($fallbackConfigurable, 'environment', ["EXAMPLE" => 'value']); @@ -142,6 +143,8 @@ public function build(Configuration $configuration, string $environment, string $serverService = $this->makeServerService($config, $projectConfigurable); $this->addRedis($config, $serverService, $infrastructure); + $this->addQueueWorker($config, $serverService, $infrastructure); + $this->addAppContainer($version, $config, $serverService, $infrastructure); $this->addVersionEnvironment($version, $config, $serverService); @@ -403,6 +406,31 @@ protected function addRedis(Configuration $config, Service $serverService, Infra } } + /** + * @param Configuration $config + * @param Service $serverService + * @param Infrastructure $infrastructure + */ + protected function addQueueWorker(Configuration $config, Service $serverService, Infrastructure $infrastructure) { + $queues = $config->get('queues', []); + foreach($queues as $key => $queue) { + $name = $config->get("queues.$key.name", 'default'); + $connection = $config->get("queues.$key.connection", 'default'); + + $laravelQueueWorker = new LaravelQueueWorker(); + $laravelQueueWorker->setName('QueueWorker' . $name); + $laravelQueueWorker->addVolumeFrom($serverService); + $laravelQueueWorker->addLinksFrom($serverService); + $laravelQueueWorker->setEnvironmentVariablesFrom($serverService); + + $laravelQueueWorker->setEnvironmentVariable('QUEUE_NAME', $name); + $laravelQueueWorker->setEnvironmentVariable('QUEUE_CONNECTION', $connection); + + $serverService->addSidekick($laravelQueueWorker); + $infrastructure->addService($laravelQueueWorker); + } + } + /** * @param Configuration $config * @param Service $serverService