diff --git a/README.md b/README.md index 86de653..c828e67 100644 --- a/README.md +++ b/README.md @@ -1,170 +1,6 @@ -PHP Cron Scheduler +Fork of PHP Cron Scheduler == -[![Build Status](https://travis-ci.org/peppeocchi/php-cron-scheduler.svg)](https://travis-ci.org/peppeocchi/php-cron-scheduler) -[![Latest Stable Version](https://poser.pugx.org/peppeocchi/php-cron-scheduler/v/stable)](https://packagist.org/packages/peppeocchi/php-cron-scheduler) [![Total Downloads](https://poser.pugx.org/peppeocchi/php-cron-scheduler/downloads)](https://packagist.org/packages/peppeocchi/php-cron-scheduler) [![Latest Unstable Version](https://poser.pugx.org/peppeocchi/php-cron-scheduler/v/unstable)](https://packagist.org/packages/peppeocchi/php-cron-scheduler) [![License](https://poser.pugx.org/peppeocchi/php-cron-scheduler/license)](https://packagist.org/packages/peppeocchi/php-cron-scheduler) - -This is a simple cron jobs scheduler inspired by the [Laravel Task Scheduling](http://laravel.com/docs/5.1/scheduling). - -## Installing via Composer -The raccomended way is to install the php-cron-scheduler is through [Composer](https://getcomposer.org/). - -Please refer to [Getting Started](https://getcomposer.org/doc/00-intro.md) on how to download and install Composer. - -After you have downloaded/installed Composer, run - -`php composer.phar require peppeocchi/php-cron-scheduler` - -or add the package to your `composer.json` -```json -{ - "require": { - "peppeocchi/php-cron-scheduler": "1.*" - } -} -``` - -## How it works -Instead of adding a new entry in the crontab for each cronjob you have to run, you can add only one cron job to your crontab and define the commands in your .php file. - -By default when you schedule a command it will run in background, you can overwrite that behavior by calling `->runInForeground()` method. -```php -$scheduler->call('myFunction')->runInForeground()->every()->minute(); -``` - -**Jobs that should send the output to email/s are always set to run in foreground** - -Create your `scheduler.php` file like this -```php - 'myemail@address.from' -]); - - -/** - * Schedule cronjob.php to run every minute - * - */ -$scheduler->php(__DIR__.'/cronjob.php')->at('* * * * *')->output(__DIR__.'/cronjob.log'); - - -/** - * Schedule a php job to run with your bin - * - */ -$scheduler->php(__DIR__.'/cronjob.php')->useBin('/usr/bin/php')->at('* * * * *')->output(__DIR__.'/cronjob_bin.log', true); - - -/** - * Schedule a raw command to tun every minute between 00 and 04 of every hour, - * send the output to raw.log - * Pass `true` as a second parameter to append the output to that file - * - */ -$scheduler->raw('ps aux | grep httpd')->at('* * * * *')->output(__DIR__.'/raw.log', true); - - -/** - * Run your own function every day at 10:30 - * - */ -$scheduler->call('myFunc')->every()->day('10:30')->output(__DIR__.'/call.log'); - -$scheduler->call(function () { - return "This works the same way"; - })->at('* * * * *')->output(__DIR__.'/call.log'); - -/** - * Run only when your func returns true - * - */ -$scheduler->php(__DIR__.'/cronjob.php') - ->at('* * * * *') - ->when(function () { - return false; - })->output(__DIR__.'/never_created.log'); - -/** - * Send the output to an email address - * - */ -$scheduler->call(function () { - return "This will be sent via email"; - })->at('* * * * *')->output(__DIR__.'/call.log')->email('myemail@address.to'); - -$scheduler->run(); -``` - -Then add to your crontab - -```` -* * * * * path/to/phpbin path/to/scheduler.php 1>> /dev/null 2>&1 -```` - -And you are ready to go. - -### Config -You can pass to the Scheduler constructor an array with your global config for the jobs - -The only supported configuration until now is the sender email address when sending the result of a job execution - -```php -... -$config = [ - 'emailFrom' => 'myEmail@address.com' -]; - -$scheduler = new Scheduler($config); -... -``` - -### Jobs execution order -The jobs that are due to run are being ordered by their execution: jobs that can run in **background** will be executed **first** - - -### Job types -After creating a new `Scheduler` instance, you can add few type of jobs -- `->php('myCommand')`, execute a `PHP` job. If you need you can set your own `PHP_BINARY` -```php -$scheduler->php('myCommand')->useBin('myBin') -``` -- `->raw('myCommand')`, execute a raw command in the shell, you can use this type if you want to pipe several commands like `ps aux | grep memcached` -- `->call('myFunction')`, execute your own function -- you can optionally write your own interpreter (if you want you can do a PR to add the interpreter to this repo), just extend `GO\Job\Job` and define the `build()` method, and an optional `init()` if it requires to be initiated before running the command - eg. to define a bin path - -### Output -You can send the output of the execution of your cron job either to a file and an email address. -- `->output('myfile')` will overwrite that file if exists -- `->output('myfile', true)` will append to that file (if exists) -If you want to send the output to an email address, you need to send first the output to a file. That file will be attached to the email -- `->output('myfile')->email('myemail')` -You can pass an array of files or emails if you want to send the output to multiple files/emails --> `output(['first_file', 'second_file'])->email(['myemail1' => 'Dev1', 'myemail2' => 'Dev2'])` - -### Conditional -You can delegate the execution of a cronjob to a truthful test. -``` -$scheduler->raw('command')->when(function () { - ..... - return true; - }); -``` - -### Schedule time -`Scheduler` uses `Cron\CronExpression` as an expression parser. - -So you can schedule the job using the `->at('myCronExpression')` method and passing to that your cron expression (eg. `* * * * *`) or one of the expression supported by [mtdowling/cron-expression](https://github.com/mtdowling/cron-expression) - -Optionally you can use the "pretty scheduling" that lets you define times in an eloquent way. To do that you should call the `->every()` followed by -- `->minute()`, the job will be scheduled to run every minute -- `->hour('02')` the job will be scheduled to run every hour. Default `minute` is `00` but you can override that with your own `minute` (in the example it will run every hour at minute `02`) -- `->day('10:23')` the job will be scheduled to run every day. Default `hour:minute` is `00:00` but you can override that with your own `hour:minute` -- `->month('25 10:30')` the job will be scheduled to run every month. Default `day hour:minute` is `01 00:00` but you can override that with your own `day hour:minute` +Fork for internal use. +Removed exec() and added late execution. +[Original repository](https://github.com/peppeocchi/php-cron-scheduler) diff --git a/src/GO/Job/Job.php b/src/GO/Job/Job.php index d71f7ee..01dc7fd 100644 --- a/src/GO/Job/Job.php +++ b/src/GO/Job/Job.php @@ -81,6 +81,10 @@ abstract class Job */ public $truthTest = true; + /** + * @var string + */ + protected $lastExecutionFile; /** * Create a new instance of Job @@ -206,6 +210,11 @@ public function getEmailsOutput() */ public function isDue() { + if ($this->lastExecutionFile && is_readable($this->lastExecutionFile)) { + $lastExecution = file_get_contents($this->lastExecutionFile); + $lastRunDate = $this->execution->getPreviousRunDate(); + return $lastRunDate->getTimestamp() > $lastExecution && $this->truthTest === true; + } return $this->execution->isDue() && $this->truthTest === true; } @@ -259,7 +268,7 @@ public function exec() Filesystem::write($return, $output, $this->mode); } } else { - $return = exec($this->compiled); + throw new Exception("Only PHP callables supported"); } if ($this->emails) { @@ -310,6 +319,9 @@ public function runInForeground() */ public function setup(array $config) { + if (isset($config['lastExecutionFile'])) { + $this->lastExecutionFile = $config['lastExecutionFile']; + } if (isset($config['emailFrom'])) { $this->emailFrom = $config['emailFrom']; } diff --git a/src/GO/Scheduler.php b/src/GO/Scheduler.php index 4b12079..650f12c 100644 --- a/src/GO/Scheduler.php +++ b/src/GO/Scheduler.php @@ -134,17 +134,6 @@ private function command($command, array $args = []) $file = basename($command); } - /** - * Raw job - * - * @param string $command - * @return instance of GO\Job\Job - */ - public function raw($command) - { - return $this->jobs[] = JobFactory::factory(\GO\Job\Raw::class, $command); - } - /** * Ping url * @@ -200,6 +189,12 @@ public function run() $this->executed[] = $job; } } + + if (isset($this->config['lastExecutionFile'])) { + $fh = fopen($this->config['lastExecutionFile'], 'w'); + fwrite($fh, time()); + fclose($fh); + } return $output; }