改进 扣费

This commit is contained in:
iVampireSP.com 2023-01-17 16:07:55 +08:00
parent 7b77e96018
commit 1001bdd955
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
4 changed files with 45 additions and 16 deletions

View File

@ -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();

View File

@ -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();
// }
// });
}
}

View File

@ -37,8 +37,6 @@ public function __construct(HostModel $host, $type = 'post')
*/
public function handle(): void
{
//
$host = $this->host;
$host->load(['module']);

View 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);
}
}