1
0

DeleteOrphanedSharesJob.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\Files_Sharing;
  9. use OCP\AppFramework\Db\TTransactional;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\BackgroundJob\TimedJob;
  12. use OCP\DB\QueryBuilder\IQueryBuilder;
  13. use OCP\IDBConnection;
  14. use PDO;
  15. use Psr\Log\LoggerInterface;
  16. use function array_map;
  17. /**
  18. * Delete all share entries that have no matching entries in the file cache table.
  19. */
  20. class DeleteOrphanedSharesJob extends TimedJob {
  21. use TTransactional;
  22. private const CHUNK_SIZE = 1000;
  23. private const INTERVAL = 24 * 60 * 60; // 1 day
  24. private IDBConnection $db;
  25. private LoggerInterface $logger;
  26. /**
  27. * sets the correct interval for this timed job
  28. */
  29. public function __construct(
  30. ITimeFactory $time,
  31. IDBConnection $db,
  32. LoggerInterface $logger
  33. ) {
  34. parent::__construct($time);
  35. $this->db = $db;
  36. $this->setInterval(self::INTERVAL); // 1 day
  37. $this->setTimeSensitivity(self::TIME_INSENSITIVE);
  38. $this->logger = $logger;
  39. }
  40. /**
  41. * Makes the background job do its work
  42. *
  43. * @param array $argument unused argument
  44. */
  45. public function run($argument) {
  46. $qbSelect = $this->db->getQueryBuilder();
  47. $qbSelect->select('id')
  48. ->from('share', 's')
  49. ->leftJoin('s', 'filecache', 'fc', $qbSelect->expr()->eq('s.file_source', 'fc.fileid'))
  50. ->where($qbSelect->expr()->isNull('fc.fileid'))
  51. ->setMaxResults(self::CHUNK_SIZE);
  52. $deleteQb = $this->db->getQueryBuilder();
  53. $deleteQb->delete('share')
  54. ->where(
  55. $deleteQb->expr()->in('id', $deleteQb->createParameter('ids'), IQueryBuilder::PARAM_INT_ARRAY)
  56. );
  57. /**
  58. * Read a chunk of orphan rows and delete them. Continue as long as the
  59. * chunk is filled and time before the next cron run does not run out.
  60. *
  61. * Note: With isolation level READ COMMITTED, the database will allow
  62. * other transactions to delete rows between our SELECT and DELETE. In
  63. * that (unlikely) case, our DELETE will have fewer affected rows than
  64. * IDs passed for the WHERE IN. If this happens while processing a full
  65. * chunk, the logic below will stop prematurely.
  66. * Note: The queries below are optimized for low database locking. They
  67. * could be combined into one single DELETE with join or sub query, but
  68. * that has shown to (dead)lock often.
  69. */
  70. $cutOff = $this->time->getTime() + self::INTERVAL;
  71. do {
  72. $deleted = $this->atomic(function () use ($qbSelect, $deleteQb) {
  73. $result = $qbSelect->executeQuery();
  74. $ids = array_map('intval', $result->fetchAll(PDO::FETCH_COLUMN));
  75. $result->closeCursor();
  76. $deleteQb->setParameter('ids', $ids, IQueryBuilder::PARAM_INT_ARRAY);
  77. $deleted = $deleteQb->executeStatement();
  78. $this->logger->debug("{deleted} orphaned share(s) deleted", [
  79. 'app' => 'DeleteOrphanedSharesJob',
  80. 'deleted' => $deleted,
  81. ]);
  82. return $deleted;
  83. }, $this->db);
  84. } while ($deleted >= self::CHUNK_SIZE && $this->time->getTime() <= $cutOff);
  85. }
  86. }