修复 扣费模式

This commit is contained in:
iVampireSP.com 2022-11-23 02:39:35 +00:00
parent a6c5e7e89a
commit 8885c2e64c
4 changed files with 53 additions and 6 deletions

View File

@ -29,7 +29,7 @@ protected function schedule(Schedule $schedule)
$schedule->command('sanctum:prune-expired --hours=24')->daily();
// 扣费
$schedule->job(new HostCost(now()->hour))->hourly()->withoutOverlapping()->onOneServer();
$schedule->job(new HostCost(now()->minute))->everyMinute()->withoutOverlapping()->onOneServer();
// 获取模块暴露的信息(服务器等)
$schedule->job(new FetchModule())->withoutOverlapping()->everyMinute();

View File

@ -13,17 +13,17 @@ class HostCost implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels, Lock;
public $hour, $cache, $user;
public $minute, $cache, $user;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($hour)
public function __construct($minute)
{
//
$this->hour = $hour;
$this->minute = $minute;
}
/**
@ -34,7 +34,7 @@ public function __construct($hour)
public function handle()
{
// chunk hosts and load user
Host::where('hour_at', $this->hour)->whereIn('status', ['running', 'stopped'])->with('user')->chunk(1000, function ($hosts) {
Host::where('minute_at', $this->minute)->whereIn('status', ['running', 'stopped'])->with('user')->chunk(500, function ($hosts) {
foreach ($hosts as $host) {
$host->cost();
}

View File

@ -201,7 +201,10 @@ public function safeDelete(): bool
{
// 如果创建时间大于大于 1 小时
if ($this->created_at->diffInHours(now()) > 1) {
$this->cost();
// 如果当前时间比扣费时间小,则说明没有扣费。执行扣费。
if (now()->minute < $this->minute_at) {
$this->cost();
}
}
dispatch(new \App\Jobs\Module\Host($this, 'delete'));

View File

@ -0,0 +1,44 @@
<?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->tinyInteger('minute_at')->index()->nullable()->after('hour_at');
});
echo PHP_EOL . '将开始刷新主机的分钟数...';
Host::chunk(100, function ($hosts) {
foreach ($hosts as $host) {
echo '刷新: ' . $host->id . PHP_EOL;
$host->minute_at = $host->created_at->minute;
$host->save();
}
});
echo ' 完成!' . PHP_EOL;
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('hosts', function (Blueprint $table) {
//
});
}
};