├── .gitignore ├── src ├── FakeCronServiceProvider.php └── Commands │ └── Cron.php ├── composer.json ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | composer.lock -------------------------------------------------------------------------------- /src/FakeCronServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 16 | $this->commands([ 17 | Cron::class, 18 | ]); 19 | } 20 | } 21 | /** 22 | * Register the application services. 23 | */ 24 | public function register() 25 | { 26 | // ... 27 | } 28 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "alexbowers/laravel-fake-cron", 3 | "authors": [ 4 | { 5 | "name": "Alex Bowers", 6 | "email": "bowersbros@gmail.com" 7 | } 8 | ], 9 | "license": "MIT", 10 | "require": { 11 | "php": "^7.1", 12 | "illuminate/support": "^5.8 | ^6.0 | ^7.0 | ^8.0", 13 | "illuminate/console": "^5.8 | ^6.0 | ^7.0 | ^8.0" 14 | }, 15 | "autoload": { 16 | "psr-4": { 17 | "AlexBowers\\LaravelFakeCron\\": "src" 18 | } 19 | }, 20 | "extra": { 21 | "laravel": { 22 | "providers": [ 23 | "AlexBowers\\LaravelFakeCron\\FakeCronServiceProvider" 24 | ] 25 | } 26 | }, 27 | "config": { 28 | "sort-packages": true 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Fake Cron 2 | 3 | --- 4 | 5 | `php artisan cron` is a daemon command that runs the `schedule:run` command. 6 | 7 | Use this package instead of adding `php artisan schedule:run` to your crontab file. 8 | 9 | Run this command under supervisor so that it will automatically restart should any crash occur. 10 | 11 | Options: 12 | --- 13 | 14 | You can use the `--interval` flag to set the interval between `schedule:run` calls. 15 | 16 | By default, this is every 60 seconds, however it can be changed to any interger second value. 17 | 18 | `php artisan cron --interval=15` would run the scheduled commands every 15 seconds. 19 | 20 | This is helpful if you want a finer granuality than a typical cron gives you. 21 | 22 | By default this command outputs a progress bar showing how long until the next trigger, if you do not want this (eg, in production), use the `--quiet` flag. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Alex Bowers 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Commands/Cron.php: -------------------------------------------------------------------------------- 1 | comment("Cron has been started"); 21 | if ($this->supportsAsyncSignals()) { 22 | $this->listenForSignals(); 23 | } 24 | 25 | 26 | while (true) { 27 | $bar = $this->output->createProgressBar($this->option('interval')); 28 | 29 | for ($i = 0; $i < $this->option('interval'); $i++) { 30 | if ($this->shouldQuit) { 31 | exit(0); 32 | } 33 | 34 | $bar->advance(); 35 | 36 | sleep(1); 37 | } 38 | 39 | $bar->finish(); 40 | $bar->clear(); 41 | 42 | if (!$this->paused) { 43 | Artisan::call('schedule:run'); 44 | $this->output->write(Artisan::output()); 45 | } 46 | 47 | } 48 | } 49 | 50 | /** 51 | * @see https://github.com/laravel/framework/blob/5.8/src/Illuminate/Queue/Worker.php#L513-L528 52 | */ 53 | protected function listenForSignals() 54 | { 55 | pcntl_async_signals(true); 56 | 57 | pcntl_signal(SIGTERM, function () { 58 | $this->shouldQuit = true; 59 | }); 60 | 61 | pcntl_signal(SIGUSR2, function () { 62 | $this->paused = true; 63 | }); 64 | 65 | pcntl_signal(SIGCONT, function () { 66 | $this->paused = false; 67 | }); 68 | } 69 | 70 | /** 71 | * @see https://github.com/laravel/framework/blob/5.8/src/Illuminate/Queue/Worker.php#L535-L538 72 | */ 73 | protected function supportsAsyncSignals() 74 | { 75 | return extension_loaded('pcntl'); 76 | } 77 | } --------------------------------------------------------------------------------