Lae/app/Jobs/Host/HostCostJob.php

57 lines
1.3 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
use App\Helpers\Lock;
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
2022-12-28 13:17:54 +00:00
class HostCostJob implements ShouldQueue
2022-08-13 08:37:17 +00:00
{
2022-09-08 16:12:02 +00:00
use InteractsWithQueue, Queueable, SerializesModels, Lock;
2022-08-13 08:37:17 +00:00
2023-01-10 13:42:27 +00:00
public int $minute;
2022-08-13 08:37:17 +00:00
/**
* Create a new job instance.
*
* @return void
*/
2022-11-23 02:39:35 +00:00
public function __construct($minute)
2022-08-13 08:37:17 +00:00
{
//
2022-11-23 02:39:35 +00:00
$this->minute = $minute;
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
{
// chunk hosts and load user
2022-11-26 13:52:30 +00:00
$host = new Host();
// if env not local, then use minute_at
if (app()->environment() != 'local') {
$host = $host->where('minute_at', $this->minute);
}
$host->whereIn('status', ['running', 'stopped'])->with('user')->chunk(500, function ($hosts) {
2022-08-13 08:37:17 +00:00
foreach ($hosts as $host) {
$host->cost();
2022-08-13 08:37:17 +00:00
}
});
2022-11-20 12:34:13 +00:00
2022-12-28 13:17:54 +00:00
// HostJob::whereIn('status', ['running', 'stopped'])->with('user')->chunk(1000, function ($hosts) {
2022-11-20 12:34:13 +00:00
// foreach ($hosts as $host) {
// $host->cost();
// }
// });
2022-08-13 08:37:17 +00:00
}
}