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

235 lines
7.5 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
2020-01-23 20:35:02 +01:00
* Copyright (c) 2020 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;
2018-08-12 14:26:11 +02:00
use Illuminate\Console\Command;
2023-05-07 20:17:29 +02:00
use InvalidArgumentException;
2023-02-22 18:03:31 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2018-08-12 14:26:11 +02:00
/**
* Class Cron
*
2018-08-12 14:26:11 +02:00
*/
class Cron extends Command
{
use ShowsFriendlyMessages;
2023-11-05 09:54:53 +01:00
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
2019-06-07 17:57:46 +02:00
protected $signature = 'firefly-iii:cron
{--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.}
';
2018-08-12 14:26:11 +02:00
/**
2019-06-07 18:13:54 +02:00
* @return int
2023-07-04 13:29:19 +02:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2018-08-12 14:26:11 +02:00
*/
public function handle(): int
{
$date = null;
try {
$date = new Carbon($this->option('date'));
2021-04-06 08:51:27 +02:00
} catch (InvalidArgumentException $e) {
$this->friendlyError(sprintf('"%s" is not a valid date', $this->option('date')));
}
2023-12-10 06:45:59 +01:00
$force = (bool)$this->option('force'); // @phpstan-ignore-line
/*
2023-07-25 09:01:44 +02:00
* Fire exchange rates cron job.
*/
2023-07-25 09:01:44 +02:00
if (true === config('cer.download_enabled')) {
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());
}
}
2020-03-14 10:25:12 +01:00
/*
* Fire recurring transaction cron job.
*/
try {
$this->recurringCronJob($force, $date);
2020-03-14 10:25:12 +01:00
} 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());
2020-03-14 10:25:12 +01:00
}
/*
* Fire auto-budget cron job:
*/
try {
$this->autoBudgetCronJob($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());
2020-03-14 10:25:12 +01:00
}
2022-03-28 12:23:46 +02:00
/*
* Fire bill warning cron job
*/
try {
$this->billWarningCronJob($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());
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
* @param bool $force
* @param Carbon|null $date
2022-10-30 12:24:51 +01:00
*/
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-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
* @param bool $force
* @param Carbon|null $date
2023-05-29 13:56:55 +02:00
*
2023-05-07 20:17:29 +02:00
* @throws ContainerExceptionInterface
2023-06-21 12:34:58 +02:00
* @throws FireflyException
2023-02-22 18:03:31 +01:00
* @throws NotFoundExceptionInterface
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
* @param bool $force
* @param Carbon|null $date
*
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-06-21 12:34:58 +02:00
* @param bool $force
* @param Carbon|null $date
2023-05-07 20:17:29 +02:00
*
* @throws FireflyException
2023-06-21 12:34:58 +02:00
* @throws ContainerExceptionInterface
2023-02-22 18:03:31 +01:00
* @throws NotFoundExceptionInterface
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
}
}
2018-08-12 14:26:11 +02:00
}