改进 暂停逻辑

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',
// 如果是立即扣费
'cost_once' => 'sometimes|boolean|nullable',
'cost_once' => 'sometimes|numeric|nullable',
]);
// if has cost_once
@ -95,7 +95,12 @@ public function update(Request $request, Host $host)
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);
}

View File

@ -42,8 +42,8 @@ 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());
case 'patch':
$response = $http->patch('hosts/' . $this->host->id, $this->host->toArray());
break;
case 'post':
$response = $http->post('hosts', $this->host->toArray());

View File

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