Skip to content

Commit

Permalink
Code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
xfra35 committed Dec 21, 2015
1 parent dd1398d commit 84c2eae
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ You can enable web routes by setting `$cron->web=TRUE`.

### CLI path

By default, the script called asynchronously in CLI mode is `index.php` located in the current working directory.
By default, the script called asynchronously is `index.php` located in the current working directory.

You may need to tweak this value if:

* your web root differs from your app root (e.g: `index.php` resides in `www/` and starts with `chdir('..')`)
* you want to handle all the scheduling in a separate file (e.g: `cron.php` instead of `index.php`)
* all your scheduling is handled in a separate file (e.g: `cron.php` instead of `index.php`)

Examples:

Expand Down Expand Up @@ -226,9 +226,9 @@ $cron->web=TRUE;// enable web interface

### clipath

**Path of the script to call asynchronously in CLI mode**
**Path of the script to call asynchronously (default='index.php')**

Defaults to index.php in the current working directory.
Defaults to `index.php` in the current working directory.

```php
$cron->clipath='htdocs/index.php';//relative to app root
Expand Down
21 changes: 8 additions & 13 deletions lib/cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Cron extends \Prefab {
public $web=FALSE;

/** @var string */
public $clipath;
public $clipath='index.php';

/** @var array */
protected $jobs=array();
Expand Down Expand Up @@ -87,12 +87,8 @@ function execute($job,$async=TRUE) {
// PHP docs: If a program is started with this function, in order for it to continue running in the background,
// the output of the program must be redirected to a file or another output stream.
// Failing to do so will cause PHP to hang until the execution of the program ends.
$dir='';
$file='index.php';
if ($this->clipath) {
$dir=dirname($this->clipath);
$file=basename($this->clipath);
}
$dir=dirname($this->clipath);
$file=basename($this->clipath);
if (@$dir[0]!='/')
$dir=getcwd().'/'.$dir;
exec(sprintf('cd "%s";php %s /cron/%s > /dev/null 2>/dev/null &',$dir,$file,$job));
Expand Down Expand Up @@ -193,12 +189,11 @@ function __get($name) {
function __construct() {
$f3=\Base::instance();
$config=(array)$f3->get('CRON');
foreach(array('log','web') as $k)
if (isset($config[$k]))
$this->$k=(bool)$config[$k];
foreach(array('clipath') as $k)
if (isset($config[$k]))
$this->$k=(string)$config[$k];
foreach(array('log','web','clipath') as $k)
if (isset($config[$k])) {
settype($config[$k],gettype($this->$k));
$this->$k=$config[$k];
}
if (isset($config['jobs']))
foreach($config['jobs'] as $job=>$arr) {
$handler=array_shift($arr);
Expand Down

0 comments on commit 84c2eae

Please sign in to comment.