Move DB queries into jobs, so that the scheduler and artisan command calls doesnt evaluate them until the job is actually run
This commit is contained in:
parent
844d509834
commit
3c7e2bbbc9
@ -34,11 +34,11 @@ class BroadbandTraffic extends Command
|
|||||||
if ($this->option('supplier')) {
|
if ($this->option('supplier')) {
|
||||||
$o = Supplier::where('name','like',$this->option('supplier'))->singleOrFail();
|
$o = Supplier::where('name','like',$this->option('supplier'))->singleOrFail();
|
||||||
|
|
||||||
Job::dispatchSync($o);
|
Job::dispatchSync($o->id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (Supplier::active()->get() as $o)
|
foreach (Supplier::active()->get() as $o)
|
||||||
Job::dispatchSync($o);
|
Job::dispatchSync($o->id);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -32,14 +32,14 @@ final class BroadbandTraffic implements ShouldQueue
|
|||||||
|
|
||||||
private const LOGKEY = 'JBT';
|
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 class_prefix = 'App\Classes\External\Supplier\\';
|
||||||
|
|
||||||
private const traffic = 'broadband';
|
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()
|
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);
|
throw new \Exception('No or missing connection details for:'.self::traffic);
|
||||||
|
|
||||||
$u = 0;
|
$u = 0;
|
||||||
|
|
||||||
// Load our class for this supplier
|
// Load our class for this supplier
|
||||||
$class = self::class_prefix.$this->o->name;
|
$class = self::class_prefix.$so->name;
|
||||||
if (class_exists($class)) {
|
if (class_exists($class)) {
|
||||||
$o = new $class($this->o);
|
$o = new $class($so);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Log::error(sprintf('%s: Class doesnt exist: %s',self::LOGKEY,$class));
|
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')));
|
Log::notice(sprintf('%s:Next update is [%s]',self::LOGKEY,$last_update->format('Y-m-d')));
|
||||||
|
|
||||||
// Delete traffic, since we'll refresh it.
|
// 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'))
|
->where('date',$last_update->format('Y-m-d'))
|
||||||
->delete();
|
->delete();
|
||||||
|
|
||||||
@ -107,7 +108,7 @@ final class BroadbandTraffic implements ShouldQueue
|
|||||||
|
|
||||||
$to = new UsageBroadband;
|
$to = new UsageBroadband;
|
||||||
$to->date = $last_update;
|
$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_peak = $row[$o->getColumnKey('Peak upload')];
|
||||||
$to->up_offpeak = $row[$o->getColumnKey('Off peak upload')];
|
$to->up_offpeak = $row[$o->getColumnKey('Off peak upload')];
|
||||||
$to->down_peak = $row[$o->getColumnKey('Peak download')];
|
$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')));
|
Log::info(sprintf('%s: Records Imported [%d] for [%s]',self::LOGKEY,$u,$last_update->format('Y-m-d')));
|
||||||
|
|
||||||
// Save our current progress.
|
// 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')]));
|
$so->detail->connections = $so->detail->connections->put(self::traffic,array_merge($connection,['last'=>$last_update->format('Y-m-d')]));
|
||||||
$this->o->detail->save();
|
$so->detail->save();
|
||||||
|
|
||||||
// Update our details for the next iteration.
|
// Update our details for the next iteration.
|
||||||
$last_update = $last_update->addDay();
|
$last_update = $last_update->addDay();
|
||||||
Arr::set($connection,'last',$last_update->format('Y-m-d'));
|
Arr::set($connection,'last',$last_update->format('Y-m-d'));
|
||||||
|
|
||||||
if ($u) {
|
if ($u) {
|
||||||
if ($this->o->trafficMismatch($date)->count())
|
if ($so->trafficMismatch($date)->count())
|
||||||
Mail::to('deon@graytech.net.au') // @todo To change
|
Mail::to('deon@graytech.net.au') // @todo To change
|
||||||
->send(new TrafficMismatch($this->o,$date));
|
->send(new TrafficMismatch($so,$date));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,7 @@
|
|||||||
use Illuminate\Support\Facades\Schedule;
|
use Illuminate\Support\Facades\Schedule;
|
||||||
|
|
||||||
use App\Jobs\BroadbandTraffic;
|
use App\Jobs\BroadbandTraffic;
|
||||||
use App\Models\Supplier;
|
|
||||||
|
|
||||||
Schedule::job(new BroadbandTraffic(Supplier::find(1)))
|
Schedule::job(new BroadbandTraffic(1))
|
||||||
->timezone('Australia/Melbourne')
|
->timezone('Australia/Melbourne')
|
||||||
->dailyAt('10:00');
|
->dailyAt('10:00');
|
||||||
|
Loading…
Reference in New Issue
Block a user