Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbowers committed Mar 25, 2019
0 parents commit 0bda99d
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
composer.lock
30 changes: 30 additions & 0 deletions composer.json
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
}
}
77 changes: 77 additions & 0 deletions src/Commands/Cron.php
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');
}
}
28 changes: 28 additions & 0 deletions src/FakeCronServiceProvider.php
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()
{
// ...
}
}

0 comments on commit 0bda99d

Please sign in to comment.