From c333e3bc3804cc5f889edeabbc351f8a8334eb7e Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Tue, 23 Aug 2022 17:36:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E7=A6=BB=E6=89=A3=E8=B4=B9=E9=83=A8?= =?UTF-8?q?=E5=88=86=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=89=A3=E8=B4=B9=E8=AF=B7?= =?UTF-8?q?=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Remote/Host/HostController.php | 38 ++++++++-- app/Jobs/HostCost.php | 44 ++++++------ app/Jobs/Remote/Host.php | 70 ++++++++++++++++++ app/Jobs/UserSave.php | 7 +- app/Models/Host.php | 72 +++++++++++++++++-- routes/api/remote.php | 2 +- 6 files changed, 193 insertions(+), 40 deletions(-) create mode 100644 app/Jobs/Remote/Host.php diff --git a/app/Http/Controllers/Remote/Host/HostController.php b/app/Http/Controllers/Remote/Host/HostController.php index 428c802..3707502 100644 --- a/app/Http/Controllers/Remote/Host/HostController.php +++ b/app/Http/Controllers/Remote/Host/HostController.php @@ -4,6 +4,7 @@ use App\Http\Controllers\Controller; use App\Models\Host; +use Cache; use Illuminate\Http\Request; class HostController extends Controller @@ -33,34 +34,59 @@ public function store(Request $request) /** * Display the specified resource. * - * @param int $id + * @param Host $host * @return \Illuminate\Http\Response */ - public function show($id) + public function show(Host $host) { + + return $this->success($host); // + + // dd($host->cost()); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request - * @param int $id + * @param Host $host * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(Request $request, Host $host) { // + $request->validate([ + 'status' => 'sometimes|in:stopped,running,suspended,error', + 'managed_price' => 'sometimes|numeric|nullable', + + // 如果是立即扣费 + 'cost_once' => 'sometimes|boolean|nullable', + ]); + + // if has cost_once + if ($request->has('cost_once')) { + $host->cost($request->cost_once); + + return $this->updated($request->cost_once); + } + + $host->update($request->all()); + + return $this->updated($host); } /** * Remove the specified resource from storage. * - * @param int $id + * @param Host $host * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy(Host $host) { // + $host->delete(); + + return $this->deleted($host); } } diff --git a/app/Jobs/HostCost.php b/app/Jobs/HostCost.php index 9536a1e..7fd6f0d 100644 --- a/app/Jobs/HostCost.php +++ b/app/Jobs/HostCost.php @@ -35,40 +35,42 @@ public function __construct() */ public function handle() { - $this->cache = Cache::tags(['users']); + // $this->cache = new Cache(); // chunk hosts and load user Host::active()->with('user')->chunk(100, function ($hosts) { foreach ($hosts as $host) { - $this->cache_key = 'user_' . $host->user_id; + $host->cost(); - // if cache has user + // $this->cache_key = 'user_' . $host->user_id; - if ($this->cache->has($this->cache_key)) { - // if user is not instances of Model - $user = $this->cache->get($this->cache_key); + // // if cache has user - if ($user instanceof User) { - $this->user = $user; - } else { - $this->user = $this->cache->put($this->cache_key, $host->user, now()->addDay()); - } - } else { - $this->user = $this->cache->put($this->cache_key, $host->user, now()->addDay()); - } + // if ($this->cache->has($this->cache_key)) { + // // if user is not instances of Model + // $user = $this->cache->get($this->cache_key); - // Log::debug($user); + // if ($user instanceof User) { + // $this->user = $user; + // } else { + // $this->user = $this->cache->put($this->cache_key, $host->user, now()->addDay()); + // } + // } else { + // $this->user = $this->cache->put($this->cache_key, $host->user, now()->addDay()); + // } - if ($host->managed_price) { - $host->price = $host->managed_price; - } + // // Log::debug($user); + + // if ($host->managed_price) { + // $host->price = $host->managed_price; + // } - $this->user->drops -= $host->price; + // $this->user->drops -= $host->price; - // update cache - $this->cache->put($this->cache_key, $this->user, now()->addDay()); + // // update cache + // $this->cache->put($this->cache_key, $this->user, now()->addDay()); } }); } diff --git a/app/Jobs/Remote/Host.php b/app/Jobs/Remote/Host.php new file mode 100644 index 0000000..05c0b1e --- /dev/null +++ b/app/Jobs/Remote/Host.php @@ -0,0 +1,70 @@ +host = $host; + $this->host->load(['module']); + $this->type = $type; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + // + + $http = Http::remote($this->host->module->api_token, $this->host->module->url); + + switch ($this->type) { + case 'put': + $response = $http->put('hosts/' . $this->host->id, $this->host->toArray()); + break; + case 'post': + $response = $http->post('hosts', $this->host->toArray()); + + break; + + case 'delete': + $response = $http->delete('hosts/' . $this->host->id); + + // if success + if ($response->successful()) { + $this->host->delete(); + } + + break; + } + + if (!$response->successful()) { + $this->host->status = 'error'; + } + + $this->host->save(); + } +} diff --git a/app/Jobs/UserSave.php b/app/Jobs/UserSave.php index 6de9048..1596f6a 100644 --- a/app/Jobs/UserSave.php +++ b/app/Jobs/UserSave.php @@ -34,18 +34,15 @@ public function __construct() */ public function handle() { - // - $this->cache = Cache::tags(['users']); - Host::active()->chunk(100, function ($hosts) { foreach ($hosts as $host) { $this->cache_key = 'user_' . $host->user_id; // if cache has user - if ($this->cache->has($this->cache_key)) { + if (Cache::has($this->cache_key)) { // if user is not instances of Model - $user = $this->cache->get($this->cache_key); + $user = Cache::get($this->cache_key); if ($user instanceof User) { $this->await($this->cache_key, function () use ($user, $host) { $user->save(); diff --git a/app/Models/Host.php b/app/Models/Host.php index 4a9a54e..1e01a35 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -2,12 +2,13 @@ namespace App\Models; -use App\Exceptions\CommonException; use App\Models\Module\Module; +use App\Exceptions\CommonException; use App\Models\WorkOrder\WorkOrder; -use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Support\Facades\Cache; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Database\Eloquent\Factories\HasFactory; class Host extends Model { @@ -31,17 +32,20 @@ class Host extends Model // user - public function user() { + public function user() + { return $this->belongsTo(User::class); } // module - public function module() { + public function module() + { return $this->belongsTo(Module::class); } // workOrders - public function workOrders() { + public function workOrders() + { return $this->hasMany(WorkOrder::class); } @@ -52,11 +56,13 @@ public function workOrders() { // scope - public function scopeActive($query) { + public function scopeActive($query) + { return $query->where('status', 'running')->where('price', '!=', 0); } - public function scopeThisUser($query, $module = null) { + public function scopeThisUser($query, $module = null) + { if ($module) { return $query->where('user_id', auth()->id())->where('module_id', $module); } else { @@ -64,6 +70,46 @@ public function scopeThisUser($query, $module = null) { } } + + // cost + public function cost($price = null) + { + + $cache_key = 'user_' . $this->user_id; + + // if cache has user + + if (Cache::has($cache_key)) { + // if user is not instances of Model + $user = Cache::get($cache_key); + + if (!($user instanceof User)) { + $user = Cache::put($cache_key, $this->user, now()->addDay()); + } + } else { + $user = Cache::put($cache_key, $this->user, now()->addDay()); + } + + + // Log::debug($user); + + if ($price !== null) { + $this->managed_price = $price; + } + + if ($this->managed_price) { + $this->price = $this->managed_price; + } + + + $user->drops -= $this->price; + + // update cache + Cache::put($cache_key, $user, now()->addDay()); + + return true; + } + // on create protected static function boot() { @@ -85,5 +131,17 @@ protected static function boot() // add to queue }); + + // when Updated + static::updated(function ($model) { + dispatch(new \App\Jobs\Remote\Host($model, 'put')); + }); + + // when delete + static::deleting(function ($model) { + return false; + + dispatch(new \App\Jobs\Remote\Host($model, 'delete')); + }); } } diff --git a/routes/api/remote.php b/routes/api/remote.php index 9dbf19a..32c4ca7 100644 --- a/routes/api/remote.php +++ b/routes/api/remote.php @@ -7,7 +7,7 @@ Route::apiResource('modules', Remote\ModuleController::class)->only(['index']); Route::apiResource('servers', Remote\ServerController::class); Route::apiResource('hosts', Remote\Host\HostController::class); - Route::patch('hosts/{host}/drops', [Remote\Host\DropController::class, 'update']); + // Route::patch('hosts/{host}', [Remote\Host\DropController::class, 'update']); Route::apiResource('hosts.tasks', Remote\Host\TaskController::class); Route::apiResource('work-orders', Remote\WorkOrder\WorkOrderController::class);