Lae/app/Jobs/Host/DispatchHostCostQueueJob.php

56 lines
1.2 KiB
PHP
Raw 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;
2022-08-13 08:37:17 +00:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
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-01-17 08:07:55 +00:00
class DispatchHostCostQueueJob implements ShouldQueue
2022-08-13 08:37:17 +00:00
{
2023-01-17 08:07:55 +00:00
use InteractsWithQueue, Queueable, SerializesModels;
2022-08-13 08:37:17 +00:00
2023-02-12 18:21:09 +00:00
protected int $minute;
2023-02-12 18:27:59 +00:00
2023-02-12 18:21:09 +00:00
protected ?Host $host;
2022-08-13 08:37:17 +00:00
/**
* Create a new job instance.
*
* @return void
*/
2023-02-12 18:21:09 +00:00
public function __construct($minute, Host $host = null)
2022-08-13 08:37:17 +00:00
{
2022-11-23 02:39:35 +00:00
$this->minute = $minute;
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.
*
* @return void
*/
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();
if (app()->environment() != 'local') {
$host = $host->where('minute_at', $this->minute);
}
2022-11-26 13:52:30 +00:00
2023-02-12 18:37:02 +00:00
$host->whereIn('status', ['running', 'stopped'])->whereNull('billing_cycle')->with('user')->chunk(500, function ($hosts) {
2023-02-12 18:21:09 +00:00
foreach ($hosts as $host) {
dispatch(new self($this->minute, $host));
}
});
2022-11-26 13:52:30 +00:00
}
2023-02-12 18:21:09 +00:00
$this->host?->cost($this->host->getPrice());
2022-08-13 08:37:17 +00:00
}
}