1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/app/Jobs/Ninja/AdjustEmailQuota.php

107 lines
2.6 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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;
2023-03-09 05:45:37 +01:00
use Illuminate\Support\Facades\Redis;
2023-03-18 08:24:56 +01:00
use Turbo124\Beacon\Facades\LightLogs;
class AdjustEmailQuota implements ShouldQueue
{
2024-01-14 05:05:00 +01:00
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use 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
2023-03-09 05:45:37 +01:00
$email_count = Cache::get("email_quota".$account->key);
2022-09-08 10:57:32 +02:00
2023-02-16 02:36:09 +01:00
if ($email_count > 0) {
try {
2023-03-09 05:45:37 +01:00
LightLogs::create(new EmailCount($email_count, $account->key))->send(); // this runs syncronously
2023-02-16 02:36:09 +01:00
} catch(\Exception $e) {
nlog($e->getMessage());
}
}
2021-08-07 12:56:42 +02:00
});
2023-03-09 05:45:37 +01:00
/** Use redis pipelines to execute bulk deletes efficiently */
$redis = Redis::connection('sentinel-cache');
$prefix = config('cache.prefix'). ":email_quota*";
$keys = $redis->keys($prefix);
2023-03-18 08:24:56 +01:00
if (is_array($keys)) {
2023-03-09 05:45:37 +01:00
$redis->pipeline(function ($pipe) use ($keys) {
foreach ($keys as $key) {
$pipe->del($key);
}
});
}
$keys = null;
$prefix = config('cache.prefix'). ":throttle_notified*";
$keys = $redis->keys($prefix);
if (is_array($keys)) {
$redis->pipeline(function ($pipe) use ($keys) {
foreach ($keys as $key) {
$pipe->del($key);
}
});
}
}
}