xfra35 / f3-cron

Job scheduling for the PHP Fat-Free Framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Initialisation is slow

ikkez opened this issue · comments

\Cron::instance(); is slow, because it always calls the binary method which uses an exec call:

f3-cron/lib/cron.php

Lines 54 to 56 in a66f447

function binary($path) {
if (function_exists('exec')) {
exec($path.' -v 2>&1',$out,$ret);

this cant be skipped, even if you'd set a valid path. Some measurements showed a bottleneck here:

Bildschirmfoto 2020-01-08 um 18 29 53

Would love to see some way of avoiding this path check with each and every page load. For now I'm using this workaround:

if ($this->fw->CLI ||
	($this->fw->exists('CRON.web',$web) && $web 
		&& preg_match('/^\/cron\/.*/',$this->fw->PATH)))
	\Cron::instance();

This way it's only called when the cron service is really used. Maybe it could fit into its contruct as well 🤔
thx bro ;)

Actually this behaviour was intentional, because the library tries to avoid the situation where asynchronous calls would be silently lost just because the PHP CLI executable could not be found.

Anyway you're right about the performance cost and I realize I've never hit it, precisely because I instanciate the class only when needed:

if (preg_match('/^\/cron/',$f3->PATH)) {
    $f3->DEBUG=2;
    $f3->ONERROR=function($f3){
        KS\Report::instance()->send();
        exit(1);
    };
    $f3->config('apps/cron.ini');
    Cron::instance();
}

So I've added an optional $force parameter to the binary() method. You can use it like this:

$cron->binary('/path/to/php',TRUE);

or in config file:

[CRON]
binary = /path/to/php, TRUE

When this option is enabled, the validation check is bypassed.

well thanks nice of cause that it checks the path automatically. But does it need to do that all the time, even if unsued? What about moving this part

f3-cron/lib/cron.php

Lines 247 to 250 in 161da4d

if (!isset($this->binary))
foreach(['php','php-cli'] as $path) // try to guess the binary name
if ($this->binary($path))
break;

into the execute method?

Thanks for the insights. I'll refactor the library when I have a bit more time. I'll see if it's possible to guess the binary "just in time" and/or skip the asynchronicity directly in config file.

commented

or in config file:

[CRON]
binary = /path/to/php, TRUE

When using this I get an Array to string conversion error:

# php index.php /cron
Array to string conversion
[/var/www/geokrety/vendor/bcosca/fatfree-core/base.php:2347] Base->error()
[/var/www/geokrety/vendor/xfra35/f3-cron/lib/cron.php:56] Base->{closure}()
[/var/www/geokrety/vendor/xfra35/f3-cron/lib/cron.php:243] Cron->binary()
[/var/www/geokrety/vendor/bcosca/fatfree-core/base.php:35] Cron->__construct()
[index.php:20] Prefab::instance()

===================================
ERROR 500 - Internal Server Error

@xfra35 was this ever addressed?