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