改进 扣费
This commit is contained in:
parent
7b77e96018
commit
1001bdd955
@ -3,7 +3,7 @@
|
||||
namespace App\Console;
|
||||
|
||||
use App\Jobs\Host\DeleteHostJob;
|
||||
use App\Jobs\Host\HostCostJob;
|
||||
use App\Jobs\Host\DispatchHostCostQueueJob;
|
||||
use App\Jobs\Host\ScanAllHostsJob;
|
||||
use App\Jobs\Module\FetchModuleJob;
|
||||
use App\Jobs\Module\SendModuleEarningsJob;
|
||||
@ -31,7 +31,7 @@ protected function schedule(Schedule $schedule): void
|
||||
$schedule->command('sanctum:prune-expired --hours=24')->daily();
|
||||
|
||||
// 扣费
|
||||
$schedule->job(new HostCostJob(now()->minute))->everyMinute()->withoutOverlapping()->onOneServer();
|
||||
$schedule->job(new DispatchHostCostQueueJob(now()->minute))->everyMinute()->withoutOverlapping()->onOneServer();
|
||||
|
||||
// 获取模块暴露的信息(服务器等)
|
||||
$schedule->job(new FetchModuleJob())->withoutOverlapping()->everyMinute();
|
||||
|
@ -2,16 +2,15 @@
|
||||
|
||||
namespace App\Jobs\Host;
|
||||
|
||||
use App\Helpers\Lock;
|
||||
use App\Models\Host;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class HostCostJob implements ShouldQueue
|
||||
class DispatchHostCostQueueJob implements ShouldQueue
|
||||
{
|
||||
use InteractsWithQueue, Queueable, SerializesModels, Lock;
|
||||
use InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public int $minute;
|
||||
|
||||
@ -33,24 +32,16 @@ public function __construct($minute)
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
// chunk hosts and load user
|
||||
$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) {
|
||||
foreach ($hosts as $host) {
|
||||
$host->cost();
|
||||
dispatch(new RealHostCostJob($host, $host->getPrice()));
|
||||
}
|
||||
});
|
||||
|
||||
// HostJob::whereIn('status', ['running', 'stopped'])->with('user')->chunk(1000, function ($hosts) {
|
||||
// foreach ($hosts as $host) {
|
||||
// $host->cost();
|
||||
// }
|
||||
// });
|
||||
}
|
||||
}
|
@ -37,8 +37,6 @@ public function __construct(HostModel $host, $type = 'post')
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
//
|
||||
|
||||
$host = $this->host;
|
||||
$host->load(['module']);
|
||||
|
||||
|
40
app/Jobs/Host/RealHostCostJob.php
Normal file
40
app/Jobs/Host/RealHostCostJob.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Host;
|
||||
|
||||
use App\Models\Host;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class RealHostCostJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public Host $host;
|
||||
public string $price;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param Host $host
|
||||
* @param string $price
|
||||
*/
|
||||
public function __construct(Host $host, string $price)
|
||||
{
|
||||
$this->host = $host;
|
||||
$this->price = $price;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
$this->host->cost($this->price);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user