Skip to content

Commit 81e8d41

Browse files
authored
Merge pull request #63 from chadicus/bug/object-as-array
Bug/object as array
2 parents 3444245 + 46a1abb commit 81e8d41

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

.travis.yml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@ language: php
22
php:
33
- 7.0
44
- 5.6
5-
env:
6-
- DB_PACKAGE=mongodb-10gen
7-
- DB_PACKAGE=mongodb-org
8-
before_install:
9-
- "sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10"
10-
- "echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list"
11-
- "sudo apt-get update"
12-
- "sudo apt-get install ${DB_PACKAGE}"
5+
services:
6+
- mongodb
137
before_script:
148
- composer self-update
15-
- printf "\n" | pecl install --force mongodb
16-
# wait for mongo, start is only needed for 2.4 package, see http://tldp.org/LDP/abs/html/devref1.html for description of this syntax.
17-
- sudo service mongodb start; bash -c 'while ! exec 6<>/dev/tcp/localhost/27017; do echo "$(date) - still trying to connect to mongo"; sleep 1; done'
9+
- yes '' | pecl install -f mongodb-1.1
1810
script: ./build.php
1911
after_script: ./vendor/bin/coveralls -v

src/Queue.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ public function get(array $query, $runningResetDuration, $waitDurationInMillis =
175175
$resetTimestamp = min(max(0, $resetTimestamp * 1000), self::MONGO_INT32_MAX);
176176

177177
$update = ['$set' => ['earliestGet' => new UTCDateTime($resetTimestamp)]];
178-
$options = ['sort' => ['priority' => 1, 'created' => 1]];
178+
$findOneAndUpdateOptions = ['sort' => ['priority' => 1, 'created' => 1]];
179+
$findOneOptions = ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']];
179180

180181
//ints overflow to floats, should be fine
181182
$end = microtime(true) + ($waitDurationInMillis / 1000.0);
@@ -189,14 +190,14 @@ public function get(array $query, $runningResetDuration, $waitDurationInMillis =
189190
} //@codeCoverageIgnoreEnd
190191

191192
while (true) {
192-
$message = $this->collection->findOneAndUpdate($completeQuery, $update, $options);
193-
//checking if _id exist because findAndModify doesnt seem to return null when it can't match the query on
194-
//older mongo extension
195-
if ($message !== null && array_key_exists('_id', $message)) {
193+
$id = $this->getIdFromMessage(
194+
$this->collection->findOneAndUpdate($completeQuery, $update, $findOneAndUpdateOptions)
195+
);
196+
if ($id !== null) {
196197
// findOneAndUpdate does not correctly return result according to typeMap options so just refetch.
197-
$message = $this->collection->findOne(['_id' => $message->_id]);
198+
$message = $this->collection->findOne(['_id' => $id], $findOneOptions);
198199
//id on left of union operator so a possible id in payload doesnt wipe it out the generated one
199-
return ['id' => $message['_id']] + (array)$message['payload'];
200+
return ['id' => $id] + $message['payload'];
200201
}
201202

202203
if (microtime(true) >= $end) {
@@ -211,6 +212,19 @@ public function get(array $query, $runningResetDuration, $waitDurationInMillis =
211212
}
212213
//@codeCoverageIgnoreEnd
213214

215+
private function getIdFromMessage($message)
216+
{
217+
if (is_array($message)) {
218+
return array_key_exists('_id', $message) ? $message['_id'] : null;
219+
}
220+
221+
if (is_object($message)) {
222+
return isset($message->_id) ? $message->_id : null;
223+
}
224+
225+
return null;
226+
}
227+
214228
/**
215229
* Count queue messages.
216230
*

0 commit comments

Comments
 (0)