{"id":34000453,"url":"https://github.com/pdscopes/task-worker","last_synced_at":"2026-04-09T00:32:31.832Z","repository":{"id":62521596,"uuid":"98652910","full_name":"pdscopes/task-worker","owner":"pdscopes","description":"Generic background task worker","archived":false,"fork":false,"pushed_at":"2020-06-26T23:23:27.000Z","size":65,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-28T05:16:47.322Z","etag":null,"topics":["background-jobs","background-tasks","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pdscopes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-28T13:36:36.000Z","updated_at":"2024-11-05T11:33:00.000Z","dependencies_parsed_at":"2022-11-02T10:31:18.394Z","dependency_job_id":null,"html_url":"https://github.com/pdscopes/task-worker","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/pdscopes/task-worker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pdscopes%2Ftask-worker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pdscopes%2Ftask-worker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pdscopes%2Ftask-worker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pdscopes%2Ftask-worker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pdscopes","download_url":"https://codeload.github.com/pdscopes/task-worker/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pdscopes%2Ftask-worker/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31579967,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-08T14:31:17.711Z","status":"ssl_error","status_checked_at":"2026-04-08T14:31:17.202Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["background-jobs","background-tasks","php"],"created_at":"2025-12-13T09:27:30.326Z","updated_at":"2026-04-09T00:32:31.823Z","avatar_url":"https://github.com/pdscopes.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MadeSimple - Task Worker\n[![Build Status](https://travis-ci.org/pdscopes/task-worker.svg?branch=master)](https://travis-ci.org/pdscopes/task-worker)\n\nThe task-worker package is a generic task worker primarily for background tasks.\n\nWorkers patiently wait to reserve Tasks from their Queue. When they receive a\ntask they prepare and perform the task. The task object itself stores the logic\nfor performing the task. If the task was successfully performed, that\nis it did not throw an exception, then the task is removed from the queue.\nIf the task threw an exception then it is put back into the queue to be performed\nagain.\n\n## Installation\nInstall via [composer](https://getcomposer.org/):\n```\n{\n    \"require\": {\n        \"madesimple/task-worker\": \"~2.0\"\n    }\n}\n```\nThen run `composer install` or run `composer require madesimple/task-worker`.\n\n## Task\n***NOTE*** `Task`'s _must_ be constructable with an empty construction, i.e.: `new ExampleTask()`.\n\nTasks are a combination of the data required and the business logic to perform them.\nTasks must `extend` the abstract `\\MadeSimple\\TaskWorker\\Task` class and only need implement `public function perform()`.\nTask data can be set using `\\ArrayAccess` (e.g. `$task['foo'] ='bar'`) or directly inside the class (e.g. `$this-\u003edata['foo'] = 'bar';`).\n\nTasks are serialized in JSON messages when they are put into a queue:\n```json\n{\n    \"identifier\": \"38213-5a4f5275644c2\",\n    \"register\": \"\\\\Namespace\\\\ClassName\",\n    \"queue\": \"task_queue\",\n    \"attempts\": 0,\n    \"data\": {\n        \"foo\": \"bar\"\n    }\n}\n```\n\nA 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`.\nA 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.\nYou can `register` inside the Task to be any string, just be sure to register the value with the `Worker` if it is not autoloadable:\n```php\n\u003c?php\nclass MyTask extends \\MadeSimple\\TaskWorker\\Task\n{\n    protected $register = 'my_task';\n\n    // ... rest of the class goes here\n}\n```\nA Task's `queue` is simply the queue it should be placed on and is generally set `$task-\u003eonQueue('queue_name')`.\nA Task's `attempts` is a simple counter which is incremented every time a `Worker` attempts to perform it.\nA Task's `data` should be a JSON serializable set of information required to perform the task.\n\n## Worker\nThe worker patiently waits for tasks from the queue. Upon receiving a task it\nperforms the task, and assuming no exception was thrown, removes the task from\nthe queue.\n\nBefore checking the queue for the next message the worker asks whether is should\ncontinue working. If it has outlived the length of time it should be alive for\nor there has been restart signal broadcast then the worker will check again.\n\n### Options\nAvailable options for Workers are:\n* `OPT_SLEEP`: how long, in seconds, to wait if not tasks are in the queue before checking again\n* `OPT_ATTEMPTS`: how many attempts a task is allowed before being failed (zero is unlimited)\n* `OPT_ALIVE`: how long, in seconds, the worker will stay alive for (zero is unlimited)\n* `OPT_REST`: how long, in milliseconds, to rest between tasks\n* `OPT_MAX_TASKS`: the maximum number of tasks a worker should perform before stopping (zero is unlimited)\n* `OPT_UNTIL_EMPTY`: set the worker to stop when the queue it empty\n\n### Handlers\nHandlers are closures you can add to a worker to help prepare a task to be performed.\nEvery handler that has been added to a worker is called before every task is performed.\nCommon usage for this would be dependency injection. Below is the signature a handler\nmust have (can be an anonymous function or invokable class):\n```php\nfunction (Task $task) {\n    // logic goes here\n}\n```\n\n## Queues\nThere are currently four types of queues support, `MySQL`, `RabbitMQ`, `Redis`, and `Synchronous`.\nCustom queues can be created and must implement the `Queue` interface. When implementing a Queue the convention is\nto provide a list of queue names. An implementation of a Queue can throw a\n`MadeSimple\\TaskWorker\\Exception\\QueueNameRequiredException` if no queue names are provided.\n\n### MySQL\nThe MySQL queue uses a single table in an MySQL compatible database.\nThere must exist a table that has the following schema (the name of the table can\nbe changed in the `MysqlQueue` options):\n```mysql\nCREATE TABLE `worker_task` (\n  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,\n  `queue` char(255) DEFAULT '',\n  `payload` longtext NOT NULL,\n  `reservedAt` int(10) unsigned DEFAULT NULL,\n  `releasedAt` int(10) unsigned NOT NULL,\n  `failedAt` int(10) unsigned DEFAULT NULL,\n  PRIMARY KEY (`id`),\n  KEY `queue` (`queue`,`releasedAt`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n```\n\nAvailable options for the MysqlQueue are:\n* `OPT_TABLE_NAME`: the name of the table to read tasks from\n\n### RabbitMQ\nThe RabbitMQ queue uses the `\"php-amqplib/php-amqplib\": \"^2.7\"` library to connect to a RabbitMQ instance (or cluster).\nThe `default` exchange is used to route tasks to the specified queue.\nDead letters are used to delay tasks, the queue name is generated as follows: `delayed_\u003cseconds\u003e_\u003cqueue_name\u003e`.\n\n### Redis\nThe Redis queue uses the `\"predis/predis\": \"^1.1\"` library to connect to a redis instance.\nThe queue names are directly used and there are the following pseudo queues:\n* `\u003cqueue_name\u003e-processing` to hold the workers current task, and\n* `\u003cqueue_name\u003e-delayed` to hold delayed tasks.\n\n\n### Synchronous\nThe synchronous queue is a faux queue which immediately performs any task at the point it is added.\nIt will respect the delay if set by sleeping the thread that called `add` for the specified time.\n\n## Commands\nThere are some commands already defined for `symfony/console`.\n\n## Examples\nThere are some examples in the namesake directory. You will need to `composer install`\nwith dev requirements for them to work. Copy `examples/.env.example` to `examples/.env` and update\nthe variable values for your environment then the examples can be run using the following command:\n\n```bash\nexport $(cat examples/.env | xargs) \u0026\u0026 php examples/\n```\n\n# External Documentation\nLinks to documentation for external dependencies:\n* [PHP Docs](http://php.net/)\n* [Logging PSR-3](http://www.php-fig.org/psr/psr-3/) ([GitHub page](https://github.com/php-fig/log))\n* [Simple Cache PSR-16](http://www.php-fig.org/psr/psr-16/) ([GitHub page](https://github.com/php-fig/simple-cache))\n\nLinks to documentation for development only external dependencies:\n* [cache/cache](http://www.php-cache.com/en/latest/)\n* [monolog/monolog](https://github.com/Seldaek/monolog)\n* [symfony/console](http://symfony.com/doc/current/components/console.html)\n* [php-amqplib/php-amqplib](https://github.com/php-amqplib/php-amqplib)\n* [predis/predis](https://github.com/nrk/predis)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdscopes%2Ftask-worker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpdscopes%2Ftask-worker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpdscopes%2Ftask-worker/lists"}