diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 391e5f4..43da504 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -3,6 +3,7 @@ namespace App\Console; use App\Jobs\ClearTasks; +use App\Jobs\DeleteHost; use App\Jobs\Remote; use App\Jobs\HostCost; use App\Jobs\UserSave; @@ -30,6 +31,8 @@ protected function schedule(Schedule $schedule) $schedule->job(new ClearTasks())->weekly(); + $schedule->job(new DeleteHost())->hourly(); + } diff --git a/app/Http/Controllers/User/HostController.php b/app/Http/Controllers/User/HostController.php index 053d526..7201eb1 100644 --- a/app/Http/Controllers/User/HostController.php +++ b/app/Http/Controllers/User/HostController.php @@ -10,7 +10,7 @@ class HostController extends Controller { - public function __invoke(Module $module) + public function index(Module $module) { // $hosts = Host::thisUser($module->id)->with('module', function ($query) { @@ -20,6 +20,18 @@ public function __invoke(Module $module) return $this->success($hosts); } + public function destroy(Host $host) + { + if ($host->user_id == auth()->id()) { + dispatch(new \App\Jobs\Remote\Host($host, 'delete')); + } else { + return $this->error('无权操作'); + } + + return $this->deleted($host); + + } + // /** // * Store a newly created resource in storage. // * diff --git a/app/Jobs/DeleteHost.php b/app/Jobs/DeleteHost.php new file mode 100644 index 0000000..776bebd --- /dev/null +++ b/app/Jobs/DeleteHost.php @@ -0,0 +1,43 @@ +where('suspended_at', '<', now()->subDays(3))->chunk(100, function ($hosts) { + foreach ($hosts as $host) { + dispatch(new \App\Jobs\Remote\Host($host, 'delete')); + } + }); + } +} diff --git a/app/Jobs/Remote/Host.php b/app/Jobs/Remote/Host.php index 6589f34..fde7ddf 100644 --- a/app/Jobs/Remote/Host.php +++ b/app/Jobs/Remote/Host.php @@ -22,7 +22,7 @@ class Host implements ShouldQueue * * @return void */ - public function __construct(HostModel $host, $type = 'post') + public function __construct($host, $type = 'post') { // $this->host = $host; @@ -37,34 +37,46 @@ public function __construct(HostModel $host, $type = 'post') public function handle() { // - $this->host->load(['module']); - - $http = Http::remote($this->host->module->api_token, $this->host->module->url); - + + // dd($this->host); + + // $host = HostModel::find($this->host); + $host = $this->host; + $host->load(['module']); + + $http = Http::remote($host->module->api_token, $host->module->url); + switch ($this->type) { case 'patch': - $response = $http->patch('hosts/' . $this->host->id, $this->host->toArray()); + $response = $http->patch('hosts/' . $host->id, $host->toArray()); break; case 'post': - $response = $http->post('hosts', $this->host->toArray()); + $response = $http->post('hosts', $host->toArray()); break; case 'delete': - $response = $http->delete('hosts/' . $this->host->id); + $response = $http->delete('hosts/' . $host->id); + + return; + // if response code is 404 + // if ($response->successful() || $response->failed()) { + // $host->delete(); + // } + // if success - if ($response->successful()) { - $this->host->delete(); - } + // if ($response->successful()) { + // $host->delete(); + // } break; } if (!$response->successful()) { - $this->host->status = 'error'; + $host->status = 'error'; } - $this->host->save(); + $host->save(); } } diff --git a/routes/api.php b/routes/api.php index 1533efb..e045ef9 100644 --- a/routes/api.php +++ b/routes/api.php @@ -13,7 +13,8 @@ Route::name('api.')->middleware(['api', 'auth:sanctum'])->group(function () { Route::apiResource('users', UserController::class); Route::get('servers', ServerController::class); - Route::get('hosts', HostController::class); + Route::get('hosts', [HostController::class, 'index']); + Route::delete('hosts/{host}', [HostController::class, 'destroy']); Route::apiResource('balances', BalanceController::class)->only(['index', 'store']);