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

197 lines
5.4 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;
2019-06-07 18:13:54 +02:00
use Exception;
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;
use FireflyIII\Support\Cronjobs\RecurringCronjob;
2020-03-21 21:32:17 +01:00
use FireflyIII\Support\Cronjobs\TelemetryCronjob;
2018-08-12 14:26:11 +02:00
use Illuminate\Console\Command;
use InvalidArgumentException;
2020-03-14 10:25:12 +01:00
use Log;
2018-08-12 14:26:11 +02:00
/**
* Class Cron
*
* @codeCoverageIgnore
2018-08-12 14:26:11 +02:00
*/
class Cron extends Command
{
/**
* The console command description.
*
* @var string
*/
protected $description = 'Runs all Firefly III cron-job related commands. Configure a cron job according to the official Firefly III documentation.';
/**
* The name and signature of the console command.
*
* @var string
*/
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
2018-08-12 14:26:11 +02:00
/**
2019-06-07 18:13:54 +02:00
* @return int
2018-08-12 14:26:11 +02:00
*/
public function handle(): int
{
$date = null;
try {
$date = new Carbon($this->option('date'));
2020-03-14 10:25:12 +01:00
} catch (InvalidArgumentException|Exception $e) {
$this->error(sprintf('"%s" is not a valid date', $this->option('date')));
$e->getMessage();
}
$force = (bool) $this->option('force');
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) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$this->error($e->getMessage());
}
/*
* Fire auto-budget cron job:
*/
try {
$this->autoBudgetCronJob($force, $date);
} catch (FireflyException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$this->error($e->getMessage());
}
2020-03-21 21:32:17 +01:00
/*
2020-05-24 12:00:14 +02:00
* Fire telemetry cron job
2020-03-21 21:32:17 +01:00
*/
try {
2020-05-24 12:00:14 +02:00
$this->telemetryCronJob($force, $date);
2020-03-21 21:32:17 +01:00
} catch (FireflyException $e) {
Log::error($e->getMessage());
Log::error($e->getTraceAsString());
$this->error($e->getMessage());
}
2020-03-14 10:25:12 +01:00
$this->info('More feedback on the cron jobs can be found in the log files.');
2020-03-21 15:43:41 +01:00
2020-05-24 12:12:06 +02:00
app('telemetry')->feature('system.command.executed', $this->signature);
2020-03-14 10:25:12 +01:00
return 0;
}
/**
* @param bool $force
* @param Carbon|null $date
2020-03-15 08:16:16 +01:00
*
2020-03-14 10:25:12 +01:00
* @throws FireflyException
2020-03-15 08:16:16 +01:00
* @throws Exception
2020-03-14 10:25:12 +01:00
*/
2020-03-15 08:16:16 +01:00
private function autoBudgetCronJob(bool $force, ?Carbon $date): void
2020-03-14 10:25:12 +01:00
{
$autoBudget = new AutoBudgetCronjob;
$autoBudget->setForce($force);
// set date in cron job:
if (null !== $date) {
$autoBudget->setDate($date);
}
$result = $autoBudget->fire();
2019-06-07 17:57:46 +02:00
2020-03-14 10:25:12 +01:00
if (false === $result) {
$this->line('The auto budget cron job did not fire.');
}
if (true === $result) {
$this->line('The auto budget cron job fired successfully.');
}
}
/**
* @param bool $force
* @param Carbon|null $date
*
* @throws FireflyException
*/
private function recurringCronJob(bool $force, ?Carbon $date): void
{
2018-08-12 14:26:11 +02:00
$recurring = new RecurringCronjob;
2020-03-14 10:25:12 +01:00
$recurring->setForce($force);
// set date in cron job:
if (null !== $date) {
$recurring->setDate($date);
}
2020-03-14 10:25:12 +01:00
$result = $recurring->fire();
2018-08-12 14:26:11 +02:00
if (false === $result) {
$this->line('The recurring transaction cron job did not fire.');
}
if (true === $result) {
$this->line('The recurring transaction cron job fired successfully.');
}
}
2020-03-21 21:32:17 +01:00
/**
* @param bool $force
* @param Carbon|null $date
*/
private function telemetryCronJob(bool $force, ?Carbon $date): void
{
if (false === config('firefly.send_telemetry') || false === config('firefly.feature_flags.telemetry')) {
// if not configured to do anything with telemetry, do nothing.
return;
}
$telemetry = new TelemetryCronjob;
2020-03-21 21:32:17 +01:00
$telemetry->setForce($force);
// set date in cron job:
if (null !== $date) {
$telemetry->setDate($date);
}
$result = $telemetry->fire();
if (false === $result) {
$this->line('The telemetry cron job did not fire.');
}
if (true === $result) {
$this->line('The telemetry cron job fired successfully.');
}
}
2018-08-12 14:26:11 +02:00
}