Files
firefly-iii/app/Console/Commands/Tools/Cron.php

242 lines
9.0 KiB
PHP
Raw Normal View History

2018-08-12 14:26:11 +02:00
<?php
/*
2018-12-31 07:48:23 +01:00
* Cron.php
* Copyright (c) 2024 james@firefly-iii.org.
2018-12-31 07:48:23 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-12-31 07:48:23 +01:00
*
* 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.
2018-12-31 07:48:23 +01:00
*
* This program is distributed in the hope that it will be useful,
2018-12-31 07:48:23 +01:00
* 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.
2018-12-31 07:48:23 +01:00
*
* 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/.
2018-12-31 07:48:23 +01:00
*/
declare(strict_types=1);
2019-06-07 17:57:46 +02:00
namespace FireflyIII\Console\Commands\Tools;
2018-08-12 14:26:11 +02:00
use Carbon\Carbon;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
2018-08-12 14:26:11 +02:00
use FireflyIII\Exceptions\FireflyException;
2020-03-14 10:25:12 +01:00
use FireflyIII\Support\Cronjobs\AutoBudgetCronjob;
2022-03-28 12:23:46 +02:00
use FireflyIII\Support\Cronjobs\BillWarningCronjob;
use FireflyIII\Support\Cronjobs\ExchangeRatesCronjob;
2022-03-29 14:56:27 +02:00
use FireflyIII\Support\Cronjobs\RecurringCronjob;
2025-02-15 06:12:02 +01:00
use FireflyIII\Support\Cronjobs\UpdateCheckCronjob;
2018-08-12 14:26:11 +02:00
use Illuminate\Console\Command;
2023-12-29 08:21:14 +01:00
use Illuminate\Support\Facades\Log;
2018-08-12 14:26:11 +02:00
class Cron extends Command
{
use ShowsFriendlyMessages;
2018-08-12 14:26:11 +02:00
protected $description = 'Runs all Firefly III cron-job related commands. Configure a cron job according to the official Firefly III documentation.';
2023-11-05 09:54:53 +01:00
protected $signature = 'firefly-iii:cron
2019-06-07 17:57:46 +02:00
{--F|force : Force the cron job(s) to execute.}
{--date= : Set the date in YYYY-MM-DD to make Firefly III think that\'s the current date.}
2025-02-15 06:12:02 +01:00
{--check-version : Check if there is a new Firefly III version. Other tasks will be skipped unless also requested.}
{--download-cer : Download exchange rates. Other tasks will be skipped unless also requested.}
{--create-recurring : Create recurring transactions. Other tasks will be skipped unless also requested.}
{--create-auto-budgets : Create auto budgets. Other tasks will be skipped unless also requested.}
{--send-bill-warnings : Send bill warnings. Other tasks will be skipped unless also requested.}
2019-06-07 17:57:46 +02:00
';
2018-08-12 14:26:11 +02:00
public function handle(): int
{
$doAll = !$this->option('download-cer')
&& !$this->option('create-recurring')
&& !$this->option('create-auto-budgets')
&& !$this->option('send-bill-warnings')
&& !$this->option('check-version');
$date = null;
2023-12-20 19:35:52 +01:00
try {
$date = new Carbon($this->option('date'));
2023-12-20 19:35:52 +01:00
} catch (\InvalidArgumentException $e) {
$this->friendlyError(sprintf('"%s" is not a valid date', $this->option('date')));
}
2024-12-22 08:43:12 +01:00
$force = (bool) $this->option('force'); // @phpstan-ignore-line
2023-12-20 19:35:52 +01:00
// Fire exchange rates cron job.
if (true === config('cer.download_enabled') && ($doAll || $this->option('download-cer'))) {
try {
$this->exchangeRatesCronJob($force, $date);
} catch (FireflyException $e) {
2023-10-29 06:32:00 +01:00
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
$this->friendlyError($e->getMessage());
}
}
2025-02-15 06:12:02 +01:00
// check for new version
if ($doAll || $this->option('check-version')) {
try {
$this->checkForUpdates($force);
} catch (FireflyException $e) {
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
$this->friendlyError($e->getMessage());
}
}
2023-12-20 19:35:52 +01:00
// Fire recurring transaction cron job.
if ($doAll || $this->option('create-recurring')) {
try {
$this->recurringCronJob($force, $date);
} catch (FireflyException $e) {
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
$this->friendlyError($e->getMessage());
}
2020-03-14 10:25:12 +01:00
}
2023-12-20 19:35:52 +01:00
// Fire auto-budget cron job:
if ($doAll || $this->option('create-auto-budgets')) {
try {
$this->autoBudgetCronJob($force, $date);
} catch (FireflyException $e) {
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
$this->friendlyError($e->getMessage());
}
2020-03-14 10:25:12 +01:00
}
2023-12-20 19:35:52 +01:00
// Fire bill warning cron job
if ($doAll || $this->option('send-bill-warnings')) {
try {
$this->billWarningCronJob($force, $date);
} catch (FireflyException $e) {
app('log')->error($e->getMessage());
app('log')->error($e->getTraceAsString());
$this->friendlyError($e->getMessage());
}
2022-03-28 12:23:46 +02:00
}
$this->friendlyInfo('More feedback on the cron jobs can be found in the log files.');
2020-03-21 15:43:41 +01:00
2020-03-14 10:25:12 +01:00
return 0;
}
2023-06-21 12:34:58 +02:00
private function exchangeRatesCronJob(bool $force, ?Carbon $date): void
2022-10-30 12:24:51 +01:00
{
2023-12-29 12:00:15 +01:00
Log::debug(sprintf('Created new ExchangeRateConverter in %s', __METHOD__));
2023-06-21 12:34:58 +02:00
$exchangeRates = new ExchangeRatesCronjob();
$exchangeRates->setForce($force);
2022-10-30 12:24:51 +01:00
// set date in cron job:
if (null !== $date) {
2023-06-21 12:34:58 +02:00
$exchangeRates->setDate($date);
2022-10-30 12:24:51 +01:00
}
2023-06-21 12:34:58 +02:00
$exchangeRates->fire();
2022-10-30 12:24:51 +01:00
2023-06-21 12:34:58 +02:00
if ($exchangeRates->jobErrored) {
$this->friendlyError(sprintf('Error in "exchange rates" cron: %s', $exchangeRates->message));
2022-10-30 12:24:51 +01:00
}
2023-06-21 12:34:58 +02:00
if ($exchangeRates->jobFired) {
$this->friendlyInfo(sprintf('"Exchange rates" cron fired: %s', $exchangeRates->message));
2022-10-30 12:24:51 +01:00
}
2023-06-21 12:34:58 +02:00
if ($exchangeRates->jobSucceeded) {
$this->friendlyPositive(sprintf('"Exchange rates" cron ran with success: %s', $exchangeRates->message));
2022-10-30 12:24:51 +01:00
}
}
/**
2023-06-21 12:34:58 +02:00
* @throws FireflyException
2020-03-14 10:25:12 +01:00
*/
2023-06-21 12:34:58 +02:00
private function recurringCronJob(bool $force, ?Carbon $date): void
2020-03-14 10:25:12 +01:00
{
2023-06-21 12:34:58 +02:00
$recurring = new RecurringCronjob();
$recurring->setForce($force);
2020-03-14 10:25:12 +01:00
// set date in cron job:
if (null !== $date) {
2023-06-21 12:34:58 +02:00
$recurring->setDate($date);
2020-03-14 10:25:12 +01:00
}
2023-06-21 12:34:58 +02:00
$recurring->fire();
if ($recurring->jobErrored) {
$this->friendlyError(sprintf('Error in "create recurring transactions" cron: %s', $recurring->message));
2021-04-06 08:51:27 +02:00
}
2023-06-21 12:34:58 +02:00
if ($recurring->jobFired) {
$this->friendlyInfo(sprintf('"Create recurring transactions" cron fired: %s', $recurring->message));
2020-03-14 10:25:12 +01:00
}
2023-06-21 12:34:58 +02:00
if ($recurring->jobSucceeded) {
$this->friendlyPositive(sprintf('"Create recurring transactions" cron ran with success: %s', $recurring->message));
2020-03-14 10:25:12 +01:00
}
}
2023-06-21 12:34:58 +02:00
private function autoBudgetCronJob(bool $force, ?Carbon $date): void
2020-03-14 10:25:12 +01:00
{
2023-06-21 12:34:58 +02:00
$autoBudget = new AutoBudgetCronjob();
$autoBudget->setForce($force);
// set date in cron job:
if (null !== $date) {
2023-06-21 12:34:58 +02:00
$autoBudget->setDate($date);
}
2023-06-21 12:34:58 +02:00
$autoBudget->fire();
2018-08-12 14:26:11 +02:00
2023-06-21 12:34:58 +02:00
if ($autoBudget->jobErrored) {
$this->friendlyError(sprintf('Error in "create auto budgets" cron: %s', $autoBudget->message));
2021-04-06 08:51:27 +02:00
}
2023-06-21 12:34:58 +02:00
if ($autoBudget->jobFired) {
$this->friendlyInfo(sprintf('"Create auto budgets" cron fired: %s', $autoBudget->message));
2018-08-12 14:26:11 +02:00
}
2023-06-21 12:34:58 +02:00
if ($autoBudget->jobSucceeded) {
$this->friendlyPositive(sprintf('"Create auto budgets" cron ran with success: %s', $autoBudget->message));
2018-08-12 14:26:11 +02:00
}
}
2022-03-28 12:23:46 +02:00
/**
2023-05-07 20:17:29 +02:00
* @throws FireflyException
2022-03-28 12:23:46 +02:00
*/
2023-06-21 12:34:58 +02:00
private function billWarningCronJob(bool $force, ?Carbon $date): void
2022-03-28 12:23:46 +02:00
{
2023-06-21 12:34:58 +02:00
$autoBudget = new BillWarningCronjob();
$autoBudget->setForce($force);
2022-03-28 12:23:46 +02:00
// set date in cron job:
if (null !== $date) {
2023-06-21 12:34:58 +02:00
$autoBudget->setDate($date);
2022-03-28 12:23:46 +02:00
}
2023-06-21 12:34:58 +02:00
$autoBudget->fire();
if ($autoBudget->jobErrored) {
$this->friendlyError(sprintf('Error in "bill warnings" cron: %s', $autoBudget->message));
2022-03-28 12:23:46 +02:00
}
2023-06-21 12:34:58 +02:00
if ($autoBudget->jobFired) {
$this->friendlyInfo(sprintf('"Send bill warnings" cron fired: %s', $autoBudget->message));
2022-03-28 12:23:46 +02:00
}
2023-06-21 12:34:58 +02:00
if ($autoBudget->jobSucceeded) {
$this->friendlyPositive(sprintf('"Send bill warnings" cron ran with success: %s', $autoBudget->message));
2022-03-28 12:23:46 +02:00
}
}
2025-02-15 06:12:02 +01:00
private function checkForUpdates(bool $force): void
{
2025-02-15 06:12:02 +01:00
$updateCheck = new UpdateCheckCronjob();
$updateCheck->setForce($force);
$updateCheck->fire();
if ($updateCheck->jobErrored) {
$this->friendlyError(sprintf('Error in "update check" cron: %s', $updateCheck->message));
}
if ($updateCheck->jobFired) {
$this->friendlyInfo(sprintf('"Update check" cron fired: %s', $updateCheck->message));
}
if ($updateCheck->jobSucceeded) {
$this->friendlyPositive(sprintf('"Update check" cron ran with success: %s', $updateCheck->message));
}
}
2018-08-12 14:26:11 +02:00
}