66use App \Filament \Resources \ServiceProccessResource \Pages \ServiceDetails ;
77use App \Filament \Resources \ServiceProccessResource \RelationManagers ;
88use App \Http \Controllers \TaskManagerController ;
9+ use App \Http \Controllers \UtilsController ;
10+ use App \Jobs \ComandExecJob ;
911use App \Models \Enviromet ;
1012use App \Models \ServiceProccess ;
1113use Filament \Forms ;
1214use Filament \Forms \Components \Hidden ;
1315use Filament \Forms \Components \Select ;
1416use Filament \Forms \Components \Textarea ;
1517use Filament \Forms \Components \TextInput ;
18+ use Filament \Forms \Components \Toggle ;
1619use Filament \Forms \Form ;
1720use Filament \Notifications \Notification ;
1821use Filament \Resources \Resource ;
1922use Filament \Tables ;
23+ use Filament \Tables \Actions \ActionGroup ;
2024use Filament \Tables \Columns \TextColumn ;
2125use Filament \Tables \Columns \ToggleColumn ;
2226use 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 ([
0 commit comments