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

Commit

Permalink
add keepalive service for php commands
Browse files Browse the repository at this point in the history
  • Loading branch information
svensp committed Aug 20, 2019
1 parent 6bef99d commit f453265
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 1 deletion.
24 changes: 23 additions & 1 deletion app/Blueprint/Infrastructure/Service/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@ public function getLabels(): array {
return $this->labels;
}

public function copyLabels(Service $copyFrom)
{
$this->labels = $copyFrom->labels;
}

/**
* @param string $name
* @param string $label
Expand All @@ -472,13 +477,23 @@ public function getSidekicks(): array {
return $this->sidekicks;
}

public function copySidekicks(Service $copyFrom)
{
$this->sidekicks = $copyFrom->sidekicks;
}

/**
* @param Service $sidekicks
*/
public function addSidekick(Service $sidekicks) {
$this->sidekicks[] = $sidekicks;
}

public function resetSidekicks()
{
$this->sidekicks = [];
}

/**
* @return bool
*/
Expand Down Expand Up @@ -528,6 +543,14 @@ public function setWorkDir( string $workDir ) {
$this->workDir = $workDir;
}

/**
* @return DefaultNetworkMode|NetworkMode
*/
public function getNetworkModeObject()
{
return $this->networkMode;
}

/**
* @return string
*/
Expand Down Expand Up @@ -572,5 +595,4 @@ public function addCopyVolumesFrom(Service $service)
$this->copyVolumesFrom[] = $service;
}


}
41 changes: 41 additions & 0 deletions app/Blueprint/Keepalive/KeepaliveService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php namespace Rancherize\Blueprint\Keepalive;

use Rancherize\Blueprint\Infrastructure\Service\NetworkMode\ShareNetworkMode;
use Rancherize\Blueprint\Infrastructure\Service\Service;

/**
* Class KeepaliveService
* @package Rancherize\Blueprint\Keepalive
*/
class KeepaliveService extends Service
{

/**
* @var Service
*/
protected $targetService;

/**
* @param Service $targetService
* @return KeepaliveService
*/
public function setTargetService(Service $targetService): KeepaliveService
{
$this->targetService = $targetService;
return $this;
}

public function takeOver()
{
$this->image = 'busybox';
$this->command = '/bin/sh';
$this->tty = true;
$this->keepStdin = true;

$this->copySidekicks($this->targetService);
$this->targetService->resetSidekicks();
$this->copyLabels($this->targetService);
$this->targetService->setNetworkMode( new ShareNetworkMode($this) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rancherize\Blueprint\Events\SidekickBuiltEvent;
use Rancherize\Blueprint\Infrastructure\Service\Maker\PhpFpm\PhpFpmMaker;
use Rancherize\Blueprint\Infrastructure\Service\Service;
use Rancherize\Blueprint\Keepalive\KeepaliveService;
use Rancherize\Blueprint\PhpCommands\Parser\PhpCommandsParser;
use Rancherize\Commands\Events\PushCommandInServiceUpgradeEvent;
use Rancherize\Commands\Events\PushCommandStartEvent;
Expand Down Expand Up @@ -95,6 +96,13 @@ public function mainServiceBuilt( MainServiceBuiltEvent $event ) {
$event = new ServiceBuiltEvent($infrastructure, $service, $command->getConfiguration(), $config);
$this->eventDispatcher->dispatch($event::NAME, $event);

if( $command->hasKeepaliveService() ) {
$keepaliveService = new KeepaliveService();
$keepaliveService->setTargetService($service)->takeOver();
$infrastructure->addService( $keepaliveService );
return;
}

$infrastructure->addService( $service );
}

Expand Down
3 changes: 3 additions & 0 deletions app/Blueprint/PhpCommands/Parser/ArrayParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public function parse( string $name, $data, $version ) {
if ( array_key_exists( 'restart', $data ) )
$phpCommand->setRestart( $data['restart'] );

if ( array_key_exists( 'keepalive', $data ) )
$phpCommand->setKeepaliveService( $data['keepalive'] );

return $phpCommand;
}
}
23 changes: 23 additions & 0 deletions app/Blueprint/PhpCommands/PhpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class PhpCommand {
*/
protected $service = false;

/**
* @var bool
*/
protected $keepaliveService = false;

/**
* @var Configuration
*/
Expand Down Expand Up @@ -110,4 +115,22 @@ public function isService(): bool {
return $this->service;
}

/**
* @param bool $keepaliveService
* @return PhpCommand
*/
public function setKeepaliveService(bool $keepaliveService): PhpCommand
{
$this->keepaliveService = $keepaliveService;
return $this;
}

/**
* @return bool
*/
public function hasKeepaliveService(): bool
{
return $this->keepaliveService;
}

}
5 changes: 5 additions & 0 deletions app/Blueprint/PhpCommands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Use for commands that need to be run periodically
"migrate":{
"command":"/var/www/app/artisan migrate && /var/www/app/artisan db:seed",
"is-service":false,
"keepalive":false,
"schedule":{
"second":0,
"hour":18,
Expand All @@ -40,3 +41,7 @@ Use for commands that need to be run periodically
}
}
```

- `keepalive` adds a dummy container which does nothing but stays active. The commands then join the network of this active container.
This helps with problems with the rancher dns service taking a few seconds after the container start to work. It also
improves reliability of sidekick services joining the network of your container because they don't have to restart with your original command.

0 comments on commit f453265

Please sign in to comment.