增加 主机删除

This commit is contained in:
iVampireSP.com 2022-09-03 14:01:51 +08:00
parent c854e562cd
commit fdcbea65c5
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
5 changed files with 86 additions and 15 deletions

View File

@ -3,6 +3,7 @@
namespace App\Console; namespace App\Console;
use App\Jobs\ClearTasks; use App\Jobs\ClearTasks;
use App\Jobs\DeleteHost;
use App\Jobs\Remote; use App\Jobs\Remote;
use App\Jobs\HostCost; use App\Jobs\HostCost;
use App\Jobs\UserSave; use App\Jobs\UserSave;
@ -30,6 +31,8 @@ protected function schedule(Schedule $schedule)
$schedule->job(new ClearTasks())->weekly(); $schedule->job(new ClearTasks())->weekly();
$schedule->job(new DeleteHost())->hourly();
} }

View File

@ -10,7 +10,7 @@
class HostController extends Controller class HostController extends Controller
{ {
public function __invoke(Module $module) public function index(Module $module)
{ {
// //
$hosts = Host::thisUser($module->id)->with('module', function ($query) { $hosts = Host::thisUser($module->id)->with('module', function ($query) {
@ -20,6 +20,18 @@ public function __invoke(Module $module)
return $this->success($hosts); 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. // * Store a newly created resource in storage.
// * // *

43
app/Jobs/DeleteHost.php Normal file
View File

@ -0,0 +1,43 @@
<?php
namespace App\Jobs;
use App\Models\Host;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
class DeleteHost implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//
// 查找暂停时间超过3天以上的 host
Host::where('status', 'suspended')->where('suspended_at', '<', now()->subDays(3))->chunk(100, function ($hosts) {
foreach ($hosts as $host) {
dispatch(new \App\Jobs\Remote\Host($host, 'delete'));
}
});
}
}

View File

@ -22,7 +22,7 @@ class Host implements ShouldQueue
* *
* @return void * @return void
*/ */
public function __construct(HostModel $host, $type = 'post') public function __construct($host, $type = 'post')
{ {
// //
$this->host = $host; $this->host = $host;
@ -37,34 +37,46 @@ public function __construct(HostModel $host, $type = 'post')
public function handle() 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) { switch ($this->type) {
case 'patch': case 'patch':
$response = $http->patch('hosts/' . $this->host->id, $this->host->toArray()); $response = $http->patch('hosts/' . $host->id, $host->toArray());
break; break;
case 'post': case 'post':
$response = $http->post('hosts', $this->host->toArray()); $response = $http->post('hosts', $host->toArray());
break; break;
case 'delete': 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 success
if ($response->successful()) { // if ($response->successful()) {
$this->host->delete(); // $host->delete();
} // }
break; break;
} }
if (!$response->successful()) { if (!$response->successful()) {
$this->host->status = 'error'; $host->status = 'error';
} }
$this->host->save(); $host->save();
} }
} }

View File

@ -13,7 +13,8 @@
Route::name('api.')->middleware(['api', 'auth:sanctum'])->group(function () { Route::name('api.')->middleware(['api', 'auth:sanctum'])->group(function () {
Route::apiResource('users', UserController::class); Route::apiResource('users', UserController::class);
Route::get('servers', ServerController::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']); Route::apiResource('balances', BalanceController::class)->only(['index', 'store']);