Skip to content

Commit

Permalink
Merge branch '5.x' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Jan 15, 2024
2 parents 1a07d61 + 22408b6 commit 1d177b1
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 16 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Release Notes

## [Unreleased](https://github.com/laravel/horizon/compare/v5.21.4...5.x)
## [Unreleased](https://github.com/laravel/horizon/compare/v5.21.5...5.x)

## [v5.21.5](https://github.com/laravel/horizon/compare/v5.21.4...v5.21.5) - 2023-12-29

* [5.x] Pass event instance to event listeners tag() method by [@mateusjatenee](https://github.com/mateusjatenee) in https://github.com/laravel/horizon/pull/1361

## [v5.21.4](https://github.com/laravel/horizon/compare/v5.21.3...v5.21.4) - 2023-11-23

Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/app.js": "/app.js?id=79bae40dcb18de9ca1b5d0008c577471",
"/app.js": "/app.js?id=b4f3f08e60211bd6948ec35e5e9de9a1",
"/app-dark.css": "/app-dark.css?id=15c72df05e2b1147fa3e4b0670cfb435",
"/app.css": "/app.css?id=4d6a1a7fe095eedc2cb2a4ce822ea8a5",
"/img/favicon.png": "/img/favicon.png?id=1542bfe8a0010dcbee710da13cce367f",
Expand Down
4 changes: 2 additions & 2 deletions resources/js/screens/recentJobs/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
*/
previous() {
this.loadJobs(
(this.page - 2) * this.perPage
(this.page - 2) * this.perPage - 1
);
this.page -= 1;
Expand All @@ -123,7 +123,7 @@
*/
next() {
this.loadJobs(
this.page * this.perPage
this.page * this.perPage - 1
);
this.page += 1;
Expand Down
2 changes: 1 addition & 1 deletion src/AutoScaler.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected function timeToClearPerQueue(Supervisor $supervisor, Collection $pools

return [$queue => [
'size' => $size,
'time' => ($size * $this->metrics->runtimeForQueue($queue)),
'time' => ($size * $this->metrics->runtimeForQueue($queue)),
]];
});
}
Expand Down
5 changes: 5 additions & 0 deletions src/HorizonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Horizon;

use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Foundation\CachesRoutes;
use Illuminate\Queue\QueueManager;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -50,6 +51,10 @@ protected function registerEvents()
*/
protected function registerRoutes()
{
if ($this->app instanceof CachesRoutes && $this->app->routesAreCached()) {
return;
}

Route::group([
'domain' => config('horizon.domain', null),
'prefix' => config('horizon.path'),
Expand Down
42 changes: 38 additions & 4 deletions src/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@

class Tags
{
/**
* The event that was last handled.
*
* @var object|null
*/
protected static $event;

/**
* Determine the tags for the given job.
*
Expand Down Expand Up @@ -52,23 +59,29 @@ public static function extractExplicitTags($job)
*/
protected static function tagsForListener($job)
{
$event = static::extractEvent($job);

static::setEvent($event);

return collect(
[static::extractListener($job), static::extractEvent($job)]
[static::extractListener($job), $event]
)->map(function ($job) {
return static::for($job);
})->collapse()->unique()->toArray();
})->collapse()->unique()->tap(function () {
static::flushEventState();
})->toArray();
}

/**
* Determine tags for the given job.
*
* @param array $jobs
* @return mixed
* @return array
*/
protected static function explicitTags(array $jobs)
{
return collect($jobs)->map(function ($job) {
return method_exists($job, 'tags') ? $job->tags() : [];
return method_exists($job, 'tags') ? $job->tags(static::$event) : [];
})->collapse()->unique()->all();
}

Expand Down Expand Up @@ -162,4 +175,25 @@ protected static function extractEvent($job)
? $job->data[0]
: new stdClass;
}

/**
* Set the event currently being handled.
*
* @param object $event
* @return void
*/
protected static function setEvent($event)
{
static::$event = $event;
}

/**
* Flush the event currently being handled.
*
* @return void
*/
protected static function flushEventState()
{
static::$event = null;
}
}
14 changes: 14 additions & 0 deletions tests/Feature/Fixtures/FakeListenerWithDynamicTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Laravel\Horizon\Tests\Feature\Fixtures;

class FakeListenerWithDynamicTags
{
public function tags(FakeEvent $event)
{
return [
'listenerTag1',
get_class($event),
];
}
}
14 changes: 14 additions & 0 deletions tests/Feature/RedisPayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Laravel\Horizon\Tests\Feature\Fixtures\FakeJobWithTagsMethod;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeListener;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeListenerSilenced;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeListenerWithDynamicTags;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeListenerWithProperties;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeListenerWithTypedProperties;
use Laravel\Horizon\Tests\Feature\Fixtures\FakeModel;
Expand Down Expand Up @@ -100,6 +101,19 @@ public function test_tags_are_correctly_extracted_for_listeners()
], $JobPayload->decoded['tags']);
}

public function test_tags_are_correctly_extracted_for_listeners_with_dynamic_event_information()
{
$JobPayload = new JobPayload(json_encode(['id' => 1]));

$job = new CallQueuedListener(FakeListenerWithDynamicTags::class, 'handle', [new FakeEvent()]);

$JobPayload->prepare($job);

$this->assertEquals([
'listenerTag1', FakeEvent::class, 'eventTag1', 'eventTag2',
], $JobPayload->decoded['tags']);
}

public function test_tags_are_correctly_determined_for_listeners()
{
$JobPayload = new JobPayload(json_encode(['id' => 1]));
Expand Down

0 comments on commit 1d177b1

Please sign in to comment.