Skip to content

Commit

Permalink
Fix ActionTask for ratelimit
Browse files Browse the repository at this point in the history
  • Loading branch information
WilcoLouwerse committed Dec 19, 2024
1 parent 6971fd1 commit 7bbb47d
Showing 1 changed file with 79 additions and 88 deletions.
167 changes: 79 additions & 88 deletions lib/Cron/ActionTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = [];
Expand All @@ -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());
Expand All @@ -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
Expand All @@ -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;
}

}

0 comments on commit 7bbb47d

Please sign in to comment.