diff options
author | Frederic Guillot <fred@kanboard.net> | 2016-05-23 20:43:51 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2016-05-23 20:43:51 -0400 |
commit | 8314c99b56e66c2b86dd32432bcf5ed94a3ece02 (patch) | |
tree | 564ed1c270a0907efb5be8da1d971526cd235c6e /app | |
parent | dc6176fca23ea697380ab115aea976fa8271f047 (diff) |
Added QueueManager to process background jobs
Diffstat (limited to 'app')
26 files changed, 366 insertions, 24 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()); |