{"id":19112911,"url":"https://github.com/chipslays/queue","last_synced_at":"2025-02-22T11:40:31.050Z","repository":{"id":62501935,"uuid":"361521124","full_name":"chipslays/queue","owner":"chipslays","description":"🛒 Simple implement queue processing in PHP.","archived":false,"fork":false,"pushed_at":"2021-11-17T13:12:20.000Z","size":42,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-03T04:52:37.521Z","etag":null,"topics":["queue","queue-manager","queue-workers"],"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/chipslays.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-04-25T19:36:36.000Z","updated_at":"2022-12-12T06:14:12.000Z","dependencies_parsed_at":"2022-11-02T09:46:31.997Z","dependency_job_id":null,"html_url":"https://github.com/chipslays/queue","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chipslays%2Fqueue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chipslays%2Fqueue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chipslays%2Fqueue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chipslays%2Fqueue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chipslays","download_url":"https://codeload.github.com/chipslays/queue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240170056,"owners_count":19759141,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["queue","queue-manager","queue-workers"],"created_at":"2024-11-09T04:34:30.416Z","updated_at":"2025-02-22T11:40:31.030Z","avatar_url":"https://github.com/chipslays.png","language":"PHP","readme":"# 🛒 Queue\n\n![Packagist Version](https://img.shields.io/packagist/v/chipslays/queue)\n![GitHub](https://img.shields.io/github/license/chipslays/queue)\n\nSimple implement queue processing in PHP.\n\n# Installation\n\n```bash\n$ composer require chipslays/queue\n```\n\n# Usage\n\n### Client\n\nWe push something to queue.\n\n```php\nuse Chipslays\\Queue\\Queue;\nuse Chipslays\\Queue\\Drivers\\File;\n\nrequire __DIR__ . '/vendor/autoload.php';\n\n$driver = new File([\n    'storage' =\u003e __DIR__ . '/storage/',\n]);\n\n$queue = new Queue($driver);\n$queue-\u003eadd('payment', ['user_id' =\u003e 1, 'amount' =\u003e 10]);\n```\n\n### Worker\n\nWe have worker, who get value from queue and starts processing.\n\n\u003cdetails\u003e\n  \u003csummary\u003e\u003cb\u003eworker.php\u003c/b\u003e\u003c/summary\u003e\n\n\n```php\nuse Chipslays\\Queue\\Queue;\nuse Chipslays\\Queue\\Drivers\\File;\n\nrequire __DIR__ . '/vendor/autoload.php';\n\n$driver = new File([\n    'storage' =\u003e __DIR__ . '/storage/',\n]);\n\n$queue = new Queue($driver);\n\nwhile (true) {\n    if (!$item = $queue-\u003enext('payment')) {\n        continue;\n    }\n\n    echo 'channel: ' . $item-\u003egetChannel() . PHP_EOL;\n    echo 'id: ' . $item-\u003egetId() . PHP_EOL;\n    echo 'data: ' . print_r($item-\u003egetData(), true) . PHP_EOL;\n\n    // also can be getting by magic getter: $item-\u003eid, $item-\u003echannel, $item-\u003edata\n}\n```\n\u003c/details\u003e\n\n\n### Cron\n\nOr instead loop worker, we can use a cron job.\n\n\u003cdetails\u003e\n  \u003csummary\u003e\u003cb\u003ecron-worker.php\u003c/b\u003e\u003c/summary\u003e\n\n```php\nuse Chipslays\\Queue\\Queue;\nuse Chipslays\\Queue\\Drivers\\File;\n\nrequire __DIR__ . '/vendor/autoload.php';\n\n$driver = new File([\n    'storage' =\u003e __DIR__ . '/storage/',\n]);\n\n$queue = new Queue($driver);\n\nif (!$item = $queue-\u003enext('payment')) {\n    exit;\n}\n\necho 'channel: ' . $item-\u003egetChannel() . PHP_EOL;\necho 'id: ' . $item-\u003egetId() . PHP_EOL;\necho 'data: ' . print_r($item-\u003egetData(), true) . PHP_EOL;\n\n// also can be getting by magic getter: $item-\u003eid, $item-\u003echannel, $item-\u003edata\n```\n\u003c/details\u003e\n\n# Queue\n\nBase class for queue manipulate.\n\n## Methods\n\n### `__construct`\n\n```php\n/**\n * @param DriverInterface $driver\n */\npublic function __construct(DriverInterface $driver);\n```\n\n**Flat File (FileSystem) driver:**\n\n```php\nuse Chipslays\\Queue\\Queue;\nuse Chipslays\\Queue\\Drivers\\File;\n\nrequire __DIR__ . '/vendor/autoload.php';\n\n$driver = new File([\n    'storage' =\u003e __DIR__ . '/storage/',\n]);\n\n$queue = new Queue($driver);\n```\n\n### `add`\n\n```php\n/**\n * Add item to queue.\n *\n * Returns the `id` of the added item.\n *\n * @param string $channel\n * @param array $data\n * @param int $sort\n * @return string\n */\npublic function add(string $channel, array $data, int $sort = QUEUE_DEFAULT_SORT): string;\n```\n\nExample:\n\n```php\nuse Chipslays\\Queue\\Queue;\n\n$queue = new Queue($driver);\n$id = $queue-\u003eadd('payment', ['key' =\u003e 'value']);\necho $queue-\u003eposition('payment', $id); // e.g. 1\n```\n\n### `get`\n\n```php\n/**\n * Get item by ID.\n *\n * @param string $channel\n * @param string $id\n * @return Item|null\n */\npublic function get(string $channel, string $id): ?Item;\n```\n\nExample:\n\n```php\nuse Chipslays\\Queue\\Queue;\n\n$queue = new Queue($driver);\n$id = $queue-\u003eadd('payment', ['key' =\u003e 'value']);\n$item = $queue-\u003eget('payment', $id));\n\necho 'channel: ' . $item-\u003egetChannel() . PHP_EOL;\necho 'id: ' . $item-\u003egetId() . PHP_EOL;\necho 'data: ' . print_r($item-\u003egetData(), true) . PHP_EOL;\n\n// also can be getting by magic getter: $item-\u003eid, $item-\u003echannel, $item-\u003edata\n```\n\n### `first`\n\n```php\n /**\n * Get first item in queue.\n *\n * If queue is empty or `channel` not exists returns `null`.\n *\n * @param string $channel\n * @return Item|null\n */\npublic function first(string $channel): ?Item;\n```\n\nExample:\n\n```php\nuse Chipslays\\Queue\\Queue;\n\n$queue = new Queue($driver);\n$item = $queue-\u003efirst('payment');\n\nif (!$item) {\n    return;\n}\n\necho 'channel: ' . $item-\u003egetChannel() . PHP_EOL;\necho 'id: ' . $item-\u003egetId() . PHP_EOL;\necho 'data: ' . print_r($item-\u003egetData(), true) . PHP_EOL;\n\n// also can be getting by magic getter: $item-\u003eid, $item-\u003echannel, $item-\u003edata\n```\n\n### `next`\n\n```php\n/**\n * Get next item in queue.\n *\n * @param string $channel\n * @return Item|null\n */\npublic function next(string $channel): ?Item;\n```\n\nExample:\n\n```php\nuse Chipslays\\Queue\\Queue;\n\n$queue = new Queue($driver);\n\n// somewhere in client code...\n$queue-\u003eadd('payment', ['currency' =\u003e 'EUR', 'amount' =\u003e 10]);\n\n// somewhere in worker/cron code...\nif (!$item = $queue-\u003enext('payment')) {\n    return;\n}\n\necho 'channel: ' . $item-\u003egetChannel() . PHP_EOL;\necho 'id: ' . $item-\u003egetId() . PHP_EOL;\necho 'data: ' . print_r($item-\u003egetData(), true) . PHP_EOL;\n\n// also can be getting by magic getter: $item-\u003eid, $item-\u003echannel, $item-\u003edata\n```\n\n### `delete`\n\n```php\n/**\n * Delete item from queue.\n *\n * Returns `true` on success delete and `false` on fail.\n *\n * @param string|Item $channel e.g. Can be passed as result from `first` method.\n * @param string $id\n * @return boolean\n */\npublic function delete($channel, string $id = null): bool;\n```\n\nExample:\n\n```php\nuse Chipslays\\Queue\\Queue;\n\n$queue = new Queue($driver);\n$item = $queue-\u003efirst('payment');\n\nif (!$item) {\n    return;\n}\n\n// Delete by pass received item from `first` method.\n$queue-\u003edelete($item);\n\n// Delete by `channel` and `id`.\n$queue-\u003edelete($item-\u003echannel, $item-\u003eid);\n```\n\n### `list`\n\n```php\n/**\n * Get list of queue items.\n *\n * Returns array of `id's`, if `channel` not exists returns `null`.\n *\n * @param string $channel\n * @return array|null\n */\npublic function list(string $channel): ?array;\n```\n\nExample:\n```php\nuse Chipslays\\Queue\\Queue;\n\n$queue = new Queue($driver);\nprint_r($queue-\u003elist('payment'));\n\n// Array\n//\n//     [0] =\u003e item_id_1\n//     [1] =\u003e item_id_2\n//     [2] =\u003e item_id_3\n// )\n```\n\n\u003e **NOTE:** For each driver, the name of the `id` may be different!\n\n### `count`\n\n```php\n/**\n * Get count of items in queue.\n *\n * Returns count, if `channel` not exists returns 0.\n *\n * @param string $channel\n * @return integer\n */\npublic function count(string $channel): int;\n```\n\nExample:\n```php\nuse Chipslays\\Queue\\Queue;\n\n$queue = new Queue($driver);\necho $queue-\u003ecount('payment'); // e.g. 32\n```\n\n### `position`\n\n```php\n/**\n * Get item position in queue.\n *\n * Return position, if `channel` or `id` not exists returns 0.\n *\n * @param string $channel\n * @param string $id\n * @return int\n */\npublic function position(string $channel, string $id): int;\n```\n\nExample:\n```php\nuse Chipslays\\Queue\\Queue;\n\n$queue = new Queue($driver);\n$id = $queue-\u003eadd('payment', ['key' =\u003e 'value']);\necho $queue-\u003eposition('payment', $id); // e.g. 1\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchipslays%2Fqueue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchipslays%2Fqueue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchipslays%2Fqueue/lists"}