-
app/Jobs/UpdateCryptoAssetPrice.php
Open in GitHubuse App\Models\Asset; use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use App\MarketInformationProviders\Coinbase; use Illuminate\Contracts\Queue\ShouldBeUnique; class UpdateCryptoAssetPrice implements ShouldQueue { use Dispatchable; use InteractsWithQueue; use Queueable; use SerializesModels; public function __construct(public Asset $asset) { // } public function handle() { $freshAsset = resolve(Coinbase::class)->fetchAsset($this->asset); if ($freshAsset->price == 0) { return; } $this->asset->updateFromFreshAsset($freshAsset); } }
-
app/Console/Commands/FetchCoinsPrices.php
Open in GitHubuse 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); }); } }