diff --git a/Core/Controller/Cron.php b/Core/Controller/Cron.php index 8a9698d2b3..9856f79477 100644 --- a/Core/Controller/Cron.php +++ b/Core/Controller/Cron.php @@ -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(); diff --git a/Core/Model/CronJob.php b/Core/Model/CronJob.php index 0d2a2b35b2..810b4f3565 100644 --- a/Core/Model/CronJob.php +++ b/Core/Model/CronJob.php @@ -85,6 +85,9 @@ class CronJob extends ModelClass /** @var bool */ private $ready = false; + /** @var Closure|null */ + private $ready_callback; + /** @var int */ public $running; @@ -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) { @@ -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; diff --git a/Core/Template/CronClass.php b/Core/Template/CronClass.php index 96920def85..ca479ec6dc 100644 --- a/Core/Template/CronClass.php +++ b/Core/Template/CronClass.php @@ -19,6 +19,7 @@ namespace FacturaScripts\Core\Template; +use FacturaScripts\Core\Tools; use FacturaScripts\Core\Where; use FacturaScripts\Dinamic\Model\CronJob; @@ -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) @@ -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; } } diff --git a/Test/Core/Model/CronJobTest.php b/Test/Core/Model/CronJobTest.php index 1287fcb396..b82c7e3cfa 100644 --- a/Test/Core/Model/CronJobTest.php +++ b/Test/Core/Model/CronJobTest.php @@ -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();