Skip to content

Commit 4c27b0e

Browse files
authored
Merge pull request #8599 from ProcessMaker/bugfix/FOUR-27846-develop
FOUR-27846 Multitenancy - Set the global container instance when switching
2 parents afd1693 + e71a64d commit 4c27b0e

3 files changed

Lines changed: 19 additions & 53 deletions

File tree

ProcessMaker/Application.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ProcessMaker;
44

55
use Igaster\LaravelTheme\Facades\Theme;
6+
use Illuminate\Container\Container;
67
use Illuminate\Filesystem\Filesystem;
78
use Illuminate\Foundation\Application as IlluminateApplication;
89
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
@@ -12,6 +13,7 @@
1213
use Illuminate\Support\Facades\App;
1314
use Illuminate\Support\Facades\Auth;
1415
use Illuminate\Support\Facades\Config;
16+
use ProcessMaker\Console\Kernel;
1517
use ProcessMaker\Multitenancy\Tenant;
1618
use ProcessMaker\Multitenancy\TenantBootstrapper;
1719

@@ -112,4 +114,11 @@ public function bootstrapWith(array $bootstrappers)
112114

113115
return parent::bootstrapWith($bootstrappers);
114116
}
117+
118+
public function reactivateConsoleApp()
119+
{
120+
Container::setInstance($this);
121+
$this->hasBeenBootstrapped = false;
122+
$this->make(Kernel::class)->bootstrap();
123+
}
115124
}

ProcessMaker/Multitenancy/TenantBootstrapper.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,6 @@ class TenantBootstrapper
2626

2727
private $app = null;
2828

29-
public static $landlordKeysToSave = [
30-
'APP_URL',
31-
'APP_KEY',
32-
'LOG_PATH',
33-
'DB_USERNAME',
34-
'DB_PASSWORD',
35-
'DB_HOSTNAME',
36-
'DB_PORT',
37-
'LANDLORD_DB_DATABASE',
38-
'REDIS_PREFIX',
39-
'CACHE_SETTING_PREFIX',
40-
'SCRIPT_MICROSERVICE_CALLBACK',
41-
'CACHE_PREFIX',
42-
];
43-
4429
public function bootstrap(Application $app)
4530
{
4631
try {
@@ -116,6 +101,14 @@ private function setTenantEnvironmentVariables($tenantData)
116101
$this->set('LOG_PATH', $this->app->storagePath('logs/processmaker.log'));
117102
}
118103

104+
// Used for debugging
105+
public static function log($message)
106+
{
107+
$message = '[TenantBootstrapper] ' . $message;
108+
$today = date('Y-m-d');
109+
file_put_contents(base_path('storage/logs/processmaker-' . $today . '.log'), date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL, FILE_APPEND);
110+
}
111+
119112
private function getOriginalValue($key, $default = '')
120113
{
121114
if (self::$landlordValues === []) {

ProcessMaker/Providers/ProcessMakerServiceProvider.php

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -258,51 +258,19 @@ private static function bootstrapTenantApp(JobProcessing|JobRetryRequested $even
258258
return;
259259
}
260260

261-
// Save the landlord's config values so we can reset them later
262-
if (self::$landlordValues === null) {
263-
foreach (TenantBootstrapper::$landlordKeysToSave as $key) {
264-
self::$landlordValues[$key] = $_SERVER[$key] ?? '';
265-
}
266-
}
267-
268261
// Create a new tenant app instance
269262
$_SERVER['TENANT'] = $tenantId;
270-
$_ENV['TENANT'] = $tenantId;
271263

272264
if (!isset(self::$tenantAppContainers[$tenantId])) {
273-
$tenantApp = require app()->bootstrapPath('app.php');
274-
$tenantApp->instance('landlordValues', self::$landlordValues);
275-
$tenantApp->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();
276-
self::$tenantAppContainers[$tenantId] = $tenantApp;
265+
self::$tenantAppContainers[$tenantId] = require base_path('bootstrap/app.php');
277266
}
267+
self::$tenantAppContainers[$tenantId]->reactivateConsoleApp();
278268

279269
// Change the job's app service container to the tenant app
280270
$event->job->getRedisQueue()->setContainer(self::$tenantAppContainers[$tenantId]);
281271
}
282272
}
283273

284-
private static function resetTenantApp($event): void
285-
{
286-
if (!method_exists($event->job, 'getRedisQueue')) {
287-
// Not a redis job
288-
return;
289-
}
290-
291-
unset($_SERVER['TENANT']);
292-
unset($_ENV['TENANT']);
293-
294-
if (!self::$landlordValues) {
295-
return;
296-
}
297-
298-
// Restore the original values since the tenant boostrapper modified them
299-
foreach (self::$landlordValues as $key => $value) {
300-
$_SERVER[$key] = $value;
301-
$_ENV[$key] = $value;
302-
putenv("$key=$value");
303-
}
304-
}
305-
306274
/**
307275
* Register app-level events.
308276
*/
@@ -316,10 +284,6 @@ protected static function registerEvents(): void
316284
self::bootstrapTenantApp($event);
317285
});
318286

319-
Facades\Event::listen(JobAttempted::class, function (JobAttempted $event) {
320-
self::resetTenantApp($event);
321-
});
322-
323287
// Listen to the events for our core screen
324288
// types and add our javascript
325289
Facades\Event::listen(ScreenBuilderStarting::class, function ($event) {

0 commit comments

Comments
 (0)