Download the PHP package ulabox/gearman-bundle without Composer
On this page you can find all versions of the php package ulabox/gearman-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package gearman-bundle
GearmanBundle
The bundle provides an interface between symfony2 projects and Gearman Job Server.
Quick installation
Require the bundle in your composer.json file:
Run composer update command:
Now add the Bundle to your Kernel:
Gearman dependency
To install Gearman Job Server for Debain/Ubuntu Package using the following commands:
Once the job server has been installed, can be started by running:
Install the Gearman driver
To install Gearman driver using the following commands:
Now we just need to enable the module. Edit your /etc/php5/conf.d/gearman.ini file and add the line:
Configuration
By default, the bundle doesn't need any configuration, but you can configure it editing your config.yml:
Writing Simple Worker
For a bundle located at src/Acme/DemoBundle, the worker classes by default should live inside src/Acme/DemoBundle/Gearman/Worker. We can create a simple worker as shown below:
Note how the the worker class is marked with @Worker() annotation and each method considered as a job is also marked with the @Job() annotation.
Executing the job
Once your job have been written, can be run in two ways:
By code:
To view the result open a console and run the command:
Then run the code above.
By the command line:
Open the first console and run:
Now open another console and run:
The commands come with a few options, you can see more details in the commands section.
Annotations
Worker annotations
- servers: Array containing servers
- iterations: The number of executions before job dies
Client annotations
- worker: The worker name
- servers: Array containing servers
Job annotations
- name: The job name
Writing Simple Client
For a bundle located at src/Acme/DemoBundle, the client classes by default should live inside src/Acme/DemoBundle/Gearman/Client. The client class by default is associated with the Worker class with the same name, for example, the AcmeClient will be associated with AcmeWorker:
Note how the the client class has a callback methods to be notified when the worker do the job, but it only makes sense when the job is executing with doNormalJob method.
Executing the job through client
Once your job have been written, can be run in two ways:
By code:
To view the result open a console and run the command:
Then run the code above.
By the command line:
Open the first console and run:
Now open another console and run:
Run Tasks
Add a task to be run in parallel with other tasks is very simple. Adding to the previous worker a new job:
Once your job have been written, can be run as show below:
To view the result open a console and run the command:
Then run the code above.
Commands
Show all workers registered:
Execute a job:
Execute a worker:
Asynchronous Events
Exploiting the potential of Gearman Job Server, we have introduced a new EventDispatcherAsync that replaces the default symfony EventDispatcher, the new dispatcher has a new method called dispatchAsync that sends the event to the gearman queue, gearman processes it and sends it back to the php application, which finally reconstructs the event and run it asynchronously.
Configuration
To use asynchronous events the first thing to do is import the routing.yml file of the gearman bundle.
And add the route in the access control from your security.yml file.
Enable the asynchronous event dispatcher in your config.yml.
The EventWorker is responsible for processing the event and send it back to the app. This worker needs to generate a url from the command line, and this requires us setup the request context globally.
Now that everything is configured the following step is run the EventWorker in the command line
The EventWorker should always be running, so we recommend using a tool such as the supervisor to ensure that the worker is always running.
Writing an async Event
An async Event is a simple event with the only difference that must implement the interface Ulabox\Bundle\GearmanBundle\Dispatcher\AsyncEventInterface. Here we show a small example:
Note that the class implements the two AsyncEventInterface methods (getArguments, setArguments), these are used internally to reconstruct the event.
Create an Event Listener
Now that the class is created, you just need to register it as a service:
Dispatch the Event
The dispatchAsync() method begins the process and notifies asynchronously all listeners of the given event
All versions of gearman-bundle with dependencies
symfony/framework-bundle Version ~2.3
doctrine/common Version ~2.2
jms/metadata Version >=0.11
jms/serializer-bundle Version 0.12.*@dev