Skip to content

Commit

Permalink
define-database-port | Merge remote-tracking branch 'origin/4.x' into…
Browse files Browse the repository at this point in the history
… define-database-port
  • Loading branch information
alexberryman committed Mar 26, 2019
2 parents 58e768e + 5bfe365 commit f183941
Show file tree
Hide file tree
Showing 34 changed files with 491 additions and 295 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* alexberryman mucha55
22 changes: 6 additions & 16 deletions bin/kojo
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,26 @@
declare(strict_types=1);
ini_set('assert.exception', '1');
error_reporting(E_ALL);
require_once __DIR__ . '/../../../../vendor/autoload.php';

use Neighborhoods\Kojo\Console\Command\Process\Pool\Server\Start;
use Neighborhoods\Kojo\ErrorHandler;
use Neighborhoods\Pylon\DependencyInjection\ContainerBuilder\Facade;
use Symfony\Component\Finder\Finder;

set_error_handler(new ErrorHandler());

if (PHP_SAPI !== 'cli') {
echo 'bin/kojo must be run as a CLI application';
exit(1);
}
try{
foreach ([
__DIR__ . '/../../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
] as $autoLoaderFilePathCandidate) {
if (file_exists($autoLoaderFilePathCandidate)) {
require_once $autoLoaderFilePathCandidate;
break;
}
}
}catch(\Exception $exception){
echo 'Autoload error: ' . $exception->getMessage();
exit(1);
}
$containerBuilderFacade = new Facade();
$discoverableDirectories[] = __DIR__ . '/../src';
$finder = new Finder();
$finder->name('*.yml');
if (isset($argv[2]) && is_string($argv[2]) && is_dir($argv[2])) {
$discoverableDirectories[] = $argv[2];
}elseif (isset($argv[1]) && is_string($argv[1]) && $argv[1] === Start::OPT_RUN_SERVER) {
} elseif (isset($argv[1]) && is_string($argv[1]) && $argv[1] === Start::OPT_RUN_SERVER) {
foreach ($argv as $argument) {
if (strstr($argument, Start::OPT_YSDP) !== false) {
$ymlServicesFilePath = explode(Start::OPT_YSDP, $argument);
Expand All @@ -52,4 +42,4 @@ $containerBuilder = $containerBuilderFacade->getContainerBuilder();
$consoleApplication = $containerBuilder->get('neighborhoods.kojo.symfony.component.console.application');
$consoleApplication->run();

return;
return;
9 changes: 4 additions & 5 deletions pylon/src/DependencyInjection/ContainerBuilder/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Neighborhoods\Pylon\Symfony\Component\FinderArrayInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
use Symfony\Component\DependencyInjection\Compiler\InlineServiceDefinitionsPass;
use Symfony\Component\Finder\Finder;
Expand Down Expand Up @@ -56,13 +56,12 @@ public function getContainerBuilder(): ContainerBuilder
foreach ($this->_getYamlServicesFilePaths() as $servicesYmlFilePath) {
$loader->import($servicesYmlFilePath);
}
$passes = [new AnalyzeServiceReferencesPass(), new InlineServiceDefinitionsPass()];
$repeatedPass = new RepeatedPass($passes);
$repeatedPass->process($containerBuilder);
$containerBuilder->addCompilerPass(new AnalyzeServiceReferencesPass());
$containerBuilder->addCompilerPass(new InlineServiceDefinitionsPass());
$containerBuilder->compile(true);
$this->_containerBuilder = $containerBuilder;
}

return $this->_containerBuilder;
}
}
}
39 changes: 25 additions & 14 deletions src/Api/V1/Job/Type/Registrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,94 +11,105 @@ class Registrar implements RegistrarInterface
use Property\Defensive\AwareTrait;
use Type\Service\Create\AwareTrait;

/** @var Type\Service\CreateInterface */
protected $typeServiceCreate;

public function save(): RegistrarInterface
{
$this->_getTypeServiceCreate()->save();
$this->getTypeServiceCreate()->save();

return $this;
}

public function setCode(string $code): RegistrarInterface
{
$this->_getTypeServiceCreate()->setCode($code);
$this->getTypeServiceCreate()->setCode($code);

return $this;
}

public function setWorkerClassUri(string $workerModelUri): RegistrarInterface
{
$this->_getTypeServiceCreate()->setWorkerClassUri($workerModelUri);
$this->getTypeServiceCreate()->setWorkerClassUri($workerModelUri);

return $this;
}

public function setWorkerMethod(string $workerMethod): RegistrarInterface
{
$this->_getTypeServiceCreate()->setWorkerMethod($workerMethod);
$this->getTypeServiceCreate()->setWorkerMethod($workerMethod);

return $this;
}

public function setName(string $name): RegistrarInterface
{
$this->_getTypeServiceCreate()->setName($name);
$this->getTypeServiceCreate()->setName($name);

return $this;
}

public function setCronExpression(string $cronExpression): RegistrarInterface
{
$this->_getTypeServiceCreate()->setCronExpression($cronExpression);
$this->getTypeServiceCreate()->setCronExpression($cronExpression);

return $this;
}

public function setCanWorkInParallel(bool $canWorkInParallel): RegistrarInterface
{
$this->_getTypeServiceCreate()->setCanWorkInParallel($canWorkInParallel);
$this->getTypeServiceCreate()->setCanWorkInParallel($canWorkInParallel);

return $this;
}

public function setDefaultImportance(int $defaultImportance): RegistrarInterface
{
$this->_getTypeServiceCreate()->setDefaultImportance($defaultImportance);
$this->getTypeServiceCreate()->setDefaultImportance($defaultImportance);

return $this;
}

public function setScheduleLimit(int $scheduleLimit): RegistrarInterface
{
$this->_getTypeServiceCreate()->setScheduleLimit($scheduleLimit);
$this->getTypeServiceCreate()->setScheduleLimit($scheduleLimit);

return $this;
}

public function setScheduleLimitAllowance(int $scheduleLimitAllowance): RegistrarInterface
{
$this->_getTypeServiceCreate()->setScheduleLimitAllowance($scheduleLimitAllowance);
$this->getTypeServiceCreate()->setScheduleLimitAllowance($scheduleLimitAllowance);

return $this;
}

public function setIsEnabled(bool $isEnabled): RegistrarInterface
{
$this->_getTypeServiceCreate()->setIsEnabled($isEnabled);
$this->getTypeServiceCreate()->setIsEnabled($isEnabled);

return $this;
}

public function setAutoCompleteSuccess(bool $autoCompleteSuccess): RegistrarInterface
{
$this->_getTypeServiceCreate()->setAutoCompleteSuccess($autoCompleteSuccess);
$this->getTypeServiceCreate()->setAutoCompleteSuccess($autoCompleteSuccess);

return $this;
}

public function setAutoDeleteIntervalDuration(string $autoDeleteIntervalDuration): RegistrarInterface
{
$this->_getTypeServiceCreate()->setAutoDeleteIntervalDuration($autoDeleteIntervalDuration);
$this->getTypeServiceCreate()->setAutoDeleteIntervalDuration($autoDeleteIntervalDuration);

return $this;
}
}

protected function getTypeServiceCreate() : Type\Service\CreateInterface
{
if ($this->typeServiceCreate === null) {
$this->typeServiceCreate = $this->_getTypeServiceCreateClone();
}
return $this->typeServiceCreate;
}
}
60 changes: 56 additions & 4 deletions src/Db/Schema/VersionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,70 @@
use Neighborhoods\Kojo\Doctrine;
use Neighborhoods\Kojo\Doctrine\Connection\DecoratorInterface;
use Neighborhoods\Pylon\Data\Property\Defensive;
use Doctrine\DBAL\Types\Type;

abstract class VersionAbstract implements VersionInterface
{
use Defensive\AwareTrait;
use Doctrine\Connection\Decorator\Repository\AwareTrait;
protected $_createTable;
protected $_tableName;
protected $_mostRecentUnsupportedType;

public function applySchemaSetupChanges(): VersionInterface
{
$connection = $this->_getDoctrineConnectionDecoratorRepository()->getConnection(DecoratorInterface::ID_SCHEMA);
if (!$connection->getSchemaManager()->tablesExist([$this->_getTableName()])) {
$connection->beginTransaction();
try {
$this->_assembleSchemaChanges();
$connection->beginTransaction();
while (!$this->_canAssembleSchemaChanges()) {
$connection->rollBack();
$connection
->getDatabasePlatform()
->registerDoctrineTypeMapping(
$this->_getMostRecentUnsupportedType(),
Type::STRING
);
$connection->beginTransaction();
}
$connection->getSchemaManager()->createTable($this->_getCreateTable());
$connection->commit();
} catch (\Throwable $throwable) {
$connection->rollBack();
throw $throwable;
}
}

return $this;
}

protected function _canAssembleSchemaChanges(): bool
{
try {
$this->_assembleSchemaChanges();
} catch (\Doctrine\DBAL\DBALException $e) {
if ($this->_isUnsupportedTypeException($e)) {
return false;
}

throw $e;
}

return true;
}

protected function _isUnsupportedTypeException(\Doctrine\DBAL\DBALException $e) : bool
{
$stackTrace = $e->getTrace();
$exceptionThrowingFunctionTrace = $stackTrace[0];

if ($exceptionThrowingFunctionTrace['function'] !== 'getDoctrineTypeMapping') {
return false;
}

$this->_setMostRecentUnsupportedType($exceptionThrowingFunctionTrace['args'][0]);
return true;
}

abstract protected function _assembleSchemaChanges(): VersionInterface;

public function applySchemaTearDownChanges(): VersionInterface
Expand Down Expand Up @@ -102,4 +140,18 @@ protected function _hasCreateTable(): bool
{
return $this->_createTable === null ? false : true;
}
}

protected function _getMostRecentUnsupportedType() : string
{
if ($this->_mostRecentUnsupportedType === null) {
throw new \LogicException('MostRecentUnsupportedType is not set.');
}
return $this->_mostRecentUnsupportedType;
}

protected function _setMostRecentUnsupportedType(string $mostRecentUnsupportedType) : VersionInterface
{
$this->_mostRecentUnsupportedType = $mostRecentUnsupportedType;
return $this;
}
}
16 changes: 16 additions & 0 deletions src/ErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Kojo;

class ErrorHandler implements ErrorHandlerInterface
{
public function __invoke(
int $errorNumber,
string $errorString,
string $errorFile,
int $errorLine
): ErrorHandlerInterface {
throw new \ErrorException($errorString, $errorNumber, $errorNumber, $errorFile, $errorLine);
}
}
14 changes: 14 additions & 0 deletions src/ErrorHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

namespace Neighborhoods\Kojo;

interface ErrorHandlerInterface
{
public function __invoke(
int $errorNumber,
string $errorString,
string $errorFile,
int $errorLine
);
}
12 changes: 7 additions & 5 deletions src/Foreman.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Neighborhoods\Kojo\Worker\Locator;
use Neighborhoods\Kojo\Process\Pool\Logger;
use Neighborhoods\Pylon\Data\Property\Defensive;
use Neighborhoods\Kojo\Apm;

class Foreman implements ForemanInterface
{
Expand Down Expand Up @@ -84,12 +83,15 @@ protected function _injectRDBMSConnectionService(): ForemanInterface
protected function _runWorker(): ForemanInterface
{
try {
restore_error_handler();
$this->_injectWorkerService();
$this->_injectRDBMSConnectionService();
call_user_func($this->_getLocator()->getCallable());
} catch (\Exception $throwable) {
set_error_handler(new ErrorHandler());
} catch (\Exception $exception) {
set_error_handler(new ErrorHandler());
$this->_crashJob();
throw $throwable;
throw $exception;
}

return $this;
Expand All @@ -101,10 +103,10 @@ protected function _updateJobAsWorking(): ForemanInterface
$updateWork = $this->_getServiceUpdateWorkFactory()->create();
$updateWork->setJob($this->_getJob());
$updateWork->save();
} catch (\Exception $exception) {
} catch (\Throwable $throwable) {
$this->_panicJob();
$this->_getSemaphore()->releaseLock($this->_getNewJobOwnerResource($this->_getJob()));
throw $exception;
throw $throwable;
}

return $this;
Expand Down
Loading

0 comments on commit f183941

Please sign in to comment.