From 3c7e2bbbc9fcde9efa9a88ebc0da8c87648dd6bb Mon Sep 17 00:00:00 2001 From: Deon George Date: Sat, 6 Jul 2024 19:56:56 +1000 Subject: [PATCH] Move DB queries into jobs, so that the scheduler and artisan command calls doesnt evaluate them until the job is actually run --- app/Console/Commands/BroadbandTraffic.php | 4 ++-- app/Jobs/BroadbandTraffic.php | 27 ++++++++++++----------- routes/console.php | 5 ++--- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/app/Console/Commands/BroadbandTraffic.php b/app/Console/Commands/BroadbandTraffic.php index eb5cf98..b162050 100644 --- a/app/Console/Commands/BroadbandTraffic.php +++ b/app/Console/Commands/BroadbandTraffic.php @@ -34,11 +34,11 @@ class BroadbandTraffic extends Command if ($this->option('supplier')) { $o = Supplier::where('name','like',$this->option('supplier'))->singleOrFail(); - Job::dispatchSync($o); + Job::dispatchSync($o->id); return; } foreach (Supplier::active()->get() as $o) - Job::dispatchSync($o); + Job::dispatchSync($o->id); } } \ No newline at end of file diff --git a/app/Jobs/BroadbandTraffic.php b/app/Jobs/BroadbandTraffic.php index 930224f..b79a953 100644 --- a/app/Jobs/BroadbandTraffic.php +++ b/app/Jobs/BroadbandTraffic.php @@ -32,14 +32,14 @@ final class BroadbandTraffic implements ShouldQueue private const LOGKEY = 'JBT'; - protected Model $o; // The supplier we are updating from + protected int $sid; // The supplier we are updating from private const class_prefix = 'App\Classes\External\Supplier\\'; private const traffic = 'broadband'; - public function __construct(Supplier $o) + public function __construct(int $sid) { - $this->o = $o; + $this->sid = $sid; } /** @@ -50,17 +50,18 @@ final class BroadbandTraffic implements ShouldQueue */ public function handle() { - Log::info(sprintf('%s:Importing Broadband Traffic from [%s]',self::LOGKEY,$this->o->name)); + $so = Supplier::findOrFail($this->sid); + Log::info(sprintf('%s:Importing Broadband Traffic from [%s]',self::LOGKEY,$so->name)); - if ((! $connection=$this->o->detail->connections->get('broadband')) || (count(array_intersect(array_keys($connection),ExternalSupplier::traffic_connection_keys)) !== 3)) + if ((! $connection=$so->detail->connections->get('broadband')) || (count(array_intersect(array_keys($connection),ExternalSupplier::traffic_connection_keys)) !== 3)) throw new \Exception('No or missing connection details for:'.self::traffic); $u = 0; // Load our class for this supplier - $class = self::class_prefix.$this->o->name; + $class = self::class_prefix.$so->name; if (class_exists($class)) { - $o = new $class($this->o); + $o = new $class($so); } else { Log::error(sprintf('%s: Class doesnt exist: %s',self::LOGKEY,$class)); @@ -75,7 +76,7 @@ final class BroadbandTraffic implements ShouldQueue Log::notice(sprintf('%s:Next update is [%s]',self::LOGKEY,$last_update->format('Y-m-d'))); // Delete traffic, since we'll refresh it. - UsageBroadband::where('supplier_id',$this->o->id) + UsageBroadband::where('supplier_id',$so->id) ->where('date',$last_update->format('Y-m-d')) ->delete(); @@ -107,7 +108,7 @@ final class BroadbandTraffic implements ShouldQueue $to = new UsageBroadband; $to->date = $last_update; - $to->supplier_id = $this->o->id; + $to->supplier_id = $so->id; $to->up_peak = $row[$o->getColumnKey('Peak upload')]; $to->up_offpeak = $row[$o->getColumnKey('Off peak upload')]; $to->down_peak = $row[$o->getColumnKey('Peak download')]; @@ -139,17 +140,17 @@ final class BroadbandTraffic implements ShouldQueue Log::info(sprintf('%s: Records Imported [%d] for [%s]',self::LOGKEY,$u,$last_update->format('Y-m-d'))); // Save our current progress. - $this->o->detail->connections = $this->o->detail->connections->put(self::traffic,array_merge($connection,['last'=>$last_update->format('Y-m-d')])); - $this->o->detail->save(); + $so->detail->connections = $so->detail->connections->put(self::traffic,array_merge($connection,['last'=>$last_update->format('Y-m-d')])); + $so->detail->save(); // Update our details for the next iteration. $last_update = $last_update->addDay(); Arr::set($connection,'last',$last_update->format('Y-m-d')); if ($u) { - if ($this->o->trafficMismatch($date)->count()) + if ($so->trafficMismatch($date)->count()) Mail::to('deon@graytech.net.au') // @todo To change - ->send(new TrafficMismatch($this->o,$date)); + ->send(new TrafficMismatch($so,$date)); } } } diff --git a/routes/console.php b/routes/console.php index 1e186ad..227bea7 100644 --- a/routes/console.php +++ b/routes/console.php @@ -3,8 +3,7 @@ use Illuminate\Support\Facades\Schedule; use App\Jobs\BroadbandTraffic; -use App\Models\Supplier; -Schedule::job(new BroadbandTraffic(Supplier::find(1))) +Schedule::job(new BroadbandTraffic(1)) ->timezone('Australia/Melbourne') - ->dailyAt('10:00'); \ No newline at end of file + ->dailyAt('10:00');