Skip to content

Commit f4a43b0

Browse files
committed
Added support for withoutOverlapping, before- and afterCallbacks
1 parent e26c755 commit f4a43b0

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

src/TaskHandler.php

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Stackkit\LaravelGoogleCloudScheduler;
44

5+
use Illuminate\Console\Scheduling\Schedule;
6+
use Illuminate\Container\Container;
7+
use Illuminate\Contracts\Console\Kernel;
58
use Illuminate\Http\Request;
69
use Illuminate\Support\Facades\Artisan;
710

@@ -10,12 +13,24 @@ class TaskHandler
1013
private $command;
1114
private $request;
1215
private $openId;
16+
private $kernel;
17+
private $schedule;
18+
private $container;
1319

14-
public function __construct(Command $command, Request $request, OpenIdVerificator $openId)
15-
{
20+
public function __construct(
21+
Command $command,
22+
Request $request,
23+
OpenIdVerificator $openId,
24+
Kernel $kernel,
25+
Schedule $schedule,
26+
Container $container
27+
) {
1628
$this->command = $command;
1729
$this->request = $request;
1830
$this->openId = $openId;
31+
$this->kernel = $kernel;
32+
$this->schedule = $schedule;
33+
$this->container = $container;
1934
}
2035

2136
/**
@@ -27,9 +42,7 @@ public function handle()
2742

2843
set_time_limit(0);
2944

30-
Artisan::call($this->command->captureWithoutArtisan());
31-
32-
$output = Artisan::output();
45+
$output = $this->runCommand($this->command->captureWithoutArtisan());
3346

3447
return $this->cleanOutput($output);
3548
}
@@ -50,6 +63,54 @@ private function authorizeRequest()
5063
$this->openId->guardAgainstInvalidOpenIdToken($decodedToken);
5164
}
5265

66+
private function runCommand($command)
67+
{
68+
if ($this->isScheduledCommand($command)) {
69+
$scheduledCommand = $this->getScheduledCommand($command);
70+
71+
if ($scheduledCommand->withoutOverlapping && ! $scheduledCommand->mutex->create($scheduledCommand)) {
72+
return null;
73+
}
74+
75+
$scheduledCommand->callBeforeCallbacks($this->container);
76+
77+
Artisan::call($command);
78+
79+
$scheduledCommand->callAfterCallbacks($this->container);
80+
} else {
81+
Artisan::call($command);
82+
}
83+
84+
return Artisan::output();
85+
}
86+
87+
private function isScheduledCommand($command)
88+
{
89+
return !is_null($this->getScheduledCommand($command));
90+
}
91+
92+
private function getScheduledCommand($command)
93+
{
94+
$events = $this->schedule->events();
95+
96+
foreach ($events as $event) {
97+
$eventCommand = $this->commandWithoutArtisan($event->command);
98+
99+
if ($command === $eventCommand) {
100+
return $event;
101+
}
102+
}
103+
104+
return null;
105+
}
106+
107+
private function commandWithoutArtisan($command)
108+
{
109+
$parts = explode('artisan', $command);
110+
111+
return substr($parts[1], 2, strlen($parts[1]));
112+
}
113+
53114
private function cleanOutput($output)
54115
{
55116
return trim($output);

tests/TaskHandlerTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Firebase\JWT\JWT;
66
use Illuminate\Console\Scheduling\Schedule;
7+
use Illuminate\Container\Container;
8+
use Illuminate\Contracts\Console\Kernel;
79
use Illuminate\Http\Request;
810
use Illuminate\Support\Facades\Log;
911
use Stackkit\LaravelGoogleCloudScheduler\CloudSchedulerException;
@@ -35,7 +37,9 @@ public function setUp(): void
3537
$this->fakeCommand,
3638
$this->request,
3739
$this->openId,
38-
app(JWT::class)
40+
app(Kernel::class),
41+
app(Schedule::class),
42+
Container::getInstance()
3943
);
4044
}
4145

0 commit comments

Comments
 (0)