Browse Source

add background job for sending trip data to subscribers

pull/5/head
Aaron Parecki 9 years ago
parent
commit
ef17d8b562
4 changed files with 492 additions and 200 deletions
  1. +120
    -0
      compass/app/Jobs/TripComplete.php
  2. +3
    -1
      compass/composer.json
  3. +336
    -199
      compass/composer.lock
  4. +33
    -0
      compass/database/migrations/2015_10_16_171131_create_failed_jobs_table.php

+ 120
- 0
compass/app/Jobs/TripComplete.php View File

@ -0,0 +1,120 @@
<?php
namespace App\Jobs;
use DB;
use Log;
use Quartz;
use p3k\Multipart;
use App\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
class TripComplete extends Job implements SelfHandling, ShouldQueue
{
private $_dbid;
private $_data;
public function __construct($dbid, $data) {
$this->_dbid = $dbid;
$this->_data = $data;
}
public function handle() {
print_r($this->_data);
$db = DB::table('databases')->where('id','=',$this->_dbid)->first();
if(!$db->micropub_endpoint) {
Log::info('No micropub endpoint configured for database ' . $db->name);
return;
}
$qz = new Quartz\DB(env('STORAGE_DIR').$db->name, 'r');
// Build the GeoJSON for this trip
$geojson = [
'type' => 'FeatureCollection',
'features' => [
[
'type' => 'Feature',
'geometry' => $this->_data['geometry'],
'properties' => []
]
]
];
$file_path = tempnam(sys_get_temp_dir(), 'compass');
file_put_contents($file_path, json_encode($geojson));
// Reverse geocode the start and end location to get an h-adr
$startAdr = [
'type' => 'h-adr',
'properties' => [
'latitude' => $this->_data['geometry']['coordinates'][1],
'longitude' => $this->_data['geometry']['coordinates'][0],
]
];
$endAdr = [
'type' => 'h-adr',
'properties' => [
'latitude' => $this->_data['geometry']['coordinates'][1],
'longitude' => $this->_data['geometry']['coordinates'][0],
]
];
$distance = 10;
$duration = 100;
$params = [
'h' => 'entry',
'created' => $this->_data['properties']['end'],
'published' => $this->_data['properties']['end'],
'route' => [
'type' => 'h-route',
'properties' => [
'activity' => $this->_data['properties']['mode'],
'start-location' => $startAdr,
'end-location' => $endAdr,
'distance' => [
'type' => 'h-measure',
'properties' => [
'num' => $distance,
'unit' => 'meter'
]
],
'duration' => [
'type' => 'h-measure',
'properties' => [
'num' => $duration,
'unit' => 'second'
]
],
// TODO: avgpace
// TODO: avgspeed
]
]
];
$multipart = new Multipart();
$multipart->addArray($params);
$multipart->addFile('geojson', $file_path, 'application/json');
$httpheaders = [
'Authorization: Bearer ' . $db->micropub_token,
'Content-type: ' . $multipart->contentType()
];
// Post to the Micropub endpoint
$ch = curl_init($db->micropub_endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders);
curl_setopt($ch, CURLOPT_POSTFIELDS, $multipart->data());
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
echo "========\n";
echo $response."\n========\n";
echo "\n";
}
}

+ 3
- 1
compass/composer.json View File

@ -10,7 +10,9 @@
"vlucas/phpdotenv": "~1.0", "vlucas/phpdotenv": "~1.0",
"p3k/quartz-db": "0.1.*", "p3k/quartz-db": "0.1.*",
"indieauth/client": "0.1.*", "indieauth/client": "0.1.*",
"guzzlehttp/guzzle":"~6.0"
"guzzlehttp/guzzle":"~6.0",
"illuminate/redis": "^5.1",
"p3k/multipart": "*"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "~4.0", "phpunit/phpunit": "~4.0",

+ 336
- 199
compass/composer.lock
File diff suppressed because it is too large
View File


+ 33
- 0
compass/database/migrations/2015_10_16_171131_create_failed_jobs_table.php View File

@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFailedJobsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('failed_jobs', function (Blueprint $table) {
$table->increments('id');
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->timestamp('failed_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('failed_jobs');
}
}

Loading…
Cancel
Save