Lae/app/Jobs/Host/DispatchHostCostMonthlyJob.php

69 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2022-08-13 08:37:17 +00:00
<?php
2023-01-13 14:11:56 +00:00
namespace App\Jobs\Host;
2022-08-13 08:37:17 +00:00
2022-08-16 10:44:16 +00:00
use App\Models\Host;
2023-03-07 16:45:29 +00:00
use Carbon\Carbon;
2022-08-13 08:37:17 +00:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
2023-03-08 00:25:52 +00:00
use Illuminate\Foundation\Bus\Dispatchable;
2022-08-16 10:44:16 +00:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2022-08-13 08:37:17 +00:00
2023-03-08 00:25:52 +00:00
class DispatchHostCostMonthlyJob implements ShouldQueue
2022-08-13 08:37:17 +00:00
{
2023-03-08 00:25:52 +00:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2022-08-13 08:37:17 +00:00
2023-03-07 16:45:29 +00:00
protected Carbon $now;
2023-02-12 18:27:59 +00:00
2023-03-08 00:25:52 +00:00
protected int $day;
protected int $hour;
2022-08-13 08:37:17 +00:00
2023-03-08 00:25:52 +00:00
protected ?Host $host;
2023-03-07 16:45:29 +00:00
2022-08-13 08:37:17 +00:00
/**
* Create a new job instance.
*
* @return void
*/
2023-03-08 00:25:52 +00:00
public function __construct(int $day, int $hour, Host $host = null)
2022-08-13 08:37:17 +00:00
{
2023-03-08 00:25:52 +00:00
$this->day = $day;
$this->hour = $hour;
2023-02-12 18:21:09 +00:00
$this->host = $host;
$this->onQueue('host-cost');
2022-08-13 08:37:17 +00:00
}
/**
* Execute the job.
*/
2022-12-11 12:59:17 +00:00
public function handle(): void
2022-08-13 08:37:17 +00:00
{
2023-02-12 19:12:26 +00:00
if (! $this->host) {
2023-02-12 18:21:09 +00:00
$host = new Host();
2023-03-08 00:25:52 +00:00
// 月度计费,需要精确到天和小时
$host = $host->where('day_at', $this->day);
$host = $host->where('hour_at', $this->hour);
$host = $host->where('cancel_at_period_end', false);
2023-03-07 16:45:29 +00:00
2023-03-08 00:25:52 +00:00
$host->where('billing_cycle', 'monthly')->whereIn('status', ['running', 'stopped'])->with(['user', 'module'])->chunk(500, function ($hosts) {
2023-03-02 11:33:48 +00:00
$hosts->each(function ($host) {
2023-03-07 16:45:29 +00:00
/* @var Host $host */
2023-03-02 11:33:48 +00:00
if ($host->module->isUp()) {
2023-03-08 00:25:52 +00:00
dispatch(new self($this->day, $this->hour, $host));
2023-03-02 11:33:48 +00:00
}
});
2023-02-12 18:21:09 +00:00
});
2023-03-02 11:33:48 +00:00
} else {
2023-03-07 16:53:59 +00:00
if (! $this->host->isNextMonthCancel() && ! $this->host->isTrial()) {
2023-03-07 16:45:29 +00:00
$this->host->cost($this->host->getPrice());
}
2022-11-26 13:52:30 +00:00
}
2022-08-13 08:37:17 +00:00
}
}