Skip to content

Commit 758bd59

Browse files
committed
Add enable/disable switch for Monitoring
1 parent 685c72a commit 758bd59

File tree

6 files changed

+186
-15
lines changed

6 files changed

+186
-15
lines changed

config/cloud-tasks.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'monitor' => [
7+
'enabled' => env('CLOUD_TASKS_MONITOR_ENABLED', false),
8+
],
9+
];

src/CloudTasks.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,24 @@ public static function check($request)
3636
{
3737
return (static::$authUsing)($request);
3838
}
39+
40+
/**
41+
* Determine if the monitor is enabled.
42+
*
43+
* @return bool
44+
*/
45+
public static function monitorEnabled(): bool
46+
{
47+
return config('cloud-tasks.monitor.enabled') === true;
48+
}
49+
50+
/**
51+
* Determine if the monitor is disabled.
52+
*
53+
* @return bool
54+
*/
55+
public static function monitorDisabled(): bool
56+
{
57+
return self::monitorEnabled() === false;
58+
}
3959
}

src/CloudTasksQueue.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
120120
$task->setScheduleTime(new Timestamp(['seconds' => $availableAt]));
121121
}
122122

123-
MonitoringService::make()->addToMonitor($queue, $task);
123+
if (CloudTasks::monitorEnabled()) {
124+
MonitoringService::make()->addToMonitor($queue, $task);
125+
}
124126

125127
$createdTask = CloudTasksApi::createTask($queueName, $task);
126128

src/CloudTasksServiceProvider.php

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function boot(QueueManager $queue, Router $router): void
2222

2323
$this->registerClient();
2424
$this->registerConnector($queue);
25+
$this->registerConfig();
2526
$this->registerViews();
2627
$this->registerAssets();
2728
$this->registerMigrations();
@@ -77,28 +78,53 @@ private function registerConnector(QueueManager $queue): void
7778
});
7879
}
7980

81+
private function registerConfig(): void
82+
{
83+
$this->publishes([
84+
__DIR__ . '/../config/cloud-tasks.php' => config_path('cloud-tasks.php'),
85+
], ['cloud-tasks']);
86+
87+
$this->mergeConfigFrom(__DIR__ . '/../config/cloud-tasks.php', 'cloud-tasks');
88+
}
89+
8090
private function registerViews(): void
8191
{
92+
if (CloudTasks::monitorDisabled()) {
93+
return;
94+
}
95+
8296
$this->loadViewsFrom(__DIR__ . '/../views', 'cloud-tasks');
8397
}
8498

8599
private function registerAssets(): void
86100
{
101+
if (CloudTasks::monitorDisabled()) {
102+
return;
103+
}
104+
87105
$this->publishes([
88106
__DIR__ . '/../dashboard/dist' => public_path('vendor/cloud-tasks'),
89-
], ['cloud-tasks-assets']);
107+
], ['cloud-tasks']);
90108
}
91109

92110
private function registerMigrations(): void
93111
{
112+
if (CloudTasks::monitorDisabled()) {
113+
return;
114+
}
115+
94116
$this->loadMigrationsFrom([
95117
__DIR__ . '/../migrations',
96118
]);
97119
}
98120

99121
private function registerRoutes(Router $router): void
100122
{
101-
$router->post('handle-task', [TaskHandler::class, 'handle']);
123+
$router->post('handle-task', [TaskHandler::class, 'handle'])->name('cloud-tasks.handle-task');
124+
125+
if (config('cloud-tasks.monitor.enabled') === false) {
126+
return;
127+
}
102128

103129
$router->middleware(Authenticate::class)->group(function () use ($router) {
104130
$router->get('cloud-tasks/{view?}', function () {
@@ -116,41 +142,61 @@ private function registerRoutes(Router $router): void
116142
'cloud-tasks.index'
117143
);
118144

119-
$router->get('cloud-tasks-api/dashboard', [CloudTasksApiController::class, 'dashboard']);
120-
$router->get('cloud-tasks-api/tasks', [CloudTasksApiController::class, 'tasks']);
121-
$router->get('cloud-tasks-api/task/{uuid}', [CloudTasksApiController::class, 'task']);
145+
$router->get('cloud-tasks-api/dashboard', [CloudTasksApiController::class, 'dashboard'])->name('cloud-tasks.api.dashboard');
146+
$router->get('cloud-tasks-api/tasks', [CloudTasksApiController::class, 'tasks'])->name('cloud-tasks.api.tasks');
147+
$router->get('cloud-tasks-api/task/{uuid}', [CloudTasksApiController::class, 'task'])->name('cloud-tasks.api.task');
122148
});
123149
}
124150

125151
private function registerMonitoring(): void
126152
{
153+
app('events')->listen(JobFailed::class, function (JobFailed $event) {
154+
if (!$event->job instanceof CloudTasksJob) {
155+
return;
156+
}
157+
158+
$config = $event->job->cloudTasksQueue->config;
159+
160+
app('queue.failer')->log(
161+
$config['connection'], $event->job->getQueue() ?: $config['queue'],
162+
$event->job->getRawBody(), $event->exception
163+
);
164+
});
165+
127166
app('events')->listen(JobProcessing::class, function (JobProcessing $event) {
167+
if (!CloudTasks::monitorEnabled()) {
168+
return;
169+
}
170+
128171
if ($event->job instanceof CloudTasksJob) {
129172
MonitoringService::make()->markAsRunning($event->job->uuid());
130173
}
131174
});
132175

133176
app('events')->listen(JobProcessed::class, function (JobProcessed $event) {
177+
if (!CloudTasks::monitorEnabled()) {
178+
return;
179+
}
180+
134181
if ($event->job instanceof CloudTasksJob) {
135182
MonitoringService::make()->markAsSuccessful($event->job->uuid());
136183
}
137184
});
138185

139186
app('events')->listen(JobExceptionOccurred::class, function (JobExceptionOccurred $event) {
187+
if (!CloudTasks::monitorEnabled()) {
188+
return;
189+
}
190+
140191
MonitoringService::make()->markAsError($event);
141192
});
142193

143194
app('events')->listen(JobFailed::class, function ($event) {
144-
MonitoringService::make()->markAsFailed(
145-
$event
146-
);
147-
148-
$config = $event->job->cloudTasksQueue->config;
195+
if (!CloudTasks::monitorEnabled()) {
196+
return;
197+
}
149198

150-
app('queue.failer')->log(
151-
$config['connection'], $event->job->getQueue() ?: $config['queue'],
152-
$event->job->getRawBody(), $event->exception
153-
);
199+
MonitoringService::make()->markAsFailed($event);
154200
});
155201
}
156202
}

tests/CloudTasksMonitoringTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
namespace Tests;
44

55
use Google\Cloud\Tasks\V2\RetryConfig;
6+
use Illuminate\Routing\Route;
7+
use Illuminate\Routing\Router;
68
use Illuminate\Support\Carbon;
9+
use Illuminate\Support\Facades\Schema;
710
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
11+
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksServiceProvider;
812
use Stackkit\LaravelGoogleCloudTasksQueue\OpenIdVerificator;
913
use Stackkit\LaravelGoogleCloudTasksQueue\StackkitCloudTask;
1014
use Tests\Support\FailingJob;
@@ -260,6 +264,22 @@ public function when_a_job_is_dispatched_it_will_be_added_to_the_monitor()
260264
$this->assertSame($payload, $job->payload);
261265
}
262266

267+
/**
268+
* @test
269+
*/
270+
public function when_monitoring_is_disabled_jobs_will_not_be_added_to_the_monitor()
271+
{
272+
// Arrange
273+
CloudTasksApi::fake();
274+
config()->set('cloud-tasks.monitor.enabled', false);
275+
276+
// Act
277+
$this->dispatch(new SimpleJob());
278+
279+
// Assert
280+
$this->assertDatabaseCount((new StackkitCloudTask())->getTable(), 0);
281+
}
282+
263283
/**
264284
* @test
265285
*/
@@ -370,4 +390,70 @@ public function when_a_job_fails_it_will_be_updated_in_the_monitor()
370390
$events[6]
371391
);
372392
}
393+
394+
/**
395+
* @test
396+
*/
397+
public function test_publish()
398+
{
399+
// Arrange
400+
config()->set('cloud-tasks.monitor.enabled', true);
401+
402+
// Act & Assert
403+
$expectedPublishBase = dirname(__DIR__);
404+
405+
$this->artisan('vendor:publish --tag=cloud-tasks --force')
406+
->expectsOutput('Copied File [' . $expectedPublishBase . '/config/cloud-tasks.php] To [/config/cloud-tasks.php]')
407+
->expectsOutput('Copied Directory [' . $expectedPublishBase . '/dashboard/dist] To [/public/vendor/cloud-tasks]')
408+
->expectsOutput('Publishing complete.');
409+
}
410+
411+
/**
412+
* @test
413+
*/
414+
public function when_monitoring_is_enabled_it_adds_the_necessary_routes()
415+
{
416+
// Act
417+
$routes = app(Router::class)->getRoutes();
418+
419+
// Assert
420+
$this->assertInstanceOf(Route::class, $routes->getByName('cloud-tasks.handle-task'));
421+
$this->assertInstanceOf(Route::class, $routes->getByName('cloud-tasks.index'));
422+
$this->assertInstanceOf(Route::class, $routes->getByName('cloud-tasks.api.dashboard'));
423+
$this->assertInstanceOf(Route::class, $routes->getByName('cloud-tasks.api.tasks'));
424+
$this->assertInstanceOf(Route::class, $routes->getByName('cloud-tasks.api.task'));
425+
}
426+
427+
/**
428+
* @test
429+
*/
430+
public function when_monitoring_is_enabled_it_adds_the_necessary_migrations()
431+
{
432+
$this->assertTrue(in_array(dirname(__DIR__) . '/src/../migrations', app('migrator')->paths()));
433+
}
434+
435+
/**
436+
* @test
437+
*/
438+
public function when_monitoring_is_disabled_it_adds_the_necessary_migrations()
439+
{
440+
$this->assertEmpty(app('migrator')->paths());
441+
}
442+
443+
/**
444+
* @test
445+
*/
446+
public function when_monitoring_is_disabled_it_does_not_add_the_monitor_routes()
447+
{
448+
// Act
449+
$routes = app(Router::class)->getRoutes();
450+
451+
// Assert
452+
$this->assertInstanceOf(Route::class, $routes->getByName('cloud-tasks.handle-task'));
453+
$this->assertNull($routes->getByName('cloud-tasks.index'));
454+
$this->assertNull($routes->getByName('cloud-tasks.api.dashboard'));
455+
$this->assertNull($routes->getByName('cloud-tasks.api.tasks'));
456+
$this->assertNull($routes->getByName('cloud-tasks.api.task'));
457+
458+
}
373459
}

tests/TestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ protected function getEnvironmentSetUp($app)
9696
]);
9797
$app['config']->set('queue.failed.driver', 'database-uuids');
9898
$app['config']->set('queue.failed.database', 'testbench');
99+
100+
$disableMonitorPrefix = 'when_monitoring_is_disabled';
101+
102+
if (substr($this->getName(), 0, strlen($disableMonitorPrefix)) === $disableMonitorPrefix) {
103+
$app['config']->set('cloud-tasks.monitor.enabled', false);
104+
} else {
105+
$app['config']->set('cloud-tasks.monitor.enabled', true);
106+
}
99107
}
100108

101109
protected function setConfigValue($key, $value)

0 commit comments

Comments
 (0)