Add glmLock trait
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 4 Jun 2019 14:56:06 +0000 (10:56 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 4 Jun 2019 14:56:36 +0000 (10:56 -0400)
Trait to use for locking processes.

classes/glmLockTrait.php [new file with mode: 0644]
index.php

diff --git a/classes/glmLockTrait.php b/classes/glmLockTrait.php
new file mode 100644 (file)
index 0000000..71c7d5a
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+trait GlmLock
+{
+    public function requestLock( $config )
+    {
+        // Setup Default config
+        $defaultConfig = array(
+            'lockName' => 'defaultLockName',
+            'timeout'  => 30,
+            'wait'     => false,
+        );
+        $config = array_merge( $defaultConfig, $config );
+
+        // Check for currently running process
+        $myId = rand();
+
+        // Try to get any current DB Lock
+        $lockName = $config['lockName'];
+        $lock     = get_option( $lockName );
+
+        // If another instance is doing a database update
+        // (If we got something back that's a 2 el array that doesn't have my ID and hasn't expired)
+        if ( $lock && is_array( $lock ) && count( $lock ) == 2 && $lock[0] != $myId && $lock[1] > microtime( true ) ) {
+            trigger_error('GlmLock::requestLock() - Lock Name: '.$config['lockName'].' - Instance: '.$myId.'. - Another instance is processing', E_USER_NOTICE );
+            return false;
+        } else {
+            trigger_error('GlmLock::requestLock() - Lock Name: '.$config['lockName'].' - Instance: '.$myId.' - This instance trying to lock', E_USER_NOTICE );
+
+            // Add our ID and timeout then wait a second
+            add_option( $lockName, array( $myId, ( microtime( true ) + $config['timeout'] ) ) );
+            sleep( 1 );
+
+            // Make sure this instance has the lock
+            $lock = get_option( $lockName );
+            if ( $lock && is_array( $lock ) && count( $lock ) == 2 && $lock[0] == $myId && $lock[1] > microtime( true ) ) {
+                return $lock;
+            } else {
+                return false;
+            }
+        }
+
+        return $lock;
+    }
+
+    public function requestUnLock( $config )
+    {
+        // Setup Default config
+        $defaultConfig = array(
+            'lockName' => 'defaultLockName',
+        );
+        $config = array_merge( $defaultConfig, $config );
+
+        delete_option( $config['lockName'] );
+        trigger_error('GlmLock::requestUnLock() - Lock Name: '.$config['lockName'].' - Removed', E_USER_NOTICE );
+    }
+}
index 5a37a4d..1c38dd5 100755 (executable)
--- a/index.php
+++ b/index.php
@@ -361,6 +361,9 @@ require_once GLM_MEMBERS_PLUGIN_PATH . '/lib/smartyTemplateSupport.php';
 // Load Script and Style Registry
 require_once GLM_MEMBERS_PLUGIN_PATH . '/classes/scriptRegistry.php';
 
+// Load GlmLock Trait
+require_once GLM_MEMBERS_PLUGIN_PATH . '/classes/glmLockTrait.php';
+
 
 // Check all database tables for all registered add-ons, allow install/update, and add various items
 if (glmCheckDatabase('install')) {