You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DeleteOrphanedSharesJob.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Vincent Petry <vincent@nextcloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files_Sharing;
  27. use OCP\AppFramework\Db\TTransactional;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\BackgroundJob\TimedJob;
  30. use OCP\DB\QueryBuilder\IQueryBuilder;
  31. use OCP\IDBConnection;
  32. use PDO;
  33. use Psr\Log\LoggerInterface;
  34. use function array_map;
  35. /**
  36. * Delete all share entries that have no matching entries in the file cache table.
  37. */
  38. class DeleteOrphanedSharesJob extends TimedJob {
  39. use TTransactional;
  40. private const CHUNK_SIZE = 1000;
  41. private const INTERVAL = 24 * 60 * 60; // 1 day
  42. private IDBConnection $db;
  43. private LoggerInterface $logger;
  44. /**
  45. * sets the correct interval for this timed job
  46. */
  47. public function __construct(
  48. ITimeFactory $time,
  49. IDBConnection $db,
  50. LoggerInterface $logger
  51. ) {
  52. parent::__construct($time);
  53. $this->db = $db;
  54. $this->setInterval(self::INTERVAL); // 1 day
  55. $this->setTimeSensitivity(self::TIME_INSENSITIVE);
  56. $this->logger = $logger;
  57. }
  58. /**
  59. * Makes the background job do its work
  60. *
  61. * @param array $argument unused argument
  62. */
  63. public function run($argument) {
  64. $qbSelect = $this->db->getQueryBuilder();
  65. $qbSelect->select('id')
  66. ->from('share', 's')
  67. ->leftJoin('s', 'filecache', 'fc', $qbSelect->expr()->eq('s.file_source', 'fc.fileid'))
  68. ->where($qbSelect->expr()->isNull('fc.fileid'))
  69. ->setMaxResults(self::CHUNK_SIZE);
  70. $deleteQb = $this->db->getQueryBuilder();
  71. $deleteQb->delete('share')
  72. ->where(
  73. $deleteQb->expr()->in('id', $deleteQb->createParameter('ids'), IQueryBuilder::PARAM_INT_ARRAY)
  74. );
  75. /**
  76. * Read a chunk of orphan rows and delete them. Continue as long as the
  77. * chunk is filled and time before the next cron run does not run out.
  78. *
  79. * Note: With isolation level READ COMMITTED, the database will allow
  80. * other transactions to delete rows between our SELECT and DELETE. In
  81. * that (unlikely) case, our DELETE will have fewer affected rows than
  82. * IDs passed for the WHERE IN. If this happens while processing a full
  83. * chunk, the logic below will stop prematurely.
  84. * Note: The queries below are optimized for low database locking. They
  85. * could be combined into one single DELETE with join or sub query, but
  86. * that has shown to (dead)lock often.
  87. */
  88. $cutOff = $this->time->getTime() + self::INTERVAL;
  89. do {
  90. $deleted = $this->atomic(function () use ($qbSelect, $deleteQb) {
  91. $result = $qbSelect->executeQuery();
  92. $ids = array_map('intval', $result->fetchAll(PDO::FETCH_COLUMN));
  93. $result->closeCursor();
  94. $deleteQb->setParameter('ids', $ids, IQueryBuilder::PARAM_INT_ARRAY);
  95. $deleted = $deleteQb->executeStatement();
  96. $this->logger->debug("{deleted} orphaned share(s) deleted", [
  97. 'app' => 'DeleteOrphanedSharesJob',
  98. 'deleted' => $deleted,
  99. ]);
  100. return $deleted;
  101. }, $this->db);
  102. } while ($deleted >= self::CHUNK_SIZE && $this->time->getTime() <= $cutOff);
  103. }
  104. }