Skip to content

Firefly III: a personal finances manager

Make jobs multi user

Sander Dorigo732f36c4f652
diff --git a/app/Jobs/CreateAutoBudgetLimits.php b/app/Jobs/CreateAutoBudgetLimits.php
index ceede3cd1c..c2629dd836 100644
--- a/app/Jobs/CreateAutoBudgetLimits.php
+++ b/app/Jobs/CreateAutoBudgetLimits.php
@@ -77,12 +77,7 @@ class CreateAutoBudgetLimits implements ShouldQueue
{
Log::debug(sprintf('Now at start of CreateAutoBudgetLimits() job for %s.', $this->date->format('D d M Y')));
- $autoBudgets = AutoBudget::leftJoin('budgets', 'auto_budgets.budget_id', '=', 'budgets.id')->where('budgets.user_id', $this->user->id)->get([
- 'auto_budgets.*',
- ]);
- if ($this->user->hasRole('owner')) {
- $autoBudgets = AutoBudget::get();
- }
+ $autoBudgets = AutoBudget::leftJoin('budgets', 'auto_budgets.budget_id', '=', 'budgets.id')->where('budgets.user_id', $this->user->id)->get(['auto_budgets.*',]);
Log::debug(sprintf('Found %d auto budgets.', $autoBudgets->count()));
foreach ($autoBudgets as $autoBudget) {
$this->handleAutoBudget($autoBudget);
diff --git a/app/Jobs/WarnAboutBills.php b/app/Jobs/WarnAboutBills.php
index c3b8c2d53d..6ec5d5e946 100644
--- a/app/Jobs/WarnAboutBills.php
+++ b/app/Jobs/WarnAboutBills.php
@@ -79,14 +79,7 @@ class WarnAboutBills implements ShouldQueue
{
Log::debug(sprintf('Now at start of WarnAboutBills() job for %s.', $this->date->format('D d M Y')));
- $users = [$this->user];
- if ($this->user->hasRole('owner')) {
- Log::debug('User is owner, will run for all users.');
- $users = User::all();
- }
-
- foreach ($users as $user) {
- $bills = $user->bills()->where('active', true)->get();
+ $bills = $this->user->bills()->where('active', true)->get();
$overdue = [];
/** @var Bill $bill */
@@ -105,8 +98,7 @@ class WarnAboutBills implements ShouldQueue
}
}
}
- $this->sendOverdueAlerts($user, $overdue);
- }
+ $this->sendOverdueAlerts($this->user, $overdue);
Log::debug('Done with handle()');
}
diff --git a/app/Support/Cronjobs/AutoBudgetCronjob.php b/app/Support/Cronjobs/AutoBudgetCronjob.php
index dff8dd97ee..dc0c52e5e0 100644
--- a/app/Support/Cronjobs/AutoBudgetCronjob.php
+++ b/app/Support/Cronjobs/AutoBudgetCronjob.php
@@ -29,6 +29,7 @@ use FireflyIII\Jobs\CreateAutoBudgetLimits;
use FireflyIII\Models\Configuration;
use FireflyIII\Support\Facades\AppConfiguration;
use FireflyIII\Support\Facades\Preferences;
+use FireflyIII\User;
use Illuminate\Support\Facades\Log;
/**
@@ -38,42 +39,47 @@ class AutoBudgetCronjob extends AbstractCronjob
{
public function fire(): void
{
- /** @var Configuration $config */
- $config = AppConfiguration::get('last_ab_job', 0);
- $lastTime = (int) $config->data;
- $diff = now(config('app.timezone'))->getTimestamp() - $lastTime;
- $diffForHumans = now(config('app.timezone'))->diffForHumans(Carbon::createFromTimestamp($lastTime), null, true);
- if (0 === $lastTime) {
- Log::info('Auto budget cron-job has never fired before.');
- }
- // less than half a day ago:
- if ($lastTime > 0 && $diff <= 43_200) {
- Log::info(sprintf('It has been %s since the auto budget cron-job has fired.', $diffForHumans));
- if (false === $this->force || false === $this->isOwner) {
- Log::info('The auto budget cron-job will not fire now.');
- $this->message = sprintf('It has been %s since the auto budget cron-job has fired. It will not fire now.', $diffForHumans);
+ /** @var User $user */
+ foreach($this->users as $user) {
+
- return;
+ /** @var Configuration $config */
+ $config = AppConfiguration::get(sprintf('last_ab_job_%d', $user->id), 0);
+ $lastTime = (int)$config->data;
+ $diff = now(config('app.timezone'))->getTimestamp() - $lastTime;
+ $diffForHumans = now(config('app.timezone'))->diffForHumans(Carbon::createFromTimestamp($lastTime), null, true);
+ if (0 === $lastTime) {
+ Log::info(sprintf('Auto budget cron-job has never fired before for user #%d.', $user->id));
}
- Log::info('Execution of the auto budget cron-job has been FORCED.');
- }
+ // less than half a day ago:
+ if ($lastTime > 0 && $diff <= 43_200) {
+ Log::info(sprintf('It has been %s since the auto budget cron-job has fired for user #%d.', $diffForHumans, $user->id));
+ if (false === $this->force || false === $user->hasRole('owner')) {
+ Log::info(sprintf('The auto budget cron-job will not fire now for user #%d.', $user->id));
+ $this->message = sprintf('It has been %s since the auto budget cron-job has fired. It will not fire now for user #%d.', $diffForHumans, $user->id);
- if ($lastTime > 0 && $diff > 43_200) {
- Log::info(sprintf('It has been %s since the auto budget cron-job has fired. It will fire now!', $diffForHumans));
- }
+ return;
+ }
+ Log::info(sprintf('Execution of the auto budget cron-job has been FORCED for user #%d.', $user->id));
+ }
+
+ if ($lastTime > 0 && $diff > 43_200) {
+ Log::info(sprintf('It has been %s since the auto budget cron-job has fired. It will fire now for user #%d!', $diffForHumans, $user->id));
+ }
- $this->fireAutoBudget();
+ $this->fireAutoBudget($user);
+ }
Preferences::mark();
}
- private function fireAutoBudget(): void
+ private function fireAutoBudget(User $user): void
{
- Log::info(sprintf('Will now fire auto budget cron job task for date "%s".', $this->date->format('Y-m-d')));
+ Log::info(sprintf('Will now fire auto budget cron job task for date "%s" and user #%d.', $this->date->format('Y-m-d'), $user->id));
/** @var CreateAutoBudgetLimits $job */
$job = app(CreateAutoBudgetLimits::class, [$this->date]);
$job->setDate($this->date);
- $job->setUser($this->user);
+ $job->setUser($user);
$job->handle();
// get stuff from job:
@@ -82,7 +88,7 @@ class AutoBudgetCronjob extends AbstractCronjob
$this->jobSucceeded = true;
$this->message = 'Auto-budget cron job fired successfully.';
- AppConfiguration::set('last_ab_job', (int) $this->date->format('U'));
+ AppConfiguration::set(sprintf('last_ab_job_%d', $user->id), (int) $this->date->format('U'));
Log::info('Done with auto budget cron job task.');
}
}
diff --git a/app/Support/Cronjobs/BillWarningCronjob.php b/app/Support/Cronjobs/BillWarningCronjob.php
index 5326d5eded..4b953b2e8e 100644
--- a/app/Support/Cronjobs/BillWarningCronjob.php
+++ b/app/Support/Cronjobs/BillWarningCronjob.php
@@ -30,6 +30,7 @@ use FireflyIII\Jobs\WarnAboutBills;
use FireflyIII\Models\Configuration;
use FireflyIII\Support\Facades\AppConfiguration;
use FireflyIII\Support\Facades\Preferences;
+use FireflyIII\User;
use Illuminate\Support\Facades\Log;
/**
@@ -43,49 +44,50 @@ class BillWarningCronjob extends AbstractCronjob
public function fire(): void
{
Log::debug(sprintf('Now in %s', __METHOD__));
-
- /** @var Configuration $config */
- $config = AppConfiguration::get('last_bw_job', 0);
- $lastTime = (int) $config->data;
- $diff = now(config('app.timezone'))->getTimestamp() - $lastTime;
- $diffForHumans = now(config('app.timezone'))->diffForHumans(Carbon::createFromTimestamp($lastTime), null, true);
-
- if (0 === $lastTime) {
- Log::info('The bill notification cron-job has never fired before.');
- }
- // less than half a day ago:
- if ($lastTime > 0 && $diff <= 43_200) {
- Log::info(sprintf('It has been %s since the bill notification cron-job has fired.', $diffForHumans));
- if (false === $this->force || false === $this->isOwner) {
- Log::info('The cron-job will not fire now.');
- $this->message = sprintf('It has been %s since the bill notification cron-job has fired. It will not fire now.', $diffForHumans);
- $this->jobFired = false;
- $this->jobErrored = false;
- $this->jobSucceeded = false;
-
- return;
+ /** @var User $user */
+ foreach($this->users as $user) {
+ /** @var Configuration $config */
+ $config = AppConfiguration::get(sprintf('last_bw_job_%d', $user->id), 0);
+ $lastTime = (int)$config->data;
+ $diff = now(config('app.timezone'))->getTimestamp() - $lastTime;
+ $diffForHumans = now(config('app.timezone'))->diffForHumans(Carbon::createFromTimestamp($lastTime), null, true);
+
+ if (0 === $lastTime) {
+ Log::info(sprintf('The bill notification cron-job has never fired before for user #%d.', $user->id));
+ }
+ // less than half a day ago:
+ if ($lastTime > 0 && $diff <= 43_200) {
+ Log::info(sprintf('It has been %s since the bill notification cron-job has fired for user.', $diffForHumans, $user->id));
+ if (false === $this->force || false === $user->hasRole('owner')) {
+ Log::info(sprintf('The cron-job will not fire now for user #%d.', $user->id));
+ $this->message = sprintf('It has been %s since the bill notification cron-job has fired. It will not fire now.', $diffForHumans);
+ $this->jobFired = false;
+ $this->jobErrored = false;
+ $this->jobSucceeded = false;
+
+ return;
+ }
+
+ Log::info(sprintf('Execution of the bill notification cron-job has been FORCED for user #%d.', $user->id));
}
- Log::info('Execution of the bill notification cron-job has been FORCED.');
- }
+ if ($lastTime > 0 && $diff > 43_200) {
+ Log::info(sprintf('It has been %s since the bill notification cron-job has fired. It will fire now for user #%d!', $diffForHumans, $user->id));
+ }
- if ($lastTime > 0 && $diff > 43_200) {
- Log::info(sprintf('It has been %s since the bill notification cron-job has fired. It will fire now!', $diffForHumans));
+ $this->fireWarnings($user);
}
-
- $this->fireWarnings();
-
Preferences::mark();
}
- private function fireWarnings(): void
+ private function fireWarnings(User $user): void
{
- Log::info(sprintf('Will now fire bill notification job task for date "%s".', $this->date->format('Y-m-d H:i:s')));
+ Log::info(sprintf('Will now fire bill notification job task for date "%s" and user #%d.', $this->date->format('Y-m-d H:i:s'), $user->id));
/** @var WarnAboutBills $job */
$job = app(WarnAboutBills::class);
$job->setDate($this->date);
- $job->setUser($this->user);
+ $job->setUser($user);
$job->setForce($this->force);
$job->handle();
@@ -95,8 +97,8 @@ class BillWarningCronjob extends AbstractCronjob
$this->jobSucceeded = true;
$this->message = 'Bill notification cron job fired successfully.';
- AppConfiguration::set('last_bw_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')));
+ AppConfiguration::set(sprintf('last_bw_job_%d', $user->id), (int) $this->date->format('U'));
+ Log::info(sprintf('Marked the last time this job has run as "%s" (%d) for user #%d', $this->date->format('Y-m-d H:i:s'), (int) $this->date->format('U'), $user->id));
Log::info('Done with bill notification cron job task.');
}
}
diff --git a/app/Support/Cronjobs/WebhookCronjob.php b/app/Support/Cronjobs/WebhookCronjob.php
index 411f3aa208..7966aea7c2 100644
--- a/app/Support/Cronjobs/WebhookCronjob.php
+++ b/app/Support/Cronjobs/WebhookCronjob.php
@@ -30,6 +30,7 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Configuration;
use FireflyIII\Support\Facades\AppConfiguration;
use FireflyIII\Support\Facades\Preferences;
+use FireflyIII\User;
use Illuminate\Support\Facades\Log;
/**
@@ -43,44 +44,47 @@ class WebhookCronjob extends AbstractCronjob
public function fire(): void
{
Log::debug(sprintf('Now in %s', __METHOD__));
+ /** @var User $user */
+ foreach($this->users as $user) {
- /** @var Configuration $config */
- $config = AppConfiguration::get('last_webhook_job', 0);
- $lastTime = (int) $config->data;
- $diff = now(config('app.timezone'))->getTimestamp() - $lastTime;
- $diffForHumans = now(config('app.timezone'))->diffForHumans(Carbon::createFromTimestamp($lastTime), null, true);
+ /** @var Configuration $config */
+ $config = AppConfiguration::get(sprintf('last_webhook_job_%d', $user->id), 0);
+ $lastTime = (int)$config->data;
+ $diff = now(config('app.timezone'))->getTimestamp() - $lastTime;
+ $diffForHumans = now(config('app.timezone'))->diffForHumans(Carbon::createFromTimestamp($lastTime), null, true);
- if (0 === $lastTime) {
- Log::info('The webhook cron-job has never fired before.');
- }
- // less than ten minutes ago.
- if ($lastTime > 0 && $diff <= 600) {
- Log::info(sprintf('It has been %s since the webhook cron-job has fired.', $diffForHumans));
- if (false === $this->force || false === $this->isOwner) {
- Log::info('The cron-job will not fire now.');
- $this->message = sprintf('It has been %s since the webhook cron-job has fired. It will not fire now.', $diffForHumans);
- $this->jobFired = false;
- $this->jobErrored = false;
- $this->jobSucceeded = false;
-
- return;
+ if (0 === $lastTime) {
+ Log::info(sprintf('The webhook cron-job has never fired before for user #%d.', $user->id));
+ }
+ // less than ten minutes ago.
+ if ($lastTime > 0 && $diff <= 600) {
+ Log::info(sprintf('It has been %s since the webhook cron-job has fired for user #%d.', $diffForHumans, $user->id));
+ if (false === $this->force || false === $user->hasRole('owner')) {
+ Log::info(sprintf('The cron-job will not fire now for user #%d.', $user->id));
+ $this->message = sprintf('It has been %s since the webhook cron-job has fired. It will not fire now.', $diffForHumans);
+ $this->jobFired = false;
+ $this->jobErrored = false;
+ $this->jobSucceeded = false;
+
+ return;
+ }
+
+ Log::info(sprintf('Execution of the webhook cron-job has been FORCED for user #%d.', $user->id));
}
- Log::info('Execution of the webhook cron-job has been FORCED.');
- }
+ if ($lastTime > 0 && $diff > 600) {
+ Log::info(sprintf('It has been %s since the webhook cron-job has fired for user #%d. It will fire now!', $diffForHumans, $user->id));
+ }
- if ($lastTime > 0 && $diff > 600) {
- Log::info(sprintf('It has been %s since the webhook cron-job has fired. It will fire now!', $diffForHumans));
+ $this->fireWebhookMessages($user);
}
- $this->fireWebhookMessages();
-
Preferences::mark();
}
- private function fireWebhookMessages(): void
+ private function fireWebhookMessages(User $user): void
{
- Log::info(sprintf('Will now send webhook messages for date "%s".', $this->date->format('Y-m-d H:i:s')));
+ Log::info(sprintf('Will now send webhook messages for date "%s" and user #%d.', $this->date->format('Y-m-d H:i:s'), $user->id));
Log::debug(sprintf('send event WebhookMessagesRequestSending from %s', __METHOD__));
event(new WebhookMessagesRequestSending());
@@ -91,7 +95,7 @@ class WebhookCronjob extends AbstractCronjob
$this->jobSucceeded = true;
$this->message = 'Send webhook messages cron job fired successfully.';
- AppConfiguration::set('last_webhook_job', (int) $this->date->format('U'));
+ AppConfiguration::set(sprintf('last_webhook_job_%d', $user->id), (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 webhook cron job task.');
}