2020-04-30 13:45:47 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-04-30 13:45:47 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-04-30 13:45:47 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-04-30 13:45:47 +02:00
|
|
|
*/
|
|
|
|
|
2020-05-02 02:04:25 +02:00
|
|
|
namespace App\Jobs\Ninja;
|
2020-04-30 13:45:47 +02:00
|
|
|
|
2022-09-08 10:57:32 +02:00
|
|
|
use App\DataMapper\Analytics\EmailCount;
|
2020-04-30 13:45:47 +02:00
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Account;
|
2021-08-07 12:56:42 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-04-30 13:45:47 +02:00
|
|
|
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;
|
|
|
|
|
2020-04-30 13:45:47 +02:00
|
|
|
class AdjustEmailQuota implements ShouldQueue
|
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
use Dispatchable;
|
|
|
|
use InteractsWithQueue;
|
|
|
|
use Queueable;
|
|
|
|
use SerializesModels;
|
2020-04-30 13:45:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! Ninja::isHosted()) {
|
2021-08-07 12:56:42 +02:00
|
|
|
return;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-08-07 12:56:42 +02:00
|
|
|
|
|
|
|
//multiDB environment, need to
|
|
|
|
foreach (MultiDB::$dbs as $db) {
|
|
|
|
MultiDB::setDB($db);
|
|
|
|
|
2020-04-30 13:45:47 +02:00
|
|
|
$this->adjust();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function adjust()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
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) {
|
2022-10-28 05:37:01 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-04-30 13:45:47 +02:00
|
|
|
}
|
|
|
|
}
|