[stable-0.7] Catch exception during cron execution and log to d… (#1421)

[stable-0.7] Catch exception during cron execution and log to debug
This commit is contained in:
Julius Härtl
2019-12-30 09:34:31 +01:00
committed by GitHub
3 changed files with 23 additions and 4 deletions

View File

@@ -27,6 +27,8 @@ use OC\BackgroundJob\Job;
use OCA\Deck\Db\Card; use OCA\Deck\Db\Card;
use OCA\Deck\Db\CardMapper; use OCA\Deck\Db\CardMapper;
use OCA\Deck\Notification\NotificationHelper; use OCA\Deck\Notification\NotificationHelper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\ILogger;
class ScheduledNotifications extends Job { class ScheduledNotifications extends Job {
@@ -34,13 +36,17 @@ class ScheduledNotifications extends Job {
protected $cardMapper; protected $cardMapper;
/** @var NotificationHelper */ /** @var NotificationHelper */
protected $notificationHelper; protected $notificationHelper;
/** @var ILogger */
protected $logger;
public function __construct( public function __construct(
CardMapper $cardMapper, CardMapper $cardMapper,
NotificationHelper $notificationHelper NotificationHelper $notificationHelper,
ILogger $logger
) { ) {
$this->cardMapper = $cardMapper; $this->cardMapper = $cardMapper;
$this->notificationHelper = $notificationHelper; $this->notificationHelper = $notificationHelper;
$this->logger = $logger;
} }
/** /**
@@ -52,8 +58,13 @@ class ScheduledNotifications extends Job {
$cards = $this->cardMapper->findOverdue(); $cards = $this->cardMapper->findOverdue();
/** @var Card $card */ /** @var Card $card */
foreach ($cards as $card) { foreach ($cards as $card) {
$this->notificationHelper->sendCardDuedate($card); try {
$this->notificationHelper->sendCardDuedate($card);
} catch (DoesNotExistException $e) {
// Skip if any error occurs
$this->logger->debug('Could not create overdue notification for card with id ' . $card->getId());
}
} }
} }
} }

View File

@@ -67,6 +67,10 @@ class NotificationHelper {
$this->currentUser = $userId; $this->currentUser = $userId;
} }
/**
* @param $card
* @throws \OCP\AppFramework\Db\DoesNotExistException
*/
public function sendCardDuedate($card) { public function sendCardDuedate($card) {
// check if notification has already been sent // check if notification has already been sent
// ideally notifications should not be deleted once seen by the user so we can // ideally notifications should not be deleted once seen by the user so we can

View File

@@ -28,6 +28,7 @@ use OCA\Deck\Db\BoardMapper;
use OCA\Deck\Db\Card; use OCA\Deck\Db\Card;
use OCA\Deck\Db\CardMapper; use OCA\Deck\Db\CardMapper;
use OCA\Deck\Notification\NotificationHelper; use OCA\Deck\Notification\NotificationHelper;
use OCP\ILogger;
class ScheduledNoificationsTest extends \Test\TestCase { class ScheduledNoificationsTest extends \Test\TestCase {
@@ -35,6 +36,8 @@ class ScheduledNoificationsTest extends \Test\TestCase {
protected $cardMapper; protected $cardMapper;
/** @var NotificationHelper|\PHPUnit\Framework\MockObject\MockObject */ /** @var NotificationHelper|\PHPUnit\Framework\MockObject\MockObject */
protected $notificationHelper; protected $notificationHelper;
/** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
protected $logger;
/** @var ScheduledNotifications */ /** @var ScheduledNotifications */
protected $scheduledNotifications; protected $scheduledNotifications;
@@ -42,7 +45,8 @@ class ScheduledNoificationsTest extends \Test\TestCase {
parent::setUp(); parent::setUp();
$this->cardMapper = $this->createMock(CardMapper::class); $this->cardMapper = $this->createMock(CardMapper::class);
$this->notificationHelper = $this->createMock(NotificationHelper::class); $this->notificationHelper = $this->createMock(NotificationHelper::class);
$this->scheduledNotifications = new ScheduledNotifications($this->cardMapper, $this->notificationHelper); $this->logger = $this->createMock(ILogger::class);
$this->scheduledNotifications = new ScheduledNotifications($this->cardMapper, $this->notificationHelper, $this->logger);
} }
public function testDeleteCron() { public function testDeleteCron() {