Skip to content

Commit

Permalink
Cronjob-Command: Messages und Status ausgeben (#5227)
Browse files Browse the repository at this point in the history
  • Loading branch information
gharlan committed Jul 18, 2022
1 parent 9d05ed3 commit 79d4e0d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
7 changes: 5 additions & 2 deletions .tools/psalm/baseline.xml
Expand Up @@ -241,27 +241,30 @@
<InvalidReturnType occurrences="1">
<code>string</code>
</InvalidReturnType>
<MixedArgument occurrences="11">
<MixedArgument occurrences="13">
<code>$job</code>
<code>$job['id']</code>
<code>$job['id']</code>
<code>$job['id']</code>
<code>$job['interval']</code>
<code>$job['name']</code>
<code>$job['type']</code>
<code>$jobs[0]</code>
<code>$jobs[0]['name']</code>
<code>$params</code>
<code>$status</code>
<code>$value</code>
<code>json_decode($interval, true)</code>
</MixedArgument>
<MixedArrayAccess occurrences="7">
<MixedArrayAccess occurrences="8">
<code>$job['id']</code>
<code>$job['id']</code>
<code>$job['id']</code>
<code>$job['interval']</code>
<code>$job['type']</code>
<code>$jobs[0]</code>
<code>$jobs[0]</code>
<code>$jobs[0]['name']</code>
</MixedArrayAccess>
<MixedArrayAssignment occurrences="3">
<code>$job['started']</code>
Expand Down
19 changes: 18 additions & 1 deletion redaxo/src/addons/cronjob/lib/command/run.php
Expand Up @@ -38,7 +38,24 @@ protected function execute(InputInterface $input, OutputInterface $output)
$nexttime = rex_package::get('cronjob')->getConfig('nexttime', 0);

if (0 != $nexttime && time() >= $nexttime) {
rex_cronjob_manager_sql::factory()->check();
$manager = rex_cronjob_manager_sql::factory();

$errors = 0;
$manager->check(static function (string $name, bool $success, string $message) use ($io, &$errors) {
/** @var int $errors */
if ($success) {
$io->success($name.': '.$message);
} else {
$io->error($name.': '.$message);
++$errors;
}
});

if ($errors) {
/** @var int $errors */
$io->error('Cronjobs checked, '.$errors.' failed.');
return 1;
}

$io->success('Cronjobs checked.');
return 0;
Expand Down
25 changes: 19 additions & 6 deletions redaxo/src/addons/cronjob/lib/manager_sql.php
Expand Up @@ -148,7 +148,10 @@ public function delete($id)
return $success;
}

public function check()
/**
* @param null|callable(string,bool,string):void $callback Callback is called after every job execution (params: job name, success status, message)
*/
public function check(?callable $callback = null)
{
$env = rex_cronjob_manager::getCurrentEnvironment();
$script = 'script' === $env;
Expand Down Expand Up @@ -209,15 +212,25 @@ public function check()
if ($script || 1 == $jobs[0]['execution_moment']) {
foreach ($jobs as &$job) {
$job['started'] = true;
$this->tryExecuteJob($job, true, true);
$success = $this->tryExecuteJob($job, true, true);

if ($callback) {
$callback($job['name'], $success, $this->getMessage());
}

$job['finished'] = true;
}
return;
}

rex_extension::register('RESPONSE_SHUTDOWN', function () use (&$jobs) {
rex_extension::register('RESPONSE_SHUTDOWN', function () use (&$jobs, $callback) {
$jobs[0]['started'] = true;
$this->tryExecuteJob($jobs[0], true, true);
$success = $this->tryExecuteJob($jobs[0], true, true);

if ($callback) {
$callback($jobs[0]['name'], $success, $this->getMessage());
}

$jobs[0]['finished'] = true;
});
}
Expand Down Expand Up @@ -247,14 +260,14 @@ public function tryExecute($id, $log = true)
}

/**
* @param array{id: int, interval: string, name: string, parameters: string, type: class-string<rex_cronjob>} $job
* @param array{id: int, interval: string, name: string, parameters: ?string, type: class-string<rex_cronjob>} $job
* @param bool $log
* @param bool $resetExecutionStart
* @return bool
*/
private function tryExecuteJob(array $job, $log = true, $resetExecutionStart = false)
{
$params = json_decode($job['parameters'], true);
$params = $job['parameters'] ? json_decode($job['parameters'], true) : [];
$cronjob = rex_cronjob::factory($job['type']);

$this->setNextTime($job['id'], $job['interval'], $resetExecutionStart);
Expand Down

0 comments on commit 79d4e0d

Please sign in to comment.