Skip to content

A multi-server lock service for Magento 2

License

Notifications You must be signed in to change notification settings

snowio/magento2-lock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Magento 2 Lock

Description

A Magento 2 module which provides a multi-server lock service. This allows developers to apply mutual exclusion locks to shared resources so that they are not accessed simultaneously.

Prerequisites

Installation

composer require snowio/magento2-lock
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:flush

Usage

The lock service can be accessed through dependency injection. Please refer the dependency injection section of the Magento DevDocs for more information on how to use dependency injection.

public boolean LockService::acquireLock(string $name, int $timeout)

Attempt to obtain a lock ####Parameters

  • $lockName : The lock identifier/name
  • $timeout : Lock timeout. A negative timeout implies an infinite timeout.

####Return Values A boolean indicating if the the lock was acquired.

public LockService::releaseLock($lockName)

Release the lock

Parameters

  • $lockName : The lock identifier/name

Example

namespace Vendor\Module\Model\Accessors;
class ResourceAccessor
{
    private $lockService;
    
    public function __construct(
        SnowIO\Lock\Api\LockService $lockService
    ) {
        $this->lockService = $lockService    
    }
    
    public function access($resource)
    {
        $lockName = //.. resource lock name
        
        //try acquire the lock
        if (!$this->lockService->acquireLock($lockName, 0)) {
            //Lock was not acquired ...
        }
        //Lock was acquired 
        
        try {
            // Process $resource 
        } finally {
            //release the lock
            $this->lockService->releaseLock($lockName);
        }
        
    }
}

Applications

License

This software is licensed under the MIT License. View the license

About

A multi-server lock service for Magento 2

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages