2022-08-13 08:37:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
use App\Helpers\Lock;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\User\Host;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
2022-08-15 13:20:42 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-08-13 08:37:17 +00:00
|
|
|
|
|
|
|
class HostCost implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Lock;
|
|
|
|
|
|
|
|
public $cache_key, $cache, $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2022-08-15 13:20:42 +00:00
|
|
|
$this->cache = Cache::tags(['users']);
|
|
|
|
|
2022-08-13 08:37:17 +00:00
|
|
|
// chunk hosts and load user
|
|
|
|
Host::active()->with('user')->chunk(100, function ($hosts) {
|
|
|
|
foreach ($hosts as $host) {
|
|
|
|
|
|
|
|
$this->cache_key = 'user_' . $host->user_id;
|
|
|
|
|
|
|
|
// if cache has user
|
|
|
|
|
|
|
|
if ($this->cache->has($this->cache_key)) {
|
|
|
|
// if user is not instances of Model
|
|
|
|
$user = $this->cache->get($this->cache_key);
|
2022-08-15 13:20:42 +00:00
|
|
|
|
|
|
|
if ($user instanceof User) {
|
2022-08-13 08:37:17 +00:00
|
|
|
$this->user = $user;
|
2022-08-15 13:20:42 +00:00
|
|
|
} else {
|
|
|
|
$this->user = $this->cache->put($this->cache_key, $host->user, now()->addDay());
|
2022-08-13 08:37:17 +00:00
|
|
|
}
|
2022-08-15 13:20:42 +00:00
|
|
|
} else {
|
|
|
|
$this->user = $this->cache->put($this->cache_key, $host->user, now()->addDay());
|
2022-08-13 08:37:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 13:20:42 +00:00
|
|
|
// Log::debug($user);
|
|
|
|
|
2022-08-15 11:10:51 +00:00
|
|
|
if ($host->managed_price) {
|
|
|
|
$host->price = $host->managed_price;
|
|
|
|
}
|
|
|
|
|
2022-08-15 13:20:42 +00:00
|
|
|
|
2022-08-13 08:37:17 +00:00
|
|
|
$this->user->drops -= $host->price;
|
|
|
|
|
|
|
|
// update cache
|
|
|
|
$this->cache->put($this->cache_key, $this->user, now()->addDay());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|