Skip to content

Commit a7d6f6d

Browse files
author
James Wigger
committed
Added incrementing attempts when geocoding. Fixed nested function calls on updating indivdual properties
1 parent 8829640 commit a7d6f6d

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

perch/addons/apps/jw_locator/classes/JwLocator_Location.class.php

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,15 @@ class JwLocator_Location extends PerchAPI_Base
6666
*/
6767
public function update($data, $force_geocoding = false, $ignore_timestamp = false)
6868
{
69-
if (!$ignore_timestamp) {
70-
$this->set_status(1);
71-
$data['locationUpdatedAt'] = date("Y-m-d H:i:s");
69+
$this->reset_attempts();
70+
$this->set_status(1);
71+
$data['locationUpdatedAt'] = date("Y-m-d H:i:s");
7272

73-
// Move here, as this is a 'normal' update
74-
$Error = $this->get_error();
73+
// Move here, as this is a 'normal' update
74+
$Error = $this->get_error();
7575

76-
if (is_object($Error)) {
77-
$Error->delete();
78-
}
76+
if (is_object($Error)) {
77+
$Error->delete();
7978
}
8079

8180
$result = parent::update($data);
@@ -280,11 +279,12 @@ public function geocode()
280279
$Marker->update($marker_data);
281280
}
282281

283-
$this->update(array(
284-
'locationProcessedAt' => date("Y-m-d H:i:s")
285-
), false, true);
282+
$this->db->update($this->table, array(
283+
'locationProcessedAt' => date("Y-m-d H:i:s"),
284+
'locationProcessingStatus' => self::STATUS_SYNCED,
285+
'locationProcessingAttempts' => 0
286+
), $this->pk, $this->id());
286287

287-
$this->set_status(3);
288288
} else {
289289
$status = $response['status'];
290290

@@ -307,6 +307,7 @@ public function geocode()
307307
break;
308308
}
309309

310+
$this->increment_attempts();
310311
$this->set_status(4);
311312
}
312313
}
@@ -340,16 +341,38 @@ public function map_preview()
340341
return '<img class="map-preview" src="' . $base_url . http_build_query($parameters) . '" />';
341342
}
342343

344+
/**
345+
* Reset attempts counter to zero
346+
*/
347+
private function reset_attempts()
348+
{
349+
$this->db->update($this->table, array(
350+
'locationProcessingAttempts' => 0
351+
), $this->pk, $this->id());
352+
353+
$this->details['locationProcessingAttempts'] = 0;
354+
}
355+
356+
/**
357+
* Update the location processing attempts column
358+
*/
359+
private function increment_attempts()
360+
{
361+
$this->db->update($this->table, array(
362+
'locationProcessingAttempts' => ($this->locationProcessingAttempts() + 1)
363+
), $this->pk, $this->id());
364+
}
365+
343366
/**
344367
* Set job status code
345368
*
346369
* @param int $status_code 1: queued, 2: processing, 3: synced, 4: Failed
347370
*/
348371
private function set_status($status_code = 1)
349372
{
350-
$this->update(array(
373+
$this->db->update($this->table, array(
351374
'locationProcessingStatus' => $status_code
352-
), false, true);
375+
), $this->pk, $this->id());
353376
}
354377

355378
/**
@@ -362,9 +385,9 @@ private function set_marker($data)
362385
$Markers = new JwLocator_Markers($this->api);
363386
$Marker = $Markers->create($data);
364387

365-
$this->update(array(
388+
$this->db->update($this->table, array(
366389
'markerID' => $Marker->id()
367-
), false, true);
390+
), $this->pk, $this->id());
368391
}
369392

370393
/**
@@ -385,5 +408,4 @@ private function set_error($error_message)
385408

386409
return $Error;
387410
}
388-
389411
}

perch/addons/apps/jw_locator/classes/JwLocator_Locations.class.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ class JwLocator_Locations extends PerchAPI_Factory
6767
'markerID'
6868
);
6969

70+
/**
71+
* Maximum attempts to geocode before erroring
72+
*
73+
* @var int
74+
*/
75+
const MAX_ATTEMPTS = 3;
76+
7077
/**
7178
* Fetch locations in array of IDs
7279
*
@@ -194,6 +201,7 @@ public function get_queued($batch_limit = 25)
194201
{
195202
$sql = 'SELECT * FROM ' . $this->table;
196203
$sql .= ' WHERE `locationUpdatedAt` > `locationProcessedAt`';
204+
$sql .= ' AND `locationProcessingAttempts` < ' . $this->db->pdb(self::MAX_ATTEMPTS);
197205
$sql .= ' ORDER BY `locationUpdatedAt` ASC';
198206
$sql .= ' LIMIT ' . $batch_limit;
199207

0 commit comments

Comments
 (0)