5
5
use Illuminate \Queue \Events \JobProcessing ;
6
6
use Illuminate \Queue \Events \JobRetryRequested ;
7
7
use Illuminate \Support \Arr ;
8
+ use Spatie \Multitenancy \Concerns \BindAsCurrentTenant ;
8
9
use Spatie \Multitenancy \Exceptions \CurrentTenantCouldNotBeDeterminedInTenantAwareJob ;
9
10
use Spatie \Multitenancy \Jobs \NotTenantAware ;
10
11
use Spatie \Multitenancy \Jobs \TenantAware ;
14
15
class MakeQueueTenantAwareAction
15
16
{
16
17
use UsesTenantModel;
18
+ use BindAsCurrentTenant;
17
19
18
20
public function execute (): void
19
21
{
@@ -41,11 +43,7 @@ protected function listenForJobsBeingQueued(): static
41
43
protected function listenForJobsBeingProcessed (): static
42
44
{
43
45
app ('events ' )->listen (JobProcessing::class, function (JobProcessing $ event ) {
44
- $ this ->getTenantModel ()::forgetCurrent ();
45
-
46
- if (array_key_exists ('tenantId ' , $ event ->job ->payload ())) {
47
- $ this ->findTenant ($ event )->makeCurrent ();
48
- }
46
+ $ this ->bindOrForgetCurrentTenant ($ event );
49
47
});
50
48
51
49
return $ this ;
@@ -54,11 +52,7 @@ protected function listenForJobsBeingProcessed(): static
54
52
protected function listenForJobsRetryRequested (): static
55
53
{
56
54
app ('events ' )->listen (JobRetryRequested::class, function (JobRetryRequested $ event ) {
57
- $ this ->getTenantModel ()::forgetCurrent ();
58
-
59
- if (array_key_exists ('tenantId ' , $ event ->payload ())) {
60
- $ this ->findTenant ($ event )->makeCurrent ();
61
- }
55
+ $ this ->bindOrForgetCurrentTenant ($ event );
62
56
});
63
57
64
58
return $ this ;
@@ -131,4 +125,15 @@ protected function getJobFromQueueable(object $queueable)
131
125
132
126
return $ queueable ->$ job ;
133
127
}
128
+
129
+ protected function bindOrForgetCurrentTenant (JobProcessing |JobRetryRequested $ event ): void
130
+ {
131
+ if (array_key_exists ('tenantId ' , $ this ->getEventPayload ($ event ))) {
132
+ $ this ->bindAsCurrentTenant ($ this ->findTenant ($ event )->makeCurrent ());
133
+
134
+ return ;
135
+ }
136
+
137
+ $ this ->getTenantModel ()::forgetCurrent ();
138
+ }
134
139
}
0 commit comments