|
1 | 1 | # KislayPHP Queue |
2 | 2 |
|
3 | | -Queue runtime for KislayPHP. |
| 3 | +[](https://php.net) |
| 4 | +[](LICENSE) |
| 5 | +[](https://github.com/KislayPHP/queue/actions) |
| 6 | +[](https://github.com/php/pie) |
4 | 7 |
|
5 | | -Primary runtime namespace is `Kislay\Queue` with backward-compatible aliases under `KislayPHP\Queue`. |
| 8 | +> **Deterministic message queue for PHP microservices.** In-process queue with adapter interface for Redis, SQS, or any backend. Sync-first API for reliable task processing. |
6 | 9 |
|
7 | | -## Concurrency Mode |
| 10 | +Part of the [KislayPHP ecosystem](https://skelves.com/kislayphp/docs). |
8 | 11 |
|
9 | | -- Default API mode is synchronous. |
10 | | -- Calls return concrete values (`bool`, payload, `int`) and do not expose Promise/Fiber APIs. |
11 | | -- Optional RPC transport is supported internally when built with stubs, but method semantics remain synchronous. |
| 12 | +--- |
12 | 13 |
|
13 | | -This module is intentionally sync-first to keep queue processing deterministic in long-lived PHP workers. |
| 14 | +## ✨ What It Does |
14 | 15 |
|
15 | | -## Installation |
| 16 | +`kislayphp/queue` provides a typed message queue primitive with a clean adapter interface. Use the built-in in-memory queue for development, or swap in Redis, SQS, or RabbitMQ for production without changing application code. |
| 17 | + |
| 18 | +```php |
| 19 | +<?php |
| 20 | +$queue = new Kislay\Queue\Queue(); |
| 21 | +$queue->enqueue('jobs', ['task' => 'send_email', 'to' => 'user@example.com']); |
| 22 | + |
| 23 | +$job = $queue->dequeue('jobs'); |
| 24 | +// process $job... |
| 25 | +``` |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## 📦 Installation |
16 | 30 |
|
17 | 31 | ```bash |
18 | | -pie install kislayphp/queue:0.0.2 |
| 32 | +pie install kislayphp/queue |
19 | 33 | ``` |
20 | 34 |
|
21 | 35 | Enable in `php.ini`: |
22 | | - |
23 | 36 | ```ini |
24 | 37 | extension=kislayphp_queue.so |
25 | 38 | ``` |
26 | 39 |
|
27 | | -## Public API |
| 40 | +--- |
| 41 | + |
| 42 | +## 🚀 Quick Start |
28 | 43 |
|
29 | | -`Kislay\Queue\Queue`: |
| 44 | +### In-Process Queue |
30 | 45 |
|
31 | | -- `__construct()` |
32 | | -- `setClient(Kislay\Queue\ClientInterface $client): bool` |
33 | | -- `enqueue(string $queue, mixed $payload): bool` |
34 | | -- `dequeue(string $queue): mixed` |
35 | | -- `peek(string $queue): mixed` |
36 | | -- `size(string $queue): int` |
37 | | -- `clear(string $queue): int` |
| 46 | +```php |
| 47 | +<?php |
| 48 | +$queue = new Kislay\Queue\Queue(); |
38 | 49 |
|
39 | | -`Kislay\Queue\ClientInterface`: |
| 50 | +// Producer: add jobs |
| 51 | +$queue->enqueue('email-jobs', ['to' => 'alice@example.com', 'template' => 'welcome']); |
| 52 | +$queue->enqueue('email-jobs', ['to' => 'bob@example.com', 'template' => 'invoice']); |
40 | 53 |
|
41 | | -- `enqueue(string $queue, mixed $payload): bool` |
42 | | -- `dequeue(string $queue): mixed` |
43 | | -- `size(string $queue): int` |
| 54 | +// Consumer: process jobs |
| 55 | +while ($job = $queue->dequeue('email-jobs')) { |
| 56 | + send_email($job['to'], $job['template']); |
| 57 | +} |
44 | 58 |
|
45 | | -Legacy aliases: |
| 59 | +echo $queue->size('email-jobs'); // 0 |
| 60 | +``` |
46 | 61 |
|
47 | | -- `KislayPHP\Queue\Queue` |
48 | | -- `KislayPHP\Queue\ClientInterface` |
| 62 | +### Custom Backend Adapter |
49 | 63 |
|
50 | | -## Quick Start |
| 64 | +Swap to Redis, SQS, etc. without changing your application: |
51 | 65 |
|
52 | 66 | ```php |
53 | 67 | <?php |
| 68 | +class RedisQueueClient implements Kislay\Queue\ClientInterface { |
| 69 | + public function __construct(private Redis $redis) {} |
| 70 | + |
| 71 | + public function enqueue(string $queue, mixed $payload): bool { |
| 72 | + return (bool) $this->redis->rpush($queue, serialize($payload)); |
| 73 | + } |
| 74 | + |
| 75 | + public function dequeue(string $queue): mixed { |
| 76 | + $raw = $this->redis->lpop($queue); |
| 77 | + return $raw ? unserialize($raw) : null; |
| 78 | + } |
| 79 | + |
| 80 | + public function size(string $queue): int { |
| 81 | + return $this->redis->llen($queue); |
| 82 | + } |
| 83 | +} |
54 | 84 |
|
55 | 85 | $queue = new Kislay\Queue\Queue(); |
| 86 | +$queue->setClient(new RedisQueueClient(new Redis())); |
56 | 87 |
|
57 | | -$queue->enqueue('jobs', ['id' => 1, 'task' => 'send_email']); |
58 | | -$queue->enqueue('jobs', ['id' => 2, 'task' => 'reindex']); |
| 88 | +// Same API, backed by Redis |
| 89 | +$queue->enqueue('tasks', ['id' => 123]); |
| 90 | +``` |
| 91 | + |
| 92 | +--- |
59 | 93 |
|
60 | | -$next = $queue->dequeue('jobs'); |
61 | | -var_dump($next); |
| 94 | +## 📖 Public API |
62 | 95 |
|
63 | | -$remaining = $queue->size('jobs'); |
64 | | -var_dump($remaining); |
| 96 | +```php |
| 97 | +namespace Kislay\Queue; |
| 98 | + |
| 99 | +class Queue { |
| 100 | + public function __construct(); |
| 101 | + public function setClient(ClientInterface $client): bool; |
| 102 | + public function enqueue(string $queue, mixed $payload): bool; |
| 103 | + public function dequeue(string $queue): mixed; |
| 104 | + public function peek(string $queue): mixed; |
| 105 | + public function size(string $queue): int; |
| 106 | + public function clear(string $queue): int; // returns count cleared |
| 107 | +} |
| 108 | + |
| 109 | +interface ClientInterface { |
| 110 | + public function enqueue(string $queue, mixed $payload): bool; |
| 111 | + public function dequeue(string $queue): mixed; |
| 112 | + public function size(string $queue): int; |
| 113 | +} |
65 | 114 | ``` |
66 | 115 |
|
67 | | -## Notes |
| 116 | +Legacy aliases: `KislayPHP\Queue\Queue`, `KislayPHP\Queue\ClientInterface` |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +## 💡 When to Use Each Extension |
| 121 | + |
| 122 | +| Need | Use | |
| 123 | +|---|---| |
| 124 | +| In-process task queue | **queue** (this) | |
| 125 | +| Realtime push / fanout to connected clients | [eventbus](https://github.com/KislayPHP/eventbus) | |
| 126 | +| Background async computation | [core](https://github.com/KislayPHP/core) `async()` | |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +## 🔗 Ecosystem |
| 131 | + |
| 132 | +[core](https://github.com/KislayPHP/core) · [gateway](https://github.com/KislayPHP/gateway) · [discovery](https://github.com/KislayPHP/discovery) · [metrics](https://github.com/KislayPHP/metrics) · **queue** · [eventbus](https://github.com/KislayPHP/eventbus) |
68 | 133 |
|
69 | | -- Use this extension for in-process queue semantics and adapter integration points. |
70 | | -- If you need event-driven fanout or socket transport, use `kislayphp/eventbus`. |
71 | | -- For full ecosystem communication guidelines, see `SERVICE_COMMUNICATION.md`. |
| 134 | +## 📄 License |
72 | 135 |
|
| 136 | +[Apache License 2.0](LICENSE) · **[Full Docs](https://skelves.com/kislayphp/docs)** |
0 commit comments