You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.2 KiB

  1. <?php
  2. namespace App\Jobs;
  3. use DB, Log;
  4. use App\Jobs\Job;
  5. use Illuminate\Contracts\Bus\SelfHandling;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use DateTime, DateTimeZone;
  8. class NotifyOfNewLocations extends Job implements SelfHandling, ShouldQueue
  9. {
  10. private $_dbid;
  11. public function __construct($dbid) {
  12. $this->_dbid = $dbid;
  13. }
  14. public function handle() {
  15. $db = DB::table('databases')->where('id','=',$this->_dbid)->first();
  16. $urls = preg_split('/\s+/', $db->ping_urls);
  17. foreach($urls as $url) {
  18. if(trim($url)) {
  19. $ch = curl_init($url);
  20. curl_setopt($ch, CURLOPT_POST, true);
  21. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  22. 'Content-Type: application/json',
  23. 'Authorization: Bearer '.$db->read_token,
  24. 'Compass-Url: '.env('BASE_URL').'api/last?token='.$db->read_token.'&geocode=1'
  25. ]);
  26. curl_setopt($ch, CURLOPT_POSTFIELDS, $db->last_location);
  27. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  28. curl_exec($ch);
  29. $timestamp = '';
  30. if($db->last_location) {
  31. $timestamp = json_decode($db->last_location)->properties->timestamp;
  32. }
  33. Log::info("Notifying ".$url." with current location: ".$timestamp);
  34. }
  35. }
  36. }
  37. }