-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0bda99d
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
vendor | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "alexbowers/laravel-fake-cron", | ||
"authors": [ | ||
{ | ||
"name": "Alex Bowers", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"license": "MIT", | ||
"require": { | ||
"php": "^7.1", | ||
"illuminate/support": "^5.8", | ||
"illuminate/console": "^5.8" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"AlexBowers\\LaravelFakeCron\\": "src" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"AlexBowers\\LaravelFakeCron\\FakeCronServiceProvider" | ||
] | ||
} | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace AlexBowers\LaravelFakeCron\Commands; | ||
|
||
use Illuminate\Support\Facades\Artisan; | ||
use Illuminate\Console\Command; | ||
|
||
|
||
class Cron extends Command | ||
{ | ||
protected $signature = 'cron {--interval=60}'; | ||
|
||
protected $description = 'A fake cron job scheduler'; | ||
|
||
protected $shouldQuit = false; | ||
protected $paused = false; | ||
|
||
public function handle() | ||
{ | ||
$this->comment("Cron has been started"); | ||
if ($this->supportsAsyncSignals()) { | ||
$this->listenForSignals(); | ||
} | ||
|
||
|
||
while (true) { | ||
$bar = $this->output->createProgressBar($this->option('interval')); | ||
|
||
for ($i = 0; $i < $this->option('interval'); $i++) { | ||
if ($this->shouldQuit) { | ||
exit(0); | ||
} | ||
|
||
$bar->advance(); | ||
|
||
sleep(1); | ||
} | ||
|
||
$bar->finish(); | ||
$bar->clear(); | ||
|
||
if (!$this->paused) { | ||
Artisan::call('schedule:run'); | ||
$this->output->write(Artisan::output()); | ||
} | ||
|
||
} | ||
} | ||
|
||
/** | ||
* @see https://github.com/laravel/framework/blob/5.8/src/Illuminate/Queue/Worker.php#L513-L528 | ||
*/ | ||
protected function listenForSignals() | ||
{ | ||
pcntl_async_signals(true); | ||
|
||
pcntl_signal(SIGTERM, function () { | ||
$this->shouldQuit = true; | ||
}); | ||
|
||
pcntl_signal(SIGUSR2, function () { | ||
$this->paused = true; | ||
}); | ||
|
||
pcntl_signal(SIGCONT, function () { | ||
$this->paused = false; | ||
}); | ||
} | ||
|
||
/** | ||
* @see https://github.com/laravel/framework/blob/5.8/src/Illuminate/Queue/Worker.php#L535-L538 | ||
*/ | ||
protected function supportsAsyncSignals() | ||
{ | ||
return extension_loaded('pcntl'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace AlexBowers\LaravelFakeCron; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
use AlexBowers\LaravelFakeCron\Commands\Cron; | ||
|
||
class FakeCronServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap the application services. | ||
*/ | ||
public function boot() | ||
{ | ||
if ($this->app->runningInConsole()) { | ||
$this->commands([ | ||
Cron::class, | ||
]); | ||
} | ||
} | ||
/** | ||
* Register the application services. | ||
*/ | ||
public function register() | ||
{ | ||
// ... | ||
} | ||
} |