Skip to content

Commit

Permalink
Add a command to check the job status and reset it
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Brahmer <[email protected]>
  • Loading branch information
Grotax committed Mar 20, 2023
1 parent 667e037 commit 3b248fe
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The format is mostly based on [Keep a Changelog](https://keepachangelog.com/en/1
# Unreleased
## [21.x.x]
### Changed

- Add a new command for occ ./occ news:updater:job allows to check and reset the update job (#2166)
### Fixed

# Releases
Expand Down
1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Report a [feed issue](https://github.com/nextcloud/news/discussions/new)
<command>OCA\News\Command\Updater\UpdateUser</command>
<command>OCA\News\Command\Updater\BeforeUpdate</command>
<command>OCA\News\Command\Updater\AfterUpdate</command>
<command>OCA\News\Command\Updater\Job</command>
<command>OCA\News\Command\Config\FolderList</command>
<command>OCA\News\Command\Config\FolderAdd</command>
<command>OCA\News\Command\Config\FolderDelete</command>
Expand Down
46 changes: 46 additions & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,52 @@ Feeds can be updated using Nextcloud's system cron or an [external updater](http
**The feed update is not run in Webcron and AJAX cron mode!**

### System Cron
!!! info

This requires Nextcloud 26 or newer and News 21.2.x or newer.

Follow this checklist:

- Check admin settings of nextcloud, was the last cron execution ok.
- Check the News admin settings, system cron is used to update news
- You should see a info card at the top (Nextcloud 26+) which will tell you when the last job execution was.
- If the card is red it is very likely that the update job is stuck.
- If it is green then maybe only some feeds are failing to update, check the Nextcloud logs.

If you believe the job is stuck you can reset it. For further steps you need to use occ.

You can check again the status of the job.
(replace www-data with your httpd user)
```bash
sudo -u www-data php ./occ news:updater:job
Checking update Status
Last Execution was 2023-03-20 12:20:03 UTC
```

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

```bash
sudo -u www-data php ./occ news:updater:job --reset
Checking update Status
Last Execution was 2023-03-20 12:20:03 UTC
Attempting to reset the job.
Done, job should execute on next schedule.
```
The output of the command should have changed.
```bash
sudo -u www-data php ./occ news:updater:job
Checking update Status
Last Execution was 1970-01-01 00:00:00 UTC
```

After some time has passed the timestamp should be close to the current time.

If this did not help, check the logs and open a issue or discussion on GitHub.

#### Outdated Steps

Follow these steps if you are running an older version of News and Nextcloud.

* Check if you are using the system cron (Cron) setting on the admin page. AJAX and Web cron will not update feeds
* Check if the cronjob exists with **crontab -u www-data -e** (replace www-data with your httpd user)
* Check the file permissions of the **cron.php** file and if **www-data** (or whatever your httpd user is called like) can read and execute that script
Expand Down
72 changes: 72 additions & 0 deletions lib/Command/Updater/Job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Nextcloud - News
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*/

namespace OCA\News\Command\Updater;

use DateTime;
use OCP\Util;
use OCA\News\Service\StatusService;
use OCA\News\Service\UpdaterService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;


class Job extends Command
{
/**
* @var StatusService Status service
*/
private $statusService;

/**
* @var UpdaterService Update service
*/
private $updaterService;

public function __construct(StatusService $statusService, UpdaterService $updaterService)
{
parent::__construct();
$this->statusService = $statusService;
$this->updaterService = $updaterService;
}

/**
* @return void
*/
protected function configure()
{
$this->setName('news:updater:job')
->addOption('reset', null, InputOption::VALUE_NONE, 'If the job should be reset, warning this might lead to issues.')
->setDescription('Console API for checking the update job status and to reset it.');
}

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

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

if($major < 26) {
$output->writeln("Error: This only works with Nextcloud 26 or newer.");
return 1;
}
$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'));

if($reset) {
$output->writeln("Attempting to reset the job.");
$this->updaterService->reset();
$output->writeln("Done, job should execute on next schedule.");
}
return 0;
}
}
19 changes: 18 additions & 1 deletion lib/Service/UpdaterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@


namespace OCA\News\Service;
use OCP\BackgroundJob\IJobList;
use OCA\News\Cron\UpdaterJob;

class UpdaterService
{
Expand All @@ -32,14 +34,19 @@ class UpdaterService
*/
private $itemService;

/** @var IJobList */
private $jobList;

public function __construct(
FolderServiceV2 $folderService,
FeedServiceV2 $feedService,
ItemServiceV2 $itemService
ItemServiceV2 $itemService,
IJobList $jobList
) {
$this->folderService = $folderService;
$this->feedService = $feedService;
$this->itemService = $itemService;
$this->jobList = $jobList;
}


Expand All @@ -60,4 +67,14 @@ public function afterUpdate(): void
{
$this->itemService->purgeOverThreshold();
}

public function reset(): int
{
$myJobList = $this->jobList->getJobsIterator(UpdaterJob::class, 1, 0);
$job = $myJobList->current();

$this->jobList->resetBackgroundJob($job);

return 0;
}
}

0 comments on commit 3b248fe

Please sign in to comment.