PHPackages                             mkrmr/emphloyer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. mkrmr/emphloyer

ActiveLibrary[Queues &amp; Workers](/categories/queues)

mkrmr/emphloyer
===============

Job processing with pluggable backends made easy

v0.6.0(7y ago)138.4k—3.8%21MITPHPPHP &gt;=7.2.0

Since Nov 7Pushed 7y ago1 watchersCompare

[ Source](https://github.com/mrkcor/emphloyer)[ Packagist](https://packagist.org/packages/mkrmr/emphloyer)[ RSS](/packages/mkrmr-emphloyer/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (16)Used By (1)

Emphloyer
=========

[](#emphloyer)

There comes a time in the life of a PHP application that async job processing becomes a requirement. If you want something flexible that you can easily adapt to fit in with your application's infrastucture, then Emphloyer may be what you are looking for.

Installation
------------

[](#installation)

You can install Emphloyer through composer with:

```
composer require mkrmr/emphloyer

```

Usage
-----

[](#usage)

Using Emphloyer is pretty simple, in your application code you queue up jobs and running Emphloyer's command line program processes the queue.

Before you can start using Emphloyer you need to:

- Define your own jobs in classes that implement the \\Emphloyer\\Job interface, you can extend the \\Emphloyer\\AbstractJob if you like.
- Hook up Emphloyer with a backend to manage the queueing of jobs.

Additionally you can use Emphloyer to schedule jobs like you would in the crontab, to do so you will need to hook up the scheduler with a backend as well.

### Defining your own jobs

[](#defining-your-own-jobs)

Here's a silly example of a job impementation:

```
use Emphloyer\AbstractJob

class NameEchoJob extends AbstractJob
{
   public function setName(string $name) : void
   {
      $this->attributes['name'] = $name;
   }

   public function perform() : void
   {
      echo "Hi, my name is {$this->attributes['name']}.\n";
   }
}
```

Anything in the attributes array will get serialized when the job gets queued up. Note that the keys 'className' and 'type' are reserved and should not be used in your own job implementations.

The perform method is what is executed when Emphloyer runs the job, if this raises an exception then the job will fail.

When a job fails the mayTryAgain method determines whether it may be attempted again or not. You could for example implement retry behaviour by keeping track of the number of retries in your job's internal attributes. Note that changing the attributes during the perform method will not persist them in the backend because the perform method is executed in a forked process and never communicated back to the master process, instead implement the beforeFail hook in your job instance and have that update the attributes in the job's instance in the master process.

You can control the number of processes for jobs based on their type. When you inherit from the \\Emphloyer\\AbstractJob class the type will be set to 'job' by default, you can use the setType method to set a specific type on an instance before you enqueue it or you can override the $type instance variable in your class to set another default.

### Hooking up a backend

[](#hooking-up-a-backend)

Emphloyer manages its jobs through its pipeline, in order to feed jobs into the pipeline and to get jobs out of it you need to connect a backend to it. You can either use a backend that someone has built already (such as [Emphloyer-PDO](https://github.com/mkremer/emphloyer-pdo)) or implement your own. To build your own backend you must implement the \\Emphloyer\\Pipeline\\Backend interface.

To enable Emphloyer to run with the backend of your choice you need to create a configuration file that you reference at runtime, here's an annotated example:

```
