from(Address::UncollectedEchomailTotal()->union(Address::UncollectedNetmailTotal())->union(Address::UncollectedFilesTotal()),'a') ->where('systems.active',true) ->where('addresses.active',TRUE) ->where('zones.active',TRUE) ->where('domains.active',TRUE) ->join('addresses',['addresses.id'=>'a.id']) ->join('systems',['systems.id'=>'a.system_id']) ->join('zones',['zones.id'=>'a.zone_id']) ->join('domains',['domains.id'=>'zones.domain_id']) ->where(function($query) { return $query->whereNull('autohold') ->orWhere('autohold',FALSE); }) ->when(! is_null($this->crash),function($query) { return $query->when( $this->crash, function($query) { return $query->where('pollmode',$this->crash); }, function($query) { return $query->whereNotNull('pollmode'); } ); }) ->groupBy('a.system_id','a.id','a.zone_id','addresses.region_id','a.host_id','a.node_id','a.point_id','addresses.hub_id','addresses.role') ->with(['system','zone.domain']) ->get(); // Return the system we poll $u = $u->transform(function($item) { if ($x=$item->parent()) { $x->uncollected_echomail = $item->uncollected_echomail; $x->uncollected_netmail = $item->uncollected_netmail; $x->uncollected_files = $item->uncollected_files; return $x; } else { return $item; } }); foreach ($u->groupBy('ftn') as $oo) { if (Job::where('queue','poll')->get()->pluck('command.address.id')->search(($x=$oo->first())->id) === FALSE) { Log::info(sprintf('%s:- Polling [%s] - we have mail for [%d] links. (%d Netmail,%d Echomail,%d Files)', self::LOGKEY, $x->ftn, $oo->count(), $oo->sum('uncollected_netmail'), $oo->sum('uncollected_echomail'), $oo->sum('uncollected_files'), )); AddressPoll::dispatch($x); } else { Log::notice(sprintf('%s:= Not scheduling poll to [%s], there is already one in the queue',self::LOGKEY,$x->ftn)); } } } }