Skip to content

Commit 4a02e15

Browse files
committed
Fix bugs and create command schedule provider
1 parent 38e1673 commit 4a02e15

File tree

9 files changed

+230
-16
lines changed

9 files changed

+230
-16
lines changed

app/Console/Commands/Supervisor.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ private function ExecuteCommand(string $command)
118118
private function CheckComandStatus(string $comand)
119119
{
120120
$process = $this->ExecuteCommand('ps aux');
121-
// dd($process);
122-
// dd(strpos($process->buffer, $comand));
123121
return strpos($process->buffer, $comand);
124122
}
125123

app/Console/Commands/TaskManager.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Jobs\ComandExecJob;
66
use App\Models\User;
77
use Illuminate\Console\Command;
8+
use Illuminate\Support\Facades\Cache;
89
use Symfony\Component\Process\Process;
910

1011
class TaskManager extends Command
@@ -37,6 +38,15 @@ public function handle()
3738

3839
while (true) {
3940
// Process::fromShellCommandline("clear");
41+
$tasklist = Cache::get('task-manager-provider');
42+
43+
if(!isset($tasklist)){
44+
$output = [];
45+
$command = "ps aux | grep -v grep";
46+
exec($command, $output);
47+
Cache::put('task-manager-provider', $output, 2);
48+
}
49+
4050
$this->info('| Checking users');
4151
foreach ($users as $user) {
4252
$this->warn("|- {$user->name}");

app/Filament/Resources/ServiceProccessResource.php

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Filament\Resources\ServiceProccessResource\Pages;
66
use App\Filament\Resources\ServiceProccessResource\Pages\ServiceDetails;
77
use App\Filament\Resources\ServiceProccessResource\RelationManagers;
8+
use App\Http\Controllers\TaskManagerController;
89
use App\Models\Enviromet;
910
use App\Models\ServiceProccess;
1011
use Filament\Forms;
@@ -13,6 +14,7 @@
1314
use Filament\Forms\Components\Textarea;
1415
use Filament\Forms\Components\TextInput;
1516
use Filament\Forms\Form;
17+
use Filament\Notifications\Notification;
1618
use Filament\Resources\Resource;
1719
use Filament\Tables;
1820
use Filament\Tables\Columns\TextColumn;
@@ -34,21 +36,20 @@ class ServiceProccessResource extends Resource
3436
public static function form(Form $form): Form
3537
{
3638
$uuid = (string)Str::uuid();
37-
39+
3840
return $form
3941
->schema([
4042
TextInput::make('command')->columnSpanFull()->prefixIcon('heroicon-c-command-line'),
41-
Select::make('env')->options(function(){
42-
if(auth()->user()->access !== 2){
43+
Select::make('env')->options(function () {
44+
if (auth()->user()->access !== 2) {
4345
return Enviromet::all()->pluck('name', 'id');
44-
4546
} else {
4647
return Enviromet::where('user', auth()->user()->id)->get()->pluck('name', 'id');
4748
}
4849
})->prefixIcon('heroicon-c-cog'),
4950
TextInput::make('interval')->numeric()->default(60)->prefixIcon('heroicon-s-clock'),
5051
Select::make('loggable')->options(['no', 'yes'])->default(1)->prefixIcon('heroicon-m-bars-3-center-left'),
51-
TextInput::make('tag')->afterStateUpdated(function($state){
52+
TextInput::make('tag')->afterStateUpdated(function ($state) {
5253
return Str::slug($state);
5354
})->prefixIcon('heroicon-s-tag')->columnSpanFull()->default(Str::random(5)),
5455
Hidden::make('uuid')->default($uuid),
@@ -60,12 +61,59 @@ public static function form(Form $form): Form
6061
public static function table(Table $table): Table
6162
{
6263
return $table
64+
->poll('5s')
6365
->columns([
64-
ToggleColumn::make('status'),
66+
ToggleColumn::make('status')
67+
->afterStateUpdated(function ($record) {
68+
if ($record->status == 0) {
69+
$exec = (new TaskManagerController($record->command))->kill();
70+
Notification::make()
71+
->info()
72+
->body('kill process')
73+
->send();
74+
} else {
75+
Notification::make()
76+
->info()
77+
->body('start process')
78+
->send();
79+
}
80+
}),
6581
TextColumn::make('pid')->label('PID')->icon('heroicon-c-list-bullet'),
82+
TextColumn::make('info')
83+
->label('Info.')
84+
->alignCenter()
85+
->getStateUsing(function ($record) {
86+
$status = (new TaskManagerController($record->command))->status();
87+
if ($status) {
88+
if ($record->status == 0) {
89+
return "KILLING";
90+
} else {
91+
return "RUNNING";
92+
}
93+
} else if ($record->status == 1) {
94+
return "PEDDING";
95+
}
96+
97+
return "STOPED";
98+
})
99+
->color(function ($record) {
100+
$status = (new TaskManagerController($record->command))->status();
101+
if ($status) {
102+
if ($record->status == 0) {
103+
return "gray";
104+
} else {
105+
return "success";
106+
}
107+
} else if ($record->status == 1) {
108+
return "warning";
109+
}
110+
111+
return "danger";
112+
})
113+
->badge(),
66114
TextColumn::make('command')->icon('heroicon-c-command-line')->limit(30),
67115
TextColumn::make('tag')->badge('primary')->icon('heroicon-s-tag'),
68-
TextColumn::make('uuid')->limit(20)->icon('heroicon-c-link')->url(function($record){
116+
TextColumn::make('uuid')->limit(20)->icon('heroicon-c-link')->url(function ($record) {
69117
return "/manager/service-proccesses/{$record->id}/details";
70118
}),
71119
TextColumn::make('interval')->icon('heroicon-s-clock')->suffix(' Sec.'),

app/Filament/Resources/ServiceProccessResource/Pages/ServiceDetails.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ServiceDetails extends Page
3030
'fullname' => "PHP: Hypertext Preprocessor",
3131
'image' => "/icons/php.svg",
3232
'isDefault' => false,
33-
'commands' => ['php']
33+
'commands' => ['php', 'php-fpm']
3434
],
3535
'python' => [
3636
'name' => "Python",
@@ -90,6 +90,13 @@ class ServiceDetails extends Page
9090
'isDefault' => false,
9191
'commands' => ['docker']
9292
],
93+
'curl' => [
94+
'name' => "curl",
95+
'fullname' => "cURL",
96+
'image' => "/icons/curl.svg",
97+
'isDefault' => false,
98+
'commands' => ['curl']
99+
],
93100
];
94101

95102
private static function determineSoftware($command) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\ServiceProccess;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Support\Collection;
8+
use Illuminate\Support\Facades\Log;
9+
use Illuminate\Support\Str;
10+
use PhpParser\Node\Expr\FuncCall;
11+
12+
class TaskManagerController extends Controller
13+
{
14+
15+
public string $command;
16+
public Collection $index;
17+
public UtilsController $utils;
18+
19+
public function __construct(string $command)
20+
{
21+
$this->command = $command;
22+
$this->utils = new UtilsController();
23+
}
24+
25+
public function psaux()
26+
{
27+
$this->index = $this->utils->monitor();
28+
return $this->index;
29+
}
30+
31+
public function status()
32+
{
33+
$command = $this->command;
34+
$status = $this->utils->CheckComandStatus($command);
35+
Log::info(["command" => $command, "status" => $status]);
36+
return $status;
37+
// dd($status);
38+
// return $this
39+
// ->psaux()
40+
// ->filter(function ($mp) use ($command) {
41+
// return $;
42+
// })
43+
// ->first() ?? false;
44+
}
45+
46+
public function kill() {
47+
return $this->utils->ExecuteCommand("pkill -f '{$this->command}'");
48+
}
49+
}

app/Http/Controllers/UtilsController.php

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
namespace App\Http\Controllers;
44

55
use Illuminate\Http\Request;
6+
use Illuminate\Support\Facades\Cache;
67
use Illuminate\Support\Facades\File;
78
use Madnest\Madzipper\Madzipper;
9+
use Symfony\Component\Process\Process;
810

911
class UtilsController extends Controller
1012
{
1113

14+
public object $proccessInfo;
15+
1216
public static function MakeZipFile($zipFilename)
1317
{
1418

@@ -47,8 +51,6 @@ public static function addresingFilestoZip(): array
4751
return $addresingFiles;
4852
}
4953

50-
51-
5254
public static function getFilesToZip(): array
5355
{
5456
$filesToZip = [];
@@ -83,4 +85,95 @@ public static function getFilesToZip(): array
8385
public static function nameDir($path) {
8486
return array_reverse(explode("/", $path))[0];
8587
}
88+
89+
private function convertTimeToSeconds($time)
90+
{
91+
$parts = explode(':', $time);
92+
$seconds = 0;
93+
if (count($parts) == 3) {
94+
$seconds = ($parts[0] * 3600) + ($parts[1] * 60) + $parts[2];
95+
} elseif (count($parts) == 2) {
96+
$seconds = ($parts[0] * 60) + $parts[1];
97+
}
98+
return $seconds;
99+
}
100+
101+
public function monitor()
102+
{
103+
104+
$processes = array();
105+
$output = Cache::get('task-manager-provider');
106+
107+
if(isset($output)){
108+
foreach ($output as $key => $line) {
109+
$columns = preg_split('/\s+/', $line);
110+
if (count($columns) >= 11) {
111+
$processes[] = (object)[
112+
'id' => $key,
113+
'user' => $columns[0],
114+
'pid' => $columns[1],
115+
'cpu' => $columns[2],
116+
'mem' => $columns[3],
117+
'vsz' => $columns[4],
118+
'rss' => $columns[5],
119+
'tt' => $columns[6],
120+
'stat' => $columns[7],
121+
'started' => $columns[8],
122+
'software' => $columns[10],
123+
'command' => implode(' ', array_slice($columns, 10)),
124+
'time' => $this->convertTimeToSeconds($columns[9]),
125+
];
126+
}
127+
}
128+
129+
unset($processes[0]);
130+
131+
return collect($processes);
132+
} else {
133+
return collect([]);
134+
}
135+
}
136+
137+
138+
139+
public function CheckComandStatus(string $comand)
140+
{
141+
return $this->monitor()->filter(function($mp) use ($comand){
142+
return strpos($mp->command, $comand);
143+
})->first() ?? false;
144+
}
145+
146+
public function ExecuteCommand(string $command)
147+
{
148+
$process = Process::fromShellCommandline($command);
149+
$process->start(function ($type, $buffer) {
150+
if (Process::ERR === $type) {
151+
$this->proccessInfo = (object)[
152+
'status' => false,
153+
'message' => "| ❌ ERROR: um erro aconteceu ao executar o processo",
154+
'buffer' => $buffer
155+
];
156+
} else {
157+
$this->proccessInfo = (object) [
158+
'status' => true,
159+
'message' => "| ✅ Comando executado com sucesso!",
160+
'buffer' => $buffer
161+
];
162+
}
163+
});
164+
165+
// Aguarda até que o processo seja iniciado
166+
while ($process->isRunning()) {
167+
sleep(1);
168+
}
169+
170+
if (isset($this->proccessInfo)) {
171+
return $this->proccessInfo;
172+
}
173+
174+
return (object) [
175+
'status' => false,
176+
'message' => "| Processo retornou null ou vazio"
177+
];
178+
}
86179
}

app/Models/ServiceProccess.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Models;
44

5+
use App\Http\Controllers\TaskManagerController;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Model;
78

@@ -18,4 +19,8 @@ public function enviroment() {
1819
public function logs() {
1920
return $this->hasMany(ServiceLogs::class, 'service', 'id');
2021
}
22+
23+
public function info(string $command) {
24+
return (new TaskManagerController($command));
25+
}
2126
}

public/icons/curl.svg

Lines changed: 4 additions & 1 deletion
Loading

resources/views/filament/resources/service-proccess-resource/pages/service-details.blade.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<x-filament-panels::page>
22
<script src="{{ asset('/js/core/core.js') }}"></script>
3-
<div style="display: flex; justify-content: start;">
4-
<x-avatar xl rounded="rounded-[1.25rem]" :src="asset($commandSource?->image)" style="width: 20vw; margin-right: 30px;"
5-
border="none" />
3+
<div style="display: flex; justify-content:space-between;">
4+
{{-- <x-avatar xl rounded="rounded-[1.25rem] w-50" :src="asset($commandSource?->image)" style="width: 20vw; margin-right: 30px;"
5+
border="none" /> --}}
6+
<img src="{{asset($commandSource?->image)}}" style=" width: 230px; margin-right: 20px;" class="hide-in-smarphone shrink-0 inline-flex items-center justify-center overflow-hidden border-secondary-200 dark:border-secondary-500 border-0 rounded-[1.25rem]" />
67
<div style="display: flex; width: 100%; position: relative; flex-wrap: wrap;">
78
<div
89
style="display: flex; justify-content: space-between; width: 100%; border: dashed 2px; height: 40px; padding: 5px; border-radius: 5px; font-size: 20px; margin-bottom: 5px; position: relative;">

0 commit comments

Comments
 (0)