PHPackages                             madesimple/task-worker - 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. madesimple/task-worker

ActiveLibrary

madesimple/task-worker
======================

Generic background task worker library

v2.1.0(5y ago)232[1 issues](https://github.com/pdscopes/task-worker/issues)MITPHPPHP &gt;=7.2CI failing

Since Aug 2Pushed 5y ago1 watchersCompare

[ Source](https://github.com/pdscopes/task-worker)[ Packagist](https://packagist.org/packages/madesimple/task-worker)[ RSS](/packages/madesimple-task-worker/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (5)Dependencies (8)Versions (8)Used By (0)

MadeSimple - Task Worker
========================

[](#madesimple---task-worker)

[![Build Status](https://camo.githubusercontent.com/b9cab3059584932505e09dc0b12a74f8d859bbd69d71b04dc9e40c7312331a7d/68747470733a2f2f7472617669732d63692e6f72672f706473636f7065732f7461736b2d776f726b65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/pdscopes/task-worker)

The task-worker package is a generic task worker primarily for background tasks.

Workers patiently wait to reserve Tasks from their Queue. When they receive a task they prepare and perform the task. The task object itself stores the logic for performing the task. If the task was successfully performed, that is it did not throw an exception, then the task is removed from the queue. If the task threw an exception then it is put back into the queue to be performed again.

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

[](#installation)

Install via [composer](https://getcomposer.org/):

```
{
    "require": {
        "madesimple/task-worker": "~2.0"
    }
}

```

Then run `composer install` or run `composer require madesimple/task-worker`.

Task
----

[](#task)

***NOTE*** `Task`'s *must* be constructable with an empty construction, i.e.: `new ExampleTask()`.

Tasks are a combination of the data required and the business logic to perform them. Tasks must `extend` the abstract `\MadeSimple\TaskWorker\Task` class and only need implement `public function perform()`. Task data can be set using `\ArrayAccess` (e.g. `$task['foo'] ='bar'`) or directly inside the class (e.g. `$this->data['foo'] = 'bar';`).

Tasks are serialized in JSON messages when they are put into a queue:

```
{
    "identifier": "38213-5a4f5275644c2",
    "register": "\\Namespace\\ClassName",
    "queue": "task_queue",
    "attempts": 0,
    "data": {
        "foo": "bar"
    }
}
```

A Task's `identifier` is automatically generated when `identifier()` is called and is created using `uniqid(getmypid() . '-')`; cloned Tasks ***do not*** share their `identifier` or `attempts`. A Task's `register` defaults to `static::class` and is used in `Task::deserialize` to work out which Task class should be used; `deserialize` firstly checks its `register` (passed to it from the `Worker`) for a match, next it attempts to autoload, finally fails if neither of these return a Task. You can `register` inside the Task to be any string, just be sure to register the value with the `Worker` if it is not autoloadable:

```
