Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
25 / 25 |
| Event | |
100.00% |
1 / 1 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
25 / 25 |
| withCronsTable | |
100.00% |
1 / 1 |
3 | |
100.00% |
19 / 19 |
|||
| hasToRun | |
100.00% |
1 / 1 |
3 | |
100.00% |
2 / 2 |
|||
| cronStart | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| cronStop | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\CommandsLaravel\Console\Scheduling; |
| 4 | |
| 5 | use Closure; |
| 6 | use Illuminate\Console\Scheduling\Event as BaseEvent; |
| 7 | use Illuminate\Support\Facades\Log; |
| 8 | use Qmp\Laravel\CommandsLaravel\Models\Cron; |
| 9 | |
| 10 | class Event extends BaseEvent |
| 11 | { |
| 12 | protected $signature; |
| 13 | |
| 14 | protected $defaultServiceName = 'service_scheduler'; |
| 15 | |
| 16 | public function withCronsTable() |
| 17 | { |
| 18 | // Get command signature |
| 19 | $this->signature = explode(' ', $this->command)[2]; |
| 20 | |
| 21 | $cron = Cron::where('name', $this->signature)->first(); |
| 22 | |
| 23 | if($cron === null) { |
| 24 | Cron::create([ |
| 25 | 'name' => $this->signature, |
| 26 | 'service' => config('micro-services.name') ? str_replace("-", "_", explode('-laravel', config('micro-services.name'))[0]) : $this->defaultServiceName, |
| 27 | 'type' => 'cron', |
| 28 | 'active' => 0, |
| 29 | 'prod' => env('PROD') |
| 30 | ]); |
| 31 | } |
| 32 | |
| 33 | $this->when(Closure::fromCallable([$this, 'hasToRun'])); |
| 34 | |
| 35 | $this->before(function () { |
| 36 | Log::info("Cron $this->signature is starting ..."); |
| 37 | $this->cronStart(); |
| 38 | }); |
| 39 | |
| 40 | $this->after(function () { |
| 41 | Log::info("Cron $this->signature has finished ..."); |
| 42 | $this->cronStop(); |
| 43 | }); |
| 44 | |
| 45 | return $this; |
| 46 | } |
| 47 | |
| 48 | protected function hasToRun() { |
| 49 | $cron = Cron::where('name', $this->signature)->first(); |
| 50 | return $cron->active && !$cron->pause && $cron->prod == env('PROD'); |
| 51 | } |
| 52 | |
| 53 | protected function cronStart() { |
| 54 | Cron::where('name', $this->signature)->update(['running' => true]); |
| 55 | } |
| 56 | |
| 57 | protected function cronStop() { |
| 58 | Cron::where('name', $this->signature)->update(['running' => false]); |
| 59 | } |
| 60 | } |