Download the PHP package amegatron/php-sync without Composer
On this page you can find all versions of the php package amegatron/php-sync. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download amegatron/php-sync
More information about amegatron/php-sync
Files in amegatron/php-sync
Package php-sync
Short Description A set of interfaces and generic implementations which provide synchronization possibilities for background PHP scripts
License BSD
Informations about the package php-sync
PHP-SYNC
This package provides a set of interfaces and generic implementations which should ease synchronization between different PHP scripts running in parallel.
Interfaces
In general any consuming side should depend on just two interfaces: LockInterface
and IntegerInterface
LockInterface
LockInterface
is meant to provide an interface which would allow the parallel process to block or wait for each other
or any other circumstances. Consider taking the following example into account:
You have some scripts running in background (let's call them workers
) which, for example, take new messages (tasks)
from any MessageQueue engine and constantly start and finish with help of supervisor
, for example. If you need to
temporarily pause them, you could introduce a lock to the system which all the workers would wait for before taking new
tasks. In general, this could look like this:
-
Before taking a new task, all workers
wait
for a Lock namedpause
: - Wherever you would like to control those workers (from web admin-panel, for example), you could just
acquire
that same Lock to pause the workers:
All the workers will pause, waiting until the lock is released
from the controlling side:
You can also check if a there is an existing (acquired) Lock by using exists
method:
See the Generic implementation
for more info.
IntegerInterface
IntegerInterface
provides a common interface to work atomically with integers from parallel scripts. One of the most common
use case for that is tracking the progress of background tasks, either for just decorational purposes (just to
show the progress to user), or even to implement some different logic for synchronizing those background tasks.
As an example, imagine you put N new tasks to the queue. At the same time, you could also introduce a new Integer,
called progress
for example, which would act as a counter. Upon completion of an individual task,
each worker would increment
that Integer.
Any time you need to display a total progress, you could just take that progress
Integer together
with N (which could also be another instance of IntegerInterface
, btw) and calculate the progress
in percents and display it somehow. You could also work with that N directly, calling decrement
each
time a task is finished:
You can always check if an Integer exists
or delete
it:
See also Generic implementation
for more info.
Specification
See the source code for both of those interfaces under /src/Core
path for a detailed specification
about how exactly implementations of them must behave.
Generic implementation
This package also provides a generic implementation of the interfaces mentioned above. This implementation does not work by
itself, meaning, it does not provide any exact implementation for making atomic Locks and Integers. Instead, it just
relies on underlying Drivers
which do the actual job.
This implementation utilizes a Singleton pattern so that Locks and Integers which represent the same entities are represented by a single object during the run of a script.
Imagine you want to get a Lock object which represents a lock named some_lock
. In this case you would do the following:
Later on, if you try to get an instance of Lock for the same some_lock
you'll get exactly the same instance:
some_lock
here is a key
(or name
) of an individual lock.
Singleton management by key
is done internally inside the Lock
class, but you can still
provide your own SingletonManagerInterface
as a third parameter in case you need to utilize
some "namespacing" for your Locks. You could even provide a new SingletonManager()
with each
call in case you don't need any "singletoning" at all for some reasons.
Everything stated above is also actual for a generic Integer
implementation.
Drivers
This generic implementation uses Drivers
for actual realization of the main functionality. Specifically,
there are two kinds of drivers: LockSyncDriverInterface
and IntegerSyncDriverInterface
, which provide
corresponding interfaces for making some atomic actions. Those drivers are NOT shipped with this package and
must be installed additionally.
For example, consider looking at a neighbor package amegatron/php-sync-fs, which provides drivers for implementing the functionality based on a local file system.