2018-08-12 14:26:11 +02:00
|
|
|
<?php
|
|
|
|
|
2024-03-20 17:48:13 +01:00
|
|
|
/*
|
2018-12-31 07:48:23 +01:00
|
|
|
* Cron.php
|
2024-03-20 17:48:13 +01:00
|
|
|
* Copyright (c) 2024 james@firefly-iii.org.
|
2018-12-31 07:48:23 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-12-31 07:48:23 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02: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
|
|
|
*
|
2019-10-02 06:37:26 +02: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
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-12-31 07:48:23 +01:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2024-03-20 17:48:13 +01:00
|
|
|
* 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
|
|
|
|
2019-07-02 06:11:06 +02:00
|
|
|
use Carbon\Carbon;
|
2023-06-20 07:16:56 +02:00
|
|
|
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;
|
2022-06-09 17:47:01 +02:00
|
|
|
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;
|
2025-08-09 07:59:38 +02:00
|
|
|
use FireflyIII\Support\Cronjobs\WebhookCronjob;
|
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;
|
2025-05-27 16:57:36 +02:00
|
|
|
use InvalidArgumentException;
|
2018-08-12 14:26:11 +02:00
|
|
|
|
|
|
|
class Cron extends Command
|
|
|
|
{
|
2023-06-20 07:16:56 +02:00
|
|
|
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
|
|
|
|
2024-01-01 14:43:56 +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.}
|
2024-11-03 08:16:46 +01:00
|
|
|
{--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.}
|
2025-08-15 20:00:29 +02:00
|
|
|
{--send-subscription-warnings : Send subscription warnings. Other tasks will be skipped unless also requested.}
|
2025-08-09 07:59:38 +02:00
|
|
|
{--send-webhook-messages : Sends any stray webhook messages (with a maximum of 5).}
|
2019-06-07 17:57:46 +02:00
|
|
|
';
|
2018-08-12 14:26:11 +02:00
|
|
|
|
|
|
|
public function handle(): int
|
|
|
|
{
|
2025-02-17 04:11:50 +01:00
|
|
|
$doAll = !$this->option('download-cer')
|
|
|
|
&& !$this->option('create-recurring')
|
|
|
|
&& !$this->option('create-auto-budgets')
|
2025-08-15 20:00:29 +02:00
|
|
|
&& !$this->option('send-subscription-warnings')
|
2025-08-09 07:59:38 +02:00
|
|
|
&& !$this->option('check-version')
|
|
|
|
&& !$this->option('send-webhook-messages');
|
2024-01-01 14:43:56 +01:00
|
|
|
$date = null;
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2019-07-02 06:11:06 +02:00
|
|
|
try {
|
|
|
|
$date = new Carbon($this->option('date'));
|
2025-05-27 16:57:36 +02:00
|
|
|
} catch (InvalidArgumentException $e) {
|
2023-06-20 07:16:56 +02:00
|
|
|
$this->friendlyError(sprintf('"%s" is not a valid date', $this->option('date')));
|
2019-07-02 06:11:06 +02:00
|
|
|
}
|
2024-12-22 08:43:12 +01:00
|
|
|
$force = (bool) $this->option('force'); // @phpstan-ignore-line
|
2019-07-02 06:11:06 +02:00
|
|
|
|
2023-12-20 19:35:52 +01:00
|
|
|
// Fire exchange rates cron job.
|
2024-11-03 08:16:46 +01:00
|
|
|
if (true === config('cer.download_enabled') && ($doAll || $this->option('download-cer'))) {
|
2022-06-09 17:47:01 +02:00
|
|
|
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());
|
2023-06-20 07:16:56 +02:00
|
|
|
$this->friendlyError($e->getMessage());
|
2022-06-09 17:47:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2024-11-03 09:01:53 +01:00
|
|
|
if ($doAll || $this->option('create-recurring')) {
|
2024-11-03 08:16:46 +01:00
|
|
|
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:
|
2024-11-03 09:01:53 +01:00
|
|
|
if ($doAll || $this->option('create-auto-budgets')) {
|
2024-11-03 08:16:46 +01:00
|
|
|
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
|
2025-08-15 20:00:29 +02:00
|
|
|
if ($doAll || $this->option('send-subscription-warnings')) {
|
2024-11-03 08:16:46 +01:00
|
|
|
try {
|
2025-08-15 20:00:29 +02:00
|
|
|
$this->subscriptionWarningCronJob($force, $date);
|
2024-11-03 08:16:46 +01:00
|
|
|
} 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
|
|
|
}
|
2025-08-09 07:59:38 +02:00
|
|
|
// Fire webhook messages cron job.
|
|
|
|
if ($doAll || $this->option('send-webhook-messages')) {
|
|
|
|
try {
|
|
|
|
$this->webhookCronJob($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
|
|
|
|
2023-06-20 07:16:56 +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:
|
2025-05-27 17:02:18 +02:00
|
|
|
if ($date instanceof Carbon) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-04 17:41:26 +02:00
|
|
|
private function checkForUpdates(bool $force): void
|
|
|
|
{
|
|
|
|
$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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
2025-05-27 17:02:18 +02:00
|
|
|
if ($date instanceof Carbon) {
|
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);
|
2019-07-02 06:11:06 +02:00
|
|
|
// set date in cron job:
|
2025-05-27 17:02:18 +02:00
|
|
|
if ($date instanceof Carbon) {
|
2023-06-21 12:34:58 +02:00
|
|
|
$autoBudget->setDate($date);
|
2019-07-02 06:11:06 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2025-08-15 20:00:29 +02:00
|
|
|
private function subscriptionWarningCronJob(bool $force, ?Carbon $date): void
|
2022-03-28 12:23:46 +02:00
|
|
|
{
|
2025-08-15 20:00:29 +02:00
|
|
|
$subscriptionWarningJob = new BillWarningCronjob();
|
|
|
|
$subscriptionWarningJob->setForce($force);
|
2022-03-28 12:23:46 +02:00
|
|
|
// set date in cron job:
|
2025-05-27 17:02:18 +02:00
|
|
|
if ($date instanceof Carbon) {
|
2025-08-15 20:00:29 +02:00
|
|
|
$subscriptionWarningJob->setDate($date);
|
2022-03-28 12:23:46 +02:00
|
|
|
}
|
|
|
|
|
2025-08-15 20:00:29 +02:00
|
|
|
$subscriptionWarningJob->fire();
|
2023-06-21 12:34:58 +02:00
|
|
|
|
2025-08-15 20:00:29 +02:00
|
|
|
if ($subscriptionWarningJob->jobErrored) {
|
|
|
|
$this->friendlyError(sprintf('Error in "subscription warnings" cron: %s', $subscriptionWarningJob->message));
|
2022-03-28 12:23:46 +02:00
|
|
|
}
|
2025-08-15 20:00:29 +02:00
|
|
|
if ($subscriptionWarningJob->jobFired) {
|
|
|
|
$this->friendlyInfo(sprintf('"Send subscription warnings" cron fired: %s', $subscriptionWarningJob->message));
|
2022-03-28 12:23:46 +02:00
|
|
|
}
|
2025-08-15 20:00:29 +02:00
|
|
|
if ($subscriptionWarningJob->jobSucceeded) {
|
|
|
|
$this->friendlyPositive(sprintf('"Send subscription warnings" cron ran with success: %s', $subscriptionWarningJob->message));
|
2022-03-28 12:23:46 +02:00
|
|
|
}
|
|
|
|
}
|
2025-08-09 08:04:02 +02:00
|
|
|
|
2025-08-09 07:59:38 +02:00
|
|
|
private function webhookCronJob(bool $force, ?Carbon $date): void
|
|
|
|
{
|
|
|
|
$webhook = new WebhookCronjob();
|
|
|
|
$webhook->setForce($force);
|
|
|
|
// set date in cron job:
|
|
|
|
if ($date instanceof Carbon) {
|
|
|
|
$webhook->setDate($date);
|
|
|
|
}
|
|
|
|
|
|
|
|
$webhook->fire();
|
|
|
|
|
|
|
|
if ($webhook->jobErrored) {
|
|
|
|
$this->friendlyError(sprintf('Error in "webhook" cron: %s', $webhook->message));
|
|
|
|
}
|
|
|
|
if ($webhook->jobFired) {
|
|
|
|
$this->friendlyInfo(sprintf('"Webhook" cron fired: %s', $webhook->message));
|
|
|
|
}
|
|
|
|
if ($webhook->jobSucceeded) {
|
|
|
|
$this->friendlyPositive(sprintf('"Webhook" cron ran with success: %s', $webhook->message));
|
|
|
|
}
|
|
|
|
}
|
2018-08-12 14:26:11 +02:00
|
|
|
}
|