Skip to content

Commit b8345de

Browse files
committed
feat(cron): prevent pending transactions forgotten by a job
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent f7777c3 commit b8345de

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

core/Service/CronService.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use OC;
1515
use OC\Authentication\LoginCredentials\Store;
16+
use OC\DB\Connection;
1617
use OC\Security\CSRF\TokenStorage\SessionStorage;
1718
use OC\Session\CryptoWrapper;
1819
use OC\Session\Memory;
@@ -38,6 +39,7 @@ public function __construct(
3839
private readonly IAppManager $appManager,
3940
private readonly ISession $session,
4041
private readonly Session $userSession,
42+
private readonly Connection $connection,
4143
private readonly CryptoWrapper $cryptoWrapper,
4244
private readonly Store $store,
4345
private readonly SessionStorage $sessionStorage,
@@ -210,6 +212,12 @@ private function runCli(string $appMode, ?array $jobClasses): void {
210212
// clean up after unclean jobs
211213
$this->setupManager->tearDown();
212214
$this->tempManager->clean();
215+
if ($this->connection->inTransaction()) {
216+
$this->connection->rollBack();
217+
$message = 'Cron job left a transaction opened after executing job ' . $jobDetails . '. The transaction was rolled back.';
218+
$this->logger->warning($message, ['app' => 'cron']);
219+
$this->verboseOutput($message);
220+
}
213221

214222
$this->verboseOutput('Job ' . $jobDetails . ' done in ' . ($timeAfter - $timeBefore) . ' seconds');
215223

0 commit comments

Comments
 (0)