Skip to content

Commit

Permalink
Merge pull request #45 from pixelant/adjustSchedulerTaskErrorHandling
Browse files Browse the repository at this point in the history
Adjust scheduler task error handling
  • Loading branch information
Pavlo Zaporozkyi authored Oct 2, 2018
2 parents aa31b0d + bdeb1c2 commit e4b03ac
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
45 changes: 44 additions & 1 deletion Classes/Utility/Task/ImportTaskUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
use Pixelant\PxaSocialFeed\Utility\Api\FacebookSDKUtility;
use Pixelant\PxaSocialFeed\Utility\Api\TwitterApi;
use Pixelant\PxaSocialFeed\Utility\LoggerUtility;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
Expand Down Expand Up @@ -90,6 +93,9 @@ public function __construct()
*/
public function run($configurationsUids)
{

$errors = false;

if (is_array($configurationsUids)) {
$configurations = $this->configurationRepository->findByUids($configurationsUids);

Expand Down Expand Up @@ -118,6 +124,7 @@ public function run($configurationsUids)
if (is_array($data)) {
$this->updateFacebookFeed($data['data'], $configuration);
} else {
$errors = true;
LoggerUtility::logImportFeed(
'Invalid data from FACEBOOK feed. Please, check credentials.',
$configuration,
Expand Down Expand Up @@ -155,6 +162,7 @@ public function run($configurationsUids)
if (is_array($data)) {
$this->saveInstagramFeed($data['data'], $configuration);
} else {
$errors = true;
LoggerUtility::logImportFeed(
'Invalid data from INSTAGRAM feed. Please, check credentials.',
$configuration,
Expand Down Expand Up @@ -186,6 +194,7 @@ public function run($configurationsUids)
if (is_array($data)) {
$this->saveTwitterFeed($data, $configuration);
} else {
$errors = true;
LoggerUtility::logImportFeed(
'Invalid data from Twitter feed. Please, check credentials.',
$configuration,
Expand All @@ -208,6 +217,7 @@ public function run($configurationsUids)
if (is_array($data)) {
$this->updateYoutubeFeed($data['items'], $configuration);
} else {
$errors = true;
LoggerUtility::logImportFeed(
'Invalid data from YOUTUBE feed. Please, check credentials.',
$configuration,
Expand All @@ -218,11 +228,17 @@ public function run($configurationsUids)
case Token::FACEBOOK_OAUTH2:
$media = $this->getInstagramFeedUsingGraphApi($configuration);

if (!$media) {
$errors = true;
break;
}

// Write media to database
$this->saveGraphInstagramFeed($media['data'], $configuration);

break;
default:
$errors = true;
LoggerUtility::logImportFeed(
'Such social type is not valid',
$configuration,
Expand All @@ -240,6 +256,23 @@ public function run($configurationsUids)
$this->objectManager->get(PersistenceManager::class)->persistAll();
}

if ($errors) {
/** @var FlashMessage $message */
$message = GeneralUtility::makeInstance(
FlashMessage::class,
'The task has finished running, but there were some errors during its execution.
Please check logs for more info',
'',
FlashMessage::WARNING
);

$flashMessageService = $this->objectManager->get(FlashMessageService::class);

/** @var FlashMessageQueue $messageQueue */
$messageQueue = $flashMessageService->getMessageQueueByIdentifier();
$messageQueue->addMessage($message);
}

return true;
}

Expand Down Expand Up @@ -292,6 +325,11 @@ protected function updateExistingFeed(QueryResultInterface $configurations)
// TODO: make it work with other feed types
if ($token->getSocialType() === Token::FACEBOOK_OAUTH2) {
$media = $this->getInstagramFeedUsingGraphApi($configuration, $externalLimit);

if (!$media) {
continue;
}

$keys = array_column($media['data'], 'id');
$values = array_values($media['data']);
$media = array_combine($keys, $values);
Expand Down Expand Up @@ -470,8 +508,13 @@ protected function getInstagramFeedUsingGraphApi(Configuration $configuration, i
$configuration->getSocialId()
);
} catch (\Exception $e) {
$errorMsg = $e->getMessage() === 'You must provide an access token.'
? 'The access token is not generated, not valid or expired. Please open the social feed back-end module
and try to generate a token.'
: $e->getMessage();

LoggerUtility::logImportFeed(
$e->getMessage(),
$errorMsg,
$configuration,
LoggerUtility::ERROR
);
Expand Down
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'uploadfolder' => '0',
'createDirs' => '',
'clearCacheOnLoad' => 0,
'version' => '1.8.1',
'version' => '1.8.2',
'constraints' => [
'depends' => [
'typo3' => '7.6.0-8.9.99',
Expand Down

0 comments on commit e4b03ac

Please sign in to comment.