3
3
namespace Jenssegers \Mongodb ;
4
4
5
5
use Exception ;
6
+ use Illuminate \Support \Facades \Log ;
6
7
use MongoDB \BSON \ObjectID ;
7
8
use MongoDB \Collection as MongoCollection ;
8
9
@@ -23,8 +24,8 @@ class Collection
23
24
protected $ collection ;
24
25
25
26
/**
26
- * @param Connection $connection
27
- * @param MongoCollection $collection
27
+ * @param Connection $connection
28
+ * @param MongoCollection $collection
28
29
*/
29
30
public function __construct (Connection $ connection , MongoCollection $ collection )
30
31
{
@@ -35,14 +36,33 @@ public function __construct(Connection $connection, MongoCollection $collection)
35
36
/**
36
37
* Handle dynamic method calls.
37
38
*
38
- * @param string $method
39
- * @param array $parameters
39
+ * @param string $method
40
+ * @param array $parameters
40
41
* @return mixed
41
42
*/
42
43
public function __call (string $ method , array $ parameters )
43
44
{
44
45
$ start = microtime (true );
45
- $ result = $ this ->collection ->$ method (...$ parameters );
46
+ try {
47
+ $ result = $ this ->collection ->$ method (...$ parameters );
48
+ } catch (Exception $ e ) {
49
+ // 11602 - operation was interrupted
50
+ // 10107 - not primary
51
+ if ($ e ->getCode () == 11602 || $ e ->getCode () == 10107 ) {
52
+ Log::info (
53
+ 'Query execution was interrupted and will be retried ' ,
54
+ ['code ' => $ e ->getCode (), 'mess ' => $ e ->getMessage (), 'params ' => $ parameters ]
55
+ );
56
+ } else {
57
+ Log::info ('Unexpected error ' , [
58
+ 'mess ' => $ e ->getMessage (),
59
+ 'code ' => $ e ->getCode (),
60
+ 'file ' => $ e ->getFile (),
61
+ 'line ' => $ e ->getLine (),
62
+ ]);
63
+ }
64
+ $ result = $ this ->collection ->$ method (...$ parameters );
65
+ }
46
66
47
67
// Once we have run the query we will calculate the time that it took to run and
48
68
// then log the query, bindings, and execution time so we will report them on
@@ -54,7 +74,7 @@ public function __call(string $method, array $parameters)
54
74
// Convert the query parameters to a json string.
55
75
array_walk_recursive ($ parameters , function (&$ item , $ key ) {
56
76
if ($ item instanceof ObjectID) {
57
- $ item = (string ) $ item ;
77
+ $ item = (string )$ item ;
58
78
}
59
79
});
60
80
@@ -67,7 +87,7 @@ public function __call(string $method, array $parameters)
67
87
}
68
88
}
69
89
70
- $ queryString = $ this ->collection ->getCollectionName (). '. ' . $ method. '( ' . implode (', ' , $ query ). ') ' ;
90
+ $ queryString = $ this ->collection ->getCollectionName () . '. ' . $ method . '( ' . implode (', ' , $ query ) . ') ' ;
71
91
72
92
$ this ->connection ->logQuery ($ queryString , [], $ time );
73
93
0 commit comments