This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
|
||||
namespace App\Bots;
|
||||
|
||||
use App\Interfaces\ScriptContract;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class BashScript implements BotContract
|
||||
class BashScript implements ScriptContract
|
||||
{
|
||||
protected array $config;
|
||||
|
||||
@@ -13,22 +14,20 @@ class BashScript implements BotContract
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function run(): void
|
||||
public function run(): string
|
||||
{
|
||||
if (!empty($this->config['script'])) {
|
||||
// Execute the bash script
|
||||
$script = $this->config['script'];
|
||||
$output = [];
|
||||
$returnVar = null;
|
||||
exec($script, $output, $returnVar);
|
||||
// Execute the bash script
|
||||
$script = $this->config['script'];
|
||||
$output = [];
|
||||
$returnVar = null;
|
||||
exec($script, $output, $returnVar);
|
||||
|
||||
if ($returnVar !== 0) {
|
||||
// Log error if the script failed
|
||||
\Illuminate\Support\Facades\Log::error("Bash script execution failed: " . implode("\n", $output));
|
||||
} else {
|
||||
Log::info("Bash script executed successfully: " . implode("\n", $output));
|
||||
}
|
||||
|
||||
if ($returnVar !== 0) {
|
||||
Log::error("Bash script execution failed", ['script' => $script, 'output' => $output, 'returnVar' => $returnVar]);
|
||||
throw new \RuntimeException("Bash script execution failed with return code {$returnVar}");
|
||||
}
|
||||
return implode("\n", $output);
|
||||
}
|
||||
|
||||
public static function configSchema(): array
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Bots;
|
||||
|
||||
use App\Interfaces\BotContract;
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
|
||||
7
app/Interfaces/BaseContract.php
Normal file
7
app/Interfaces/BaseContract.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace App\Interfaces;
|
||||
|
||||
interface BaseContract
|
||||
{
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Bots;
|
||||
namespace App\Interfaces;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
interface BotContract
|
||||
interface BotContract extends BaseContract
|
||||
{
|
||||
public function run(): JsonResponse;
|
||||
public static function configSchema(): array;
|
||||
9
app/Interfaces/ScriptContract.php
Normal file
9
app/Interfaces/ScriptContract.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\Interfaces;
|
||||
|
||||
interface ScriptContract extends BaseContract
|
||||
{
|
||||
public function run(): string;
|
||||
public static function configSchema(): array;
|
||||
}
|
||||
@@ -2,10 +2,9 @@
|
||||
|
||||
namespace App\Livewire;
|
||||
|
||||
use App\Bots\BotContract;
|
||||
use App\Interfaces\BaseContract;
|
||||
use App\Models\Bot;
|
||||
use Cron\CronExpression;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Illuminate\Validation\Rule;
|
||||
@@ -59,7 +58,7 @@ class CreateEditBot extends Component
|
||||
$class = $baseNamespace . '\\' . $relativeNamespace;
|
||||
|
||||
// Make sure class exists and implements BotContract
|
||||
if (class_exists($class) && in_array(BotContract::class, class_implements($class))) {
|
||||
if (class_exists($class) && in_array(BaseContract::class, class_implements($class))) {
|
||||
$label = method_exists($class, 'label')
|
||||
? $class::label()
|
||||
: class_basename($class);
|
||||
@@ -117,7 +116,7 @@ class CreateEditBot extends Component
|
||||
return;
|
||||
}
|
||||
|
||||
if (!in_array(\App\Bots\BotContract::class, class_implements($this->class))) {
|
||||
if (!in_array(BaseContract::class, class_implements($this->class))) {
|
||||
$this->addError('class', 'The specified class does not exist.');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Interfaces\BaseContract;
|
||||
use App\Models\Bot;
|
||||
use App\Bots\BotContract;
|
||||
use App\Interfaces\BotContract;
|
||||
use App\Interfaces\ScriptContract;
|
||||
use App\Jobs\RunBot;
|
||||
use Cron\CronExpression;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@@ -25,7 +27,7 @@ class BotService
|
||||
try {
|
||||
$instance = app($bot->class, ['config' => $bot->config]);
|
||||
|
||||
if ($instance instanceof BotContract) {
|
||||
if ($instance instanceof BaseContract) {
|
||||
RunBot::dispatch(bot_id: $bot->id, log_id: $log->id);
|
||||
$log->update([
|
||||
'started_at' => now(),
|
||||
|
||||
Reference in New Issue
Block a user