occ news:updater:job exits with code 2 if last update was too long ago

Signed-off-by: mortee <mortee@kavemalna.hu>
This commit is contained in:
mortee 2024-02-15 18:30:11 +01:00 committed by Benjamin Brahmer
parent 1ae4a36155
commit d90769d2c6
3 changed files with 28 additions and 3 deletions

View File

@ -5,6 +5,7 @@ The format is mostly based on [Keep a Changelog](https://keepachangelog.com/en/1
# Unreleased
## [25.x.x]
### Changed
- make occ news:updater:job exit with code 2 if last update was too long ago
### Fixed

View File

@ -9,7 +9,10 @@
namespace OCA\News\Command\Updater;
use DateTime;
use DateInterval;
use OCP\Util;
use OCP\IConfig;
use OCA\News\AppInfo\Application;
use OCA\News\Service\StatusService;
use OCA\News\Service\UpdaterService;
use Symfony\Component\Console\Command\Command;
@ -19,6 +22,11 @@ use Symfony\Component\Console\Output\OutputInterface;
class Job extends Command
{
/**
* @var IConfig
*/
private $config;
/**
* @var StatusService Status service
*/
@ -29,9 +37,10 @@ class Job extends Command
*/
private $updaterService;
public function __construct(StatusService $statusService, UpdaterService $updaterService)
public function __construct(IConfig $config, StatusService $statusService, UpdaterService $updaterService)
{
parent::__construct();
$this->config = $config;
$this->statusService = $statusService;
$this->updaterService = $updaterService;
}
@ -64,12 +73,27 @@ class Job extends Command
$output->writeln("Checking update Status");
$date = new DateTime();
$date->setTimestamp($this->statusService->getUpdateTime());
$output->writeln("Last Execution was ".$date->format('Y-m-d H:i:s e'));
$now = new DateTime('now');
$elapsedInterval = $now->diff($date);
$output->writeln("Last Execution was ".$date->format('Y-m-d H:i:s e').
$elapsedInterval->format("; %h hours, %i minutes, %s seconds ago"));
if ($reset) {
$output->writeln("Attempting to reset the job.");
$this->updaterService->reset();
$output->writeln("Done, job should execute on next schedule.");
} else {
$updateInterval = $this->config->getAppValue(
Application::NAME,
'updateInterval',
Application::DEFAULT_SETTINGS['updateInterval']
);
$threshold = ($updateInterval * 2) + 900;
$elapsedSeconds = $now->getTimestamp() - $date->getTimestamp();
if ($elapsedSeconds > $threshold) {
$output->writeln("Something's wrong.");
return 2;
}
}
return 0;
}

View File

@ -11,7 +11,7 @@ TESTSUITE="Update"
@test "[$TESTSUITE] Job status" {
run ./occ news:updater:job
assert_success
[ "$status" -eq 0 -o "$status" -eq 2 ]
}
@test "[$TESTSUITE] Job reset" {