1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 09:21:34 +02:00
invoiceninja/app/Jobs/Ninja/AdjustEmailQuota.php

82 lines
1.9 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\Ninja;
2022-09-08 10:57:32 +02:00
use App\DataMapper\Analytics\EmailCount;
use App\Libraries\MultiDB;
use App\Models\Account;
2021-08-07 12:56:42 +02:00
use App\Utils\Ninja;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2021-08-07 12:56:42 +02:00
use Illuminate\Support\Facades\Cache;
2022-09-08 10:57:32 +02:00
use Turbo124\Beacon\Facades\LightLogs;
class AdjustEmailQuota implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if (! Ninja::isHosted()) {
2021-08-07 12:56:42 +02:00
return;
}
2021-08-07 12:56:42 +02:00
//multiDB environment, need to
foreach (MultiDB::$dbs as $db) {
MultiDB::setDB($db);
$this->adjust();
}
}
public function adjust()
{
Account::query()->cursor()->each(function ($account) {
2021-08-11 03:12:03 +02:00
nlog("resetting email quota for {$account->key}");
2022-09-08 10:57:32 +02:00
$email_count = Cache::get($account->key);
if($email_count > 0){
try{
LightLogs::create(new EmailCount($email_count, $account->key))->send();
}
catch(\Exception $e){
nlog($e->getMessage());
}
}
2022-09-08 10:57:32 +02:00
2021-08-07 12:56:42 +02:00
Cache::forget($account->key);
Cache::forget("throttle_notified:{$account->key}");
2022-09-08 10:57:32 +02:00
2021-08-07 12:56:42 +02:00
});
}
}