Routes not respecting Autoload when run from Cron

367 views
Skip to first unread message

Randy Hintz

unread,
Nov 1, 2015, 12:12:17 PM11/1/15
to Fat-Free Framework
I'm not sure if I'm missing something or if this is a bug.  I am trying to set up some cron jobs and I am running into issues.

If my index.php file looks like this:
<?php
$f3
=require('lib/base.php');
$f3
->set('AUTOLOAD','app/');
require_once
'applib/init.php';

$f3
->route('GET /crontest', 'testing::test_cron');

$f3
->run();
?>
then I can run mydomain.com/crontest without an issue. 
However, /usr/bin/php /home/mydomain/public_html/index.php "/crontest" does not perform.  (FYI, crontest writes a few records to the database).

If I change index.php to looks like this:
<?php
$f3
=require('lib/base.php');
$f3
->set('AUTOLOAD','app/');
require_once
'applib/init.php';

$f3
->route('GET /crontest',
   
function($f3) {
        include
'app/testing.php';
        testing
::test_cron($f3);
   
}
);

$f3
->run();
?>
It works great for cron, and from the browser.
So, it looks like the route from cron is not auto-loading the classes for the routes as it would for browser initiated hits.
I downloaded the latest framework, and I searched the forum for "autoload classes cron" and "cron routes", I did not find any hits.
Any help or insight would be appreciated!

-Randy


Anatol Buchholz

unread,
Nov 1, 2015, 2:03:15 PM11/1/15
to f3-fra...@googlegroups.com
Hi Randy,

unfortunately I cannot reproduce your error. My test looks as follows:

route in index:

class dolog{
 
function logit(){
 $logger
= new Log('check.log');
 $logger
->write("ok");
 
}
}

$f3
->route('GET /log','dolog->logit');

Cronjob:

* * * * * curl http://localhost/fatfree-composer-app/log


Which logs every minute to check.log. I´m testing with latest f3.

Would you please post your crontab entry and check if your webserver error.log 

logs something?


cheers,

- anatol


EDIT:

Just read your question again, it targets autoload therfore I´ve tested again having a file in folder app/contoller:


<?php
namespace Controller;
class dolog{
 
function logit(){
 $logger
= new \Log('check.log');
 $logger
->write("ok");
 
}
}

 and in my index:

$f3->set('AUTOLOAD', 'app/');

$f3
->route('GET /log','Controller\dolog->logit');
Crontab entry is the same … log is logging … as static function Controller\dolog::logit as well.

Randy Hintz

unread,
Nov 1, 2015, 3:39:09 PM11/1/15
to f3-fra...@googlegroups.com
Hi anatol, thanks for the reply.

My cron job looks like this:
45 * * * * /usr/bin/php /home/mycat/public_html/index.php "/crontest"
The error_log file is empty with regards to this issue.

I think the big difference is that your cron job is actually invoking curl, which means it runs php through apache.  My cron job runs php from the command line meaning that, internally, php_sapi_name() will return 'cli'.  I believe this is key to the problem I am experiencing.
-Randy

xfra35

unread,
Nov 2, 2015, 6:03:13 AM11/2/15
to f3-fra...@googlegroups.com
Hi Randy,

the reason of this issue is that your AUTOLOAD folder is relative to the current working directory.

When the script is run via Apache, the working directory is the webroot: /home/mycat/public_html/, which is fine.
When the script is run via cron, the working directory is different (probably the unix user directory), which explains why the AUTOLOAD folder cannot be found.

There are various ways to fix this:

1) define AUTOLOAD as an absolute path:
$f3->set('AUTOLOAD','/home/mycat/public_html/app/');

2) set the correct working directory inside your crontab:
45 * * * * cd /home/mycat/public_html/; /usr/bin/php index.php "/crontest"

3) set the correct working directory inside your script:
<?php
chdir(__DIR__);
$f3=require('lib/base.php');


Randy Hintz

unread,
Nov 2, 2015, 7:59:53 AM11/2/15
to Fat-Free Framework
BRILLIANT!  That makes absolute sense!  I'll give one of your suggestions a try before the end of the day :)
Thanks for the help, xfra35

-Randy

 
Reply all
Reply to author
Forward
0 new messages