diff options
29 files changed, 425 insertions, 32 deletions
diff --git a/app/Console/WorkerCommand.php b/app/Console/WorkerCommand.php new file mode 100644 index 00000000..e332624b --- /dev/null +++ b/app/Console/WorkerCommand.php @@ -0,0 +1,28 @@ +<?php + +namespace Kanboard\Console; + +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * Class WorkerCommand + * + * @package Kanboard\Console + * @author Frederic Guillot + */ +class WorkerCommand extends BaseCommand +{ + protected function configure() + { + $this + ->setName('worker') + ->setDescription('Execute queue worker') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $this->queueManager->listen(); + } +} diff --git a/app/Core/Base.php b/app/Core/Base.php index 39a8ccd6..e490484c 100644 --- a/app/Core/Base.php +++ b/app/Core/Base.php @@ -27,6 +27,7 @@ use Pimple\Container; * @property \Kanboard\Core\Http\Response $response * @property \Kanboard\Core\Http\Router $router * @property \Kanboard\Core\Http\Route $route + * @property \Kanboard\Core\Queue\QueueManager $queueManager * @property \Kanboard\Core\Mail\Client $emailClient * @property \Kanboard\Core\ObjectStorage\ObjectStorageInterface $objectStorage * @property \Kanboard\Core\Plugin\Hook $hook diff --git a/app/Core/Mail/Client.php b/app/Core/Mail/Client.php index 641b6abe..44f4753a 100644 --- a/app/Core/Mail/Client.php +++ b/app/Core/Mail/Client.php @@ -2,6 +2,7 @@ namespace Kanboard\Core\Mail; +use Kanboard\Job\EmailJob; use Pimple\Container; use Kanboard\Core\Base; @@ -46,23 +47,29 @@ class Client extends Base public function send($email, $name, $subject, $html) { if (! empty($email)) { - $this->logger->debug('Sending email to '.$email.' ('.MAIL_TRANSPORT.')'); - - $start_time = microtime(true); - $author = 'Kanboard'; + $this->queueManager->push(EmailJob::getInstance($this->container) + ->withParams($email, $name, $subject, $html, $this->getAuthor()) + ); + } - if ($this->userSession->isLogged()) { - $author = e('%s via Kanboard', $this->helper->user->getFullname()); - } + return $this; + } - $this->getTransport(MAIL_TRANSPORT)->sendEmail($email, $name, $subject, $html, $author); + /** + * Get email author + * + * @access public + * @return string + */ + public function getAuthor() + { + $author = 'Kanboard'; - if (DEBUG) { - $this->logger->debug('Email sent in '.round(microtime(true) - $start_time, 6).' seconds'); - } + if ($this->userSession->isLogged()) { + $author = e('%s via Kanboard', $this->helper->user->getFullname()); } - return $this; + return $author; } /** diff --git a/app/Core/Queue/JobHandler.php b/app/Core/Queue/JobHandler.php new file mode 100644 index 00000000..a2c4a2c7 --- /dev/null +++ b/app/Core/Queue/JobHandler.php @@ -0,0 +1,50 @@ +<?php + +namespace Kanboard\Core\Queue; + +use Kanboard\Core\Base; +use Kanboard\Job\BaseJob; +use SimpleQueue\Job; + +/** + * Class JobHandler + * + * @package Kanboard\Core\Queue + * @author Frederic Guillot + */ +class JobHandler extends Base +{ + /** + * Serialize a job + * + * @access public + * @param BaseJob $job + * @return Job + */ + public function serializeJob(BaseJob $job) + { + return new Job(array( + 'class' => get_class($job), + 'params' => $job->getJobParams(), + )); + } + + /** + * Execute a job + * + * @access public + * @param Job $job + */ + public function executeJob(Job $job) + { + $payload = $job->getBody(); + $className = $payload['class']; + + if (DEBUG) { + $this->logger->debug(__METHOD__.' Received job => '.$className); + } + + $worker = new $className($this->container); + call_user_func_array(array($worker, 'execute'), $payload['params']); + } +} diff --git a/app/Core/Queue/QueueManager.php b/app/Core/Queue/QueueManager.php new file mode 100644 index 00000000..f34cb220 --- /dev/null +++ b/app/Core/Queue/QueueManager.php @@ -0,0 +1,71 @@ +<?php + +namespace Kanboard\Core\Queue; + +use Kanboard\Core\Base; +use Kanboard\Job\BaseJob; +use LogicException; +use SimpleQueue\Queue; + +/** + * Class QueueManager + * + * @package Kanboard\Core\Queue + * @author Frederic Guillot + */ +class QueueManager extends Base +{ + /** + * @var Queue + */ + protected $queue = null; + + /** + * Set queue driver + * + * @access public + * @param Queue $queue + * @return $this + */ + public function setQueue(Queue $queue) + { + $this->queue = $queue; + return $this; + } + + /** + * Send a new job to the queue + * + * @access public + * @param BaseJob $job + * @return $this + */ + public function push(BaseJob $job) + { + if ($this->queue !== null) { + $this->queue->push(JobHandler::getInstance($this->container)->serializeJob($job)); + } else { + call_user_func_array(array($job, 'execute'), $job->getJobParams()); + } + + return $this; + } + + /** + * Wait for new jobs + * + * @access public + * @throws LogicException + */ + public function listen() + { + if ($this->queue === null) { + throw new LogicException('No Queue Driver defined!'); + } + + while ($job = $this->queue->pull()) { + JobHandler::getInstance($this->container)->executeJob($job); + $this->queue->completed($job); + } + } +} diff --git a/app/Job/BaseJob.php b/app/Job/BaseJob.php new file mode 100644 index 00000000..60522ac6 --- /dev/null +++ b/app/Job/BaseJob.php @@ -0,0 +1,33 @@ +<?php + +namespace Kanboard\Job; + +use Kanboard\Core\Base; + +/** + * Class BaseJob + * + * @package Kanboard\Job + * @author Frederic Guillot + */ +abstract class BaseJob extends Base +{ + /** + * Job parameters + * + * @access protected + * @var array + */ + protected $jobParams = array(); + + /** + * Get job parameters + * + * @access public + * @return array + */ + public function getJobParams() + { + return $this->jobParams; + } +} diff --git a/app/Job/EmailJob.php b/app/Job/EmailJob.php new file mode 100644 index 00000000..9293a1d4 --- /dev/null +++ b/app/Job/EmailJob.php @@ -0,0 +1,54 @@ +<?php + +namespace Kanboard\Job; + +/** + * Class EmailJob + * + * @package Kanboard\Job + * @author Frederic Guillot + */ +class EmailJob extends BaseJob +{ + /** + * Set job parameters + * + * @access public + * @param string $email + * @param string $name + * @param string $subject + * @param string $html + * @param string $author + * @return $this + */ + public function withParams($email, $name, $subject, $html, $author) + { + $this->jobParams = array($email, $name, $subject, $html, $author); + return $this; + } + + /** + * Execute job + * + * @access public + * @param string $email + * @param string $name + * @param string $subject + * @param string $html + * @param string $author + */ + public function execute($email, $name, $subject, $html, $author) + { + $this->logger->debug(__METHOD__.' Sending email to '.$email.' via '.MAIL_TRANSPORT); + $startTime = microtime(true); + + $this->emailClient + ->getTransport(MAIL_TRANSPORT) + ->sendEmail($email, $name, $subject, $html, $author) + ; + + if (DEBUG) { + $this->logger->debug('Email sent in '.round(microtime(true) - $startTime, 6).' seconds'); + } + } +} diff --git a/app/Job/ProjectMetricJob.php b/app/Job/ProjectMetricJob.php new file mode 100644 index 00000000..2c3e589c --- /dev/null +++ b/app/Job/ProjectMetricJob.php @@ -0,0 +1,40 @@ +<?php + +namespace Kanboard\Job; + +/** + * Class ProjectMetricJob + * + * @package Kanboard\Job + * @author Frederic Guillot + */ +class ProjectMetricJob extends BaseJob +{ + /** + * Set job parameters + * + * @access public + * @param integer $projectId + * @return $this + */ + public function withParams($projectId) + { + $this->jobParams = array($projectId); + return $this; + } + + /** + * Execute job + * + * @access public + * @param integer $projectId + */ + public function execute($projectId) + { + $this->logger->debug(__METHOD__.' Run project metrics calculation'); + $now = date('Y-m-d'); + + $this->projectDailyColumnStats->updateTotals($projectId, $now); + $this->projectDailyStats->updateTotals($projectId, $now); + } +} diff --git a/app/ServiceProvider/ActionProvider.php b/app/ServiceProvider/ActionProvider.php index 7c92f3ce..34202052 100644 --- a/app/ServiceProvider/ActionProvider.php +++ b/app/ServiceProvider/ActionProvider.php @@ -36,7 +36,7 @@ use Kanboard\Action\TaskCloseNoActivity; /** * Action Provider * - * @package serviceProvider + * @package Kanboard\ServiceProvider * @author Frederic Guillot */ class ActionProvider implements ServiceProviderInterface diff --git a/app/ServiceProvider/AuthenticationProvider.php b/app/ServiceProvider/AuthenticationProvider.php index 6b037940..d4f130e2 100644 --- a/app/ServiceProvider/AuthenticationProvider.php +++ b/app/ServiceProvider/AuthenticationProvider.php @@ -17,7 +17,7 @@ use Kanboard\Auth\ReverseProxyAuth; /** * Authentication Provider * - * @package serviceProvider + * @package Kanboard\ServiceProvider * @author Frederic Guillot */ class AuthenticationProvider implements ServiceProviderInterface diff --git a/app/ServiceProvider/AvatarProvider.php b/app/ServiceProvider/AvatarProvider.php index aac4fcab..d17985ed 100644 --- a/app/ServiceProvider/AvatarProvider.php +++ b/app/ServiceProvider/AvatarProvider.php @@ -12,7 +12,7 @@ use Kanboard\User\Avatar\LetterAvatarProvider; /** * Avatar Provider * - * @package serviceProvider + * @package Kanboard\ServiceProvider * @author Frederic Guillot */ class AvatarProvider implements ServiceProviderInterface diff --git a/app/ServiceProvider/ClassProvider.php b/app/ServiceProvider/ClassProvider.php index 31e3a20b..154b921f 100644 --- a/app/ServiceProvider/ClassProvider.php +++ b/app/ServiceProvider/ClassProvider.php @@ -11,6 +11,12 @@ use Kanboard\Core\Http\OAuth2; use Kanboard\Core\Tool; use Kanboard\Core\Http\Client as HttpClient; +/** + * Class ClassProvider + * + * @package Kanboard\ServiceProvider + * @author Frederic Guillot + */ class ClassProvider implements ServiceProviderInterface { private $classes = array( diff --git a/app/ServiceProvider/DatabaseProvider.php b/app/ServiceProvider/DatabaseProvider.php index c0dcd366..a3f57457 100644 --- a/app/ServiceProvider/DatabaseProvider.php +++ b/app/ServiceProvider/DatabaseProvider.php @@ -8,6 +8,12 @@ use Pimple\Container; use Pimple\ServiceProviderInterface; use PicoDb\Database; +/** + * Class DatabaseProvider + * + * @package Kanboard\ServiceProvider + * @author Frederic Guillot + */ class DatabaseProvider implements ServiceProviderInterface { /** diff --git a/app/ServiceProvider/EventDispatcherProvider.php b/app/ServiceProvider/EventDispatcherProvider.php index 6b3dc098..57543fe4 100644 --- a/app/ServiceProvider/EventDispatcherProvider.php +++ b/app/ServiceProvider/EventDispatcherProvider.php @@ -15,6 +15,12 @@ use Kanboard\Subscriber\SubtaskTimeTrackingSubscriber; use Kanboard\Subscriber\TransitionSubscriber; use Kanboard\Subscriber\RecurringTaskSubscriber; +/** + * Class EventDispatcherProvider + * + * @package Kanboard\ServiceProvider + * @author Frederic Guillot + */ class EventDispatcherProvider implements ServiceProviderInterface { public function register(Container $container) diff --git a/app/ServiceProvider/ExternalLinkProvider.php b/app/ServiceProvider/ExternalLinkProvider.php index 8b71ec81..2cec768d 100644 --- a/app/ServiceProvider/ExternalLinkProvider.php +++ b/app/ServiceProvider/ExternalLinkProvider.php @@ -12,7 +12,7 @@ use Kanboard\ExternalLink\FileLinkProvider; /** * External Link Provider * - * @package serviceProvider + * @package Kanboard\ServiceProvider * @author Frederic Guillot */ class ExternalLinkProvider implements ServiceProviderInterface diff --git a/app/ServiceProvider/FilterProvider.php b/app/ServiceProvider/FilterProvider.php index f3918d77..b79c5185 100644 --- a/app/ServiceProvider/FilterProvider.php +++ b/app/ServiceProvider/FilterProvider.php @@ -37,7 +37,7 @@ use Pimple\ServiceProviderInterface; /** * Filter Provider * - * @package serviceProvider + * @package Kanboard\ServiceProvider * @author Frederic Guillot */ class FilterProvider implements ServiceProviderInterface diff --git a/app/ServiceProvider/GroupProvider.php b/app/ServiceProvider/GroupProvider.php index b222b218..08548c73 100644 --- a/app/ServiceProvider/GroupProvider.php +++ b/app/ServiceProvider/GroupProvider.php @@ -11,7 +11,7 @@ use Kanboard\Group\LdapBackendGroupProvider; /** * Group Provider * - * @package serviceProvider + * @package Kanboard\ServiceProvider * @author Frederic Guillot */ class GroupProvider implements ServiceProviderInterface diff --git a/app/ServiceProvider/HelperProvider.php b/app/ServiceProvider/HelperProvider.php index bf3956a2..114212d2 100644 --- a/app/ServiceProvider/HelperProvider.php +++ b/app/ServiceProvider/HelperProvider.php @@ -7,6 +7,12 @@ use Kanboard\Core\Template; use Pimple\Container; use Pimple\ServiceProviderInterface; +/** + * Class HelperProvider + * + * @package Kanboard\ServiceProvider + * @author Frederic Guillot + */ class HelperProvider implements ServiceProviderInterface { public function register(Container $container) diff --git a/app/ServiceProvider/LoggingProvider.php b/app/ServiceProvider/LoggingProvider.php index 4d6adbe4..cb6d0baa 100644 --- a/app/ServiceProvider/LoggingProvider.php +++ b/app/ServiceProvider/LoggingProvider.php @@ -11,6 +11,12 @@ use SimpleLogger\Stdout; use SimpleLogger\Syslog; use SimpleLogger\File; +/** + * Class LoggingProvider + * + * @package Kanboard\ServiceProvider + * @author Frederic Guillot + */ class LoggingProvider implements ServiceProviderInterface { public function register(Container $container) diff --git a/app/ServiceProvider/NotificationProvider.php b/app/ServiceProvider/NotificationProvider.php index 83daf65d..23d1d516 100644 --- a/app/ServiceProvider/NotificationProvider.php +++ b/app/ServiceProvider/NotificationProvider.php @@ -12,7 +12,7 @@ use Kanboard\Notification\Web as WebNotification; /** * Notification Provider * - * @package serviceProvider + * @package Kanboard\ServiceProvider * @author Frederic Guillot */ class NotificationProvider implements ServiceProviderInterface diff --git a/app/ServiceProvider/PluginProvider.php b/app/ServiceProvider/PluginProvider.php index d2f1666b..4cf57251 100644 --- a/app/ServiceProvider/PluginProvider.php +++ b/app/ServiceProvider/PluginProvider.php @@ -9,7 +9,7 @@ use Kanboard\Core\Plugin\Loader; /** * Plugin Provider * - * @package serviceProvider + * @package Kanboard\ServiceProvider * @author Frederic Guillot */ class PluginProvider implements ServiceProviderInterface diff --git a/app/ServiceProvider/QueueProvider.php b/app/ServiceProvider/QueueProvider.php new file mode 100644 index 00000000..946b436a --- /dev/null +++ b/app/ServiceProvider/QueueProvider.php @@ -0,0 +1,27 @@ +<?php + +namespace Kanboard\ServiceProvider; + +use Kanboard\Core\Queue\QueueManager; +use Pimple\Container; +use Pimple\ServiceProviderInterface; + +/** + * Class QueueProvider + * + * @package Kanboard\ServiceProvider + * @author Frederic Guillot + */ +class QueueProvider implements ServiceProviderInterface +{ + /** + * Registers services on the given container. + * + * @param Container $container + */ + public function register(Container $container) + { + $container['queueManager'] = new QueueManager($container); + return $container; + } +} diff --git a/app/ServiceProvider/RouteProvider.php b/app/ServiceProvider/RouteProvider.php index a7338994..4a126b58 100644 --- a/app/ServiceProvider/RouteProvider.php +++ b/app/ServiceProvider/RouteProvider.php @@ -10,7 +10,7 @@ use Kanboard\Core\Http\Router; /** * Route Provider * - * @package serviceProvider + * @package Kanboard\ServiceProvider * @author Frederic Guillot */ class RouteProvider implements ServiceProviderInterface diff --git a/app/ServiceProvider/SessionProvider.php b/app/ServiceProvider/SessionProvider.php index 0999d531..96dcac2e 100644 --- a/app/ServiceProvider/SessionProvider.php +++ b/app/ServiceProvider/SessionProvider.php @@ -11,7 +11,7 @@ use Kanboard\Core\Session\FlashMessage; /** * Session Provider * - * @package serviceProvider + * @package Kanboard\ServiceProvider * @author Frederic Guillot */ class SessionProvider implements ServiceProviderInterface diff --git a/app/Subscriber/ProjectDailySummarySubscriber.php b/app/Subscriber/ProjectDailySummarySubscriber.php index 44138f43..982240c1 100644 --- a/app/Subscriber/ProjectDailySummarySubscriber.php +++ b/app/Subscriber/ProjectDailySummarySubscriber.php @@ -3,6 +3,7 @@ namespace Kanboard\Subscriber; use Kanboard\Event\TaskEvent; +use Kanboard\Job\ProjectMetricJob; use Kanboard\Model\Task; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -23,8 +24,7 @@ class ProjectDailySummarySubscriber extends BaseSubscriber implements EventSubsc { if (isset($event['project_id']) && !$this->isExecuted()) { $this->logger->debug('Subscriber executed: '.__METHOD__); - $this->projectDailyColumnStats->updateTotals($event['project_id'], date('Y-m-d')); - $this->projectDailyStats->updateTotals($event['project_id'], date('Y-m-d')); + $this->queueManager->push(ProjectMetricJob::getInstance($this->container)->withParams($event['project_id'])); } } } diff --git a/app/common.php b/app/common.php index fa95773b..e018aa12 100644 --- a/app/common.php +++ b/app/common.php @@ -45,4 +45,5 @@ $container->register(new Kanboard\ServiceProvider\ActionProvider()); $container->register(new Kanboard\ServiceProvider\ExternalLinkProvider()); $container->register(new Kanboard\ServiceProvider\AvatarProvider()); $container->register(new Kanboard\ServiceProvider\FilterProvider()); +$container->register(new Kanboard\ServiceProvider\QueueProvider()); $container->register(new Kanboard\ServiceProvider\PluginProvider()); diff --git a/composer.json b/composer.json index df91d0c5..c5c3b68e 100644 --- a/composer.json +++ b/composer.json @@ -30,6 +30,7 @@ "fguillot/picodb" : "1.0.11", "fguillot/simpleLogger" : "1.0.1", "fguillot/simple-validator" : "1.0.0", + "fguillot/simple-queue" : "dev-master", "paragonie/random_compat": "@stable", "pimple/pimple" : "~3.0", "ramsey/array_column": "@stable", diff --git a/composer.lock b/composer.lock index 91d88ee2..a89e184f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "7679ea6537bad9f8be67619d321a72f9", - "content-hash": "ef2d3ad0af1dcad85710d537150ec151", + "hash": "0c3cc3fb800c021f6829868a36cc0f1e", + "content-hash": "02f905c6f442f47221f2b9f5b9f05766", "packages": [ { "name": "christian-riesen/base32", @@ -275,6 +275,53 @@ "time": "2016-05-15 01:02:48" }, { + "name": "fguillot/simple-queue", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/fguillot/simple-queue.git", + "reference": "ce5a4c502feb619a7adffc0d39dfd10b00ca9623" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/fguillot/simple-queue/zipball/ce5a4c502feb619a7adffc0d39dfd10b00ca9623", + "reference": "ce5a4c502feb619a7adffc0d39dfd10b00ca9623", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "mariano/disque-php": "~2.0", + "pda/pheanstalk": "~3.0", + "php-amqplib/php-amqplib": "2.6.*", + "phpunit/phpunit": "5.3.*" + }, + "suggest": { + "mariano/disque-php": "Required to use the Disque queue driver (~2.0).", + "pda/pheanstalk": "Required to use the Beanstalk queue driver (~3.0).", + "php-amqplib/php-amqplib": "Required to use the RabbitMQ queue driver (2.6.*)." + }, + "type": "library", + "autoload": { + "psr-4": { + "SimpleQueue\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Frédéric Guillot" + } + ], + "description": "Abstraction layer for multiple queue systems", + "homepage": "https://github.com/fguillot/SimpleQueue", + "time": "2016-05-23 18:42:15" + }, + { "name": "fguillot/simple-validator", "version": "1.0.0", "source": { @@ -747,16 +794,16 @@ }, { "name": "symfony/polyfill-mbstring", - "version": "v1.1.1", + "version": "v1.2.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "1289d16209491b584839022f29257ad859b8532d" + "reference": "dff51f72b0706335131b00a7f49606168c582594" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/1289d16209491b584839022f29257ad859b8532d", - "reference": "1289d16209491b584839022f29257ad859b8532d", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/dff51f72b0706335131b00a7f49606168c582594", + "reference": "dff51f72b0706335131b00a7f49606168c582594", "shasum": "" }, "require": { @@ -768,7 +815,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.1-dev" + "dev-master": "1.2-dev" } }, "autoload": { @@ -802,7 +849,7 @@ "portable", "shim" ], - "time": "2016-01-20 09:13:37" + "time": "2016-05-18 14:26:46" } ], "packages-dev": [ @@ -859,6 +906,7 @@ "aliases": [], "minimum-stability": "stable", "stability-flags": { + "fguillot/simple-queue": 20, "paragonie/random_compat": 0, "ramsey/array_column": 0 }, @@ -6,6 +6,7 @@ use Kanboard\Console\PluginUninstallCommand; use Kanboard\Console\PluginUpgradeCommand; use Kanboard\Console\ResetPasswordCommand; use Kanboard\Console\ResetTwoFactorCommand; +use Kanboard\Console\WorkerCommand; use Symfony\Component\Console\Application; use Symfony\Component\EventDispatcher\Event; use Kanboard\Console\TaskOverdueNotificationCommand; @@ -37,6 +38,7 @@ try { $application->add(new LocaleComparatorCommand($container)); $application->add(new TaskTriggerCommand($container)); $application->add(new CronjobCommand($container)); + $application->add(new WorkerCommand($container)); $application->add(new ResetPasswordCommand($container)); $application->add(new ResetTwoFactorCommand($container)); $application->add(new PluginUpgradeCommand($container)); |