From 0ca5c8de5133cd0c6c92bb3968e998cf3298357f Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Mon, 23 Jan 2017 22:41:02 +0700 Subject: [PATCH 1/6] Renamed app config worker section to queue + removed max_jobs option (use supervisord) --- app/config.example.yml | 7 +++--- src/PHPCensor/Command/InstallCommand.php | 8 +++--- src/PHPCensor/Command/WorkerCommand.php | 7 +++--- src/PHPCensor/Service/BuildService.php | 8 +++--- src/PHPCensor/Worker/BuildWorker.php | 31 ------------------------ 5 files changed, 15 insertions(+), 46 deletions(-) diff --git a/app/config.example.yml b/app/config.example.yml index b0574a30..c7c05952 100644 --- a/app/config.example.yml +++ b/app/config.example.yml @@ -14,9 +14,10 @@ php-censor: email_settings: from_address: 'no-reply@php-censor.local' smtp_address: - worker: - host: localhost - queue: php-censor-queue + queue: + host: localhost + name: php-censor-queue + lifetime: 600 github: token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' comments: diff --git a/src/PHPCensor/Command/InstallCommand.php b/src/PHPCensor/Command/InstallCommand.php index e4b31f06..8bbfbbd3 100644 --- a/src/PHPCensor/Command/InstallCommand.php +++ b/src/PHPCensor/Command/InstallCommand.php @@ -244,8 +244,8 @@ class InstallCommand extends Command $config['language'] = 'en'; $config['per_page'] = 10; - $config['url'] = $url; - $config['worker'] = $this->getQueueInformation($input, $output, $helper); + $config['url'] = $url; + $config['queue'] = $this->getQueueInformation($input, $output, $helper); return $config; } @@ -278,9 +278,9 @@ class InstallCommand extends Command $rtn['host'] = $helper->ask($input, $output, $questionQueue); } - if (!$rtn['queue'] = $input->getOption('queue-name')) { + if (!$rtn['name'] = $input->getOption('queue-name')) { $questionName = new Question('Enter the queue (tube) name to use [php-censor-queue]: ', 'php-censor-queue'); - $rtn['queue'] = $helper->ask($input, $output, $questionName); + $rtn['name'] = $helper->ask($input, $output, $questionName); } return $rtn; diff --git a/src/PHPCensor/Command/WorkerCommand.php b/src/PHPCensor/Command/WorkerCommand.php index 323e8807..a211d63e 100644 --- a/src/PHPCensor/Command/WorkerCommand.php +++ b/src/PHPCensor/Command/WorkerCommand.php @@ -73,16 +73,15 @@ class WorkerCommand extends Command define('DEBUG_MODE', true); } - $config = Config::getInstance()->get('php-censor.worker', []); + $config = Config::getInstance()->get('php-censor.queue', []); - if (empty($config['host']) || empty($config['queue'])) { + if (empty($config['host']) || empty($config['name'])) { $error = 'The worker is not configured. You must set a host and queue in your config.yml file.'; throw new \Exception($error); } - $worker = new BuildWorker($config['host'], $config['queue']); + $worker = new BuildWorker($config['host'], $config['name']); $worker->setLogger($this->logger); - $worker->setMaxJobs(Config::getInstance()->get('php-censor.worker.max_jobs', -1)); $worker->startWorker(); } } diff --git a/src/PHPCensor/Service/BuildService.php b/src/PHPCensor/Service/BuildService.php index 2ac88557..aaf5a85b 100644 --- a/src/PHPCensor/Service/BuildService.php +++ b/src/PHPCensor/Service/BuildService.php @@ -160,9 +160,9 @@ class BuildService } $config = Config::getInstance(); - $settings = $config->get('php-censor.worker', []); + $settings = $config->get('php-censor.queue', []); - if (!empty($settings['host']) && !empty($settings['queue'])) { + if (!empty($settings['host']) && !empty($settings['name'])) { try { $jobData = [ 'type' => 'php-censor.build', @@ -174,12 +174,12 @@ class BuildService } $pheanstalk = new Pheanstalk($settings['host']); - $pheanstalk->useTube($settings['queue']); + $pheanstalk->useTube($settings['name']); $pheanstalk->put( json_encode($jobData), PheanstalkInterface::DEFAULT_PRIORITY, PheanstalkInterface::DEFAULT_DELAY, - $config->get('php-censor.worker.job_timeout', 600) + $config->get('php-censor.queue.lifetime', 600) ); } catch (\Exception $ex) { $this->queueError = true; diff --git a/src/PHPCensor/Worker/BuildWorker.php b/src/PHPCensor/Worker/BuildWorker.php index 515fb1b4..2302b3b2 100644 --- a/src/PHPCensor/Worker/BuildWorker.php +++ b/src/PHPCensor/Worker/BuildWorker.php @@ -25,13 +25,6 @@ class BuildWorker */ protected $run = true; - /** - * The maximum number of jobs this worker should run before exiting. - * Use -1 for no limit. - * @var int - */ - protected $maxJobs = -1; - /** * The logger for builds to use. * @var \Monolog\Logger @@ -71,14 +64,6 @@ class BuildWorker $this->pheanstalk = new Pheanstalk($this->host); } - /** - * @param int $maxJobs - */ - public function setMaxJobs($maxJobs = -1) - { - $this->maxJobs = $maxJobs; - } - /** * @param Logger $logger */ @@ -100,8 +85,6 @@ class BuildWorker // Get a job from the queue: $job = $this->pheanstalk->reserve(); - $this->checkJobLimit(); - // Get the job data and run the job: $jobData = json_decode($job->getData(), true); @@ -169,20 +152,6 @@ class BuildWorker $this->run = false; } - /** - * Checks if this worker has done the amount of jobs it is allowed to do, and if so tells it to stop - * after this job completes. - */ - protected function checkJobLimit() - { - // Make sure we don't run more than maxJobs jobs on this worker: - $this->totalJobs++; - - if ($this->maxJobs != -1 && $this->maxJobs <= $this->totalJobs) { - $this->stopWorker(); - } - } - /** * Checks that the job received is actually from PHPCI, and has a valid type. * @param Job $job From ced30f27375a01e04e37cb310a6d20403284f606 Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Tue, 24 Jan 2017 08:24:20 +0700 Subject: [PATCH 2/6] Closed app for search robots --- public/robots.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/robots.txt b/public/robots.txt index 0ad279c7..c6742d8a 100644 --- a/public/robots.txt +++ b/public/robots.txt @@ -1,2 +1,2 @@ User-Agent: * -Disallow: +Disallow: / From fc49c34d06e368e22a0a578d226886edcf646be0 Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Wed, 25 Jan 2017 19:07:42 +0700 Subject: [PATCH 3/6] Removed Settings controller (Use manual editing of config.yml file) --- .../Controller/SettingsController.php | 498 ------------------ src/PHPCensor/View/layout.phtml | 9 - 2 files changed, 507 deletions(-) delete mode 100644 src/PHPCensor/Controller/SettingsController.php diff --git a/src/PHPCensor/Controller/SettingsController.php b/src/PHPCensor/Controller/SettingsController.php deleted file mode 100644 index 5e274405..00000000 --- a/src/PHPCensor/Controller/SettingsController.php +++ /dev/null @@ -1,498 +0,0 @@ - - * @package PHPCI - * @subpackage Web - */ -class SettingsController extends Controller -{ - - /** - * @var array - */ - protected $settings; - - /** - * Initialise the controller, set up stores and services. - */ - public function init() - { - parent::init(); - - $parser = new Parser(); - $yaml = file_get_contents(APP_DIR . 'config.yml'); - $this->settings = $parser->parse($yaml); - } - - /** - * Display settings forms. - * - * @return string - */ - public function index() - { - $this->requireAdmin(); - - $this->layout->title = Lang::get('settings'); - - $this->view->settings = $this->settings; - - $basicSettings = []; - if (isset($this->settings['php-censor']['basic'])) { - $basicSettings = $this->settings['php-censor']['basic']; - } - - $buildSettings = []; - if (isset($this->settings['php-censor']['build'])) { - $buildSettings = $this->settings['php-censor']['build']; - } - - $emailSettings = []; - if (isset($this->settings['php-censor']['email_settings'])) { - $emailSettings = $this->settings['php-censor']['email_settings']; - } - - $securitySettings = []; - if (isset($this->settings['php-censor']['security'])) { - $securitySettings = $this->settings['php-censor']['security']; - } - - $this->view->configFile = APP_DIR . 'config.yml'; - $this->view->basicSettings = $this->getBasicForm($basicSettings); - $this->view->buildSettings = $this->getBuildForm($buildSettings); - $this->view->github = $this->getGithubForm(); - $this->view->emailSettings = $this->getEmailForm($emailSettings); - $this->view->securitySettings = $this->getAuthenticationForm($securitySettings); - $this->view->isWriteable = $this->canWriteConfig(); - - if (!empty($this->settings['php-censor']['github']['token'])) { - $this->view->githubUser = $this->getGithubUser($this->settings['php-censor']['github']['token']); - } - - return $this->view->render(); - } - - /** - * Save Github settings. - */ - public function github() - { - $this->requireAdmin(); - - $this->settings['php-censor']['github']['id'] = $this->getParam('githubid', ''); - $this->settings['php-censor']['github']['secret'] = $this->getParam('githubsecret', ''); - $error = $this->storeSettings(); - - $response = new b8\Http\Response\RedirectResponse(); - - if ($error) { - $response->setHeader('Location', APP_URL . 'settings?saved=2'); - } else { - $response->setHeader('Location', APP_URL . 'settings?saved=1'); - } - - return $response; - } - - /** - * Save email settings. - */ - public function email() - { - $this->requireAdmin(); - - $this->settings['php-censor']['email_settings'] = $this->getParams(); - $this->settings['php-censor']['email_settings']['smtp_encryption'] = $this->getParam('smtp_encryption', 0); - - $error = $this->storeSettings(); - - $response = new b8\Http\Response\RedirectResponse(); - - if ($error) { - $response->setHeader('Location', APP_URL . 'settings?saved=2'); - } else { - $response->setHeader('Location', APP_URL . 'settings?saved=1'); - } - - return $response; - } - - /** - * Save build settings. - */ - public function build() - { - $this->requireAdmin(); - - $this->settings['php-censor']['build'] = $this->getParams(); - - $error = $this->storeSettings(); - - $response = new b8\Http\Response\RedirectResponse(); - - if ($error) { - $response->setHeader('Location', APP_URL . 'settings?saved=2'); - } else { - $response->setHeader('Location', APP_URL . 'settings?saved=1'); - } - - return $response; - } - - /** - * Save basic settings. - */ - public function basic() - { - $this->requireAdmin(); - - $this->settings['php-censor']['basic'] = $this->getParams(); - $error = $this->storeSettings(); - - $response = new b8\Http\Response\RedirectResponse(); - - if ($error) { - $response->setHeader('Location', APP_URL . 'settings?saved=2'); - } else { - $response->setHeader('Location', APP_URL . 'settings?saved=1'); - } - - return $response; - } - - /** - * Handle authentication settings - */ - public function authentication() - { - $this->requireAdmin(); - - $this->settings['php-censor']['security']['disable_auth'] = (boolean)$this->getParam('disable_authentication', false); - $this->settings['php-censor']['security']['default_user_id'] = (integer)$_SESSION['php-censor-user-id']; - - $error = $this->storeSettings(); - - $response = new b8\Http\Response\RedirectResponse(); - - if ($error) { - $response->setHeader('Location', APP_URL . 'settings?saved=2'); - } else { - $response->setHeader('Location', APP_URL . 'settings?saved=1'); - } - - return $response; - } - - /** - * Github redirects users back to this URL when t - */ - public function githubCallback() - { - $code = $this->getParam('code', null); - $github = $this->settings['php-censor']['github']; - - if (!is_null($code)) { - $http = new HttpClient(); - $url = 'https://github.com/login/oauth/access_token'; - $params = ['client_id' => $github['id'], 'client_secret' => $github['secret'], 'code' => $code]; - $resp = $http->post($url, $params); - - if ($resp['success']) { - parse_str($resp['body'], $resp); - - $this->settings['php-censor']['github']['token'] = $resp['access_token']; - $this->storeSettings(); - - $response = new b8\Http\Response\RedirectResponse(); - $response->setHeader('Location', APP_URL . 'settings?linked=1'); - return $response; - } - } - - $response = new b8\Http\Response\RedirectResponse(); - $response->setHeader('Location', APP_URL . 'settings?linked=2'); - return $response; - } - - /** - * Convert config to yaml and store to file. - * - * @return mixed - */ - protected function storeSettings() - { - $dumper = new Dumper(); - $yaml = $dumper->dump($this->settings, 4); - file_put_contents(APP_DIR . 'config.yml', $yaml); - - if (error_get_last()) { - $error_get_last = error_get_last(); - return $error_get_last['message']; - } - } - - /** - * Get the Github settings form. - * @return Form - */ - protected function getGithubForm() - { - $form = new Form(); - $form->setMethod('POST'); - $form->setAction(APP_URL . 'settings/github'); - $form->addField(new Form\Element\Csrf('csrf')); - - $field = new Form\Element\Text('githubid'); - $field->setRequired(true); - $field->setPattern('[a-zA-Z0-9]+'); - $field->setLabel(Lang::get('application_id')); - $field->setClass('form-control'); - $field->setContainerClass('form-group'); - $form->addField($field); - - if (isset($this->settings['php-censor']['github']['id'])) { - $field->setValue($this->settings['php-censor']['github']['id']); - } - - $field = new Form\Element\Text('githubsecret'); - $field->setRequired(true); - $field->setPattern('[a-zA-Z0-9]+'); - $field->setLabel(Lang::get('application_secret')); - $field->setClass('form-control'); - $field->setContainerClass('form-group'); - $form->addField($field); - - if (isset($this->settings['php-censor']['github']['secret'])) { - $field->setValue($this->settings['php-censor']['github']['secret']); - } - - $field = new Form\Element\Submit(); - $field->setValue(Lang::get('save')); - $field->setClass('btn btn-success pull-right'); - $form->addField($field); - - return $form; - } - - /** - * Get the email settings form. - * @param array $values - * @return Form - */ - protected function getEmailForm($values = []) - { - $form = new Form(); - $form->setMethod('POST'); - $form->setAction(APP_URL . 'settings/email'); - $form->addField(new Form\Element\Csrf('csrf')); - - $field = new Form\Element\Text('smtp_address'); - $field->setRequired(false); - $field->setLabel(Lang::get('smtp_server')); - $field->setClass('form-control'); - $field->setContainerClass('form-group'); - $field->setValue('localhost'); - $form->addField($field); - - $field = new Form\Element\Text('smtp_port'); - $field->setRequired(false); - $field->setPattern('[0-9]+'); - $field->setLabel(Lang::get('smtp_port')); - $field->setClass('form-control'); - $field->setContainerClass('form-group'); - $field->setValue(25); - $form->addField($field); - - $field = new Form\Element\Text('smtp_username'); - $field->setRequired(false); - $field->setLabel(Lang::get('smtp_username')); - $field->setClass('form-control'); - $field->setContainerClass('form-group'); - $form->addField($field); - - $field = new Form\Element\Password('smtp_password'); - $field->setRequired(false); - $field->setLabel(Lang::get('smtp_password')); - $field->setClass('form-control'); - $field->setContainerClass('form-group'); - $form->addField($field); - - $field = new Form\Element\Email('from_address'); - $field->setRequired(false); - $field->setLabel(Lang::get('from_email_address')); - $field->setClass('form-control'); - $field->setContainerClass('form-group'); - $form->addField($field); - - $field = new Form\Element\Email('default_mailto_address'); - $field->setRequired(false); - $field->setLabel(Lang::get('default_notification_address')); - $field->setClass('form-control'); - $field->setContainerClass('form-group'); - $form->addField($field); - - $field = new Form\Element\Select('smtp_encryption'); - $field->setOptions(['' => Lang::get('none'), 'tls' => Lang::get('tls'), 'ssl' => Lang::get('ssl')]); - $field->setRequired(false); - $field->setLabel(Lang::get('use_smtp_encryption')); - $field->setContainerClass('form-group'); - $field->setValue(1); - $form->addField($field); - - $field = new Form\Element\Submit(); - $field->setValue(Lang::get('save')); - $field->setClass('btn btn-success pull-right'); - $form->addField($field); - - $form->setValues($values); - - return $form; - } - - /** - * Call Github API for our Github user object. - * @param $token - * @return mixed - */ - protected function getGithubUser($token) - { - $http = new HttpClient('https://api.github.com'); - $user = $http->get('/user', ['access_token' => $token]); - - return $user['body']; - } - - /** - * Check if we can write the PHPCI config file. - * @return bool - */ - protected function canWriteConfig() - { - return is_writeable(APP_DIR . 'config.yml'); - } - - /** - * Get the Build settings form. - * @param array $values - * @return Form - */ - protected function getBuildForm($values = []) - { - $form = new Form(); - $form->setMethod('POST'); - $form->setAction(APP_URL . 'settings/build'); - - $field = new Form\Element\Select('failed_after'); - $field->setRequired(false); - $field->setLabel(Lang::get('failed_after')); - $field->setClass('form-control'); - $field->setContainerClass('form-group'); - $field->setOptions([ - 300 => Lang::get('5_mins'), - 900 => Lang::get('15_mins'), - 1800 => Lang::get('30_mins'), - 3600 => Lang::get('1_hour'), - 10800 => Lang::get('3_hours'), - ]); - $field->setValue(1800); - $form->addField($field); - - - $field = new Form\Element\Submit(); - $field->setValue(Lang::get('save')); - $field->setClass('btn btn-success pull-right'); - $form->addField($field); - - $form->setValues($values); - - return $form; - } - - /** - * Get the Basic settings form. - * @param array $values - * @return Form - */ - protected function getBasicForm($values = []) - { - $form = new Form(); - $form->setMethod('POST'); - $form->setAction(APP_URL . 'settings/basic'); - - $field = new Form\Element\Select('language'); - $field->setRequired(true); - $field->setLabel(Lang::get('language')); - $field->setClass('form-control'); - $field->setContainerClass('form-group'); - $field->setOptions(Lang::getLanguageOptions()); - $field->setValue(Lang::getLanguage()); - $form->addField($field); - - - $field = new Form\Element\Submit(); - $field->setValue(Lang::get('save')); - $field->setClass('btn btn-success pull-right'); - $form->addField($field); - - $form->setValues($values); - - return $form; - } - - /** - * Form for disabling user authentication while using a default user - * - * @param array $values - * @return Form - */ - protected function getAuthenticationForm($values = []) - { - $form = new Form(); - $form->setMethod('POST'); - $form->setAction(APP_URL . 'settings/authentication'); - $form->addField(new Form\Element\Csrf('csrf')); - - $field = new Form\Element\Checkbox('disable_authentication'); - $field->setCheckedValue(1); - $field->setRequired(false); - $field->setLabel('Disable Authentication?'); - $field->setContainerClass('form-group'); - $field->setValue(0); - - if (isset($values['disable_auth'])) { - $field->setValue((integer)$values['disable_auth']); - } - - $form->addField($field); - - $field = new Form\Element\Submit(); - $field->setValue('Save »'); - $field->setClass('btn btn-success pull-right'); - $form->addField($field); - - $form->setValues($values); - - return $form; - } -} diff --git a/src/PHPCensor/View/layout.phtml b/src/PHPCensor/View/layout.phtml index 709880f4..19cddb47 100644 --- a/src/PHPCensor/View/layout.phtml +++ b/src/PHPCensor/View/layout.phtml @@ -143,26 +143,17 @@ -