Skip to content

Commit

Permalink
PISHPS-348: fixed memory issue of ExpireOrderTaskHandler; fixed stan …
Browse files Browse the repository at this point in the history
…error attribute not repeatable (#822)
  • Loading branch information
m-muxfeld-diw authored and Vitalij Mik committed Sep 5, 2024
1 parent 3c0faa6 commit 73546cd
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 64 deletions.
6 changes: 6 additions & 0 deletions src/Resources/config/services/scheduled_tasks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
<tag name="messenger.message_handler"/>
</service>

<service id="Kiener\MolliePayments\ScheduledTask\Subscription\RenewalReminder\RenewalReminderTaskDevHandler">
<argument type="service" id="Kiener\MolliePayments\Repository\ScheduledTask\ScheduledTaskRepository"/>
<argument type="service" id="Kiener\MolliePayments\Components\Subscription\SubscriptionManager"/>
<argument type="service" id="mollie_payments.logger"/>
<tag name="messenger.message_handler"/>
</service>

<service id="Kiener\MolliePayments\ScheduledTask\OrderStatus\ExpireOrderTask">
<tag name="shopware.scheduled.task"/>
Expand Down
3 changes: 3 additions & 0 deletions src/ScheduledTask/OrderStatus/ExpireOrderTaskHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler;

#[\Symfony\Component\Messenger\Attribute\AsMessageHandler(handles: ExpireOrderTask::class)]
Expand Down Expand Up @@ -40,6 +41,8 @@ public function run(): void
$criteria = new Criteria();
$criteria->addAssociation('transactions.stateMachineState');
$criteria->getAssociation('transactions.stateMachineState')->addFilter(new EqualsFilter('technicalName', OrderStates::STATE_IN_PROGRESS));
$criteria->addSorting(new FieldSorting('orderDateTime', FieldSorting::DESCENDING));
$criteria->setLimit(10);

$this->logger->debug('Search for orders which are in progress state');

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php declare(strict_types=1);

namespace Kiener\MolliePayments\ScheduledTask\Subscription\RenewalReminder;

use Kiener\MolliePayments\Components\Subscription\SubscriptionManager;
use Kiener\MolliePayments\Repository\ScheduledTask\ScheduledTaskRepositoryInterface;
use Psr\Log\LoggerInterface;
use Shopware\Core\Framework\Api\Context\SystemSource;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler;

abstract class AbstractRenewalReminderTaskHandler extends ScheduledTaskHandler
{
/**
* @var SubscriptionManager
*/
private $subscriptionManager;

/**
* @var LoggerInterface
*/
private $logger;


/**
* @param ScheduledTaskRepositoryInterface $scheduledTaskRepository
* @param SubscriptionManager $subscriptionManager
* @param LoggerInterface $logger
*/
public function __construct(ScheduledTaskRepositoryInterface $scheduledTaskRepository, SubscriptionManager $subscriptionManager, LoggerInterface $logger)
{
/** @phpstan-ignore-next-line */
parent::__construct($scheduledTaskRepository->getRepository(), $logger);

$this->subscriptionManager = $subscriptionManager;
$this->logger = $logger;
}

/**
* @return iterable<mixed>
*/
public static function getHandledMessages(): iterable
{
return [
RenewalReminderTask::class,
RenewalReminderTaskDev::class,
];
}

/**
*
*/
public function run(): void
{
try {
$this->logger->info('Starting Subscription Renewal Reminder from Scheduled Tasks.');

$context = new Context(new SystemSource());

$remindedCount = $this->subscriptionManager->remindSubscriptionRenewal($context);

$this->logger->debug($remindedCount . ' subscriptions renewal reminders have been processed successfully!');
} catch (\Throwable $ex) {
$this->logger->error(
'Error when running Scheduled Task for Subscription Renewal Reminders. ' . $ex->getMessage()
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\ScheduledTask\Subscription\RenewalReminder;

#[\Symfony\Component\Messenger\Attribute\AsMessageHandler(handles: RenewalReminderTaskDev::class)]
class RenewalReminderTaskDevHandler extends AbstractRenewalReminderTaskHandler
{
}
67 changes: 3 additions & 64 deletions src/ScheduledTask/Subscription/RenewalReminder/RenewalReminderTaskHandler.php
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,70 +1,9 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Kiener\MolliePayments\ScheduledTask\Subscription\RenewalReminder;

use Kiener\MolliePayments\Components\Subscription\SubscriptionManager;
use Kiener\MolliePayments\Repository\ScheduledTask\ScheduledTaskRepositoryInterface;
use Psr\Log\LoggerInterface;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\MessageQueue\ScheduledTask\ScheduledTaskHandler;

#[\Symfony\Component\Messenger\Attribute\AsMessageHandler(handles: RenewalReminderTask::class)]
#[\Symfony\Component\Messenger\Attribute\AsMessageHandler(handles: RenewalReminderTaskDev::class)]
class RenewalReminderTaskHandler extends ScheduledTaskHandler
class RenewalReminderTaskHandler extends AbstractRenewalReminderTaskHandler
{
/**
* @var SubscriptionManager
*/
private $subscriptionManager;

/**
* @var LoggerInterface
*/
private $logger;


/**
* @param ScheduledTaskRepositoryInterface $scheduledTaskRepository
* @param SubscriptionManager $subscriptionManager
* @param LoggerInterface $logger
*/
public function __construct(ScheduledTaskRepositoryInterface $scheduledTaskRepository, SubscriptionManager $subscriptionManager, LoggerInterface $logger)
{
/** @phpstan-ignore-next-line */
parent::__construct($scheduledTaskRepository->getRepository(), $logger);

$this->subscriptionManager = $subscriptionManager;
$this->logger = $logger;
}

/**
* @return iterable<mixed>
*/
public static function getHandledMessages(): iterable
{
return [
RenewalReminderTask::class,
RenewalReminderTaskDev::class,
];
}

/**
*
*/
public function run(): void
{
try {
$this->logger->info('Starting Subscription Renewal Reminder from Scheduled Tasks.');

$context = Context::createDefaultContext();

$remindedCount = $this->subscriptionManager->remindSubscriptionRenewal($context);

$this->logger->debug($remindedCount . ' subscriptions renewal reminders have been processed successfully!');
} catch (\Throwable $ex) {
$this->logger->error(
'Error when running Scheduled Task for Subscription Renewal Reminders. ' . $ex->getMessage()
);
}
}
}

0 comments on commit 73546cd

Please sign in to comment.