firefly-iii/app/Support/Cronjobs/TelemetryCronjob.php

111 lines
3.6 KiB
PHP
Raw Normal View History

2020-03-21 15:32:17 -05:00
<?php
2020-06-30 12:05:35 -05:00
2020-03-21 15:32:17 -05:00
/**
* TelemetryCronjob.php
* Copyright (c) 2020 james@firefly-iii.org
*
* 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 12:05:35 -05:00
declare(strict_types=1);
2020-03-21 15:32:17 -05:00
namespace FireflyIII\Support\Cronjobs;
use Carbon\Carbon;
2020-05-24 05:00:14 -05:00
use FireflyIII\Exceptions\FireflyException;
2020-03-21 15:32:17 -05:00
use FireflyIII\Jobs\SubmitTelemetryData;
use FireflyIII\Models\Configuration;
use Log;
/**
* Class TelemetryCronjob
*/
class TelemetryCronjob extends AbstractCronjob
{
/**
* @inheritDoc
2020-05-24 05:00:14 -05:00
* @throws FireflyException
2020-03-21 15:32:17 -05:00
*/
2021-03-21 05:06:08 -05:00
public function fire(): void
2020-03-21 15:32:17 -05:00
{
2020-05-24 05:00:14 -05:00
// do not fire if telemetry is disabled.
if (false === config('firefly.send_telemetry') || false === config('firefly.feature_flags.telemetry')) {
2021-03-21 05:06:08 -05:00
$msg = 'Telemetry is disabled. The cron job will do nothing.';
$this->message = $msg;
Log::warning($msg);
2021-03-21 03:15:40 -05:00
2021-03-21 05:06:08 -05:00
return;
2020-05-24 05:00:14 -05:00
}
2020-03-21 15:32:17 -05:00
/** @var Configuration $config */
$config = app('fireflyconfig')->get('last_tm_job', 0);
2021-03-21 03:15:40 -05:00
$lastTime = (int)$config->data;
2020-03-21 15:32:17 -05:00
$diff = time() - $lastTime;
$diffForHumans = Carbon::now()->diffForHumans(Carbon::createFromTimestamp($lastTime), true);
if (0 === $lastTime) {
Log::info('Telemetry cron-job has never fired before.');
}
2020-05-24 05:00:14 -05:00
// less than a week ago:
if ($lastTime > 0 && $diff <= 604800) {
2020-03-21 15:32:17 -05:00
Log::info(sprintf('It has been %s since the telemetry cron-job has fired.', $diffForHumans));
if (false === $this->force) {
Log::info('The cron-job will not fire now.');
2021-03-21 05:06:08 -05:00
$this->message = sprintf('It has been %s since the telemetry cron-job has fired. It will not fire now.', $diffForHumans);
return;
2020-03-21 15:32:17 -05:00
}
// fire job regardless.
if (true === $this->force) {
Log::info('Execution of the telemetry cron-job has been FORCED.');
}
}
2020-05-24 05:00:14 -05:00
// more than a week ago.
if ($lastTime > 0 && $diff > 604799) {
2020-03-21 15:32:17 -05:00
Log::info(sprintf('It has been %s since the telemetry cron-job has fired. It will fire now!', $diffForHumans));
}
$this->fireTelemetry();
app('preferences')->mark();
}
/**
2020-05-24 05:00:14 -05:00
* @throws FireflyException
2020-03-21 15:32:17 -05:00
*/
private function fireTelemetry(): void
{
Log::info(sprintf('Will now fire telemetry cron job task for date "%s".', $this->date->format('Y-m-d')));
/** @var SubmitTelemetryData $job */
$job = app(SubmitTelemetryData::class);
$job->setDate($this->date);
$job->setForce($this->force);
$job->handle();
2020-05-24 09:58:17 -05:00
2021-03-21 05:06:08 -05:00
$this->jobFired = true;
$this->jobErrored = false;
$this->jobSucceeded = true;
$this->message = 'Telemetry cron job fired successfully.';
2020-05-24 09:58:17 -05:00
// TODO remove old, submitted telemetry data.
2021-03-21 03:15:40 -05:00
app('fireflyconfig')->set('last_tm_job', (int)$this->date->format('U'));
2020-03-21 15:32:17 -05:00
Log::info('Done with telemetry cron job task.');
}
2020-05-24 05:00:14 -05:00
}