Skip to content

Commit 2163cda

Browse files
committed
Update Service Proccess Page and Core Services #1
1 parent ef0adb5 commit 2163cda

File tree

17 files changed

+598
-62
lines changed

17 files changed

+598
-62
lines changed

app/Console/Commands/InternalMonitor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function handle()
4646
};
4747
})
4848
->map(function ($mp) {
49-
dump($mp);
49+
// dump($mp);
5050
return [
5151
$mp->id,
5252
$mp->pid,
@@ -58,7 +58,7 @@ public function handle()
5858
})
5959
->values();
6060

61-
dump($result);
61+
// dump($result);
6262

6363
$this->table([
6464
'#ID',

app/Console/Commands/Sandbox.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ class Sandbox extends Command
2020
*
2121
* @var string
2222
*/
23-
protected $description = 'Command description';
23+
protected $description = 'Tests enviroment';
2424

2525
/**
2626
* Execute the console command.
2727
*/
2828
public function handle()
2929
{
30-
// --
30+
dd((new TaskManagerController())->ExecPid("watch ps aux"));
3131
}
3232
}

app/Console/Commands/Supervisor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function handle()
5252
}
5353
}
5454

55-
// Log::info($checkComandProccess);
55+
info($checkComandProccess);
5656

5757
foreach ($checkComandProccess as $check => $comand) {
5858
$checker = $this->CheckComandStatus($check);

app/Filament/Resources/ServiceProccessResource.php

Lines changed: 124 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@
66
use App\Filament\Resources\ServiceProccessResource\Pages\ServiceDetails;
77
use App\Filament\Resources\ServiceProccessResource\RelationManagers;
88
use App\Http\Controllers\TaskManagerController;
9+
use App\Http\Controllers\UtilsController;
10+
use App\Jobs\ComandExecJob;
911
use App\Models\Enviromet;
1012
use App\Models\ServiceProccess;
1113
use Filament\Forms;
1214
use Filament\Forms\Components\Hidden;
1315
use Filament\Forms\Components\Select;
1416
use Filament\Forms\Components\Textarea;
1517
use Filament\Forms\Components\TextInput;
18+
use Filament\Forms\Components\Toggle;
1619
use Filament\Forms\Form;
1720
use Filament\Notifications\Notification;
1821
use Filament\Resources\Resource;
1922
use Filament\Tables;
23+
use Filament\Tables\Actions\ActionGroup;
2024
use Filament\Tables\Columns\TextColumn;
2125
use Filament\Tables\Columns\ToggleColumn;
2226
use Filament\Tables\Table;
@@ -66,10 +70,18 @@ public static function table(Table $table): Table
6670
ToggleColumn::make('status')
6771
->afterStateUpdated(function ($record) {
6872
if ($record->status == 0) {
69-
$exec = (new TaskManagerController($record->command))->kill();
73+
if ($record->pid > 0) {
74+
$exec = (new TaskManagerController($record->command))->kill($record->pid);
75+
} else {
76+
$exec = (new TaskManagerController($record->command))->kill();
77+
}
78+
79+
// dd($exec);
80+
81+
$record->update(['pid' => 0]);
7082
Notification::make()
7183
->info()
72-
->body($exec->message)
84+
->body($exec->message ?? "")
7385
->send();
7486
} else {
7587
Notification::make()
@@ -78,7 +90,9 @@ public static function table(Table $table): Table
7890
->send();
7991
}
8092
}),
81-
TextColumn::make('pid')->label('PID')->icon('heroicon-c-list-bullet'),
93+
TextColumn::make('pid')
94+
->label('PID')
95+
->icon('heroicon-c-list-bullet'),
8296
TextColumn::make('info')
8397
->label('Info.')
8498
->alignCenter()
@@ -111,22 +125,118 @@ public static function table(Table $table): Table
111125
return "danger";
112126
})
113127
->badge(),
114-
TextColumn::make('command')->icon('heroicon-c-command-line')->limit(30),
115-
TextColumn::make('tag')->badge('primary')->icon('heroicon-s-tag'),
116-
TextColumn::make('uuid')->limit(20)->icon('heroicon-c-link')->url(function ($record) {
117-
return "/manager/service-proccesses/{$record->id}/details";
118-
}),
119-
TextColumn::make('interval')->icon('heroicon-s-clock')->suffix(' Sec.'),
120-
TextColumn::make('last_execution')->dateTime('d/m/Y H:i')
128+
TextColumn::make('command')
129+
->icon('heroicon-c-command-line')
130+
->limit(30),
131+
TextColumn::make('tag')
132+
->badge('primary')
133+
->icon('heroicon-s-tag'),
134+
TextColumn::make('uuid')
135+
->limit(20)
136+
->icon('heroicon-c-link')
137+
->url(function ($record) {
138+
return "/manager/service-proccesses/{$record->id}/details";
139+
}),
140+
TextColumn::make('interval')
141+
->icon('heroicon-s-clock')
142+
->suffix(' Sec.'),
143+
TextColumn::make('nex_execution')
144+
->label('Next execution')
145+
->getStateUsing(function ($record) {
146+
$timmer = (strtotime($record->last_execution) + $record->interval) - time();
147+
return ($timmer > 0 ? $timmer : 0);
148+
})
149+
->icon('heroicon-s-clock')
150+
->color(function ($record) {
151+
$timmer = (strtotime($record->last_execution) + $record->interval) - time();
152+
if ($record->status == 0) {
153+
return 'gray';
154+
} else if ($timmer < 0) {
155+
return 'danger';
156+
} else {
157+
return 'success';
158+
}
159+
})
160+
->suffix(' Sec.'),
161+
TextColumn::make('last_execution')
162+
->dateTime('d/m/Y H:i')
121163
])
122164
->filters([
123165
//
124166
])
125167
->actions([
126-
Tables\Actions\EditAction::make(),
127-
Tables\Actions\DeleteAction::make()->before(function ($record) {
128-
$record->logs()->delete();
129-
}),
168+
Tables\Actions\Action::make('force-stop')
169+
->label(function ($record) {
170+
$status = (new TaskManagerController($record->command))->status();
171+
if ($status) {
172+
return 'Kill process';
173+
} else {
174+
return 'Start Proccess';
175+
}
176+
})
177+
->color(function ($record) {
178+
$status = (new TaskManagerController($record->command))->status();
179+
if ($status) {
180+
return 'danger';
181+
} else {
182+
return 'success';
183+
}
184+
})
185+
->icon(function ($record) {
186+
$status = (new TaskManagerController($record->command))->status();
187+
if ($status) {
188+
return 'heroicon-s-stop-circle';
189+
} else {
190+
return 'heroicon-s-play-circle';
191+
}
192+
})
193+
->requiresConfirmation()
194+
->form([
195+
Toggle::make('background')
196+
->label('Execute process in background')
197+
])
198+
->action(function ($record, $data) {
199+
if ($data['background']) {
200+
if ($record->status == 0) {
201+
ComandExecJob::dispatch($record)->onQueue($record->enviroment()->first()->queue()->first()->name);
202+
Notification::make()
203+
->warning()
204+
->body("Process running in background")
205+
->send();
206+
} else {
207+
// ComandExecJob::dispatch($record)->onQueue($record->enviroment()->first()->queue()->first()->name);
208+
(new UtilsController())->ExecuteCommand("pkill if '{$record->command}' &");
209+
Notification::make()
210+
->warning()
211+
->body("Process running in background")
212+
->send();
213+
}
214+
} else {
215+
if ($record->status == 0) {
216+
$prepare = "cd {$record->enviroment()->first()->path} && {$record->command}";
217+
$pid = (new TaskManagerController())->ExecPid($prepare);
218+
$record->update(['status' => 1, 'pid' => $pid->pid, 'last_execution' => now()]);
219+
Notification::make()
220+
->success()
221+
->body("Command started with success")
222+
->send();
223+
} else {
224+
$prepare = "cd {$record->enviroment()->first()->path} && {$record->command}";
225+
$pid = (new TaskManagerController())->ExecPid("pkill -f '{$prepare}'");
226+
$record->update(['status' => 0, 'pid' => 0, 'last_execution' => now()]);
227+
Notification::make()
228+
->success()
229+
->body("Command killed with success")
230+
->send();
231+
}
232+
}
233+
}),
234+
ActionGroup::make([
235+
Tables\Actions\EditAction::make(),
236+
Tables\Actions\DeleteAction::make()->before(function ($record) {
237+
$record->logs()->delete();
238+
}),
239+
])
130240
])
131241
->bulkActions([
132242
Tables\Actions\BulkActionGroup::make([
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use GuzzleHttp\Client;
6+
use Illuminate\Http\Request;
7+
8+
class ApiConnectorController extends Controller
9+
{
10+
public Client $client;
11+
private string $apikey;
12+
public function __construct()
13+
{
14+
$this->apikey = (new TaskManagerController)->apikey;
15+
$this->client = new Client([
16+
'base_uri' => env("APP_URL")
17+
/**
18+
* Insert any Network Config Here
19+
*/
20+
]);
21+
}
22+
23+
/**
24+
* Exec command and get
25+
* PID from process
26+
*/
27+
public function RunCommandAndGetPid(string $command, bool $isArray = false) {
28+
$response = $this->client->post("/api/v1/pid", [
29+
'json' => [
30+
'command' => $command
31+
],
32+
'headers' => [
33+
'api-key' => $this->apikey
34+
]
35+
]);
36+
37+
return json_decode($response->getBody()->getContents(), $isArray);
38+
}
39+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Support\Facades\Log;
7+
8+
class InternalApiV1Controller extends Controller
9+
{
10+
public function pid(Request $reques) {
11+
// Log::warning("Carregando processo...");
12+
$pid = (new TaskManagerController())->ExecPid($reques->command);
13+
Log::info([$pid]);
14+
// Log::info("PID do processo: {$pid}");
15+
return (object)[
16+
'status' => true,
17+
'pid' => $pid->pid,
18+
'buffer' => $pid->buffer
19+
];
20+
}
21+
22+
public function monitor() {
23+
$psaux = (new TaskManagerController())->psaux();
24+
return (object)[
25+
'status' => true,
26+
'process' => $psaux->values()
27+
];
28+
}
29+
}

app/Http/Controllers/TaskManagerController.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,22 @@
88
use Illuminate\Support\Facades\Log;
99
use Illuminate\Support\Str;
1010
use PhpParser\Node\Expr\FuncCall;
11+
use Symfony\Component\Process\Process;
1112

1213
class TaskManagerController extends Controller
1314
{
1415

1516
public string $command;
17+
public mixed $pid;
18+
public string $apikey;
19+
public string $uffer;
1620
public Collection $index;
1721
public UtilsController $utils;
1822

1923
public function __construct(string $command = "")
2024
{
2125
$this->command = $command;
26+
$this->apikey = hash('sha256', env('APP_KEY'));
2227
$this->utils = new UtilsController();
2328
}
2429

@@ -28,14 +33,48 @@ public function psaux()
2833
return $this->index;
2934
}
3035

36+
public function ExecPid($command)
37+
{
38+
// $command = $command . " > /dev/null 2>&1 & echo $!";
39+
$command = $command . " &";
40+
$pid = $this->utils->ExecuteCommand($command);
41+
$this->pid = $pid->pid;
42+
return $pid;
43+
}
44+
3145
public function status()
3246
{
3347
$command = $this->command;
3448
$status = $this->utils->CheckComandStatus($command);
3549
return $status;
3650
}
3751

38-
public function kill() {
39-
return $this->utils->ExecuteCommand("pkill -f '{$this->command}'");
52+
public function kill(int $pid = 0)
53+
{
54+
$kill = false;
55+
if($pid > 0){
56+
$kill = $this->utils->ExecuteCommand("kill {$pid}");
57+
if(posix_kill($pid, 0)){
58+
return $kill;
59+
}
60+
} else if(!empty($this->command)){
61+
$kill = $this->utils->ExecuteCommand("pkill -f '{$this->command}'");
62+
if(posix_kill($pid, 0)){
63+
return $kill;
64+
}
65+
} else if(!empty($this->pid)){
66+
$kill = $this->utils->ExecuteCommand("kill -f {$this->pid}");
67+
if(posix_kill($pid, 0)){
68+
return $kill;
69+
}
70+
}
71+
72+
return $kill;
73+
}
74+
75+
public function execute(string $command)
76+
{
77+
$connector = (new ApiConnectorController())->RunCommandAndGetPid($command);
78+
return $connector;
4079
}
4180
}

0 commit comments

Comments
 (0)