Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions ProcessMaker/Managers/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function install(string $repoUrl, ?string $branch = null, ?string $tag =
$this->logRunning('Running plugin install command...', $repoName, $userId);
$this->runCommand($installCommand, $repoName, $userId);

$this->rebuildRouteCache($repoName, $userId);
$this->refreshRuntimeAfterPluginChange($repoName, $userId);

$this->logDone('Plugin installed successfully', $repoName, $userId);
}
Expand Down Expand Up @@ -127,7 +127,7 @@ public function uninstall(string $pluginName, ?int $userId = null): void
$this->logRunning('Removing plugin directory...', $pluginName, $userId);
$this->deleteDirectory($pluginPath, $pluginName, $userId);

$this->rebuildRouteCache($pluginName, $userId);
$this->refreshRuntimeAfterPluginChange($pluginName, $userId);

$this->logDone('Plugin uninstalled successfully', $pluginName, $userId);
}
Expand All @@ -145,18 +145,24 @@ private function runCommand(string $command, string $pluginName, ?int $userId =

if (!$result->successful()) {
if (str_contains($result->output(), 'is not defined')) {
\Log::info("Plugin does not have a {$command} command", ['output' => $result->output()]);
$this->logRunning("Plugin does not have a {$command} command", $pluginName, $userId);
} else {
\Log::info("Plugin {$command} command failed. Got output:\n\n{$result->output()}", ['output' => $result->output()]);
$this->logError("Plugin {$command} command failed. Got output:\n\n{$result->output()}", $pluginName, $userId);
}
} else {
\Log::info("Plugin {$command} command output:\n\n{$result->output()}", ['output' => $result->output()]);
$this->logRunning("Plugin {$command} command output:\n\n{$result->output()}", $pluginName, $userId);
}
}

/**
* Refresh routes and queue workers after plugin code changes.
*/
private function refreshRuntimeAfterPluginChange(string $pluginName, ?int $userId = null): void
{
$this->rebuildRouteCache($pluginName, $userId);
$this->terminateHorizon($pluginName, $userId);
}

/**
* Rebuild the tenant's route cache in a fresh artisan process.
*
Expand All @@ -172,6 +178,15 @@ private function rebuildRouteCache(string $pluginName, ?int $userId = null): voi
$this->reloadCachedRoutes();
}

/**
* Always stop current Horizon workers so a new process picks up plugin code.
*/
private function terminateHorizon(string $pluginName, ?int $userId = null): void
{
$this->logRunning('Restarting queue workers...', $pluginName, $userId);
$this->runCommand('horizon:terminate', $pluginName, $userId);
}

/**
* Environment variables for artisan subprocesses started from a web request.
*/
Expand Down Expand Up @@ -273,7 +288,7 @@ public function installFromZip(string $zipPath, ?int $userId = null): void
$this->logRunning('Running plugin install command...', $repoName, $userId);
$this->runCommand($installCommand, $repoName, $userId);

$this->rebuildRouteCache($repoName, $userId);
$this->refreshRuntimeAfterPluginChange($repoName, $userId);

$this->logDone('Plugin installed successfully', $repoName, $userId);
} finally {
Expand Down Expand Up @@ -311,7 +326,7 @@ public function toggle(string $pluginName, ?int $userId = null): bool
throw new RuntimeException("Failed to disable plugin: {$pluginName}");
}

$this->rebuildRouteCache($pluginName, $userId);
$this->refreshRuntimeAfterPluginChange($pluginName, $userId);

return false;
}
Expand All @@ -325,7 +340,7 @@ public function toggle(string $pluginName, ?int $userId = null): bool
throw new RuntimeException("Failed to enable plugin: {$pluginName}");
}

$this->rebuildRouteCache($enabledName, $userId);
$this->refreshRuntimeAfterPluginChange($enabledName, $userId);

return true;
}
Expand Down Expand Up @@ -775,16 +790,14 @@ protected function logDone(string $message, string $pluginName, ?int $userId = n
*/
protected function log(string $message, string $type, string $pluginName, ?int $userId = null): void
{
if ($type === 'error') {
\Log::error($message);
} else {
\Log::info($message);
}

if ($userId) {
event(new PluginLog($message, $type, $pluginName, $userId));
} else {
if ($type === 'done') {
\Log::info($message);
} elseif ($type === 'error') {
\Log::error($message);
} else {
\Log::info($message);
}
}
}
}
61 changes: 48 additions & 13 deletions tests/Managers/PluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Tests\Managers;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Process;
use ProcessMaker\Events\PluginLog;
use ProcessMaker\Managers\PluginManager;
use ProcessMaker\Models\Plugin;
use RuntimeException;
use Tests\TestCase;

Expand Down Expand Up @@ -62,10 +60,6 @@ public function testInstallClonesRepositoryAndValidatesPlugin()
'*' => Process::result('', exitCode: 0),
]);

// Mock Artisan::all() to return empty array (no plugin install command)
Artisan::shouldReceive('all')
->andReturn([]);

Event::fake([PluginLog::class]);

$manager = new PluginManager();
Expand All @@ -74,6 +68,7 @@ public function testInstallClonesRepositoryAndValidatesPlugin()
Event::assertDispatched(PluginLog::class, function ($event) {
return str_contains($event->message, 'installed successfully');
});
$this->assertProcessRanCommand('horizon:terminate');
}

public function testInstallValidatesComposerJsonNamespace()
Expand Down Expand Up @@ -127,10 +122,6 @@ public function testUninstallRemovesPluginDirectory()
mkdir($this->tempPluginDir, 0755, true);
file_put_contents($this->tempPluginDir . '/test.txt', 'test');

// Mock Artisan::all() to return empty array (no plugin uninstall command)
Artisan::shouldReceive('all')
->andReturn([]);

Event::fake([PluginLog::class]);

Process::fake([
Expand All @@ -144,6 +135,7 @@ public function testUninstallRemovesPluginDirectory()
Event::assertDispatched(PluginLog::class, function ($event) {
return str_contains($event->message, 'uninstalled successfully');
});
$this->assertProcessRanCommand('horizon:terminate');
}

public function testUninstallThrowsExceptionForNonExistentPlugin()
Expand Down Expand Up @@ -266,7 +258,7 @@ public function testArtisanEnvironmentIncludesCurrentTenant()
$this->assertSame(['TENANT' => '7'], $environment);
}

public function testToggleRebuildsRouteCache()
public function testToggleRebuildsRouteCacheAndTerminatesHorizon()
{
$this->setUpTestPluginManager();

Expand All @@ -286,12 +278,55 @@ public function testToggleRebuildsRouteCache()
$this->assertFalse(is_dir($pluginPath));
$this->assertTrue(is_dir($this->pluginsDir . '/_' . $pluginName));

Process::assertRan(function ($process) {
$this->assertProcessRanCommand('route:cache');
$this->assertProcessRanCommand('horizon:terminate');
}

public function testToggleEnableTerminatesHorizon()
{
$this->setUpTestPluginManager();

$pluginName = 'toggle-plugin';
$disabledPath = $this->pluginsDir . '/_' . $pluginName;
$this->tempPluginDir = $this->pluginsDir . '/' . $pluginName;
mkdir($disabledPath, 0755, true);

Process::fake([
'*' => Process::result('', exitCode: 0),
]);

$manager = new PluginManager();
$enabled = $manager->toggle($pluginName);

$this->assertTrue($enabled);
$this->assertTrue(is_dir($this->pluginsDir . '/' . $pluginName));
$this->assertProcessRanCommand('horizon:terminate');
}

public function testRunCommandLogsSuccessfulOutputOnce()
{
Process::fake([
'*' => Process::result("Routes cached successfully.\n", exitCode: 0),
]);
Event::fake([PluginLog::class]);

$method = new \ReflectionMethod(PluginManager::class, 'runCommand');
$method->invoke(new PluginManager(), 'route:cache', 'test-plugin', 1);

Event::assertDispatchedTimes(PluginLog::class, 1);
Event::assertDispatched(PluginLog::class, function (PluginLog $event) {
return str_contains($event->message, 'Plugin route:cache command output');
});
}

private function assertProcessRanCommand(string $artisanCommand): void
{
Process::assertRan(function ($process) use ($artisanCommand) {
$command = is_array($process->command)
? implode(' ', $process->command)
: (string) $process->command;

return str_contains($command, 'route:cache');
return str_contains($command, $artisanCommand);
});
}

Expand Down
Loading