BulkCleanup.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AsynchronousOperations\Cron;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\AsynchronousOperations\Api\Data\BulkSummaryInterface;
  9. use Magento\Framework\EntityManager\MetadataPool;
  10. use Magento\Framework\Stdlib\DateTime;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. class BulkCleanup
  13. {
  14. /**
  15. * @var DateTime
  16. */
  17. private $dateTime;
  18. /**
  19. * @var MetadataPool
  20. */
  21. private $metadataPool;
  22. /**
  23. * @var ResourceConnection
  24. */
  25. private $resourceConnection;
  26. /**
  27. * @var ScopeConfigInterface
  28. */
  29. private $scopeConfig;
  30. /**
  31. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  32. */
  33. private $date;
  34. /**
  35. * BulkCleanup constructor.
  36. * @param MetadataPool $metadataPool
  37. * @param ResourceConnection $resourceConnection
  38. * @param DateTime $dateTime
  39. * @param ScopeConfigInterface $scopeConfig
  40. * @param DateTime\DateTime $time
  41. */
  42. public function __construct(
  43. MetadataPool $metadataPool,
  44. ResourceConnection $resourceConnection,
  45. DateTime $dateTime,
  46. ScopeConfigInterface $scopeConfig,
  47. \Magento\Framework\Stdlib\DateTime\DateTime $time
  48. ) {
  49. $this->metadataPool = $metadataPool;
  50. $this->resourceConnection = $resourceConnection;
  51. $this->dateTime = $dateTime;
  52. $this->scopeConfig = $scopeConfig;
  53. $this->date = $time;
  54. }
  55. /**
  56. * Remove all expired bulks and corresponding operations
  57. *
  58. * @return void
  59. */
  60. public function execute()
  61. {
  62. $metadata = $this->metadataPool->getMetadata(BulkSummaryInterface::class);
  63. $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName());
  64. $bulkLifetime = 3600 * 24 * (int)$this->scopeConfig->getValue('system/bulk/lifetime');
  65. $maxBulkStartTime = $this->dateTime->formatDate($this->date->gmtTimestamp() - $bulkLifetime);
  66. $connection->delete($metadata->getEntityTable(), ['start_time <= ?' => $maxBulkStartTime]);
  67. }
  68. }