改进 暂停逻辑

This commit is contained in:
iVampireSP.com 2022-08-29 18:59:32 +08:00
parent 3b1e919d4b
commit 7ef301ceff
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
4 changed files with 83 additions and 35 deletions

View File

@ -85,7 +85,7 @@ public function update(Request $request, Host $host)
// 'managed_price' => 'sometimes|numeric|nullable', // 'managed_price' => 'sometimes|numeric|nullable',
// 如果是立即扣费 // 如果是立即扣费
'cost_once' => 'sometimes|boolean|nullable', 'cost_once' => 'sometimes|numeric|nullable',
]); ]);
// if has cost_once // if has cost_once
@ -95,7 +95,12 @@ public function update(Request $request, Host $host)
return $this->updated($request->cost_once); return $this->updated($request->cost_once);
} }
$host->update($request->all()); $update = $request->all();
// module_id 不能被更新
unset($update['module_id']);
unset($update['user_id']);
$host->update($update);
return $this->updated($host); return $this->updated($host);
} }

View File

@ -42,8 +42,8 @@ public function handle()
$http = Http::remote($this->host->module->api_token, $this->host->module->url); $http = Http::remote($this->host->module->api_token, $this->host->module->url);
switch ($this->type) { switch ($this->type) {
case 'put': case 'patch':
$response = $http->put('hosts/' . $this->host->id, $this->host->toArray()); $response = $http->patch('hosts/' . $this->host->id, $this->host->toArray());
break; break;
case 'post': case 'post':
$response = $http->post('hosts', $this->host->toArray()); $response = $http->post('hosts', $this->host->toArray());

View File

@ -27,7 +27,9 @@ class Host extends Model
]; ];
protected $casts = [ protected $casts = [
'configuration' => 'array' // 'configuration' => 'array',
'suspended_at' => 'datetime',
]; ];
@ -102,7 +104,7 @@ public function cost($price = null)
} }
$user->drops -= $this->price; $user->drops -= (int) $this->price;
// update cache // update cache
Cache::put($cache_key, $user, now()->addDay()); Cache::put($cache_key, $user, now()->addDay());
@ -119,40 +121,48 @@ public function cost($price = null)
* @deprecated * @deprecated
*/ */
// on create // on create
// protected static function boot() protected static function boot()
// { {
// parent::boot(); parent::boot();
// // static::creating(function ($model) { // static::creating(function ($model) {
// // // if sanctum // // if sanctum
// // // if (auth('sanctum')->check()) { // // if (auth('sanctum')->check()) {
// // // $model->user_id = auth('sanctum')->id(); // // $model->user_id = auth('sanctum')->id();
// // // } else { // // } else {
// // // // if user_id is null // // // if user_id is null
// // // // check user_id is exists // // // check user_id is exists
// // // throw_if(!User::find($model->user_id), CommonException::class, 'user is not exists'); // // throw_if(!User::find($model->user_id), CommonException::class, 'user is not exists');
// // // } // // }
// // // // set price to 0 // // // set price to 0
// // // $model->price = 0; // // $model->price = 0;
// // // $model->load('module'); // // $model->load('module');
// // // $model->module->load(['provider', 'module']); // // $model->module->load(['provider', 'module']);
// // // add to queue // // add to queue
// // }); // });
// // when Updated static::updating(function ($model) {
// static::updated(function ($model) { if ($model->status == 'suspended') {
// dispatch(new \App\Jobs\Remote\Host($model, 'put')); $model->suspended_at = now();
// }); } else if ($model->status == 'running') {
$model->suspended_at = null;
}
});
// // when delete // when Updated
// static::deleting(function ($model) { static::updated(function ($model) {
// // return false; dispatch(new \App\Jobs\Remote\Host($model, 'patch'));
});
// dispatch(new \App\Jobs\Remote\Host($model, 'delete')); // // when delete
// }); // static::deleting(function ($model) {
// } // // return false;
// // dispatch(new \App\Jobs\Remote\Host($model, 'delete'));
// });
}
} }

View File

@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('hosts', function (Blueprint $table) {
//
$table->timestamp('suspended_at')->nullable()->index()->after('status');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('hosts', function (Blueprint $table) {
//
});
}
};