Installation
composer require mondalaci/supervisor-client
(Note: Consider migrating to SupervisorPHP as this package is deprecated.)
Basic Connection For Unix socket (default Supervisor config):
$supervisor = new \SupervisorClient\SupervisorClient('unix:///var/run/supervisor.sock');
For TCP/IP:
$supervisor = new \SupervisorClient\SupervisorClient('your-server.com', 9001);
First Use Case: List All Processes
$processes = $supervisor->supervisor.getAllProcessInfo();
dd($processes);
SupervisorClient class – Review constructor and methods like setAuth(), setTimeout().tests/ – Practical usage patterns for common tasks (e.g., starting/stopping processes).$supervisor->supervisor.startProcess('your_process_name');
$supervisor->supervisor.stopProcess('your_process_name');
$supervisor->supervisor.restartProcess('your_process_name');
$supervisor->supervisor.reloadConfig();
$status = $supervisor->supervisor.getProcessInfo('your_process_name');
$allProcesses = $supervisor->supervisor.getAllProcessInfo();
supervisor.read)$events = $supervisor->supervisor.read(1000); // Timeout in ms
$supervisor->setAuth('admin', 'securepassword');
$supervisor->setTimeout(5000); // 5 seconds
Service Provider Binding
Bind the client in AppServiceProvider for dependency injection:
public function register()
{
$this->app->singleton('supervisor', function ($app) {
$supervisor = new \SupervisorClient\SupervisorClient('unix:///var/run/supervisor.sock');
$supervisor->setAuth(config('supervisor.username'), config('supervisor.password'));
return $supervisor;
});
}
Artisan Commands Create custom commands for CLI process management:
use Illuminate\Console\Command;
use SupervisorClient\SupervisorClient;
class SupervisorCommand extends Command
{
protected $supervisor;
public function __construct(SupervisorClient $supervisor)
{
parent::__construct();
$this->supervisor = $supervisor;
}
public function handle()
{
$this->info('Restarting all processes...');
$this->supervisor->supervisor.restartAllProcesses();
}
}
Event Listeners Trigger actions based on Supervisor events (e.g., log failures):
$events = $supervisor->supervisor.read(1000);
foreach ($events as $event) {
if ($event['eventname'] === 'PROCESS_STATE_EVENT') {
event(new ProcessStateChanged($event['data']));
}
}
Configurable Endpoints
Store connection details in .env:
SUPERVISOR_SOCKET=unix:///var/run/supervisor.sock
SUPERVISOR_USERNAME=admin
SUPERVISOR_PASSWORD=secret
Load in config/supervisor.php:
return [
'socket' => env('SUPERVISOR_SOCKET'),
'auth' => [
'username' => env('SUPERVISOR_USERNAME'),
'password' => env('SUPERVISOR_PASSWORD'),
],
];
Deprecation Warning
Connection Issues
/var/run/supervisor.sock).
chmod 666 /var/run/supervisor.sock
setTimeout() for slow environments.Authentication Failures
[inet_http_server] or [unix_http_server] section in supervisord.conf:
[inet_http_server]
username=admin
password=secret
chmod permissions are correct for the socket file.XML-RPC Quirks
supervisor.getAllProcessInfo).try-catch for SupervisorClientException:
try {
$supervisor->supervisor.invalidMethod();
} catch (\SupervisorClient\Exception\SupervisorClientException $e) {
Log::error($e->getMessage());
}
Process Naming
supervisord.conf.getAllProcessInfo() to debug naming issues.Enable Supervisor Logging
Add to supervisord.conf:
[supervisord]
logfile=/var/log/supervisor/supervisord.log
logfile_maxbytes=1MB
loglevel=debug
Raw XML-RPC Debugging
Use curl to test the API directly:
curl -X POST --data-binary '<?xml version="1.0"?><methodCall><methodName>supervisor.getAllProcessInfo</methodName></methodCall>' http://admin:secret@localhost:9001/RPC2
Check Socket Path
Verify the socket path in supervisord.conf:
[unix_http_server]
file=/var/run/supervisor.sock
Custom RPC Methods Extend the client to wrap Supervisor’s undocumented methods:
class ExtendedSupervisorClient extends \SupervisorClient\SupervisorClient
{
public function getProcessStats($processName)
{
return $this->call('supervisor.getProcessStats', [$processName]);
}
}
Event Dispatcher Integration Create a Laravel event listener for Supervisor events:
$supervisor->supervisor.read(1000); // Poll every second
$events = json_decode($supervisor->supervisor.read(1000), true);
foreach ($events as $event) {
if ($event['eventname'] === 'PROCESS_STATE_EVENT') {
event(new ProcessFailed($event['data']['name']));
}
}
Retry Logic Implement exponential backoff for transient failures:
$attempts = 0;
$maxAttempts = 3;
while ($attempts < $maxAttempts) {
try {
$supervisor->supervisor.restartProcess('critical_job');
break;
} catch (\Exception $e) {
$attempts++;
sleep(2 ** $attempts);
}
}
Note: For new projects, evaluate SupervisorPHP for its modern features (async, Promises, better Laravel integration).
How can I help you explore Laravel packages today?