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

Commit

Permalink
Merge pull request #4 from justb81/v2
Browse files Browse the repository at this point in the history
implement laravel queue worker for webserver-blueprint
  • Loading branch information
justb81 authored Apr 7, 2017
2 parents a47359c + dc25d55 commit 77562f3
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
35 changes: 35 additions & 0 deletions app/Blueprint/Infrastructure/Service/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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[]
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php namespace Rancherize\Blueprint\Infrastructure\Service\Services;
use Rancherize\Blueprint\Infrastructure\Service\Service;

/**
* Class LaravelQueueWorker
* @package Rancherize\Blueprint\Infrastructure\Service\Services
*/
class LaravelQueueWorker extends Service {

public function __construct() {
$this->setName('LaravelQueueWorker');
$this->setImage('ipunktbs/laravel-queue-worker:php7.0-v1.0');
$this->setRestart( self::RESTART_UNLESS_STOPPED );
// $this->setTty(true);
// $this->setKeepStdin(true);
}
}
1 change: 1 addition & 0 deletions app/Blueprint/Webserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
32 changes: 30 additions & 2 deletions app/Blueprint/Webserver/WebserverBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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']);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 77562f3

Please sign in to comment.