Skip to content

Commit

Permalink
Merge pull request #123 from ConductionNL/feature/CONNECTOR-150/joblogs
Browse files Browse the repository at this point in the history
Fixes on jobs and log cleanup
  • Loading branch information
rjzondervan authored Dec 6, 2024
2 parents f61236c + 13deff5 commit 1adab7b
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 6 deletions.
6 changes: 5 additions & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The OpenConnector Nextcloud app provides a ESB-framework to work together in an
- 🆓 Map and translate API calls
]]></description>
<version>0.1.20</version>
<version>0.1.21</version>
<licence>agpl</licence>
<category>integration</category>
<author mail="[email protected]" homepage="https://www.conduction.nl/">Conduction</author>
Expand All @@ -33,6 +33,10 @@ The OpenConnector Nextcloud app provides a ESB-framework to work together in an
<owncloud max-version="0" min-version="0"/>
</dependencies>

<background-jobs>
<job>OCA\OpenConnector\Cron\LogCleanUpTask</job>
</background-jobs>

<navigations>
<navigation>
<id>openconnector</id>
Expand Down
4 changes: 3 additions & 1 deletion lib/Cron/ActionTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ public function run($argument)

// Update the job
$job->setLastRun(new DateTime());
$job->setNextRun(new DateTime());
$nextRun = new DateTime('now + '.$job->getInterval().' seconds');
$nextRun->setTime(hour: $nextRun->format('H'), minute: $nextRun->format('i'));
$job->setNextRun($nextRun);
$this->jobMapper->update($job);

// Log the job
Expand Down
28 changes: 25 additions & 3 deletions lib/Cron/LogCleanUpTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,35 @@
namespace OCA\OpenConnector\Cron;

use OCA\OpenConnector\Db\CallLogMapper;
use OCA\OpenConnector\Db\JobLogMapper;
use OCA\OpenConnector\Db\JobMapper;
use OCP\BackgroundJob\IJobList;
use OCP\BackgroundJob\TimedJob;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IUserManager;
use OCP\IUserSession;

class LogCleanUpTask
class LogCleanUpTask extends TimedJob
{
public function doCron(array $arguments, CallLogMapper $callLogMapper){
$callLogMapper = new ClearLogs();
public function __construct(
ITimeFactory $time,
private readonly CallLogMapper $callLogMapper,
) {
parent::__construct($time);

// Run every 5 minutes
$this->setInterval(300);

// Delay until low-load time
//$this->setTimeSensitivity(\OCP\BackgroundJob\IJob::TIME_SENSITIVE);
// Or $this->setTimeSensitivity(\OCP\BackgroundJob\IJob::TIME_INSENSITIVE);

// Only run one instance of this job at a time
$this->setAllowParallelRuns(false);
}

public function run(mixed $argument){
$this->callLogMapper->clearLogs();
}

}
2 changes: 1 addition & 1 deletion lib/Db/CallLogMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function clearLogs(): bool

// Build the delete query
$qb->delete('openconnector_call_logs')
->where($qb->expr()->lt('expired', $qb->createFunction('NOW()')));
->where($qb->expr()->lt('expires', $qb->createFunction('NOW()')));

// Execute the query and get the number of affected rows
$result = $qb->execute();
Expand Down
66 changes: 66 additions & 0 deletions lib/Migration/Version1Date20241206095007.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\OpenConnector\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

/**
* FIXME Auto-generated migration step: Please modify to your needs!
*/
class Version1Date20241206095007 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
}

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
/**
* @var ISchemaWrapper $schema
*/
$schema = $schemaClosure();

if ($schema->hasTable('openconnector_sources') === true) {
$table = $schema->getTable('openconnector_sources');

if ($table->hasColumn('logRetention') === true) {
$table->dropColumn('logRetention');
$table->addColumn('log_retention', Types::INTEGER)->setNotnull(false)->setDefault(3600);
}
if ($table->hasColumn('errorRetention') === true) {
$table->dropColumn('errorRetention');
$table->addColumn('error_retention', Types::INTEGER)->setNotnull(false)->setDefault(86400);
}
}

return $schema;
}

/**
* @param IOutput $output
* @param Closure(): ISchemaWrapper $schemaClosure
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
}
}
4 changes: 4 additions & 0 deletions lib/Service/CallService.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public function call(
$callLog->setStatusCode(409);
$callLog->setStatusMessage("This source is not enabled");
$callLog->setCreated(new \DateTime());
$callLog->setExpires(new \DateTime('now + '.$source->getErrorRetention().' seconds'));

$this->callLogMapper->insert($callLog);

Expand All @@ -152,6 +153,7 @@ public function call(
$callLog->setStatusCode(409);
$callLog->setStatusMessage("This source has no location");
$callLog->setCreated(new \DateTime());
$callLog->setExpires(new \DateTime('now + '.$source->getErrorRetention().' seconds'));

$this->callLogMapper->insert($callLog);

Expand All @@ -178,6 +180,7 @@ public function call(
$callLog->setStatusCode(429); //
$callLog->setStatusMessage("The rate limit for this source has been exceeded. Try again later.");
$callLog->setCreated(new \DateTime());
$callLog->setExpires(new \DateTime('now + '.$source->getErrorRetention().' seconds'));

$this->callLogMapper->insert($callLog);

Expand Down Expand Up @@ -282,6 +285,7 @@ public function call(
$callLog->setRequest($data['request']);
$callLog->setResponse($data['response']);
$callLog->setCreated(new \DateTime());
$callLog->setExpires(new \DateTime('now + '.($data['response']['statusCode'] < 400 ? $source->getLogRetention() : $source->getErrorRetention()).' seconds'));

$this->callLogMapper->insert($callLog);

Expand Down

0 comments on commit 1adab7b

Please sign in to comment.