Skip to content

Commit

Permalink
Forget tenant when job is not tenant aware (#408)
Browse files Browse the repository at this point in the history
* Forget tenant when job is not tenant aware

* Fix styling

* Make it a major release

* Fix styling

* wip

* Fix styling

* Fix tests

* Fix styling

Co-authored-by: riasvdv <[email protected]>
  • Loading branch information
riasvdv and riasvdv committed Oct 31, 2022
1 parent 081bcbe commit 09b122f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

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


## 3.0.0 2022-10-28

### Breaking change
- `NotTenantAware` jobs will now forget any current tenant that might have been set when starting processing.

## 2.3.9 - 2022-10-30

- Fix an issue where the `Illuminate\Cache\Repository` was not being cleared from the container

**Full Changelog**: https://github.com/spatie/laravel-multitenancy/compare/2.3.8...2.3.9


## 2.3.8 - 2022-09-08

### What's Changed
Expand Down
4 changes: 4 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# From v1 to v3

There are no API changes between v2 and v3. There is one behaviour change where jobs that are `NotTenantAware` will forget any current tenant before processing.

# From v1 to v2

There are no API changes between v1 and v2. If you're on PHP 8, you should be able to upgrade without having to make any changes.
12 changes: 8 additions & 4 deletions src/Actions/MakeQueueTenantAwareAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class MakeQueueTenantAwareAction
{
use UsesTenantModel;

public function execute()
public function execute(): void
{
$this
->listenForJobsBeingQueued()
->listenForJobsBeingProcessed()
->listenForJobsRetryRequested();
}

protected function listenForJobsBeingQueued(): self
protected function listenForJobsBeingQueued(): static
{
app('queue')->createPayloadUsing(function ($connectionName, $queue, $payload) {
$queueable = $payload['data']['command'];
Expand All @@ -38,10 +38,12 @@ protected function listenForJobsBeingQueued(): self
return $this;
}

protected function listenForJobsBeingProcessed(): self
protected function listenForJobsBeingProcessed(): static
{
app('events')->listen(JobProcessing::class, function (JobProcessing $event) {
if (! array_key_exists('tenantId', $event->job->payload())) {
$this->getTenantModel()::forgetCurrent();

return;
}

Expand All @@ -51,10 +53,12 @@ protected function listenForJobsBeingProcessed(): self
return $this;
}

protected function listenForJobsRetryRequested(): self
protected function listenForJobsRetryRequested(): static
{
app('events')->listen(JobRetryRequested::class, function (JobRetryRequested $event) {
if (! array_key_exists('tenantId', $event->payload())) {
$this->getTenantModel()::forgetCurrent();

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function () {
}
);

it('will not touch the tenant if the job is not tenant aware', function () {
it('will forget any current tenant when starting a not tenant aware job', function () {
$this->tenant->makeCurrent();

$job = new NotTenantAwareTestJob($this->valuestore);
Expand All @@ -87,6 +87,6 @@ function () {

app(Dispatcher::class)->dispatch($job);

// Assert that the active tenant was not modified
expect($this->tenant->id)->toBe(Tenant::current()->id);
// Assert that the active tenant was forgotten
$this->assertNull(Tenant::current());
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

app(Dispatcher::class)->dispatch(new TestJob($this->valuestore));

expect($this->valuestore->get('tenantId'))->toEqual($this->tenant->id)
expect($this->valuestore->get('tenantId'))->toBeNull()
->and($this->valuestore->has('tenantIdInPayload'))->toBeTrue()
->and($this->valuestore->get('tenantIdInPayload'))->toBeNull();
});

0 comments on commit 09b122f

Please sign in to comment.