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

Code cleanup after PR 127 #130

Open
wants to merge 18 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
ca320b5
w.i.p. RateLimit for Synchronizations
WilcoLouwerse Nov 26, 2024
34b2497
Merge branch 'feature/CONNECTOR-144/rate-limits' into feature/CONNECT…
WilcoLouwerse Nov 26, 2024
712c07d
Merge branch 'development' into feature/CONNECTOR-144/rate-limits-sync
WilcoLouwerse Nov 26, 2024
05f444c
Updated migration docblock
WilcoLouwerse Nov 26, 2024
8d67010
w.i.p. try to somehow set job nextrun when RateLimit has been reached
WilcoLouwerse Nov 26, 2024
6977d12
Merge branch 'development' into feature/CONNECTOR-144/rate-limits-sync
WilcoLouwerse Nov 26, 2024
927e629
Improvements for checking RateLimit during synchronization
WilcoLouwerse Nov 29, 2024
2eba4fd
Merge branch 'development' into feature/CONNECTOR-144/rate-limits-sync
WilcoLouwerse Nov 29, 2024
1fdc4bb
Merge branch 'development' into feature/CONNECTOR-144/rate-limits-sync
WilcoLouwerse Dec 5, 2024
ece68cd
Merge branch 'development' into feature/CONNECTOR-144/rate-limits-sync
WilcoLouwerse Dec 10, 2024
43f4ac7
Create logs for rate limit and tested setting the job nextrun correctly
WilcoLouwerse Dec 10, 2024
6843ff4
Fix timezone for setting job next run
WilcoLouwerse Dec 10, 2024
71e938e
Fix migrations & docblocks
WilcoLouwerse Dec 10, 2024
98f3f47
Code cleanup, remove duplicates and use recursion instead of while loops
WilcoLouwerse Dec 10, 2024
0846592
Small fix for syncaction stacktrace
WilcoLouwerse Dec 10, 2024
4aa9866
Fix for Job test runs & better logging for jobs in general
WilcoLouwerse Dec 13, 2024
0ffe91d
Fixes
WilcoLouwerse Dec 13, 2024
bab8d12
Merge branch 'feature/BEHEER-3021/fix-jobs' into feature/CONNECTOR-14…
WilcoLouwerse Dec 13, 2024
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
11 changes: 10 additions & 1 deletion lib/Action/SynchronizationAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use OCA\OpenConnector\Service\SynchronizationService;
use OCA\OpenConnector\Db\SynchronizationMapper;
use OCA\OpenConnector\Db\SynchronizationContractMapper;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;

/**
* This action handles the synchronization of data from the source to the target.
Expand Down Expand Up @@ -70,7 +71,15 @@ public function run(array $argument = []): array
$response['stackTrace'][] = 'Doing the synchronization';
try {
$objects = $this->synchronizationService->synchronize($synchronization);
} catch (Exception $e) {
} catch (TooManyRequestsHttpException $e) {
$response['level'] = 'WARNING';
$response['stackTrace'][] = $response['message'] = 'Stopped synchronization: ' . $e->getMessage();
if (isset($e->getHeaders()['X-RateLimit-Reset']) === true) {
$response['nextRun'] = $e->getHeaders()['X-RateLimit-Reset'];
$response['stackTrace'][] = 'Returning X-RateLimit-Reset header to update Job nextRun: ' . $response['nextRun'];
}
return $response;
} catch (Exception $e) {
$response['level'] = 'ERROR';
$response['stackTrace'][] = $response['message'] = 'Failed to synchronize: ' . $e->getMessage();
return $response;
Expand Down
19 changes: 12 additions & 7 deletions lib/Controller/JobsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,18 @@ public function run(int $id): JSONResponse
}

try {
$this->jobList->getById($job->getJobListId())->start($this->jobList);
$lastLog = $this->jobLogMapper->getLastCallLog();
if ($lastLog !== null) {
return new JSONResponse(data: $lastLog, statusCode: 200);
}

return new JSONResponse(data: ['error' => 'No job log could be found, job did not went succesfully or failed to log anything'], statusCode: 500);
$job = $this->jobList->getById($job->getJobListId());
if ($job !== null) {
$job->setArgument(['jobId' => $id, 'forceRun' => true]);
$job->start($this->jobList);

$lastLog = $this->jobLogMapper->getLastCallLog();
if ($lastLog !== null && ($lastLog->getJobId() === null || (int) $lastLog->getJobId() === $id)) {
return new JSONResponse(data: $lastLog, statusCode: 200);
}
}

return new JSONResponse(data: ['error' => 'No job log could be found, job did not go successfully or failed to log anything'], statusCode: 500);
} catch (Exception $exception) {
return new JSONResponse(data: ['error' => $exception->getMessage()], statusCode: 400);
}
Expand Down
87 changes: 51 additions & 36 deletions lib/Controller/SynchronizationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OCA\OpenConnector\Controller;

use GuzzleHttp\Exception\GuzzleException;
use OCA\OpenConnector\Service\ObjectService;
use OCA\OpenConnector\Service\SearchService;
use OCA\OpenConnector\Service\SynchronizationService;
Expand All @@ -15,6 +16,8 @@
use OCP\IRequest;
use Exception;
use OCP\AppFramework\Db\DoesNotExistException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class SynchronizationsController extends Controller
{
Expand Down Expand Up @@ -213,33 +216,36 @@ public function logs(int $id): JSONResponse
}
}

/**
* Tests a synchronization
*
* This method tests a synchronization without persisting anything to the database.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $id The ID of the synchronization
*
* @return JSONResponse A JSON response containing the test results
*
* @example
* Request:
* empty POST
*
* Response:
* {
* "resultObject": {
* "fullName": "John Doe",
* "userAge": 30,
* "contactEmail": "[email protected]"
* },
* "isValid": true,
* "validationErrors": []
* }
*/
/**
* Tests a synchronization
*
* This method tests a synchronization without persisting anything to the database.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $id The ID of the synchronization
*
* @return JSONResponse A JSON response containing the test results
* @throws GuzzleException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*
* @example
* Request:
* empty POST
*
* Response:
* {
* "resultObject": {
* "fullName": "John Doe",
* "userAge": 30,
* "contactEmail": "[email protected]"
* },
* "isValid": true,
* "validationErrors": []
* }
*/
public function test(int $id): JSONResponse
{
try {
Expand All @@ -250,17 +256,26 @@ public function test(int $id): JSONResponse

// Try to synchronize
try {
$logAndContractArray = $this->synchronizationService->synchronize(synchronization: $synchronization, isTest: true);
$logAndContractArray = $this->synchronizationService->synchronize(
synchronization: $synchronization,
isTest: true
);

// Return the result as a JSON response
return new JSONResponse(data: $logAndContractArray, statusCode: 200);
} catch (Exception $e) {
// If synchronizaiton fails, return an error response
return new JSONResponse([
'error' => 'Synchronization error',
'message' => $e->getMessage()
], 400);
}
// Check if getHeaders method exists and use it if available
$headers = method_exists($e, 'getHeaders') ? $e->getHeaders() : [];

return new JSONResponse($resultFromTest, 200);
// If synchronization fails, return an error response
return new JSONResponse(
data: [
'error' => 'Synchronization error',
'message' => $e->getMessage()
],
statusCode: $e->getCode() ?? 400,
headers: $headers
);
}
}
}
}
82 changes: 57 additions & 25 deletions lib/Cron/ActionTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use OCP\BackgroundJob\IJobList;
use OCP\IUserManager;
use OCP\IUserSession;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\Uid\Uuid;
use DateInterval;
use DateTime;
Expand Down Expand Up @@ -59,32 +61,56 @@ public function __construct(
* @param $argument
*
* @return JobLog|void
* @throws \OCP\DB\Exception
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function run($argument)
{
// if we do not have a job id then everything is wrong
if (isset($arguments['jobId']) === true && is_int($argument['jobId']) === true) {
return;
if (isset($argument['jobId']) === false || is_int($argument['jobId']) === false) {
return $this->jobLogMapper->createFromArray([
'jobId' => 'null',
'level' => 'ERROR',
'message' => "Couldn't find a jobId in the action argument"
]);
}

// Let's get the job, the user might have deleted it in the mean time
// Let's get the job, the user might have deleted it in the meantime
try {
$job = $this->jobMapper->find($argument['jobId']);
} catch (Exception $e) {
return;
return $this->jobLogMapper->createFromArray([
'jobId' => $argument['jobId'],
'level' => 'ERROR',
'message' => "Couldn't find a Job with this jobId, message: ".$e->getMessage()
]);
}

$forceRun = false;
$stackTrace = [];
if (isset($argument['forceRun']) === true && $argument['forceRun'] === true) {
$forceRun = true;
$stackTrace[] = 'Doing a force run for this job, ignoring "enabled" & "nextRun" check...';
}

// If the job is not enabled, we don't need to do anything
if ($job->getIsEnabled() === false) {
return;
if ($forceRun === false && $job->getIsEnabled() === false) {
return $this->jobLogMapper->createForJob($job, [
'level' => 'WARNING',
'message' => 'This job is disabled'
]);
}

// if the next run is in the the future, we don't need to do anything
if ($job->getNextRun() !== null && $job->getNextRun() > new DateTime()) {
return;
// if the next run is in the future, we don't need to do anything
if ($forceRun === false && $job->getNextRun() !== null && $job->getNextRun() > new DateTime()) {
return $this->jobLogMapper->createForJob($job, [
'level' => 'WARNING',
'message' => 'Next Run is still in the future for this job'
]);
}

if(empty($job->getUserId()) === false && $this->userSession->getUser() === null) {
if (empty($job->getUserId()) === false && $this->userSession->getUser() === null) {
$user = $this->userManager->get($job->getUserId());
$this->userSession->setUser($user);
}
Expand All @@ -102,26 +128,30 @@ public function run($argument)
$executionTime = ( $time_end - $time_start ) * 1000;

// deal with single run
if ($job->isSingleRun() === true) {
if ($forceRun === false && $job->isSingleRun() === true) {
$job->setIsEnabled(false);
}


// Update the job
$job->setLastRun(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);
$job->setLastRun(new DateTime());
if ($forceRun === false) {
$nextRun = new DateTime('now + '.$job->getInterval().' seconds');
if (isset($result['nextRun']) === true) {
$nextRun = DateTime::createFromFormat('U', $result['nextRun'], $nextRun->getTimezone());
// Check if the current seconds part is not zero, and if so, round up to the next minute
if ($nextRun->format('s') !== '00') {
$nextRun->modify('next minute');
}
}
$nextRun->setTime(hour: $nextRun->format('H'), minute: $nextRun->format('i'));
$job->setNextRun($nextRun);
}
$this->jobMapper->update($job);

// Log the job
$jobLog = $this->jobLogMapper->createFromArray([
'jobId' => $job->getId(),
'jobClass' => $job->getJobClass(),
'jobListId' => $job->getJobListId(),
'arguments' => $job->getArguments(),
'lastRun' => $job->getLastRun(),
'nextRun' => $job->getNextRun(),
$jobLog = $this->jobLogMapper->createForJob($job, [
'level' => 'INFO',
'message' => 'Succes',
'executionTime' => $executionTime
]);

Expand All @@ -134,10 +164,12 @@ public function run($argument)
$jobLog->setMessage($result['message']);
}
if (isset($result['stackTrace']) === true) {
$jobLog->setStackTrace($result['stackTrace']);
$stackTrace = array_merge($stackTrace, $result['stackTrace']);
}
}

$jobLog->setStackTrace($stackTrace);

$this->jobLogMapper->update(entity: $jobLog);

// Let's report back about what we have just done
Expand Down
20 changes: 20 additions & 0 deletions lib/Db/JobLogMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,28 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
return $this->findEntities($qb);
}

public function createForJob(Job $job, array $object): JobLog
{
$jobObject = [
'jobId' => $job->getId(),
'jobClass' => $job->getJobClass(),
'jobListId' => $job->getJobListId(),
'arguments' => $job->getArguments(),
'lastRun' => $job->getLastRun(),
'nextRun' => $job->getNextRun(),
];

$object = array_merge($jobObject, $object);

return $this->createFromArray($object);
}

public function createFromArray(array $object): JobLog
{
if (isset($object['executionTime']) === false) {
$object['executionTime'] = 0;
}

$obj = new JobLog();
$obj->hydrate($object);
// Set uuid
Expand Down
3 changes: 3 additions & 0 deletions lib/Db/Synchronization.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Synchronization extends Entity implements JsonSerializable
protected ?DateTime $sourceLastChanged = null; // The last changed date of the source object
protected ?DateTime $sourceLastChecked = null; // The last checked date of the source object
protected ?DateTime $sourceLastSynced = null; // The last synced date of the source object
protected ?int $currentPage = 1; // The last page synced. Used for keeping track where to continue syncing after Rate Limit has been exceeded on source with pagination.
// Target
protected ?string $targetId = null; // The id of the target object
protected ?string $targetType = null; // The type of the target object (e.g. api, database, register/schema.)
Expand Down Expand Up @@ -51,6 +52,7 @@ public function __construct() {
$this->addType('sourceLastChanged', 'datetime');
$this->addType('sourceLastChecked', 'datetime');
$this->addType('sourceLastSynced', 'datetime');
$this->addType('currentPage', 'integer');
$this->addType('targetId', 'string');
$this->addType('targetType', 'string');
$this->addType('targetHash', 'string');
Expand Down Expand Up @@ -126,6 +128,7 @@ public function jsonSerialize(): array
'sourceLastChanged' => isset($this->sourceLastChanged) === true ? $this->sourceLastChanged->format('c') : null,
'sourceLastChecked' => isset($this->sourceLastChecked) === true ? $this->sourceLastChecked->format('c') : null,
'sourceLastSynced' => isset($this->sourceLastSynced) === true ? $this->sourceLastSynced->format('c') : null,
'currentPage' => $this->currentPage,
'targetId' => $this->targetId,
'targetType' => $this->targetType,
'targetHash' => $this->targetHash,
Expand Down
3 changes: 3 additions & 0 deletions lib/Db/SynchronizationContractLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class SynchronizationContractLog extends Entity implements JsonSerializable
{
protected ?string $uuid = null;
protected ?string $message = null;
protected ?string $synchronizationId = null;
protected ?string $synchronizationContractId = null;
protected ?array $source = [];
Expand All @@ -20,6 +21,7 @@ class SynchronizationContractLog extends Entity implements JsonSerializable

public function __construct() {
$this->addType('uuid', 'string');
$this->addType('message', 'string');
$this->addType('synchronizationId', 'string');
$this->addType('synchronizationContractId', 'string');
$this->addType('source', 'json');
Expand Down Expand Up @@ -65,6 +67,7 @@ public function jsonSerialize(): array
return [
'id' => $this->id,
'uuid' => $this->uuid,
'message' => $this->message,
'synchronizationId' => $this->synchronizationId,
'synchronizationContractId' => $this->synchronizationContractId,
'source' => $this->source,
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/Version0Date20240826193657.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
$table->addIndex(['user_id'], 'openconnector_job_logs_user_id_index');
}

if (!$schema->hasTable('openconnector_source_contract_logs')) {
if (!$schema->hasTable('openconnector_synchronization_contract_logs')) {
$table = $schema->createTable('openconnector_synchronization_contract_logs');
$table->addColumn('id', Types::BIGINT, ['autoincrement' => true, 'notnull' => true, 'length' => 20]);
$table->addColumn('uuid', Types::STRING, ['notnull' => true, 'length' => 36]);
Expand Down
Loading
Loading