2017 © Pedro Peláez
 

symfony-bundle semaphore-bundle

A generic locking Symfony bundle for PHP, that uses named locks for semaphore acquire and release, to guarantee atomicity

image

avtonom/semaphore-bundle

A generic locking Symfony bundle for PHP, that uses named locks for semaphore acquire and release, to guarantee atomicity

  • Friday, November 18, 2016
  • by avtonomspb
  • Repository
  • 1 Watchers
  • 10 Stars
  • 581 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 1 Open issues
  • 6 Versions
  • 1 % Grown

The README.md

Avtonom semaphore bundle

Integrates zerkalica/semaphore library into Symfony2., (*1)

A generic locking Symfony bundle for PHP, that uses named locks for semaphore acquire and release, to guarantee atomicity., (*2)

Locking is useful for controlling access to resources in a multi-process or distributed environment., (*3)

The idea is that the actual locking mechanism can be implemented in any way you like, in order to fit your technology stack., (*4)

Page bundle: https://github.com/Avtonom/semaphore-bundle, (*5)

Support adapters

  • redis
  • memcached
  • sem (native functionality)
  • apc
  • flock (filesystem)
    • doctrine/orm , pdo, sql - (in future versions)

Features

  • Check for attempts to lock the same process. There are two variants of this reaction: throw an exception or extend the lifetime of the key.
  • Check before removing the key, he put this process. It is to be found that the key did not die at the time and did not put another process.
  • Notification that the script finished and forgot to remove the lock.
  • Logging operations with all the necessary information on a specific channel in monolog (channel: semaphore)
  • Select the class for the lock manager (SemaphoreManager)
  • Select the object class contains a list of key locks (KeyStorage)
  • Demo mode - keeping all logging but does not execute set and validates the key.
  • Setting parameters such as:
    • Number of attempts to acquire a lock
    • Waiting time between attempts to acquire a lock
    • Lifetime lock
    • Prefix key lock

Maybe in the future: * Notice that the lock had died on key lifetimes * Adapters for doctrine/orm, pdo, sql * The administrative interface (Sonata Project) for a list of locks and log management actions * Stopwatch to see semaphore usage in the timeline of the symfony debug toolbar, (*6)

To Install

Run the following in your project root, assuming you have composer set up for your project, (*7)


composer.phar require avtonom/semaphore-bundle ^1.4 composer require snc/redis-bundle

Switching ^1.4 for the most recent tag., (*8)

Add the bundle to app/AppKernel.php, (*9)


$bundles( ... new Avtonom\SemaphoreBundle\AvtonomSemaphoreBundle(), new Snc\RedisBundle\SncRedisBundle(), ... );

Configuration options (config.yaml):, (*10)

``` yaml, (*11)

snc_redis: clients: semaphore: type: predis alias: semaphore dsn: redis://localhost/2, (*12)

avtonom_semaphore: adapter_redis_client: snc_redis.semaphore key_storage_class: Application\Avtonom\SemaphoreBundle\SemaphoreKeyStorage #mode: demo # activation of the demo mode - keeping all logging but does not execute set and validates the key. # default # adapter: avtonom_semaphore.adapter.redis # manager_class: Avtonom\SemaphoreBundle\Model\SemaphoreManager # is_exception_repeat_block_key: true # Generate an error when you try to re-key block in the same process. # use_extended_methods: true, (*13)


Configuration options (parameters.yaml): ``` yaml parameters: # default # avtonom_semaphore.try_count: 240 # try count, if lock not acquired. 240 count * 1/2 sec (sleep wait) = 120 sec wait # avtonom_semaphore.sleep_time: 500000 # sleep time in microseconds, if lock not acquired. 1.000.000 microseconds = 1 seconds # avtonom_semaphore.max_lock_time: 60 # ttl - max lock time # avtonom_semaphore.prefix: 'lock_'

``` php, (*14)

use Avtonom\SemaphoreBundle\Traits\SemaphoreTrait\SemaphoreTrait;, (*15)

// or, (*16)

/** @var $semaphore \Avtonom\Semaphore\Model\SemaphoreManagerInterface */ $semaphore = $container->get('avtonom_semaphore.manager');, (*17)

$lockKeyStorage = $this->getLockKeyStorage(); $lockKey = [$lockKeyStorage::MY_KEY, $param1, $param2, $paramN]; $this->lockAcquire($lockKey, METHOD, 10 /* 10 - if you personal lock expire time in seconds. default 60 */);, (*18)

// Do something thread-safe, (*19)

$this->lockRelease($lockKey, METHOD);, (*20)


Create Application\Avtonom\SemaphoreBundle\KeyStorage class: ``` php <?php namespace Application\Avtonom\SemaphoreBundle; use Avtonom\SemaphoreBundle\Model\SemaphoreKeyStorageInterface; class SemaphoreKeyStorage implements SemaphoreKeyStorageInterface { const MY_KEY = 'M_K_', MY_OTHER_KEY = 'M_O_K_' ; }

Need Help?

  1. Please look the log file %kernel.logs_dir%/%kernel.environment%.semaphore.log
  2. Create an issue if you've found a bug,

The Versions

18/11 2016

dev-master

9999999-dev https://github.com/Avtonom/semaphore-bundle

A generic locking Symfony bundle for PHP, that uses named locks for semaphore acquire and release, to guarantee atomicity

  Sources   Download

MIT

The Requires

 

by Anton U

redis memcached symfony2 bundle atom lock locking release semaphore flock sem atomicity acquire

26/02 2016

v1.1.6

1.1.6.0 https://github.com/Avtonom/semaphore-bundle

A generic locking Symfony bundle for PHP, that uses named locks for semaphore acquire and release, to guarantee atomicity

  Sources   Download

MIT

The Requires

 

by Anton U

redis memcached symfony2 bundle atom lock locking release semaphore flock sem atomicity acquire

21/10 2015

v1.1.5

1.1.5.0 https://github.com/Avtonom/semaphore-bundle

A generic locking Symfony bundle for PHP, that uses named locks for semaphore acquire and release, to guarantee atomicity

  Sources   Download

MIT

The Requires

 

by Anton U

redis memcached symfony2 bundle atom lock locking release semaphore flock sem atomicity acquire

19/10 2015

v1.1.4

1.1.4.0 https://github.com/Avtonom/semaphore-bundle

A generic locking Symfony bundle for PHP, that uses named locks for semaphore acquire and release, to guarantee atomicity

  Sources   Download

MIT

The Requires

 

by Anton U

redis memcached symfony2 bundle atom lock locking release semaphore flock sem atomicity acquire

13/10 2015

v1.1.3

1.1.3.0 https://github.com/Avtonom/semaphore-bundle

A generic locking Symfony bundle for PHP, that uses named locks for semaphore acquire and release, to guarantee atomicity

  Sources   Download

MIT

The Requires

 

by Anton U

redis memcached symfony2 bundle atom lock locking release semaphore flock sem atomicity acquire

13/10 2015

v1.1.1

1.1.1.0 https://github.com/Avtonom/semaphore-bundle

A generic locking Symfony bundle for PHP, that uses named locks for semaphore acquire and release, to guarantee atomicity

  Sources   Download

MIT

The Requires

 

by Anton U

redis memcached symfony2 bundle atom lock locking release semaphore flock sem atomicity acquire