. */ declare(strict_types=1); namespace FireflyIII\Support\Cronjobs; use Carbon\Carbon; use FireflyIII\Jobs\DownloadExchangeRates; use FireflyIII\Models\Configuration; use Illuminate\Support\Facades\Log; /** * Class ExchangeRatesCronjob */ class ExchangeRatesCronjob extends AbstractCronjob { public function fire(): void { /** @var Configuration $config */ $config = app('fireflyconfig')->get('last_cer_job', 0); $lastTime = (int) $config->data; $diff = Carbon::now()->getTimestamp() - $lastTime; $diffForHumans = today(config('app.timezone'))->diffForHumans(Carbon::createFromTimestamp($lastTime), null, true); if (0 === $lastTime) { Log::info('Exchange rates cron-job has never fired before.'); } // less than half a day ago: if ($lastTime > 0 && $diff <= 43200) { Log::info(sprintf('It has been %s since the exchange rates cron-job has fired.', $diffForHumans)); if (false === $this->force) { Log::info('The exchange rates cron-job will not fire now.'); $this->message = sprintf('It has been %s since the exchange rates cron-job has fired. It will not fire now.', $diffForHumans); return; } Log::info('Execution of the exchange rates cron-job has been FORCED.'); } if ($lastTime > 0 && $diff > 43200) { Log::info(sprintf('It has been %s since the exchange rates cron-job has fired. It will fire now!', $diffForHumans)); } $this->fireExchangeRateJob(); app('preferences')->mark(); } private function fireExchangeRateJob(): void { Log::info(sprintf('Will now fire exchange rates cron job task for date "%s".', $this->date->format('Y-m-d'))); /** @var DownloadExchangeRates $job */ $job = app(DownloadExchangeRates::class); $job->setDate($this->date); $job->handle(); // get stuff from job: $this->jobFired = true; $this->jobErrored = false; $this->jobSucceeded = true; $this->message = 'Exchange rates cron job fired successfully.'; app('fireflyconfig')->set('last_cer_job', (int) $this->date->format('U')); Log::info('Done with exchange rates job task.'); } }