Files
firefly-iii/app/Support/Cronjobs/AutoBudgetCronjob.php

95 lines
3.3 KiB
PHP
Raw Normal View History

2020-03-14 10:25:12 +01:00
<?php
2020-06-30 19:05:35 +02:00
2020-03-14 10:25:12 +01:00
/**
* AutoBudgetCronjob.php
2020-06-30 19:05:35 +02:00
* Copyright (c) 2020 james@firefly-iii.org
2020-03-14 10:25:12 +01:00
*
* 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/>.
*/
2020-06-30 19:05:35 +02:00
declare(strict_types=1);
2020-03-14 10:25:12 +01:00
namespace FireflyIII\Support\Cronjobs;
2020-03-14 10:25:12 +01:00
use Carbon\Carbon;
use FireflyIII\Jobs\CreateAutoBudgetLimits;
use FireflyIII\Models\Configuration;
2023-04-01 07:04:42 +02:00
use Illuminate\Support\Facades\Log;
2020-03-14 10:25:12 +01:00
/**
* Class AutoBudgetCronjob
*/
class AutoBudgetCronjob extends AbstractCronjob
{
/**
* @inheritDoc
*/
2021-03-21 11:06:08 +01:00
public function fire(): void
2020-03-14 10:25:12 +01:00
{
/** @var Configuration $config */
$config = app('fireflyconfig')->get('last_ab_job', 0);
2022-12-29 19:42:26 +01:00
$lastTime = (int)$config->data;
2020-03-14 10:25:12 +01:00
$diff = time() - $lastTime;
2023-02-11 07:36:45 +01:00
$diffForHumans = today(config('app.timezone'))->diffForHumans(Carbon::createFromTimestamp($lastTime), null, true);
2020-03-14 10:25:12 +01:00
if (0 === $lastTime) {
2023-10-29 06:31:27 +01:00
app('log')->info('Auto budget cron-job has never fired before.');
2020-03-14 10:25:12 +01: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 auto budget cron-job has fired.', $diffForHumans));
2020-03-14 10:25:12 +01:00
if (false === $this->force) {
2023-10-29 06:31:27 +01:00
app('log')->info('The auto budget cron-job will not fire now.');
2021-03-21 11:06:08 +01:00
$this->message = sprintf('It has been %s since the auto budget cron-job has fired. It will not fire now.', $diffForHumans);
2021-03-21 11:06:08 +01:00
return;
2020-03-14 10:25:12 +01:00
}
// fire job regardless.
if (true === $this->force) {
2023-10-29 06:31:27 +01:00
app('log')->info('Execution of the auto budget cron-job has been FORCED.');
2020-03-14 10:25:12 +01:00
}
}
if ($lastTime > 0 && $diff > 43200) {
2023-10-29 06:31:27 +01:00
app('log')->info(sprintf('It has been %s since the auto budget cron-job has fired. It will fire now!', $diffForHumans));
2020-03-14 10:25:12 +01:00
}
$this->fireAutoBudget();
app('preferences')->mark();
}
/**
*
*/
private function fireAutoBudget(): void
{
2023-10-29 06:31:27 +01:00
app('log')->info(sprintf('Will now fire auto budget cron job task for date "%s".', $this->date->format('Y-m-d')));
2020-03-14 10:25:12 +01:00
/** @var CreateAutoBudgetLimits $job */
2023-09-06 05:45:35 +02:00
$job = app(CreateAutoBudgetLimits::class, [$this->date]);
2020-03-14 10:25:12 +01:00
$job->setDate($this->date);
$job->handle();
2021-03-21 11:06:08 +01:00
// get stuff from job:
$this->jobFired = true;
$this->jobErrored = false;
$this->jobSucceeded = true;
$this->message = 'Auto-budget cron job fired successfully.';
2022-12-29 19:42:26 +01:00
app('fireflyconfig')->set('last_ab_job', (int)$this->date->format('U'));
2023-10-29 06:31:27 +01:00
app('log')->info('Done with auto budget cron job task.');
2020-03-14 10:25:12 +01:00
}
}