|
9 | 9 | use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; |
10 | 10 | use Illuminate\Notifications\Events\BroadcastNotificationCreated; |
11 | 11 | use Illuminate\Notifications\Events\NotificationSent; |
| 12 | +use Illuminate\Support\Arr; |
12 | 13 | use Illuminate\Support\Env; |
13 | 14 | use Illuminate\Support\Facades; |
14 | 15 | use Illuminate\Support\Facades\DB; |
|
43 | 44 | use RuntimeException; |
44 | 45 | use Spatie\Multitenancy\Events\MadeTenantCurrentEvent; |
45 | 46 | use Spatie\Multitenancy\Events\TenantNotFoundForRequestEvent; |
| 47 | +use Spatie\Multitenancy\TenantCollection; |
46 | 48 |
|
47 | 49 | /** |
48 | 50 | * Provide our ProcessMaker specific services. |
@@ -93,26 +95,51 @@ public function boot(): void |
93 | 95 | // This sets up individual supervisors for each tenant so that one tenant does not block |
94 | 96 | // the queue for another tenant. This must be done here instead of SwitchTenant.php because |
95 | 97 | // there is a single horizon instance for all tenants. |
96 | | - if ($this->app->runningInConsole() && config('app.multitenancy')) { |
97 | | - $tenantIds = Tenant::all()->pluck('id')->toArray(); |
| 98 | + if ($this->app->runningInConsole() && config('app.multitenancy') && $this->horizonTenantsNotSet()) { |
| 99 | + $tenants = Tenant::all(); |
98 | 100 | $config = config('horizon.environments'); |
99 | | - $config = $this->addTenantSupervisors($config, $tenantIds); |
| 101 | + $config = $this->addTenantSupervisors($config, $tenants); |
100 | 102 | config(['horizon.environments' => $config]); |
101 | 103 | } |
102 | 104 | } |
103 | 105 |
|
104 | | - private function addTenantSupervisors(array $config, array $tenantIds): array |
| 106 | + private function horizonTenantsNotSet(): bool |
105 | 107 | { |
| 108 | + $firstKey = array_keys(config('horizon.environments.production'))[0]; |
| 109 | + if (str_starts_with($firstKey, 'tenant')) { |
| 110 | + // Already cached |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + private function addTenantSupervisors(array $config, TenantCollection $tenants): array |
| 118 | + { |
| 119 | + $tenantsConfigById = $tenants->mapWithKeys(function ($tenant) { |
| 120 | + return [$tenant->id => $tenant->config]; |
| 121 | + }); |
| 122 | + |
106 | 123 | foreach ($config as $env => &$supervisors) { |
107 | 124 | $newSupervisors = []; |
108 | 125 |
|
109 | 126 | foreach ($supervisors as $supervisorName => $settings) { |
110 | | - foreach ($tenantIds as $tenantId) { |
| 127 | + foreach ($tenants as $tenant) { |
| 128 | + $tenantId = $tenant->id; |
111 | 129 | $tenantSupervisorName = "tenant-{$tenantId}-{$supervisorName}"; |
112 | 130 |
|
113 | 131 | // Copy original settings |
114 | 132 | $tenantSettings = $settings; |
115 | 133 |
|
| 134 | + // Set tenant-specific settings |
| 135 | + $tenantConfig = $tenantsConfigById[$tenantId]; |
| 136 | + foreach (['balance', 'tries', 'timeout', 'minProcesses', 'maxProcesses'] as $key) { |
| 137 | + $value = Arr::get($tenantConfig, "horizon.{$supervisorName}.{$key}", null); |
| 138 | + if ($value !== null) { |
| 139 | + $tenantSettings[$key] = $value; |
| 140 | + } |
| 141 | + } |
| 142 | + |
116 | 143 | // Prepend tenant ID to each queue |
117 | 144 | if (isset($tenantSettings['queue']) && is_array($tenantSettings['queue'])) { |
118 | 145 | $tenantSettings['queue'] = array_map(function ($queue) use ($tenantId) { |
|
0 commit comments