修复 扣费问题

This commit is contained in:
iVampireSP.com 2023-03-08 08:25:52 +08:00
parent d994c13bf9
commit 1830891c62
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
4 changed files with 84 additions and 23 deletions

View File

@ -4,7 +4,8 @@
use App\Jobs\Host\CancelExpiredHostJob;
use App\Jobs\Host\DeleteHostJob;
use App\Jobs\Host\DispatchHostCostQueueJob;
use App\Jobs\Host\DispatchHostCostHourlyJob;
use App\Jobs\Host\DispatchHostCostMonthlyJob;
use App\Jobs\Host\ScanErrorHostsJob;
use App\Jobs\Module\DispatchFetchModuleJob;
use App\Jobs\Module\SendModuleEarningsJob;
@ -31,8 +32,8 @@ protected function schedule(Schedule $schedule): void
$schedule->command('sanctum:prune-expired --hours=24')->daily()->runInBackground()->onOneServer()->name('清理过期的 Token。');
// 扣费
$schedule->job(new DispatchHostCostQueueJob(now(), null, 'hourly'))->everyMinute()->withoutOverlapping()->onOneServer()->name('部署扣费任务 (小时)');
$schedule->job(new DispatchHostCostQueueJob(now(), null, 'monthly'))->hourly()->withoutOverlapping()->onOneServer()->name('部署扣费任务 (月度)');
$schedule->job(new DispatchHostCostHourlyJob(now()->minute, null))->everyMinute()->withoutOverlapping()->onOneServer()->name('部署扣费任务 (小时)');
$schedule->job(new DispatchHostCostMonthlyJob(now()->day, now()->hour, null))->hourly()->withoutOverlapping()->onOneServer()->name('部署扣费任务 (月度)');
$schedule->job(new CancelExpiredHostJob())->hourly()->withoutOverlapping()->onOneServer()->name('部署清理到期主机任务');
// 获取模块暴露的信息(服务器等,检查模块状态)

View File

@ -3,32 +3,29 @@
namespace App\Jobs\Host;
use App\Models\Host;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class DispatchHostCostQueueJob implements ShouldQueue
class DispatchHostCostHourlyJob implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected Carbon $now;
protected int $minute;
protected ?Host $host;
protected string $type;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Carbon $now, Host $host = null, $type = 'hourly')
public function __construct(int $minute, Host $host = null)
{
$this->now = $now;
$this->minute = $minute;
$this->host = $host;
$this->type = $type;
$this->onQueue('host-cost');
}
@ -41,21 +38,16 @@ public function handle(): void
if (! $this->host) {
$host = new Host();
if ($this->type == 'monthly') {
// 月度计费,需要精确到天和小时
$host = $host->where('day_at', $this->now->day);
$host = $host->where('hour_at', $this->now->hour);
$host = $host->where('cancel_at_period_end', false);
} elseif (app()->environment() != 'local') {
if (app()->environment() != 'local') {
$host = $host->where('minute_at', $this->minute);
}
$host->where('billing_cycle', $this->type)->whereIn('status', ['running', 'stopped'])->with(['user', 'module'])->chunk(500, function ($hosts) {
$host->where('billing_cycle', 'hourly')->whereIn('status', ['running', 'stopped'])->with(['user', 'module'])->chunk(500, function ($hosts) {
$hosts->each(function ($host) {
/* @var Host $host */
if ($host->module->isUp()) {
dispatch(new self($this->now, $host, $this->type));
dispatch(new self($this->minute, $host));
}
});
});

View File

@ -0,0 +1,68 @@
<?php
namespace App\Jobs\Host;
use App\Models\Host;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class DispatchHostCostMonthlyJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected Carbon $now;
protected int $day;
protected int $hour;
protected ?Host $host;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(int $day, int $hour, Host $host = null)
{
$this->day = $day;
$this->hour = $hour;
$this->host = $host;
$this->onQueue('host-cost');
}
/**
* Execute the job.
*/
public function handle(): void
{
if (! $this->host) {
$host = new Host();
// 月度计费,需要精确到天和小时
$host = $host->where('day_at', $this->day);
$host = $host->where('hour_at', $this->hour);
$host = $host->where('cancel_at_period_end', false);
$host->where('billing_cycle', 'monthly')->whereIn('status', ['running', 'stopped'])->with(['user', 'module'])->chunk(500, function ($hosts) {
$hosts->each(function ($host) {
/* @var Host $host */
if ($host->module->isUp()) {
dispatch(new self($this->day, $this->hour, $host));
}
});
});
} else {
if (! $this->host->isNextMonthCancel() && ! $this->host->isTrial()) {
$this->host->cost($this->host->getPrice());
}
}
}
}

View File

@ -215,9 +215,9 @@ public function isNextMonthCancel(): bool
public function cost(
string $amount = null, $auto = true, $description = null
): bool {
// if ($this->isTrial() && $this->trial_ends_at->()) {
// return true;
// }
if ($this->isTrial() && ! $this->trial_ends_at->isPast()) {
return true;
}
$this->load('user');
$user = $this->user;