Make sure that some event subscribers are not executed multiple times

This commit is contained in:
Frederic Guillot
2016-01-16 21:06:36 -05:00
parent 6a0895ef76
commit 6a7b8ec60f
10 changed files with 95 additions and 39 deletions

View File

@@ -14,7 +14,8 @@ Improvements:
* Move validators to a separate namespace
* Improve and write unit tests for reports
* Reduce the number of SQL queries for project daily column stats
* Remove subscriber to update date_moved field
* Remove event subscriber to update date_moved field
* Make sure that some event subscribers are not executed multiple times
Bug fixes:

View File

@@ -3,7 +3,6 @@
namespace Kanboard\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Kanboard\Core\Base;
use Kanboard\Core\Security\AuthenticationManager;
use Kanboard\Core\Session\SessionManager;
use Kanboard\Event\AuthSuccessEvent;
@@ -15,7 +14,7 @@ use Kanboard\Event\AuthFailureEvent;
* @package subscriber
* @author Frederic Guillot
*/
class AuthSubscriber extends Base implements EventSubscriberInterface
class AuthSubscriber extends BaseSubscriber implements EventSubscriberInterface
{
/**
* Get event listeners
@@ -41,6 +40,8 @@ class AuthSubscriber extends Base implements EventSubscriberInterface
*/
public function afterLogin(AuthSuccessEvent $event)
{
$this->logger->debug('Subscriber executed: '.__CLASS__.'::'.__METHOD__);
$userAgent = $this->request->getUserAgent();
$ipAddress = $this->request->getIpAddress();
@@ -70,6 +71,7 @@ class AuthSubscriber extends Base implements EventSubscriberInterface
*/
public function afterLogout()
{
$this->logger->debug('Subscriber executed: '.__CLASS__.'::'.__METHOD__);
$credentials = $this->rememberMeCookie->read();
if ($credentials !== false) {
@@ -90,6 +92,7 @@ class AuthSubscriber extends Base implements EventSubscriberInterface
*/
public function onLoginFailure(AuthFailureEvent $event)
{
$this->logger->debug('Subscriber executed: '.__CLASS__.'::'.__METHOD__);
$username = $event->getUsername();
if (! empty($username)) {

View File

@@ -0,0 +1,40 @@
<?php
namespace Kanboard\Subscriber;
use Kanboard\Core\Base;
/**
* Base class for subscribers
*
* @package subscriber
* @author Frederic Guillot
*/
class BaseSubscriber extends Base
{
/**
* Method called
*
* @access private
* @var array
*/
private $called = array();
/**
* Check if a method has been executed
*
* @access public
* @param string $method
* @return boolean
*/
public function isExecuted($method = '')
{
if (isset($this->called[$method])) {
return true;
}
$this->called[$method] = true;
return false;
}
}

View File

@@ -3,9 +3,8 @@
namespace Kanboard\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Kanboard\Core\Base;
class BootstrapSubscriber extends Base implements EventSubscriberInterface
class BootstrapSubscriber extends BaseSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
@@ -16,6 +15,7 @@ class BootstrapSubscriber extends Base implements EventSubscriberInterface
public function execute()
{
$this->logger->debug('Subscriber executed: '.__CLASS__.'::'.__METHOD__);
$this->config->setupTranslations();
$this->config->setupTimezone();
$this->actionManager->attachEvents();

View File

@@ -2,7 +2,6 @@
namespace Kanboard\Subscriber;
use Kanboard\Core\Base;
use Kanboard\Event\GenericEvent;
use Kanboard\Model\Task;
use Kanboard\Model\Comment;
@@ -10,7 +9,7 @@ use Kanboard\Model\Subtask;
use Kanboard\Model\File;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class NotificationSubscriber extends Base implements EventSubscriberInterface
class NotificationSubscriber extends BaseSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
@@ -35,14 +34,17 @@ class NotificationSubscriber extends Base implements EventSubscriberInterface
public function handleEvent(GenericEvent $event, $event_name)
{
$event_data = $this->getEventData($event);
if (! $this->isExecuted()) {
$this->logger->debug('Subscriber executed: '.__CLASS__.'::'.__METHOD__);
$event_data = $this->getEventData($event);
if (! empty($event_data)) {
if (! empty($event['mention'])) {
$this->userNotification->sendUserNotification($event['mention'], $event_name, $event_data);
} else {
$this->userNotification->sendNotifications($event_name, $event_data);
$this->projectNotification->sendNotifications($event_data['task']['project_id'], $event_name, $event_data);
if (! empty($event_data)) {
if (! empty($event['mention'])) {
$this->userNotification->sendUserNotification($event['mention'], $event_name, $event_data);
} else {
$this->userNotification->sendNotifications($event_name, $event_data);
$this->projectNotification->sendNotifications($event_data['task']['project_id'], $event_name, $event_data);
}
}
}
}

View File

@@ -6,22 +6,23 @@ use Kanboard\Event\TaskEvent;
use Kanboard\Model\Task;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProjectDailySummarySubscriber extends \Kanboard\Core\Base implements EventSubscriberInterface
class ProjectDailySummarySubscriber extends BaseSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
Task::EVENT_CREATE_UPDATE => array('execute', 0),
Task::EVENT_CLOSE => array('execute', 0),
Task::EVENT_OPEN => array('execute', 0),
Task::EVENT_MOVE_COLUMN => array('execute', 0),
Task::EVENT_MOVE_SWIMLANE => array('execute', 0),
Task::EVENT_CREATE_UPDATE => 'execute',
Task::EVENT_CLOSE => 'execute',
Task::EVENT_OPEN => 'execute',
Task::EVENT_MOVE_COLUMN => 'execute',
Task::EVENT_MOVE_SWIMLANE => 'execute',
);
}
public function execute(TaskEvent $event)
{
if (isset($event['project_id'])) {
if (isset($event['project_id']) && !$this->isExecuted()) {
$this->logger->debug('Subscriber executed: '.__CLASS__.'::'.__METHOD__);
$this->projectDailyColumnStats->updateTotals($event['project_id'], date('Y-m-d'));
$this->projectDailyStats->updateTotals($event['project_id'], date('Y-m-d'));
}

View File

@@ -6,25 +6,26 @@ use Kanboard\Event\GenericEvent;
use Kanboard\Model\Task;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProjectModificationDateSubscriber extends \Kanboard\Core\Base implements EventSubscriberInterface
class ProjectModificationDateSubscriber extends BaseSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
Task::EVENT_CREATE_UPDATE => array('execute', 0),
Task::EVENT_CLOSE => array('execute', 0),
Task::EVENT_OPEN => array('execute', 0),
Task::EVENT_MOVE_SWIMLANE => array('execute', 0),
Task::EVENT_MOVE_COLUMN => array('execute', 0),
Task::EVENT_MOVE_POSITION => array('execute', 0),
Task::EVENT_MOVE_PROJECT => array('execute', 0),
Task::EVENT_ASSIGNEE_CHANGE => array('execute', 0),
Task::EVENT_CREATE_UPDATE => 'execute',
Task::EVENT_CLOSE => 'execute',
Task::EVENT_OPEN => 'execute',
Task::EVENT_MOVE_SWIMLANE => 'execute',
Task::EVENT_MOVE_COLUMN => 'execute',
Task::EVENT_MOVE_POSITION => 'execute',
Task::EVENT_MOVE_PROJECT => 'execute',
Task::EVENT_ASSIGNEE_CHANGE => 'execute',
);
}
public function execute(GenericEvent $event)
{
if (isset($event['project_id'])) {
if (isset($event['project_id']) && !$this->isExecuted()) {
$this->logger->debug('Subscriber executed: '.__CLASS__.'::'.__METHOD__);
$this->project->updateModificationDate($event['project_id']);
}
}

View File

@@ -6,18 +6,20 @@ use Kanboard\Event\TaskEvent;
use Kanboard\Model\Task;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RecurringTaskSubscriber extends \Kanboard\Core\Base implements EventSubscriberInterface
class RecurringTaskSubscriber extends BaseSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
Task::EVENT_MOVE_COLUMN => array('onMove', 0),
Task::EVENT_CLOSE => array('onClose', 0),
Task::EVENT_MOVE_COLUMN => 'onMove',
Task::EVENT_CLOSE => 'onClose',
);
}
public function onMove(TaskEvent $event)
{
$this->logger->debug('Subscriber executed: '.__CLASS__.'::'.__METHOD__);
if ($event['recurrence_status'] == Task::RECURRING_STATUS_PENDING) {
if ($event['recurrence_trigger'] == Task::RECURRING_TRIGGER_FIRST_COLUMN && $this->board->getFirstColumn($event['project_id']) == $event['src_column_id']) {
$this->taskDuplication->duplicateRecurringTask($event['task_id']);
@@ -29,6 +31,8 @@ class RecurringTaskSubscriber extends \Kanboard\Core\Base implements EventSubscr
public function onClose(TaskEvent $event)
{
$this->logger->debug('Subscriber executed: '.__CLASS__.'::'.__METHOD__);
if ($event['recurrence_status'] == Task::RECURRING_STATUS_PENDING && $event['recurrence_trigger'] == Task::RECURRING_TRIGGER_CLOSE) {
$this->taskDuplication->duplicateRecurringTask($event['task_id']);
}

View File

@@ -6,13 +6,13 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Kanboard\Model\Subtask;
use Kanboard\Event\SubtaskEvent;
class SubtaskTimeTrackingSubscriber extends \Kanboard\Core\Base implements EventSubscriberInterface
class SubtaskTimeTrackingSubscriber extends BaseSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
Subtask::EVENT_CREATE => array('updateTaskTime', 0),
Subtask::EVENT_DELETE => array('updateTaskTime', 0),
Subtask::EVENT_CREATE => 'updateTaskTime',
Subtask::EVENT_DELETE => 'updateTaskTime',
Subtask::EVENT_UPDATE => array(
array('logStartEnd', 10),
array('updateTaskTime', 0),
@@ -23,6 +23,7 @@ class SubtaskTimeTrackingSubscriber extends \Kanboard\Core\Base implements Event
public function updateTaskTime(SubtaskEvent $event)
{
if (isset($event['task_id'])) {
$this->logger->debug('Subscriber executed: '.__CLASS__.'::'.__METHOD__);
$this->subtaskTimeTracking->updateTaskTimeTracking($event['task_id']);
}
}
@@ -30,6 +31,7 @@ class SubtaskTimeTrackingSubscriber extends \Kanboard\Core\Base implements Event
public function logStartEnd(SubtaskEvent $event)
{
if (isset($event['status']) && $this->config->get('subtask_time_tracking') == 1) {
$this->logger->debug('Subscriber executed: '.__CLASS__.'::'.__METHOD__);
$subtask = $this->subtask->getById($event['id']);
if (empty($subtask['user_id'])) {

View File

@@ -6,17 +6,19 @@ use Kanboard\Event\TaskEvent;
use Kanboard\Model\Task;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class TransitionSubscriber extends \Kanboard\Core\Base implements EventSubscriberInterface
class TransitionSubscriber extends BaseSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
Task::EVENT_MOVE_COLUMN => array('execute', 0),
Task::EVENT_MOVE_COLUMN => 'execute',
);
}
public function execute(TaskEvent $event)
{
$this->logger->debug('Subscriber executed: '.__CLASS__.'::'.__METHOD__);
$user_id = $this->userSession->getId();
if (! empty($user_id)) {