Skip to content

Commit

Permalink
Extends the default Schedule Run Command
Browse files Browse the repository at this point in the history
  • Loading branch information
richan-fongdasen committed Sep 21, 2023
1 parent 67498d8 commit ce98617
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 14 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"require": {
"php": "^7.4|^8.0",
"illuminate/console": "^8.0|^9.0|^10.0",
"illuminate/queue": "^8.0|^9.0|^10.0",
"illuminate/support": "^8.0|^9.0|^10.0",
"kainxspirits/laravel-pubsub-queue": "^0.6|^0.7|^0.8"
Expand Down
37 changes: 37 additions & 0 deletions src/Console/Commands/ScheduleRunCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace RichanFongdasen\GCRWorker\Console\Commands;

use Illuminate\Console\Scheduling\ScheduleRunCommand as BaseScheduleRunCommand;

class ScheduleRunCommand extends BaseScheduleRunCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'gcr-schedule:run';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();

$this->fixStartTime();
}

/**
* Fix the start time of the command.
*
* @return void
*/
protected function fixStartTime(): void
{
$this->startedAt->second(0);
}
}
8 changes: 3 additions & 5 deletions src/Controllers/ScheduledJobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ public function index(): JsonResponse
$this->increaseExecutionTime();
$this->increaseMemoryLimit();

Artisan::call('schedule:run');
Artisan::call('gcr-schedule:run');

$response = [
return response()->json([
'info' => 'The scheduled job has completed.',
];

return response()->json($reponse);
]);
}
}
15 changes: 15 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider as Provider;
use RichanFongdasen\GCRWorker\Console\Commands\ScheduleRunCommand;

class ServiceProvider extends Provider
{
Expand Down Expand Up @@ -33,6 +34,20 @@ public function register(): void
if ($configPath !== false) {
$this->mergeConfigFrom($configPath, 'gcr-worker');
}

$this->registerCommands();
}

/**
* Register the package's console commands.
*
* @return void
*/
protected function registerCommands(): void
{
$this->commands([
ScheduleRunCommand::class,
]);
}

/**
Expand Down
9 changes: 0 additions & 9 deletions tests/Feature/ScheduledJobHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ public function it_can_handle_scheduled_job_invocation_as_expected()
{
config(['gcr-worker.allow_event_invocation' => true]);

$artisan = \Mockery::mock(Kernel::class);
app()->bind(KernelContract::class, function () use ($artisan) {
return $artisan;
});

$artisan->shouldReceive('call')
->withArgs(['schedule:run'])
->once();

$this->getJson('/gcr-worker/run-scheduled-job')
->assertStatus(200)
->assertJsonFragment(['info' => 'The scheduled job has completed.']);
Expand Down

0 comments on commit ce98617

Please sign in to comment.