Final things.

This commit is contained in:
James Cole
2021-03-21 11:06:08 +01:00
parent 206845575c
commit 97a687e40a
17 changed files with 305 additions and 67 deletions

View File

@@ -33,12 +33,15 @@ use Exception;
*/
abstract class AbstractCronjob
{
/** @var int */
public $timeBetweenRuns = 43200;
/** @var Carbon */
protected $date;
/** @var bool */
protected $force;
public int $timeBetweenRuns = 43200;
protected Carbon $date;
protected bool $force;
public bool $jobFired;
public bool $jobSucceeded;
public bool $jobErrored;
public ?string $message;
/**
* AbstractCronjob constructor.
@@ -49,12 +52,16 @@ abstract class AbstractCronjob
{
$this->force = false;
$this->date = today(config('app.timezone'));
$this->jobErrored = false;
$this->jobSucceeded = false;
$this->jobFired = false;
$this->message = null;
}
/**
* @return bool
*
*/
abstract public function fire(): bool;
abstract public function fire(): void;
/**
* @param Carbon $date

View File

@@ -39,7 +39,7 @@ class AutoBudgetCronjob extends AbstractCronjob
/**
* @inheritDoc
*/
public function fire(): bool
public function fire(): void
{
/** @var Configuration $config */
$config = app('fireflyconfig')->get('last_ab_job', 0);
@@ -54,8 +54,8 @@ class AutoBudgetCronjob extends AbstractCronjob
Log::info(sprintf('It has been %s since the auto budget cron-job has fired.', $diffForHumans));
if (false === $this->force) {
Log::info('The auto budget cron-job will not fire now.');
return false;
$this->message = sprintf('It has been %s since the auto budget cron-job has fired. It will not fire now.', $diffForHumans);
return;
}
// fire job regardless.
@@ -69,10 +69,7 @@ class AutoBudgetCronjob extends AbstractCronjob
}
$this->fireAutoBudget();
app('preferences')->mark();
return true;
}
/**
@@ -85,6 +82,13 @@ class AutoBudgetCronjob extends AbstractCronjob
$job = app(CreateAutoBudgetLimits::class);
$job->setDate($this->date);
$job->handle();
// get stuff from job:
$this->jobFired = true;
$this->jobErrored = false;
$this->jobSucceeded = true;
$this->message = 'Auto-budget cron job fired successfully.';
app('fireflyconfig')->set('last_ab_job', (int)$this->date->format('U'));
Log::info('Done with auto budget cron job task.');
}

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Support\Cronjobs;
use Carbon\Carbon;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Jobs\CreateRecurringTransactions;
use FireflyIII\Models\Configuration;
@@ -35,16 +36,17 @@ use Log;
class RecurringCronjob extends AbstractCronjob
{
/**
* @return bool
* @throws FireflyException
*/
public function fire(): bool
public function fire(): void
{
Log::debug(sprintf('Now in %s', __METHOD__));
/** @var Configuration $config */
$config = app('fireflyconfig')->get('last_rt_job', 0);
$lastTime = (int)$config->data;
$diff = time() - $lastTime;
$diffForHumans = Carbon::now()->diffForHumans(Carbon::createFromTimestamp($lastTime), true);
if (0 === $lastTime) {
Log::info('Recurring transactions cron-job has never fired before.');
}
@@ -53,8 +55,9 @@ class RecurringCronjob extends AbstractCronjob
Log::info(sprintf('It has been %s since the recurring transactions cron-job has fired.', $diffForHumans));
if (false === $this->force) {
Log::info('The cron-job will not fire now.');
$this->message = sprintf('It has been %s since the recurring transactions cron-job has fired. It will not fire now.', $diffForHumans);
return false;
return;
}
// fire job regardless.
@@ -70,8 +73,6 @@ class RecurringCronjob extends AbstractCronjob
$this->fireRecurring();
app('preferences')->mark();
return true;
}
/**
@@ -85,6 +86,13 @@ class RecurringCronjob extends AbstractCronjob
$job->setDate($this->date);
$job->setForce($this->force);
$job->handle();
// get stuff from job:
$this->jobFired = true;
$this->jobErrored = false;
$this->jobSucceeded = true;
$this->message = 'Recurring transactions cron job fired successfully.';
app('fireflyconfig')->set('last_rt_job', (int)$this->date->format('U'));
Log::info(sprintf('Marked the last time this job has run as "%s" (%d)', $this->date->format('Y-m-d H:i:s'), (int)$this->date->format('U')));
Log::info('Done with recurring cron job task.');

View File

@@ -40,13 +40,15 @@ class TelemetryCronjob extends AbstractCronjob
* @inheritDoc
* @throws FireflyException
*/
public function fire(): bool
public function fire(): void
{
// do not fire if telemetry is disabled.
if (false === config('firefly.send_telemetry') || false === config('firefly.feature_flags.telemetry')) {
Log::warning('Telemetry is disabled. The cron job will do nothing.');
$msg = 'Telemetry is disabled. The cron job will do nothing.';
$this->message = $msg;
Log::warning($msg);
return false;
return;
}
@@ -63,8 +65,8 @@ class TelemetryCronjob extends AbstractCronjob
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.');
return false;
$this->message = sprintf('It has been %s since the telemetry cron-job has fired. It will not fire now.', $diffForHumans);
return;
}
// fire job regardless.
@@ -80,8 +82,6 @@ class TelemetryCronjob extends AbstractCronjob
$this->fireTelemetry();
app('preferences')->mark();
return true;
}
@@ -97,6 +97,11 @@ class TelemetryCronjob extends AbstractCronjob
$job->setForce($this->force);
$job->handle();
$this->jobFired = true;
$this->jobErrored = false;
$this->jobSucceeded = true;
$this->message = 'Telemetry cron job fired successfully.';
// TODO remove old, submitted telemetry data.
app('fireflyconfig')->set('last_tm_job', (int)$this->date->format('U'));

View File

@@ -30,7 +30,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\User;
use Laravel\Passport\Passport;
use Log;
use phpseclib3\Crypt\RSA;
/**
* Trait CreateStuff
@@ -110,7 +110,7 @@ trait CreateStuff
$keys = $rsa->createKey(4096);
}
if (8 === PHP_MAJOR_VERSION) {
$keys = RSA::createKeys(4096);
$keys = \phpseclib3\Crypt\RSA::createKey(4096);
}
[$publicKey, $privateKey] = [

View File

@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Support\Http\Controllers;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Support\Cronjobs\AutoBudgetCronjob;
use FireflyIII\Support\Cronjobs\RecurringCronjob;
@@ -34,60 +35,97 @@ use FireflyIII\Support\Cronjobs\TelemetryCronjob;
trait CronRunner
{
/**
* @return string
* @param bool $force
* @param Carbon $date
*
* @return array
*/
protected function runAutoBudget(): string
protected function runAutoBudget(bool $force, Carbon $date): array
{
/** @var AutoBudgetCronjob $autoBudget */
$autoBudget = app(AutoBudgetCronjob::class);
$autoBudget->setForce($force);
$autoBudget->setDate($date);
try {
$result = $autoBudget->fire();
$autoBudget->fire();
} catch (FireflyException $e) {
return $e->getMessage();
}
if (false === $result) {
return 'The auto budget cron job did not fire.';
return [
'job_fired' => false,
'job_succeeded' => false,
'job_errored' => true,
'message' => $e->getMessage(),
];
}
return 'The auto budget cron job fired successfully.';
return [
'job_fired' => $autoBudget->jobFired,
'job_succeeded' => $autoBudget->jobSucceeded,
'job_errored' => $autoBudget->jobErrored,
'message' => $autoBudget->message,
];
}
/**
* @return string
* @param bool $force
* @param Carbon $date
*
* @return array
*/
protected function runRecurring(): string
protected function runRecurring(bool $force, Carbon $date): array
{
/** @var RecurringCronjob $recurring */
$recurring = app(RecurringCronjob::class);
$recurring->setForce($force);
$recurring->setDate($date);
try {
$result = $recurring->fire();
$recurring->fire();
} catch (FireflyException $e) {
return $e->getMessage();
}
if (false === $result) {
return 'The recurring transaction cron job did not fire. It was fired less than half a day ago.';
return [
'job_fired' => false,
'job_succeeded' => false,
'job_errored' => true,
'message' => $e->getMessage(),
];
}
return 'The recurring transaction cron job fired successfully.';
return [
'job_fired' => $recurring->jobFired,
'job_succeeded' => $recurring->jobSucceeded,
'job_errored' => $recurring->jobErrored,
'message' => $recurring->message,
];
}
/**
* @return string
* @param bool $force
* @param Carbon $date
*
* @return array
*/
protected function runTelemetry(): string
protected function runTelemetry(bool $force, Carbon $date): array
{
/** @var TelemetryCronjob $telemetry */
$telemetry = app(TelemetryCronjob::class);
$telemetry->setForce($force);
$telemetry->setDate($date);
try {
$result = $telemetry->fire();
$telemetry->fire();
} catch (FireflyException $e) {
return $e->getMessage();
}
if (false === $result) {
return 'The telemetry cron job did not fire.';
return [
'job_fired' => false,
'job_succeeded' => false,
'job_errored' => true,
'message' => $e->getMessage(),
];
}
return 'The telemetry cron job fired successfully.';
return [
'job_fired' => $telemetry->jobFired,
'job_succeeded' => $telemetry->jobSucceeded,
'job_errored' => $telemetry->jobErrored,
'message' => $telemetry->message,
];
}
}