From dfc4fe5195c086214074f813c7513dbd653e5bab Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Fri, 6 Dec 2024 11:54:33 +0100 Subject: [PATCH 1/2] Job and calllog fixes --- appinfo/info.xml | 6 +- lib/Cron/ActionTask.php | 2 +- lib/Cron/LogCleanUpTask.php | 28 ++++++++- lib/Db/CallLogMapper.php | 2 +- lib/Migration/Version1Date20241206095007.php | 66 ++++++++++++++++++++ lib/Service/CallService.php | 4 ++ 6 files changed, 102 insertions(+), 6 deletions(-) create mode 100644 lib/Migration/Version1Date20241206095007.php diff --git a/appinfo/info.xml b/appinfo/info.xml index 768b23e..3b695c7 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -13,7 +13,7 @@ The OpenConnector Nextcloud app provides a ESB-framework to work together in an - 🆓 Map and translate API calls ]]> - 0.1.20 + 0.1.21 agpl integration Conduction @@ -33,6 +33,10 @@ The OpenConnector Nextcloud app provides a ESB-framework to work together in an + + OCA\OpenCatalogi\Cron\LogCleanUpTask + + openconnector diff --git a/lib/Cron/ActionTask.php b/lib/Cron/ActionTask.php index 471f186..cd7bf88 100644 --- a/lib/Cron/ActionTask.php +++ b/lib/Cron/ActionTask.php @@ -109,7 +109,7 @@ public function run($argument) // Update the job $job->setLastRun(new DateTime()); - $job->setNextRun(new DateTime()); + $job->setNextRun(new DateTime('now + '.$job->getInterval().' seconds')); $this->jobMapper->update($job); // Log the job diff --git a/lib/Cron/LogCleanUpTask.php b/lib/Cron/LogCleanUpTask.php index 0af048a..0aa7cf3 100644 --- a/lib/Cron/LogCleanUpTask.php +++ b/lib/Cron/LogCleanUpTask.php @@ -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(); } } diff --git a/lib/Db/CallLogMapper.php b/lib/Db/CallLogMapper.php index 49cac82..615b5bb 100644 --- a/lib/Db/CallLogMapper.php +++ b/lib/Db/CallLogMapper.php @@ -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(); diff --git a/lib/Migration/Version1Date20241206095007.php b/lib/Migration/Version1Date20241206095007.php new file mode 100644 index 0000000..d26a0c8 --- /dev/null +++ b/lib/Migration/Version1Date20241206095007.php @@ -0,0 +1,66 @@ +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 { + } +} diff --git a/lib/Service/CallService.php b/lib/Service/CallService.php index e40bc95..8db6434 100644 --- a/lib/Service/CallService.php +++ b/lib/Service/CallService.php @@ -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); @@ -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); @@ -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); @@ -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); From 13deff537510210f6cd7502aecb95bf54fdb85dd Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Fri, 6 Dec 2024 13:43:23 +0100 Subject: [PATCH 2/2] Round nextRun to start of minute --- appinfo/info.xml | 2 +- lib/Cron/ActionTask.php | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/appinfo/info.xml b/appinfo/info.xml index 3b695c7..502cb6b 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -34,7 +34,7 @@ The OpenConnector Nextcloud app provides a ESB-framework to work together in an - OCA\OpenCatalogi\Cron\LogCleanUpTask + OCA\OpenConnector\Cron\LogCleanUpTask diff --git a/lib/Cron/ActionTask.php b/lib/Cron/ActionTask.php index cd7bf88..0ad5a34 100644 --- a/lib/Cron/ActionTask.php +++ b/lib/Cron/ActionTask.php @@ -109,7 +109,9 @@ public function run($argument) // Update the job $job->setLastRun(new DateTime()); - $job->setNextRun(new DateTime('now + '.$job->getInterval().' seconds')); + $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