Viewing file: ScheduleController.php (6.02 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers;
use App\Events\SendMail; use App\Events\SmsProcessed; use App\Models\Contact; use App\Models\DynamicGateway; use App\Models\Message; use App\Models\Number; use App\Models\SenderId; use App\Models\WhatsAppNumber; use App\SmsProvider\SendSMS; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Bus; use Illuminate\Support\Facades\Log; use Illuminate\Support\Carbon;
class ScheduleController extends Controller { public function process() { //TODO:: check exception table for sending sms cache()->put('cronjob', 'true'); Log::info("processing queues"); $totalNewQueueCount = 40; $today = now()->format('l'); $time = now()->toTimeString();
$sendingServersNotNull = DynamicGateway::whereStatus('active')->where('offdays', 'not like', "%$today%") ->whereNotNull('start_time') ->whereNotNull('end_time') ->whereTime('start_time', '<=', $time) ->whereTime('end_time', '>=', $time) ->get();
$sendingServersNull = DynamicGateway::whereStatus('active')->where('offdays', 'not like', "%$today%") ->whereNull('start_time') ->whereNull('end_time') ->get();
$sendingServers=$sendingServersNotNull->merge($sendingServersNull);
if ($sendingServers->isEmpty()) { $this->debugExit("No available servers"); }
#region Minute Limit Check $minuteEmailQueue = Message::selectRaw("count(*) as total,dynamic_gateway_id") ->where('schedule_completed', 'yes') ->whereBetween('schedule_datetime', [now()->subMinute(), now()]) ->whereIn('dynamic_gateway_id', $sendingServers->pluck('id')->toArray()) ->groupBy('dynamic_gateway_id') ->pluck('total', 'dynamic_gateway_id') ->all();
$availableMinutelySendingServerIds = []; foreach ($sendingServers as $sendingServer) { if (isset($minuteEmailQueue[$sendingServer->id])) { $totalUsed = $minuteEmailQueue[$sendingServer->id] + $totalNewQueueCount; // count new queue ; if ($sendingServer->send_limit > 0 && ($sendingServer->minute_limit / $sendingServer->send_limit) > $totalUsed) { $availableMinutelySendingServerIds[] = $sendingServer->id; } } else { $availableMinutelySendingServerIds[] = $sendingServer->id; } } if (empty($availableMinutelySendingServerIds)) { $this->debugExit("No available sending servers (minute limit)"); } #endregion
#region Daily Limit Check $dailyEmailQueue = Message::selectRaw("count(*) as total,dynamic_gateway_id") ->where('schedule_completed', 'yes') ->whereIn('dynamic_gateway_id', $availableMinutelySendingServerIds) ->whereBetween('schedule_datetime', [now()->subDay(), now()]) ->groupBy('dynamic_gateway_id') ->pluck('total', 'dynamic_gateway_id') ->all();
$availableDailySendingServerIds = []; foreach ($sendingServers as $sendingServer) { if (isset($dailyEmailQueue[$sendingServer->id])) { $totalUsed = $dailyEmailQueue[$sendingServer->id] + $totalNewQueueCount; // count new queue ; if ($sendingServer->daily_limit > $totalUsed) { $availableDailySendingServerIds[] = $sendingServer->id; } } else { $availableDailySendingServerIds[] = $sendingServer->id; } } if (empty($availableDailySendingServerIds)) { $this->debugExit("No available sending servers (daily limit)"); } #endregion
#region Monthly Limit Check $monthlyEmailQueue = Message::selectRaw("count(*) as total,dynamic_gateway_id") ->where('schedule_completed', 'yes') ->whereIn('dynamic_gateway_id', $availableDailySendingServerIds) ->whereBetween('schedule_datetime', [now()->subMonth(), now()]) ->groupBy('dynamic_gateway_id') ->pluck('total', 'dynamic_gateway_id') ->all();
$availableSendingServerIds = []; foreach ($sendingServers as $sendingServer) { if (isset($monthlyEmailQueue[$sendingServer->id])) { $totalUsed = $monthlyEmailQueue[$sendingServer->id] + $totalNewQueueCount; // count new queue ; if ($sendingServer->monthly_limit > $totalUsed) { $availableSendingServerIds[] = $sendingServer->id; } } else { $availableSendingServerIds[] = $sendingServer->id; } } if (empty($availableSendingServerIds)) { $this->debugExit("No available sending servers (monthly limit"); } #endregion
$smsQueues = Message::whereIn('dynamic_gateway_id', $availableSendingServerIds) ->where('schedule_completed', 'no') ->whereNotNull('schedule_datetime') ->where('schedule_datetime', '<', now()) ->whereNull('delivered_at') ->whereStatus('running') ->whereType('sent') ->limit($totalNewQueueCount) ->get();
foreach ($smsQueues as $queue) { SmsProcessed::dispatch($queue); }
Message::whereIn('id',$smsQueues->pluck('id'))->update(['schedule_completed' => 'yes']);
Log::info("schedule completed"); }
public static function processQueue() { (new ScheduleController)->process(); }
public function processEmail() { Artisan::call("queue:work", ['--stop-when-empty' => true]); }
public function processDbBackup() { Artisan::call("db:backup"); }
public function cronJob(){ Artisan::call("schedule:run"); } protected function debugExit($message) { if (config('app.debug')) { Log::info($message); } exit; } }
|