Created
October 24, 2014 20:23
-
-
Save bouassaba/516d3e82fd9a4316d053 to your computer and use it in GitHub Desktop.
Magento Lock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BigThings | |
{ | |
/** | |
* Our process ID. | |
*/ | |
const PROCESS_ID = 'bigthings'; | |
/** | |
* Mage_Index_Model_Process will provide us a lock file API. | |
* | |
* @var Mage_Index_Model_Process $indexProcess | |
*/ | |
private $indexProcess; | |
/** | |
* Constructor. Instantiate the Process model, and set our custom | |
* batch process ID. | |
*/ | |
public function __construct() | |
{ | |
$this->indexProcess = new Mage_Index_Model_Process(); | |
$this->indexProcess->setId(self::PROCESS_ID); | |
} | |
/** | |
* Process all the things! | |
*/ | |
public function changeEverything() | |
{ | |
if ($this->indexProcess->isLocked()) | |
{ | |
$this->log(sprintf("Another %s process is running! Abort", self::PROCESS_ID)); | |
return false; | |
} | |
// Set an exclusive lock. | |
$this->indexProcess->lockAndBlock(); | |
$this->log("Starting...hold on to your hats."); | |
for ($i = 1; $i < 10000000000; $i++) | |
{ | |
// Do something complicated with $i. | |
} | |
$this->log("Finished! Whew."); | |
// Remove the lock. | |
$this->indexProcess->unlock(); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment