From 7bbb47d4a5b291e3f1093f164d9973ed916b8570 Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Thu, 19 Dec 2024 14:33:49 +0100 Subject: [PATCH] Fix ActionTask for ratelimit --- lib/Cron/ActionTask.php | 167 +++++++++++++++++++--------------------- 1 file changed, 79 insertions(+), 88 deletions(-) diff --git a/lib/Cron/ActionTask.php b/lib/Cron/ActionTask.php index 68c48fd6..f08507dc 100644 --- a/lib/Cron/ActionTask.php +++ b/lib/Cron/ActionTask.php @@ -24,35 +24,35 @@ */ class ActionTask extends TimedJob { - private JobMapper $jobMapper; - private JobLogMapper $jobLogMapper; - private IJobList $jobList; - private ContainerInterface $containerInterface; - - public function __construct( - ITimeFactory $time, - JobMapper $jobMapper, - JobLogMapper $jobLogMapper, - IJobList $jobList, - ContainerInterface $containerInterface, + private JobMapper $jobMapper; + private JobLogMapper $jobLogMapper; + private IJobList $jobList; + private ContainerInterface $containerInterface; + + public function __construct( + ITimeFactory $time, + JobMapper $jobMapper, + JobLogMapper $jobLogMapper, + IJobList $jobList, + ContainerInterface $containerInterface, private IUserSession $userSession, private IUserManager $userManager, - ) { - parent::__construct($time); - $this->jobMapper = $jobMapper; - $this->jobLogMapper = $jobLogMapper; - $this->jobList = $jobList; - $this->containerInterface = $containerInterface; - // 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); - } + ) { + parent::__construct($time); + $this->jobMapper = $jobMapper; + $this->jobLogMapper = $jobLogMapper; + $this->jobList = $jobList; + $this->containerInterface = $containerInterface; + // 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); + } /** * @todo @@ -65,27 +65,27 @@ public function __construct( * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public function run($argument) - { - // if we do not have a job id then everything is wrong - if (isset($argument['jobId']) === false || is_int($argument['jobId']) === false) { + public function run($argument) + { + // if we do not have a job id then everything is wrong + 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 meantime - try { - $job = $this->jobMapper->find($argument['jobId']); - } catch (Exception $e) { - return $this->jobLogMapper->createFromArray([ + // 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 $this->jobLogMapper->createFromArray([ 'jobId' => $argument['jobId'], 'level' => 'ERROR', 'message' => "Couldn't find a Job with this jobId, message: ".$e->getMessage() ]); - } + } $forceRun = false; $stackTrace = []; @@ -94,30 +94,21 @@ public function run($argument) $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 ($forceRun === false && $job->getIsEnabled() === false) { + // If the job is not enabled, we don't need to do anything + 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()) { - $jobLog = $this->jobLogMapper->createFromArray([ + // 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', - 'jobId' => $job->getId(), - 'jobClass' => $job->getJobClass(), - 'jobListId' => $job->getJobListId(), - 'arguments' => $job->getArguments(), - 'lastRun' => $job->getLastRun(), - 'nextRun' => $job->getNextRun(), - 'executionTime' => 0 + 'message' => 'Next Run is still in the future for this job' ]); - - return $jobLog; - } + } if (empty($job->getUserId()) === false && $this->userSession->getUser() === null) { $user = $this->userManager->get($job->getUserId()); @@ -126,25 +117,25 @@ public function run($argument) $time_start = microtime(true); - $action = $this->containerInterface->get($job->getJobClass()); - $arguments = $job->getArguments(); - if (is_array($arguments) === false) { - $arguments = []; - } - $result = $action->run($arguments); + $action = $this->containerInterface->get($job->getJobClass()); + $arguments = $job->getArguments(); + if (is_array($arguments) === false) { + $arguments = []; + } + $result = $action->run($arguments); - $time_end = microtime(true); - $executionTime = ( $time_end - $time_start ) * 1000; + $time_end = microtime(true); + $executionTime = ($time_end - $time_start) * 1000; - // deal with single run - if ($forceRun === false && $job->isSingleRun() === true) { - $job->setIsEnabled(false); - } + // deal with single run + if ($forceRun === false && $job->isSingleRun() === true) { + $job->setIsEnabled(false); + } - // Update the job + // Update the job $job->setLastRun(new DateTime()); if ($forceRun === false) { - $nextRun = new DateTime('now + '.$job->getInterval().' seconds'); + $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 @@ -157,32 +148,32 @@ public function run($argument) } $this->jobMapper->update($job); - // Log the job - $jobLog = $this->jobLogMapper->createForJob($job, [ + // Log the job + $jobLog = $this->jobLogMapper->createForJob($job, [ 'level' => 'INFO', 'message' => 'Succes', - 'executionTime' => $executionTime - ]); - - // Get the result and set it to the job log - if (is_array($result) === true) { - if (isset($result['level']) === true) { - $jobLog->setLevel($result['level']); - } - if (isset($result['message']) === true) { - $jobLog->setMessage($result['message']); - } - if (isset($result['stackTrace']) === true) { + 'executionTime' => $executionTime + ]); + + // Get the result and set it to the job log + if (is_array($result) === true) { + if (isset($result['level']) === true) { + $jobLog->setLevel($result['level']); + } + if (isset($result['message']) === true) { + $jobLog->setMessage($result['message']); + } + if (isset($result['stackTrace']) === true) { $stackTrace = array_merge($stackTrace, $result['stackTrace']); - } - } + } + } $jobLog->setStackTrace($stackTrace); $this->jobLogMapper->update(entity: $jobLog); - // Let's report back about what we have just done - return $jobLog; - } + // Let's report back about what we have just done + return $jobLog; + } }