Skip to content

Commit

Permalink
Merge pull request #26 from spatie/job-middleware
Browse files Browse the repository at this point in the history
Add support for job middleware
  • Loading branch information
rubenvanassche authored Oct 2, 2020
2 parents 7bdd4dc + b1d5ebb commit 0b0eebb
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-queueable-actions` will be documented in this file

## 2.7.1 - 2020-10-02

- Add support for job middleware

## 2.7.0 - 2020-09-29

- Add `Spatie\QueueableAction\Testing\QueueableActionFake` class with test helpers (#27)
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,23 @@ class CustomTagsAction
}
```

### Job Middleware

Middleware where action job passes through can be added by overriding the `middleware()` function.

``` php
class CustomTagsAction
{
use QueueableAction;

// ...

public function middleware() {
return [new RateLimited()];
}
}
```

### What is the difference between actions and jobs?

In short: constructor injection allows for much more flexibility.
Expand Down
8 changes: 8 additions & 0 deletions src/ActionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public function __construct($action, array $parameters = [])

if (is_object($action)) {
$this->tags = $action->tags();
$this->middleware = $action->middleware();

$this->middleware = $action->middleware();

if (method_exists($action, 'failed')) {
$this->onFailCallback = [$action, 'failed'];
Expand All @@ -51,6 +54,11 @@ public function tags()
return $this->tags;
}

public function middleware()
{
return $this->middleware;
}

public function failed(Exception $exception)
{
if ($this->onFailCallback) {
Expand Down
6 changes: 6 additions & 0 deletions src/QueueableAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function onQueue(?string $queue = null)
/** @var self $class */
$class = new class($this, $queue) {
protected $action;

protected $queue;

public function __construct(object $action, ?string $queue)
Expand Down Expand Up @@ -47,4 +48,9 @@ public function tags(): array
{
return ['action_job'];
}

public function middleware(): array
{
return [];
}
}
20 changes: 19 additions & 1 deletion tests/QueueableActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Spatie\QueueableAction\ActionJob;
use Spatie\QueueableAction\Tests\TestClasses\ActionWithFailedMethod;
use Spatie\QueueableAction\Tests\TestClasses\ComplexAction;
use Spatie\QueueableAction\Tests\TestClasses\ContinueMiddleware;
use Spatie\QueueableAction\Tests\TestClasses\DataObject;
use Spatie\QueueableAction\Tests\TestClasses\FailingAction;
use Spatie\QueueableAction\Tests\TestClasses\MiddlewareAction;
use Spatie\QueueableAction\Tests\TestClasses\SimpleAction;
use Spatie\QueueableAction\Tests\TestClasses\TaggedAction;

Expand Down Expand Up @@ -129,13 +131,13 @@ public function an_action_can_have_custom_job_tags()
});
}

/** @test */
public function an_action_can_have_a_custom_failed_callback()
{
Queue::fake();

$action = new ActionWithFailedMethod();


$action->onQueue()->execute();

Queue::assertPushed(ActionJob::class, function ($action) {
Expand All @@ -158,4 +160,20 @@ public function the_failed_callback_is_executed_on_failure()

unset($_SERVER['_test_failed_message']); // cleanup
}

/** @test */
public function an_action_can_have_job_middleware()
{
Queue::fake();

$action = new MiddlewareAction();

$action->onQueue()->execute();

Queue::assertPushed(ActionJob::class, function ($action) {
return is_array($action->middleware())
&& count($action->middleware()) === 1
&& $action->middleware[0] instanceof ContinueMiddleware;
});
}
}
11 changes: 11 additions & 0 deletions tests/TestClasses/ContinueMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Spatie\QueueableAction\Tests\TestClasses;

class ContinueMiddleware
{
public function handle($job, $next)
{
$next($job);
}
}
20 changes: 20 additions & 0 deletions tests/TestClasses/MiddlewareAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Spatie\QueueableAction\Tests\TestClasses;

use Spatie\QueueableAction\QueueableAction;

class MiddlewareAction
{
use QueueableAction;

public function execute()
{
//
}

public function middleware()
{
return [new ContinueMiddleware()];
}
}

0 comments on commit 0b0eebb

Please sign in to comment.