diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 98617af..5d432f7 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -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(); diff --git a/app/Jobs/HostCost.php b/app/Jobs/HostCost.php index f22a32a..acbf6fe 100644 --- a/app/Jobs/HostCost.php +++ b/app/Jobs/HostCost.php @@ -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(); } diff --git a/app/Models/Host.php b/app/Models/Host.php index f6faef0..a0e08b6 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -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')); diff --git a/database/migrations/2022_11_23_103355_add_minute_at_to_hosts.php b/database/migrations/2022_11_23_103355_add_minute_at_to_hosts.php new file mode 100644 index 0000000..bcda233 --- /dev/null +++ b/database/migrations/2022_11_23_103355_add_minute_at_to_hosts.php @@ -0,0 +1,44 @@ +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) { + // + }); + } +};