use App\Jobs\UpdateCryptoAssetPrice;
use App\Models\Asset;
use Illuminate\Console\Command;
class FetchCoinsPrices extends Command
{
protected $signature = 'coins:fetch-price';
protected $description = 'Command description';
protected $shouldQuit = false;
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->info('Fetching coin prices.');
$this->listenForSignals();
while (true) {
if ($this->shouldQuit) {
return false;
}
$this->fetchPrices();
sleep(20);
}
}
public function listenForSignals()
{
pcntl_signal(SIGTERM, function () {
$this->shouldQuit = true;
});
}
protected function fetchPrices()
{
$this->info('Fetching...');
Asset::crypto()->get()->each(function (Asset $asset) {
UpdateCryptoAssetPrice::dispatch($asset);
});
}
}