forked from ashishjob0424/magento2-patches
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPatch-Algolia-Reduce-Operations.patch
54 lines (50 loc) · 1.98 KB
/
Patch-Algolia-Reduce-Operations.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
From 506938c2696156a7e97589ad4476579c3573e53a Mon Sep 17 00:00:00 2001
From: Alessandro Pagnin <[email protected]>
Date: Thu, 20 Dec 2018 16:37:29 +0100
Subject: [PATCH] Avoid duplicated entry to process
---
Model/Queue.php | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/Model/Queue.php b/Model/Queue.php
index c9ad2ad4..842b2d1a 100644
--- a/Model/Queue.php
+++ b/Model/Queue.php
@@ -64,6 +64,23 @@ public function __construct(
$this->maxSingleJobDataSize = $this->configHelper->getNumberOfElementByPage();
}
+ /**
+ * Check if there is already an item in the queue with the same values
+ */
+ public function alreadyInQueueToProcess($className, $method, $encodedData, $data_size = 1) {
+ $data = $this->db->query(
+ $this->db->select()
+ ->from($this->table, ['jobs_count' => 'COUNT(job_id)'])
+ ->where('class = ?', $className)
+ ->where('method = ?', $method)
+ ->where('data = ?', $encodedData)
+ ->where('data_size = ?', $data_size)
+ );
+ $result = $data->fetch();
+
+ return (int) $result['jobs_count'] > 0 ? true : false;
+ }
+
public function addToQueue($className, $method, $data, $data_size = 1)
{
if (is_object($className)) {
@@ -80,11 +97,15 @@ public function addToQueue($className, $method, $data, $data_size = 1)
private function insert($class, $method, $data, $data_size)
{
+ $encodedData = json_encode($data);
+ if ($this->alreadyInQueueToProcess($class, $method, $encodedData, $data_size)) {
+ return;
+ }
$this->db->insert($this->table, [
'created' => date('Y-m-d H:i:s'),
'class' => $class,
'method' => $method,
- 'data' => json_encode($data),
+ 'data' => $encodedData,
'data_size' => $data_size,
'pid' => null,
]);