Clean up repositories and cron code.

This commit is contained in:
James Cole
2019-06-07 17:57:46 +02:00
parent a845cb9af9
commit e4a9abc315
19 changed files with 547 additions and 1188 deletions

View File

@@ -28,13 +28,43 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Jobs\CreateRecurringTransactions;
use FireflyIII\Models\Configuration;
use Log;
use Preferences;
/**
* Class RecurringCronjob
*/
class RecurringCronjob extends AbstractCronjob
{
/** @var bool */
private $force;
/** @var Carbon */
private $date;
/**
* RecurringCronjob constructor.
* @throws \Exception
*/
public function __construct()
{
$this->force = false;
$this->date = new Carbon;
}
/**
* @param bool $force
*/
public function setForce(bool $force): void
{
$this->force = $force;
}
/**
* @param Carbon $date
*/
public function setDate(Carbon $date): void
{
$this->date = $date;
}
/**
* @return bool
@@ -48,17 +78,25 @@ class RecurringCronjob extends AbstractCronjob
$diff = time() - $lastTime;
$diffForHumans = Carbon::now()->diffForHumans(Carbon::createFromTimestamp($lastTime), true);
if (0 === $lastTime) {
Log::info('Recurring transactions cronjob has never fired before.');
Log::info('Recurring transactions cron-job has never fired before.');
}
// less than half a day ago:
if ($lastTime > 0 && $diff <= 43200) {
Log::info(sprintf('It has been %s since the recurring transactions cronjob has fired. It will not fire now.', $diffForHumans));
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.');
return false;
return false;
}
// fire job regardless.
if (true === $this->force) {
Log::info('Execution of the recurring transaction cron-job has been FORCED.');
}
}
if ($lastTime > 0 && $diff > 43200) {
Log::info(sprintf('It has been %s since the recurring transactions cronjob has fired. It will fire now!', $diffForHumans));
Log::info(sprintf('It has been %s since the recurring transactions cron-job has fired. It will fire now!', $diffForHumans));
}
try {
@@ -68,7 +106,8 @@ class RecurringCronjob extends AbstractCronjob
Log::error($e->getTraceAsString());
throw new FireflyException(sprintf('Could not run recurring transaction cron job: %s', $e->getMessage()));
}
Preferences::mark();
app('preferences')->mark();
return true;
}
@@ -79,8 +118,9 @@ class RecurringCronjob extends AbstractCronjob
*/
private function fireRecurring(): void
{
$job = new CreateRecurringTransactions(new Carbon);
$job = new CreateRecurringTransactions($this->date);
$job->setForce($this->force);
$job->handle();
app('fireflyconfig')->set('last_rt_job', time());
app('fireflyconfig')->set('last_rt_job', $this->date->format('U'));
}
}