Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 0 additions & 3 deletions Core/Controller/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,6 @@ protected function runPlugins(): void
continue;
}

echo PHP_EOL . Tools::trans('running-plugin-cron', ['%pluginName%' => $pluginName]) . ' ... ';
Tools::log('cron')->notice('running-plugin-cron', ['%pluginName%' => $pluginName]);

try {
$cron = new $cronClass($pluginName);
$cron->run();
Expand Down
13 changes: 13 additions & 0 deletions Core/Model/CronJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class CronJob extends ModelClass
/** @var bool */
private $ready = false;

/** @var Closure|null */
private $ready_callback;

/** @var int */
public $running;

Expand Down Expand Up @@ -261,6 +264,10 @@ public function run(Closure $function): bool
return false;
}

if ($this->ready_callback !== null) {
($this->ready_callback)();
}

try {
$function();
} catch (Throwable $e) {
Expand Down Expand Up @@ -305,6 +312,12 @@ public function run(Closure $function): bool
return true;
}

public function setReadyCallback(?Closure $callback): self
{
$this->ready_callback = $callback;
return $this;
}

public function setMockDateTime(?string $dateTime, bool $update_microtime = true): void
{
$this->mock_date_time = $dateTime;
Expand Down
14 changes: 14 additions & 0 deletions Core/Template/CronClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace FacturaScripts\Core\Template;

use FacturaScripts\Core\Tools;
use FacturaScripts\Core\Where;
use FacturaScripts\Dinamic\Model\CronJob;

Expand All @@ -27,6 +28,9 @@ abstract class CronClass
/** @var string */
public $pluginName;

/** @var bool */
private $runningLog = false;

abstract public function run(): void;

public function __construct(string $pluginName)
Expand All @@ -50,6 +54,16 @@ protected function job(string $name): CronJob
// si es un proceso zombie, lo liberamos
$job->releaseIfStale();

$job->setReadyCallback(function () {
if ($this->runningLog) {
return;
}

echo PHP_EOL . Tools::trans('running-plugin-cron', ['%pluginName%' => $this->pluginName]) . ' ... ';
Tools::log('cron')->notice('running-plugin-cron', ['%pluginName%' => $this->pluginName]);
$this->runningLog = true;
});

return $job;
}
}
21 changes: 21 additions & 0 deletions Test/Core/Model/CronJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,27 @@ public function testRunFailLogIncludesFileAndLine(): void
$this->assertTrue($job->delete());
}

public function testReadyCallback(): void
{
$called = false;
$job = new CronJob();
$job->jobname = 'TestReadyCallback';
$job->pluginname = 'TestPluginReadyCallback';
$job->setReadyCallback(function () use (&$called) {
$called = true;
});

$this->assertFalse($job->run(function () {
}));
$this->assertFalse($called);

$this->assertTrue($job->everyDayAt(0)->run(function () {
}));
$this->assertTrue($called);

$this->assertTrue($job->delete());
}

public function testRunningCounterAfterSuccess(): void
{
$job = new CronJob();
Expand Down
Loading