| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * ExchangeRatesCronjob.php | 
					
						
							|  |  |  |  * Copyright (c) 2020 james@firefly-iii.org | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Support\Cronjobs; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | use FireflyIII\Jobs\DownloadExchangeRates; | 
					
						
							|  |  |  | use FireflyIII\Models\Configuration; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class ExchangeRatesCronjob | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class ExchangeRatesCronjob extends AbstractCronjob | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function fire(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Configuration $config */ | 
					
						
							|  |  |  |         $config        = app('fireflyconfig')->get('last_cer_job', 0); | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $lastTime      = (int) $config->data; | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |         $diff          = time() - $lastTime; | 
					
						
							| 
									
										
										
										
											2023-02-11 07:36:45 +01:00
										 |  |  |         $diffForHumans = today(config('app.timezone'))->diffForHumans(Carbon::createFromTimestamp($lastTime), null, true); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |         if (0 === $lastTime) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:27 +01:00
										 |  |  |             app('log')->info('Exchange rates cron-job has never fired before.'); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         // less than half a day ago:
 | 
					
						
							|  |  |  |         if ($lastTime > 0 && $diff <= 43200) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:27 +01:00
										 |  |  |             app('log')->info(sprintf('It has been %s since the exchange rates cron-job has fired.', $diffForHumans)); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |             if (false === $this->force) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:27 +01:00
										 |  |  |                 app('log')->info('The exchange rates cron-job will not fire now.'); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |                 $this->message = sprintf('It has been %s since the exchange rates cron-job has fired. It will not fire now.', $diffForHumans); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-05 16:55:16 +01:00
										 |  |  |             app('log')->info('Execution of the exchange rates cron-job has been FORCED.'); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($lastTime > 0 && $diff > 43200) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:27 +01:00
										 |  |  |             app('log')->info(sprintf('It has been %s since the exchange rates cron-job has fired. It will fire now!', $diffForHumans)); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  |         $this->fireExchangeRateJob(); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |         app('preferences')->mark(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-16 09:25:10 +02:00
										 |  |  |     private function fireExchangeRateJob(): void | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:27 +01:00
										 |  |  |         app('log')->info(sprintf('Will now fire exchange rates cron job task for date "%s".', $this->date->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |         /** @var DownloadExchangeRates $job */ | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $job                = app(DownloadExchangeRates::class); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |         $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.'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         app('fireflyconfig')->set('last_cer_job', (int) $this->date->format('U')); | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:27 +01:00
										 |  |  |         app('log')->info('Done with exchange rates job task.'); | 
					
						
							| 
									
										
										
										
											2022-06-09 17:47:01 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } |