Skip to content

Commit eb97b07

Browse files
author
Mehrdad Amini
committed
init
0 parents  commit eb97b07

9 files changed

Lines changed: 1066 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# larastack-supervisor

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "ariadata/larastack-supervisor",
3+
"version": "1.0.0",
4+
"type": "package",
5+
"description": "Larastack Supervisor manager via artisan",
6+
"keywords": ["supervisor", "supervisord", "laravel", "artisan","larastack"],
7+
"autoload": {
8+
"psr-4": {
9+
"Ariadata\\LarastackSupervisor\\": "src/"
10+
},
11+
"files": [
12+
"src/helpers.php"
13+
]
14+
},
15+
"extra": {
16+
"laravel": {
17+
"providers": [
18+
"Ariadata\\LarastackSupervisor\\LarastackSupervisorServiceProvider"
19+
],
20+
"aliases": {
21+
"LarastackSupervisor": "Ariadata\\LarastackSupervisor\\Facades\\LarastackSupervisorFacade"
22+
}
23+
}
24+
},
25+
"require": {
26+
"php": "^8.2",
27+
"guzzlehttp/guzzle": "^7.0",
28+
"lstrojny/fxmlrpc": "^0.22",
29+
"supervisorphp/supervisor": "^5.0"
30+
},
31+
"minimum-stability": "dev",
32+
"prefer-stable": true,
33+
"license": "MIT"
34+
}

config/larastack-supervisor.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
return [
4+
'auth' => [
5+
'rpc' => [
6+
'host' => env('SUPERVISOR_RPC_HOST', 'supervisor'), // supervisor
7+
'port' => env('SUPERVISOR_RPC_PORT', 9001),
8+
'user' => env('SUPERVISOR_RPC_USER', 'user'),
9+
'pass' => env('SUPERVISOR_RPC_PASS', 'pass'),
10+
],
11+
]
12+
];
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace Ariadata\LarastackSupervisor\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Ariadata\LarastackSupervisor\LarastackSupervisor;
7+
8+
class SupervisorCommand extends Command
9+
{
10+
// Signature to handle various subcommands and arguments
11+
protected $signature = 'supervisor {action?} {service?}';
12+
protected $description = 'Manage Larastack Supervisor processes';
13+
14+
public function handle()
15+
{
16+
// Retrieve the action and service arguments
17+
$action = $this->argument('action');
18+
$service = $this->argument('service') ?? 'all';
19+
20+
// If no action is provided or help is requested, show help
21+
if (!$action || $action === 'help') {
22+
return $this->showHelp();
23+
}
24+
25+
// Handle the various actions
26+
switch ($action) {
27+
case 'list':
28+
return $this->listProcesses();
29+
30+
case 'status':
31+
return $this->status($service);
32+
33+
case 'start':
34+
return $this->start($service);
35+
36+
case 'stop':
37+
return $this->stop($service);
38+
39+
case 'restart':
40+
return $this->restart($service);
41+
42+
default:
43+
// If the action is unknown, show the help message
44+
$this->error("Unknown action: $action");
45+
return $this->showHelp();
46+
}
47+
}
48+
49+
/**
50+
* Show help information if no arguments are provided or 'help' is requested.
51+
*/
52+
protected function showHelp()
53+
{
54+
$this->info('Usage:');
55+
$this->line(' php artisan supervisor list');
56+
$this->line(' php artisan supervisor status [all|service-name]');
57+
$this->line(' php artisan supervisor start [all|service-name]');
58+
$this->line(' php artisan supervisor stop [all|service-name]');
59+
$this->line(' php artisan supervisor restart [all|service-name]');
60+
$this->line(' php artisan supervisor help');
61+
return 0;
62+
}
63+
64+
/**
65+
* List all processes managed by Supervisor.
66+
*/
67+
protected function listProcesses()
68+
{
69+
$processes = LarastackSupervisor::list();
70+
if (empty($processes)) {
71+
$this->info('No processes found.');
72+
} else {
73+
foreach ($processes as $process) {
74+
$this->line($process);
75+
}
76+
}
77+
}
78+
79+
/**
80+
* Show the status of a specific service or all services.
81+
*/
82+
protected function status(string $service): void
83+
{
84+
$statuses = LarastackSupervisor::status($service);
85+
if (isset($statuses['name'])) {
86+
// Single service status
87+
$this->info("Service: {$statuses['name']} - Status: {$statuses['status']}");
88+
} else {
89+
// Multiple services status
90+
foreach ($statuses as $process) {
91+
$this->info("Service: {$process['name']} - Status: {$process['status']}");
92+
}
93+
}
94+
}
95+
96+
/**
97+
* Start a specific service or all services.
98+
*/
99+
protected function start(string $service): void
100+
{
101+
if (LarastackSupervisor::start($service)) {
102+
$this->info("Supervisor process {$service} has been started.");
103+
} else {
104+
$this->error("Failed to start Supervisor process {$service}.");
105+
}
106+
}
107+
108+
/**
109+
* Stop a specific service or all services.
110+
*/
111+
protected function stop(string $service): void
112+
{
113+
if (LarastackSupervisor::stop($service)) {
114+
$this->info("Supervisor process {$service} has been stopped.");
115+
} else {
116+
$this->error("Failed to stop Supervisor process {$service}.");
117+
}
118+
}
119+
120+
/**
121+
* Restart a specific service or all services.
122+
*/
123+
protected function restart(string $service): void
124+
{
125+
if (LarastackSupervisor::restart($service)) {
126+
$this->info("Supervisor process {$service} has been restarted.");
127+
} else {
128+
$this->error("Failed to restart Supervisor process {$service}.");
129+
}
130+
}
131+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Ariadata\LarastackSupervisor\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
class LarastackSupervisorFacade extends Facade
7+
{
8+
protected static function getFacadeAccessor(): string
9+
{
10+
return 'larastack-supervisor';
11+
}
12+
}

src/LarastackSupervisor.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace Ariadata\LarastackSupervisor;
4+
5+
use Supervisor\Supervisor;
6+
use fXmlRpc\Client;
7+
use fXmlRpc\Transport\PsrTransport;
8+
use GuzzleHttp\Psr7\HttpFactory;
9+
use GuzzleHttp\Client as GuzzleClient;
10+
11+
class LarastackSupervisor
12+
{
13+
protected static function supervisorClient(): Supervisor
14+
{
15+
$client = new Client(
16+
'http://' . config('larastack-supervisor.auth.rpc.host') . ':' . config('larastack-supervisor.auth.rpc.port') . '/RPC2',
17+
new PsrTransport(
18+
new HttpFactory(),
19+
new GuzzleClient([
20+
'auth' => [
21+
config('larastack-supervisor.auth.rpc.user'),
22+
config('larastack-supervisor.auth.rpc.pass'),
23+
],
24+
])
25+
)
26+
);
27+
return new Supervisor($client);
28+
}
29+
30+
public static function list(): array
31+
{
32+
$res = self::supervisorClient()->getAllProcessInfo();
33+
$processes = [];
34+
foreach($res as $process) {
35+
$processes[] = $process['name'];
36+
}
37+
return $processes;
38+
}
39+
public static function start(string $service = null,bool $wait = true): bool
40+
{
41+
if ($service == 'all' || $service == null) {
42+
$allWithStatuses = self::status();
43+
foreach($allWithStatuses as $process) {
44+
if($process['status'] != 'RUNNING') {
45+
try {
46+
self::supervisorClient()->startProcess($process['name'], $wait);
47+
} catch (\Exception $e) {
48+
return false;
49+
}
50+
}
51+
}
52+
return true;
53+
}
54+
try {
55+
$status = self::status($service);
56+
if($status['status'] != 'RUNNING') {
57+
return self::supervisorClient()->startProcess($service, $wait);
58+
}
59+
return true;
60+
} catch (\Exception $e) {
61+
return false;
62+
}
63+
}
64+
65+
public static function stop(string $service = null,bool $wait = true): bool
66+
{
67+
if ($service == 'all' || $service == null) {
68+
$allWithStatuses = self::status();
69+
foreach($allWithStatuses as $process) {
70+
if($process['status'] == 'RUNNING') {
71+
try {
72+
self::supervisorClient()->stopProcess($process['name'], $wait);
73+
} catch (\Exception $e) {
74+
return false;
75+
}
76+
}
77+
}
78+
return true;
79+
}
80+
try {
81+
$status = self::status($service);
82+
if($status['status'] == 'RUNNING') {
83+
return self::supervisorClient()->stopProcess($service, $wait);
84+
}
85+
return true;
86+
} catch (\Exception $e) {
87+
return false;
88+
}
89+
}
90+
91+
public static function restart(string $service = null,bool $wait = true): bool
92+
{
93+
try {
94+
self::stop($service, $wait);
95+
return self::start($service, $wait);
96+
} catch (\Exception $e) {
97+
return false;
98+
}
99+
100+
}
101+
102+
public static function status(string $service = null): array
103+
{
104+
if($service == 'all' || $service == null) {
105+
$res = self::supervisorClient()->getAllProcessInfo();
106+
$processes = [];
107+
foreach($res as $process) {
108+
$processes[] = ['name' => $process['name'], 'status' => $process['statename']];
109+
}
110+
return $processes;
111+
}
112+
$res = self::supervisorClient()->getProcessInfo($service);
113+
return ['name' => $res['name'], 'status' => $res['statename']];
114+
}
115+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Ariadata\LarastackSupervisor;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class LarastackSupervisorServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Register services.
11+
*/
12+
public function register(): void
13+
{
14+
$this->mergeConfigFrom(
15+
dirname(__DIR__) . '/config/larastack-supervisor.php',
16+
'larastack-supervisor'
17+
);
18+
$this->app->alias('larastack-supervisor', LarastackSupervisor::class);
19+
$this->app->singleton('larastack-supervisor', function () {
20+
return new LarastackSupervisor();
21+
});
22+
$commands = array_map(fn($command) => 'Ariadata\LarastackSupervisor\Console\Commands\\' . basename($command, '.php'), glob(__DIR__ . '/Console/Commands/*.php'));
23+
$this->commands($commands);
24+
}
25+
26+
/**
27+
* Bootstrap services.
28+
*/
29+
public function boot(): void
30+
{
31+
$this->publishes([
32+
dirname(__DIR__) . '/config/larastack-supervisor.php' => config_path('larastack-supervisor.php'),
33+
]);
34+
}
35+
}

0 commit comments

Comments
 (0)