29
29
* ------------------------------------------------------------------------------
30
30
*/
31
31
32
- use GlpiPlugin \Flyvemdm \Exception \FusionInventoryRuleInconsistency ;
32
+ use GlpiPlugin \Flyvemdm \Exception \FusionInventoryRuleInconsistency ;
33
+ use GlpiPlugin \Flyvemdm \Exception \EntityRuleEditionException ;
33
34
34
35
if (!defined ('GLPI_ROOT ' )) {
35
36
die ("Sorry. You can't access this file directly " );
@@ -39,6 +40,9 @@ class PluginFlyvemdmFusionInventory {
39
40
40
41
const RULE_NAME = 'Flyve MDM invitation to entity ' ;
41
42
43
+ const LOCK_NAME = 'flyvemdm_entityRule ' ;
44
+
45
+
42
46
/**
43
47
* Creates or updates an entity rule
44
48
*
@@ -52,6 +56,10 @@ public function addInvitationRule(PluginFlyvemdmInvitation $invitation) {
52
56
Session::addMessageAfterRedirect (__ ('Unable to get rule for entity ' , 'flyvemdm ' ),
53
57
true , ERROR );
54
58
return ;
59
+ } catch (EntityRuleEditionException $ exception ) {
60
+ Session::addMessageAfterRedirect (__ ('Cannot get lock for entity rules edition ' , 'flyvemdm ' ),
61
+ true , ERROR );
62
+ return ;
55
63
}
56
64
57
65
$ ruleCriteria = new RuleCriteria ();
@@ -117,6 +125,19 @@ public function deleteInvitationRuleCriteria(PluginFlyvemdmInvitation $invitatio
117
125
]);
118
126
119
127
$ ruleId = $ row [$ ruleFk ];
128
+
129
+ // get a lock
130
+ $ attempts = 0 ;
131
+ $ locked = 0 ;
132
+ do {
133
+ $ locked = $ DB ->getLock (self ::LOCK_NAME );
134
+ usleep (50000 ); // 50 milliseconds
135
+ $ attempts ++;
136
+ } while ($ locked !== 1 && $ attempts < 10 );
137
+ if ($ locked !== 1 ) {
138
+ return ; // No lock, then give up disabling
139
+ }
140
+
120
141
$ rows = $ ruleCriteria ->find ("` $ ruleFk` = ' $ ruleId' AND `criteria` = 'tag' AND `condition` = '0' " );
121
142
if (count ($ rows ) === 0 ) {
122
143
$ rule = new PluginFusioninventoryInventoryRuleEntity ();
@@ -125,6 +146,7 @@ public function deleteInvitationRuleCriteria(PluginFlyvemdmInvitation $invitatio
125
146
'is_active ' => '0 ' ,
126
147
]);
127
148
}
149
+ $ DB ->releaseLock (self ::LOCK_NAME );
128
150
}
129
151
130
152
/**
@@ -144,10 +166,23 @@ private function getRuleCriteriaValue(PluginFlyvemdmInvitation $invitation) {
144
166
*
145
167
* @return PluginFusioninventoryInventoryRuleEntity|null
146
168
* @throws FusionInventoryRuleInconsistency
169
+ * @throws EntityRuleEditionException
147
170
*/
148
171
private function getRule ($ entityId , $ create = true ) {
149
172
global $ DB ;
150
173
174
+ // get a lock
175
+ $ attempts = 0 ;
176
+ $ locked = 0 ;
177
+ do {
178
+ $ locked = $ DB ->getLock (self ::LOCK_NAME );
179
+ usleep (50000 ); // 50 milliseconds
180
+ $ attempts ++;
181
+ } while ($ locked !== 1 && $ attempts < 10 );
182
+ if ($ locked !== 1 ) {
183
+ throw new EntityRuleEditionException (__ ('Cannot get lock for entity rules edition ' ));
184
+ }
185
+
151
186
$ ruleEntityTable = PluginFusioninventoryInventoryRuleEntity::getTable ();
152
187
$ ruleActionTable = RuleAction::getTable ();
153
188
$ request = [
@@ -175,13 +210,16 @@ private function getRule($entityId, $create = true) {
175
210
$ rule = new PluginFusioninventoryInventoryRuleEntity ();
176
211
$ row = $ result ->next ();
177
212
$ rule ->getFromDB ($ row ['id ' ]);
213
+ $ DB ->releaseLock (self ::LOCK_NAME );
178
214
return $ rule ;
179
215
}
180
216
if ($ result ->count () > 1 ) {
217
+ $ DB ->releaseLock (self ::LOCK_NAME );
181
218
throw new FusionInventoryRuleInconsistency (__ ('Import rule is not unique ' ));
182
219
}
183
220
184
221
if (!$ create ) {
222
+ $ DB ->releaseLock (self ::LOCK_NAME );
185
223
return null ;
186
224
}
187
225
@@ -203,6 +241,7 @@ private function getRule($entityId, $create = true) {
203
241
'field ' => Entity::getForeignKeyField (),
204
242
'value ' => $ entityId ,
205
243
]);
244
+ $ DB ->releaseLock (self ::LOCK_NAME );
206
245
return $ rule ;
207
246
}
208
247
}
0 commit comments