Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ Checking update Status
Last Execution was 2023-03-20 12:20:03 UTC
```

The same check that is done in the News admin settings can be done using occ too.
Adding the --check-elapsed option displays the time elapsed since the last execution,
and if it's considered too long ago, a message will be displayed, and the command returns
with exit code 2. This can be used in scripts to send an alert for example.
```bash
sudo -u www-data php ./occ news:updater:job --check-elapsed
mortee marked this conversation as resolved.
Show resolved Hide resolved
Checking update Status
Last Execution was 2023-03-20 12:20:03 UTC
8 hours, 21 minutes, 20 seconds ago
Something is wrong with the news cronjob, execution delay exceeded the configured interval.
```

If you think the job is stuck you can reset it, this may lead to issues if the job is currently running!

```bash
Expand Down
2 changes: 2 additions & 0 deletions l10n/es_MX.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ OC.L10N.register(
"Download audio" : "Descargar audio",
"Download video" : "Descargar video",
"Open website" : "Abrir sitio web",
"{num} hours ago" : "Hace {num} horas",
"Ajax or webcron mode detected! Your feeds will not be updated!" : "¡Se ha detectado el modo Ajax o webron! ¡Tus fuentes no serán actualizadas!",
"How to set up the operating system cron" : "Cómo establecer las tareas programadas del sistema operativo",
"Install and set up a faster parallel updater that uses the News app's update API" : "Instala y configura un actualizador en paralelo más rápido que use el API de actualización de la nueva aplicación",
Expand All @@ -60,6 +61,7 @@ OC.L10N.register(
"Unstar article" : "Desmarcar artículo",
"Keep article unread" : "Mantener el artículo como no-leído",
"Remove keep article unread" : "Eliminar mantener artículo como no leído",
"Error while searching for users" : "Error al buscar usuarios",
"Keyboard shortcut" : "Atajo del teclado",
"Description" : "Descripción",
"right" : "derecha",
Expand Down
2 changes: 2 additions & 0 deletions l10n/es_MX.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"Download audio" : "Descargar audio",
"Download video" : "Descargar video",
"Open website" : "Abrir sitio web",
"{num} hours ago" : "Hace {num} horas",
"Ajax or webcron mode detected! Your feeds will not be updated!" : "¡Se ha detectado el modo Ajax o webron! ¡Tus fuentes no serán actualizadas!",
"How to set up the operating system cron" : "Cómo establecer las tareas programadas del sistema operativo",
"Install and set up a faster parallel updater that uses the News app's update API" : "Instala y configura un actualizador en paralelo más rápido que use el API de actualización de la nueva aplicación",
Expand All @@ -58,6 +59,7 @@
"Unstar article" : "Desmarcar artículo",
"Keep article unread" : "Mantener el artículo como no-leído",
"Remove keep article unread" : "Eliminar mantener artículo como no leído",
"Error while searching for users" : "Error al buscar usuarios",
"Keyboard shortcut" : "Atajo del teclado",
"Description" : "Descripción",
"right" : "derecha",
Expand Down
39 changes: 38 additions & 1 deletion lib/Command/Updater/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +22,11 @@

class Job extends Command
{
/**
* @var IConfig
*/
private $config;

/**
* @var StatusService Status service
*/
Expand All @@ -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;
}
Expand All @@ -48,12 +57,19 @@ protected function configure()
InputOption::VALUE_NONE,
'If the job should be reset, warning this might lead to issues.'
)
->addOption(
'check-elapsed',
null,
InputOption::VALUE_NONE,
'Check if the last job execution was too long ago. Return exit code 2 if so.'
)
->setDescription('Console API for checking the update job status and to reset it.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$reset = (bool) $input->getOption('reset');
$checkElapsed = (bool) $input->getOption('check-elapsed');

[$major, $minor, $micro] = Util::getVersion();

Expand All @@ -64,13 +80,34 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln("Checking update Status");
$date = new DateTime();
$date->setTimestamp($this->statusService->getUpdateTime());
$now = new DateTime('now');
$elapsedInterval = $now->diff($date);
$output->writeln("Last Execution was ".$date->format('Y-m-d H:i:s e'));
if ($checkElapsed) {
$output->writeln($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.");
return 0;
}

if ($checkElapsed) {
$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 is wrong with the news cronjob, execution delay exceeded the configured interval.");
return 2;
}
}

return 0;
}
}
2 changes: 1 addition & 1 deletion tests/command/update.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down