From 835f4a20cacb77de433ccaa572c17d9da06bc4ad Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 28 Mar 2025 15:59:59 +1300 Subject: [PATCH 1/8] Add method to get consumer to allow reclaiming individually --- composer.lock | 207 +++++++++++++++---------------- src/Queue/Adapter.php | 9 ++ src/Queue/Server.php | 12 +- tests/Queue/E2E/Adapter/Base.php | 2 +- 4 files changed, 117 insertions(+), 113 deletions(-) diff --git a/composer.lock b/composer.lock index 579c374..6b1ad24 100644 --- a/composer.lock +++ b/composer.lock @@ -8,16 +8,16 @@ "packages": [ { "name": "brick/math", - "version": "0.12.1", + "version": "0.12.3", "source": { "type": "git", "url": "https://github.com/brick/math.git", - "reference": "f510c0a40911935b77b86859eb5223d58d660df1" + "reference": "866551da34e9a618e64a819ee1e01c20d8a588ba" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/brick/math/zipball/f510c0a40911935b77b86859eb5223d58d660df1", - "reference": "f510c0a40911935b77b86859eb5223d58d660df1", + "url": "https://api.github.com/repos/brick/math/zipball/866551da34e9a618e64a819ee1e01c20d8a588ba", + "reference": "866551da34e9a618e64a819ee1e01c20d8a588ba", "shasum": "" }, "require": { @@ -26,7 +26,7 @@ "require-dev": { "php-coveralls/php-coveralls": "^2.2", "phpunit/phpunit": "^10.1", - "vimeo/psalm": "5.16.0" + "vimeo/psalm": "6.8.8" }, "type": "library", "autoload": { @@ -56,7 +56,7 @@ ], "support": { "issues": "https://github.com/brick/math/issues", - "source": "https://github.com/brick/math/tree/0.12.1" + "source": "https://github.com/brick/math/tree/0.12.3" }, "funding": [ { @@ -64,7 +64,7 @@ "type": "github" } ], - "time": "2023-11-29T23:19:16+00:00" + "time": "2025-02-28T13:11:00+00:00" }, { "name": "composer/semver", @@ -149,16 +149,16 @@ }, { "name": "google/protobuf", - "version": "v4.29.3", + "version": "v4.30.2", "source": { "type": "git", "url": "https://github.com/protocolbuffers/protobuf-php.git", - "reference": "ab5077c2cfdd1f415f42d11fdbdf903ba8e3d9b7" + "reference": "a4c4d8565b40b9f76debc9dfeb221412eacb8ced" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/protocolbuffers/protobuf-php/zipball/ab5077c2cfdd1f415f42d11fdbdf903ba8e3d9b7", - "reference": "ab5077c2cfdd1f415f42d11fdbdf903ba8e3d9b7", + "url": "https://api.github.com/repos/protocolbuffers/protobuf-php/zipball/a4c4d8565b40b9f76debc9dfeb221412eacb8ced", + "reference": "a4c4d8565b40b9f76debc9dfeb221412eacb8ced", "shasum": "" }, "require": { @@ -187,9 +187,9 @@ "proto" ], "support": { - "source": "https://github.com/protocolbuffers/protobuf-php/tree/v4.29.3" + "source": "https://github.com/protocolbuffers/protobuf-php/tree/v4.30.2" }, - "time": "2025-01-08T21:00:13+00:00" + "time": "2025-03-26T18:01:50+00:00" }, { "name": "nyholm/psr7", @@ -337,16 +337,16 @@ }, { "name": "open-telemetry/api", - "version": "1.2.1", + "version": "1.2.3", "source": { "type": "git", "url": "https://github.com/opentelemetry-php/api.git", - "reference": "74b1a03263be8c5acb578f41da054b4bac3af4a0" + "reference": "199d7ddda88f5f5619fa73463f1a5a7149ccd1f1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/opentelemetry-php/api/zipball/74b1a03263be8c5acb578f41da054b4bac3af4a0", - "reference": "74b1a03263be8c5acb578f41da054b4bac3af4a0", + "url": "https://api.github.com/repos/opentelemetry-php/api/zipball/199d7ddda88f5f5619fa73463f1a5a7149ccd1f1", + "reference": "199d7ddda88f5f5619fa73463f1a5a7149ccd1f1", "shasum": "" }, "require": { @@ -403,7 +403,7 @@ "issues": "https://github.com/open-telemetry/opentelemetry-php/issues", "source": "https://github.com/open-telemetry/opentelemetry-php" }, - "time": "2025-01-20T23:35:16+00:00" + "time": "2025-03-05T21:42:54+00:00" }, { "name": "open-telemetry/context", @@ -466,16 +466,16 @@ }, { "name": "open-telemetry/exporter-otlp", - "version": "1.2.0", + "version": "1.2.1", "source": { "type": "git", "url": "https://github.com/opentelemetry-php/exporter-otlp.git", - "reference": "243d9657c44a06f740cf384f486afe954c2b725f" + "reference": "b7580440b7481a98da97aceabeb46e1b276c8747" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/opentelemetry-php/exporter-otlp/zipball/243d9657c44a06f740cf384f486afe954c2b725f", - "reference": "243d9657c44a06f740cf384f486afe954c2b725f", + "url": "https://api.github.com/repos/opentelemetry-php/exporter-otlp/zipball/b7580440b7481a98da97aceabeb46e1b276c8747", + "reference": "b7580440b7481a98da97aceabeb46e1b276c8747", "shasum": "" }, "require": { @@ -526,7 +526,7 @@ "issues": "https://github.com/open-telemetry/opentelemetry-php/issues", "source": "https://github.com/open-telemetry/opentelemetry-php" }, - "time": "2025-01-08T23:50:03+00:00" + "time": "2025-03-06T23:21:56+00:00" }, { "name": "open-telemetry/gen-otlp-protobuf", @@ -593,16 +593,16 @@ }, { "name": "open-telemetry/sdk", - "version": "1.2.1", + "version": "1.2.2", "source": { "type": "git", "url": "https://github.com/opentelemetry-php/sdk.git", - "reference": "96aeaee5b7cb8c0bc4af7ff4717b429f2d9f67e1" + "reference": "37eec0fe47ddd627911f318f29b6cd48196be0c0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/opentelemetry-php/sdk/zipball/96aeaee5b7cb8c0bc4af7ff4717b429f2d9f67e1", - "reference": "96aeaee5b7cb8c0bc4af7ff4717b429f2d9f67e1", + "url": "https://api.github.com/repos/opentelemetry-php/sdk/zipball/37eec0fe47ddd627911f318f29b6cd48196be0c0", + "reference": "37eec0fe47ddd627911f318f29b6cd48196be0c0", "shasum": "" }, "require": { @@ -679,24 +679,24 @@ "issues": "https://github.com/open-telemetry/opentelemetry-php/issues", "source": "https://github.com/open-telemetry/opentelemetry-php" }, - "time": "2025-01-09T23:17:14+00:00" + "time": "2025-01-29T21:40:28+00:00" }, { "name": "open-telemetry/sem-conv", - "version": "1.27.1", + "version": "1.30.0", "source": { "type": "git", "url": "https://github.com/opentelemetry-php/sem-conv.git", - "reference": "1dba705fea74bc0718d04be26090e3697e56f4e6" + "reference": "4178c9f390da8e4dbca9b181a9d1efd50cf7ee0a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/opentelemetry-php/sem-conv/zipball/1dba705fea74bc0718d04be26090e3697e56f4e6", - "reference": "1dba705fea74bc0718d04be26090e3697e56f4e6", + "url": "https://api.github.com/repos/opentelemetry-php/sem-conv/zipball/4178c9f390da8e4dbca9b181a9d1efd50cf7ee0a", + "reference": "4178c9f390da8e4dbca9b181a9d1efd50cf7ee0a", "shasum": "" }, "require": { - "php": "^8.1" + "php": "^8.0" }, "type": "library", "extra": { @@ -736,7 +736,7 @@ "issues": "https://github.com/open-telemetry/opentelemetry-php/issues", "source": "https://github.com/open-telemetry/opentelemetry-php" }, - "time": "2024-08-28T09:20:31+00:00" + "time": "2025-02-06T00:21:48+00:00" }, { "name": "paragonie/constant_time_encoding", @@ -857,16 +857,16 @@ }, { "name": "php-amqplib/php-amqplib", - "version": "v3.7.2", + "version": "v3.7.3", "source": { "type": "git", "url": "https://github.com/php-amqplib/php-amqplib.git", - "reference": "738a73eb0019b6c99d9bc25d7a0c0dd8f56a5199" + "reference": "9f50fe69a9f1a19e2cb25596a354d705de36fe59" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/php-amqplib/php-amqplib/zipball/738a73eb0019b6c99d9bc25d7a0c0dd8f56a5199", - "reference": "738a73eb0019b6c99d9bc25d7a0c0dd8f56a5199", + "url": "https://api.github.com/repos/php-amqplib/php-amqplib/zipball/9f50fe69a9f1a19e2cb25596a354d705de36fe59", + "reference": "9f50fe69a9f1a19e2cb25596a354d705de36fe59", "shasum": "" }, "require": { @@ -932,9 +932,9 @@ ], "support": { "issues": "https://github.com/php-amqplib/php-amqplib/issues", - "source": "https://github.com/php-amqplib/php-amqplib/tree/v3.7.2" + "source": "https://github.com/php-amqplib/php-amqplib/tree/v3.7.3" }, - "time": "2024-11-21T09:21:41+00:00" + "time": "2025-02-18T20:11:13+00:00" }, { "name": "php-http/discovery", @@ -1390,16 +1390,16 @@ }, { "name": "ramsey/collection", - "version": "2.0.0", + "version": "2.1.1", "source": { "type": "git", "url": "https://github.com/ramsey/collection.git", - "reference": "a4b48764bfbb8f3a6a4d1aeb1a35bb5e9ecac4a5" + "reference": "344572933ad0181accbf4ba763e85a0306a8c5e2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/ramsey/collection/zipball/a4b48764bfbb8f3a6a4d1aeb1a35bb5e9ecac4a5", - "reference": "a4b48764bfbb8f3a6a4d1aeb1a35bb5e9ecac4a5", + "url": "https://api.github.com/repos/ramsey/collection/zipball/344572933ad0181accbf4ba763e85a0306a8c5e2", + "reference": "344572933ad0181accbf4ba763e85a0306a8c5e2", "shasum": "" }, "require": { @@ -1407,25 +1407,22 @@ }, "require-dev": { "captainhook/plugin-composer": "^5.3", - "ergebnis/composer-normalize": "^2.28.3", - "fakerphp/faker": "^1.21", + "ergebnis/composer-normalize": "^2.45", + "fakerphp/faker": "^1.24", "hamcrest/hamcrest-php": "^2.0", - "jangregor/phpstan-prophecy": "^1.0", - "mockery/mockery": "^1.5", + "jangregor/phpstan-prophecy": "^2.1", + "mockery/mockery": "^1.6", "php-parallel-lint/php-console-highlighter": "^1.0", - "php-parallel-lint/php-parallel-lint": "^1.3", - "phpcsstandards/phpcsutils": "^1.0.0-rc1", - "phpspec/prophecy-phpunit": "^2.0", - "phpstan/extension-installer": "^1.2", - "phpstan/phpstan": "^1.9", - "phpstan/phpstan-mockery": "^1.1", - "phpstan/phpstan-phpunit": "^1.3", - "phpunit/phpunit": "^9.5", - "psalm/plugin-mockery": "^1.1", - "psalm/plugin-phpunit": "^0.18.4", - "ramsey/coding-standard": "^2.0.3", - "ramsey/conventional-commits": "^1.3", - "vimeo/psalm": "^5.4" + "php-parallel-lint/php-parallel-lint": "^1.4", + "phpspec/prophecy-phpunit": "^2.3", + "phpstan/extension-installer": "^1.4", + "phpstan/phpstan": "^2.1", + "phpstan/phpstan-mockery": "^2.0", + "phpstan/phpstan-phpunit": "^2.0", + "phpunit/phpunit": "^10.5", + "ramsey/coding-standard": "^2.3", + "ramsey/conventional-commits": "^1.6", + "roave/security-advisories": "dev-latest" }, "type": "library", "extra": { @@ -1463,19 +1460,9 @@ ], "support": { "issues": "https://github.com/ramsey/collection/issues", - "source": "https://github.com/ramsey/collection/tree/2.0.0" + "source": "https://github.com/ramsey/collection/tree/2.1.1" }, - "funding": [ - { - "url": "https://github.com/ramsey", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/ramsey/collection", - "type": "tidelift" - } - ], - "time": "2022-12-31T21:50:55+00:00" + "time": "2025-03-22T05:38:12+00:00" }, { "name": "ramsey/uuid", @@ -1638,16 +1625,16 @@ }, { "name": "symfony/http-client", - "version": "v7.2.3", + "version": "v7.2.4", "source": { "type": "git", "url": "https://github.com/symfony/http-client.git", - "reference": "7ce6078c79a4a7afff931c413d2959d3bffbfb8d" + "reference": "78981a2ffef6437ed92d4d7e2a86a82f256c6dc6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-client/zipball/7ce6078c79a4a7afff931c413d2959d3bffbfb8d", - "reference": "7ce6078c79a4a7afff931c413d2959d3bffbfb8d", + "url": "https://api.github.com/repos/symfony/http-client/zipball/78981a2ffef6437ed92d4d7e2a86a82f256c6dc6", + "reference": "78981a2ffef6437ed92d4d7e2a86a82f256c6dc6", "shasum": "" }, "require": { @@ -1713,7 +1700,7 @@ "http" ], "support": { - "source": "https://github.com/symfony/http-client/tree/v7.2.3" + "source": "https://github.com/symfony/http-client/tree/v7.2.4" }, "funding": [ { @@ -1729,7 +1716,7 @@ "type": "tidelift" } ], - "time": "2025-01-28T15:51:35+00:00" + "time": "2025-02-13T10:27:23+00:00" }, { "name": "symfony/http-client-contracts", @@ -2197,16 +2184,16 @@ }, { "name": "utopia-php/fetch", - "version": "0.3.0", + "version": "0.3.1", "source": { "type": "git", "url": "https://github.com/utopia-php/fetch.git", - "reference": "02b12c05aec13399dcc2da8d51f908e328ab63f4" + "reference": "524dd50afa8c64670c4fb18f1df4db9b5bb4b3d0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/fetch/zipball/02b12c05aec13399dcc2da8d51f908e328ab63f4", - "reference": "02b12c05aec13399dcc2da8d51f908e328ab63f4", + "url": "https://api.github.com/repos/utopia-php/fetch/zipball/524dd50afa8c64670c4fb18f1df4db9b5bb4b3d0", + "reference": "524dd50afa8c64670c4fb18f1df4db9b5bb4b3d0", "shasum": "" }, "require": { @@ -2230,22 +2217,22 @@ "description": "A simple library that provides an interface for making HTTP Requests.", "support": { "issues": "https://github.com/utopia-php/fetch/issues", - "source": "https://github.com/utopia-php/fetch/tree/0.3.0" + "source": "https://github.com/utopia-php/fetch/tree/0.3.1" }, - "time": "2025-01-17T06:11:10+00:00" + "time": "2025-03-05T18:08:55+00:00" }, { "name": "utopia-php/framework", - "version": "0.33.16", + "version": "0.33.19", "source": { "type": "git", "url": "https://github.com/utopia-php/http.git", - "reference": "e91d4c560d1b809e25faa63d564fef034363b50f" + "reference": "64c7b7bb8a8595ffe875fa8d4b7705684dbf46c0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/http/zipball/e91d4c560d1b809e25faa63d564fef034363b50f", - "reference": "e91d4c560d1b809e25faa63d564fef034363b50f", + "url": "https://api.github.com/repos/utopia-php/http/zipball/64c7b7bb8a8595ffe875fa8d4b7705684dbf46c0", + "reference": "64c7b7bb8a8595ffe875fa8d4b7705684dbf46c0", "shasum": "" }, "require": { @@ -2277,22 +2264,22 @@ ], "support": { "issues": "https://github.com/utopia-php/http/issues", - "source": "https://github.com/utopia-php/http/tree/0.33.16" + "source": "https://github.com/utopia-php/http/tree/0.33.19" }, - "time": "2025-01-16T15:58:50+00:00" + "time": "2025-03-06T11:37:49+00:00" }, { "name": "utopia-php/telemetry", - "version": "0.1.0", + "version": "0.1.1", "source": { "type": "git", "url": "https://github.com/utopia-php/telemetry.git", - "reference": "d35f2f0632f4ee0be63fb7ace6a94a6adda71a80" + "reference": "437f0021777f0e575dfb9e8a1a081b3aed75e33f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/telemetry/zipball/d35f2f0632f4ee0be63fb7ace6a94a6adda71a80", - "reference": "d35f2f0632f4ee0be63fb7ace6a94a6adda71a80", + "url": "https://api.github.com/repos/utopia-php/telemetry/zipball/437f0021777f0e575dfb9e8a1a081b3aed75e33f", + "reference": "437f0021777f0e575dfb9e8a1a081b3aed75e33f", "shasum": "" }, "require": { @@ -2313,7 +2300,7 @@ "type": "library", "autoload": { "psr-4": { - "Utopia\\": "src/" + "Utopia\\Telemetry\\": "src/Telemetry" } }, "notification-url": "https://packagist.org/downloads/", @@ -2327,9 +2314,9 @@ ], "support": { "issues": "https://github.com/utopia-php/telemetry/issues", - "source": "https://github.com/utopia-php/telemetry/tree/0.1.0" + "source": "https://github.com/utopia-php/telemetry/tree/0.1.1" }, - "time": "2024-11-13T10:29:53+00:00" + "time": "2025-03-17T11:57:52+00:00" } ], "packages-dev": [ @@ -2471,16 +2458,16 @@ }, { "name": "myclabs/deep-copy", - "version": "1.12.1", + "version": "1.13.0", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "123267b2c49fbf30d78a7b2d333f6be754b94845" + "reference": "024473a478be9df5fdaca2c793f2232fe788e414" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/123267b2c49fbf30d78a7b2d333f6be754b94845", - "reference": "123267b2c49fbf30d78a7b2d333f6be754b94845", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/024473a478be9df5fdaca2c793f2232fe788e414", + "reference": "024473a478be9df5fdaca2c793f2232fe788e414", "shasum": "" }, "require": { @@ -2519,7 +2506,7 @@ ], "support": { "issues": "https://github.com/myclabs/DeepCopy/issues", - "source": "https://github.com/myclabs/DeepCopy/tree/1.12.1" + "source": "https://github.com/myclabs/DeepCopy/tree/1.13.0" }, "funding": [ { @@ -2527,7 +2514,7 @@ "type": "tidelift" } ], - "time": "2024-11-08T17:47:46+00:00" + "time": "2025-02-12T12:17:51+00:00" }, { "name": "nikic/php-parser", @@ -2707,16 +2694,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.12.16", + "version": "1.12.23", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "e0bb5cb78545aae631220735aa706eac633a6be9" + "reference": "29201e7a743a6ab36f91394eab51889a82631428" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/e0bb5cb78545aae631220735aa706eac633a6be9", - "reference": "e0bb5cb78545aae631220735aa706eac633a6be9", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/29201e7a743a6ab36f91394eab51889a82631428", + "reference": "29201e7a743a6ab36f91394eab51889a82631428", "shasum": "" }, "require": { @@ -2761,7 +2748,7 @@ "type": "github" } ], - "time": "2025-01-21T14:50:05+00:00" + "time": "2025-03-23T14:57:32+00:00" }, { "name": "phpunit/php-code-coverage", @@ -4306,7 +4293,7 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": {}, "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/src/Queue/Adapter.php b/src/Queue/Adapter.php index 64c378d..a31ee4a 100644 --- a/src/Queue/Adapter.php +++ b/src/Queue/Adapter.php @@ -46,4 +46,13 @@ abstract public function workerStop(callable $callback): self; * @return mixed */ abstract public function getNative(): mixed; + + /** + * Returns the Consumer object from the Adapter. + * @return Consumer + */ + public function getConsumer(): Consumer + { + return $this->consumer; + } } diff --git a/src/Queue/Server.php b/src/Queue/Server.php index a8ab1f8..e52c863 100644 --- a/src/Queue/Server.php +++ b/src/Queue/Server.php @@ -50,8 +50,6 @@ class Server /** * Hook that is called when worker starts - * - * @var Hook */ protected Hook $workerStartHook; @@ -86,6 +84,16 @@ public function job(): Job return $this->job; } + /** + * Returns the underlying consumer of the Server. + * + * @return Consumer + */ + public function getConsumer(): Consumer + { + return $this->adapter->getConsumer(); + } + /** * If a resource has been created return it, otherwise create it and then return it * diff --git a/tests/Queue/E2E/Adapter/Base.php b/tests/Queue/E2E/Adapter/Base.php index 3181486..1885cf7 100644 --- a/tests/Queue/E2E/Adapter/Base.php +++ b/tests/Queue/E2E/Adapter/Base.php @@ -72,7 +72,7 @@ public function testEvents(): void sleep(1); } - protected function testConcurrency(): void + public function testConcurrency(): void { run(function () { $publisher = $this->getPublisher(); From f4b40cd4f11ba1b4a16a50cd1daee700c70decfb Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 28 Mar 2025 16:05:08 +1300 Subject: [PATCH 2/8] Add test workflow --- .github/workflows/tests.yml | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 .github/workflows/tests.yml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..19b6286 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,50 @@ +name: "Tests" + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +on: [pull_request] + +jobs: + unit_test: + name: Unit Test + runs-on: ubuntu-latest + + steps: + - name: checkout + uses: actions/checkout@v4 + + - name: Load and Start Services + run: | + docker compose up -d + sleep 10 + + - name: Run Unit Tests + run: docker compose exec tests vendor/bin/phpunit /usr/src/code/tests/unit + + adapter_test: + name: Adapter Tests + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + adapter: + [ + AMQP, + SwooleRedisCluster, + Swoole, + Workerman, + ] + + steps: + - name: checkout + uses: actions/checkout@v4 + + - name: Load and Start Services + run: | + docker compose up -d + sleep 10 + + - name: Run Tests + run: docker compose exec -T tests vendor/bin/phpunit /usr/src/code/tests/Queue/E2E/Adapter/${{matrix.adapter}}Test.php --debug From 9f06074d71823007c3cf7616e675d789be9d7f6b Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 28 Mar 2025 16:23:44 +1300 Subject: [PATCH 3/8] Fix stan --- .github/workflows/phpstan.yml | 20 ++++++++++++++++++++ .github/workflows/{linter.yml => pint.yml} | 2 +- composer.json | 2 +- src/Queue/Broker/AMQP.php | 8 ++++---- src/Queue/Broker/Redis.php | 2 +- src/Queue/Connection/RedisCluster.php | 5 +++-- src/Queue/Consumer.php | 11 ++++++++--- src/Queue/Publisher.php | 2 +- src/Queue/Server.php | 15 ++++++++------- 9 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 .github/workflows/phpstan.yml rename .github/workflows/{linter.yml => pint.yml} (92%) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..a8874e4 --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,20 @@ +name: "CodeQL" + +on: [ pull_request ] +jobs: + lint: + name: CodeQL + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 2 + + - run: git checkout HEAD^2 + + - name: Run CodeQL + run: | + docker run --rm -v $PWD:/app composer sh -c \ + "composer install --profile --ignore-platform-reqs && composer check" \ No newline at end of file diff --git a/.github/workflows/linter.yml b/.github/workflows/pint.yml similarity index 92% rename from .github/workflows/linter.yml rename to .github/workflows/pint.yml index 9e898b6..26a4907 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/pint.yml @@ -9,7 +9,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 with: fetch-depth: 2 diff --git a/composer.json b/composer.json index 2bab812..3c0cd51 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ }, "scripts":{ "test": "phpunit", - "analyse": "vendor/bin/phpstan analyse", + "check": "vendor/bin/phpstan analyse", "format": "vendor/bin/pint", "lint": "vendor/bin/pint --test" }, diff --git a/src/Queue/Broker/AMQP.php b/src/Queue/Broker/AMQP.php index 130587d..0f78354 100644 --- a/src/Queue/Broker/AMQP.php +++ b/src/Queue/Broker/AMQP.php @@ -76,7 +76,7 @@ public function consume(Queue $queue, callable $messageCallback, callable $succe try { $nextMessage = json_decode($amqpMessage->getBody(), associative: true) ?? false; if (!$nextMessage) { - $amqpMessage->nack(requeue: false); + $amqpMessage->nack(); return; } @@ -92,10 +92,10 @@ public function consume(Queue $queue, callable $messageCallback, callable $succe $successCallback($message); } catch (Retryable $e) { $amqpMessage->nack(requeue: true); - $errorCallback($message, $e); + $errorCallback($message ?? null, $e); } catch (\Throwable $th) { $amqpMessage->nack(requeue: false); - $errorCallback($message, $th); + $errorCallback($message ?? null, $th); } }; @@ -149,7 +149,7 @@ public function enqueue(Queue $queue, array $payload): bool return true; } - public function retry(Queue $queue, int $limit = null): void + public function retry(Queue $queue, ?int $limit = null): void { // This is a no-op for AMQP } diff --git a/src/Queue/Broker/Redis.php b/src/Queue/Broker/Redis.php index 34ee22e..0e3beb7 100644 --- a/src/Queue/Broker/Redis.php +++ b/src/Queue/Broker/Redis.php @@ -109,7 +109,7 @@ public function enqueue(Queue $queue, array $payload): bool * Take all jobs from the failed queue and re-enqueue them. * @param int|null $limit The amount of jobs to retry */ - public function retry(Queue $queue, int $limit = null): void + public function retry(Queue $queue, ?int $limit = null): void { $start = \time(); $processed = 0; diff --git a/src/Queue/Connection/RedisCluster.php b/src/Queue/Connection/RedisCluster.php index 3df0eb7..50a2b46 100644 --- a/src/Queue/Connection/RedisCluster.php +++ b/src/Queue/Connection/RedisCluster.php @@ -110,7 +110,8 @@ public function remove(string $key): bool public function move(string $queue, string $destination): bool { - return $this->getRedis()->move($queue, $destination); + // Move is not supported for Redis Cluster + return false; } public function setArray(string $key, array $value): bool @@ -160,7 +161,7 @@ public function ping(): bool } return true; - } catch (Exception $e) { + } catch (\Throwable) { return false; } } diff --git a/src/Queue/Consumer.php b/src/Queue/Consumer.php index 7f130cd..c71ce6e 100644 --- a/src/Queue/Consumer.php +++ b/src/Queue/Consumer.php @@ -9,15 +9,20 @@ interface Consumer { /** * @param Queue $queue - * @param callable(Message $message): Commit|NoCommit|mixed $messageCallback + * @param callable(Message $message): (Commit|NoCommit|mixed) $messageCallback * @param callable(Message $message): void $successCallback * @param callable(Message $message, \Throwable $th): void $errorCallback * @return void */ - public function consume(Queue $queue, callable $messageCallback, callable $successCallback, callable $errorCallback): void; + public function consume( + Queue $queue, + callable $messageCallback, + callable $successCallback, + callable $errorCallback + ): void; /** - * Closes the consumer and free's any underlying resources. + * Closes the consumer and frees any underlying resources. */ public function close(): void; } diff --git a/src/Queue/Publisher.php b/src/Queue/Publisher.php index da07481..1778656 100644 --- a/src/Queue/Publisher.php +++ b/src/Queue/Publisher.php @@ -20,7 +20,7 @@ public function enqueue(Queue $queue, array $payload): bool; * @param int|null $limit * @return void */ - public function retry(Queue $queue, int $limit = null): void; + public function retry(Queue $queue, ?int $limit = null): void; /** * Returns the amount of pending messages in the queue. diff --git a/src/Queue/Server.php b/src/Queue/Server.php index e52c863..f08fa6a 100644 --- a/src/Queue/Server.php +++ b/src/Queue/Server.php @@ -281,16 +281,16 @@ function (Message $message) { } } } - Console::success("[Job] ({$message->getPid()}) successfully run."); }, - function (Message $message, Throwable $th) { - Console::error("[Job] ({$message->getPid()}) failed to run."); - Console::error("[Job] ({$message->getPid()}) {$th->getMessage()}"); + function (?Message $message, Throwable $th) { + Console::error("[Job] ({$message?->getPid()}) failed to run."); + Console::error("[Job] ({$message?->getPid()}) {$th->getMessage()}"); self::setResource('error', fn () => $th); + foreach ($this->errorHooks as $hook) { - call_user_func_array($hook->getAction(), $this->getArguments($hook)); + ($hook->getAction())(...$this->getArguments($hook)); } }, ); @@ -330,10 +330,11 @@ public function getWorkerStart(): Hook /** * Is called when a Worker stops. - * @param callable $callback + * @param callable|null $callback * @return self + * @throws Exception */ - public function workerStop(callable $callback = null): self + public function workerStop(?callable $callback = null): self { try { $this->adapter->workerStop(function (string $workerId) use ($callback) { From 0206a58be36b0b2c1ec25975fe26cb8cdfe8368b Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 28 Mar 2025 16:34:49 +1300 Subject: [PATCH 4/8] Fix tests --- Dockerfile | 2 +- docker-compose.yml | 5 ----- tests/Queue/E2E/Adapter/Base.php | 19 +++++++++++++++---- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Dockerfile b/Dockerfile index 741f3d3..94354a4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,4 +19,4 @@ COPY . . COPY --from=composer /usr/local/src/vendor /usr/local/src/vendor -CMD ["sleep","3600"] +CMD ["tail", "-f", "/dev/null"] diff --git a/docker-compose.yml b/docker-compose.yml index 09c2320..8d38c59 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,12 +1,7 @@ -version: '3.1' - services: tests: container_name: tests build: . - command: - - vendor/bin/phpunit - - tests volumes: - ./:/usr/local/src depends_on: diff --git a/tests/Queue/E2E/Adapter/Base.php b/tests/Queue/E2E/Adapter/Base.php index 1885cf7..e507d0d 100644 --- a/tests/Queue/E2E/Adapter/Base.php +++ b/tests/Queue/E2E/Adapter/Base.php @@ -93,23 +93,34 @@ public function testRetry(): void { $publisher = $this->getPublisher(); - $publisher->enqueue($this->getQueue(), [ + $published = $publisher->enqueue($this->getQueue(), [ 'type' => 'test_exception', 'id' => 1 ]); - $publisher->enqueue($this->getQueue(), [ + + $this->assertTrue($published); + + $published = $publisher->enqueue($this->getQueue(), [ 'type' => 'test_exception', 'id' => 2 ]); - $publisher->enqueue($this->getQueue(), [ + + $this->assertTrue($published); + + $published = $publisher->enqueue($this->getQueue(), [ 'type' => 'test_exception', 'id' => 3 ]); - $publisher->enqueue($this->getQueue(), [ + + $this->assertTrue($published); + + $published = $publisher->enqueue($this->getQueue(), [ 'type' => 'test_exception', 'id' => 4 ]); + $this->assertTrue($published); + sleep(1); $publisher->retry($this->getQueue()); sleep(1); From 85315b9da9be1b119158d759185db59761f84401 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 28 Mar 2025 16:38:19 +1300 Subject: [PATCH 5/8] Build --- .github/workflows/tests.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 19b6286..da6d904 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -17,6 +17,7 @@ jobs: - name: Load and Start Services run: | + docker compose build docker compose up -d sleep 10 @@ -43,6 +44,7 @@ jobs: - name: Load and Start Services run: | + docker compose build docker compose up -d sleep 10 From 62795c12a61b10856cfe38da23503561de9b2d07 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 28 Mar 2025 16:53:47 +1300 Subject: [PATCH 6/8] Fix volumes --- docker-compose.yml | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 8d38c59..5db1285 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,8 @@ services: container_name: tests build: . volumes: - - ./:/usr/local/src + - ./src:/usr/local/src/src + - ./tests:/usr/local/src/tests depends_on: - swoole - swoole-amqp @@ -15,7 +16,8 @@ services: build: ./tests/Queue/servers/Swoole/. command: php /usr/src/code/tests/Queue/servers/Swoole/worker.php volumes: - - ./:/usr/src/code + - ./src:/usr/local/src/src + - ./tests:/usr/local/src/tests depends_on: - redis @@ -24,7 +26,8 @@ services: build: ./tests/Queue/servers/SwooleRedisCluster/. command: php /usr/src/code/tests/Queue/servers/SwooleRedisCluster/worker.php volumes: - - ./:/usr/src/code + - ./src:/usr/local/src/src + - ./tests:/usr/local/src/tests depends_on: redis-cluster-0: condition: service_healthy @@ -34,7 +37,8 @@ services: build: ./tests/Queue/servers/AMQP/. command: php /usr/src/code/tests/Queue/servers/AMQP/worker.php volumes: - - ./:/usr/src/code + - ./src:/usr/local/src/src + - ./tests:/usr/local/src/tests depends_on: amqp: condition: service_healthy @@ -44,7 +48,8 @@ services: build: ./tests/Queue/servers/Workerman/. command: php /usr/src/code/tests/Queue/servers/Workerman/worker.php start volumes: - - ./:/usr/src/code + - ./src:/usr/local/src/src + - ./tests:/usr/local/src/tests depends_on: - redis @@ -110,5 +115,5 @@ services: RABBITMQ_DEFAULT_PASS: amqp RABBITMQ_DEFAULT_VHOST: "/" healthcheck: - test: [ "CMD", "rabbitmqctl", "node_health_check"] + test: [ "CMD", "rabbitmqctl", "node_health_check" ] start_interval: 1s \ No newline at end of file From d9fb51ec2f303b8e336a2dc487444e25538c1e46 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 28 Mar 2025 16:57:42 +1300 Subject: [PATCH 7/8] Fix path --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index da6d904..78e5b7f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -49,4 +49,4 @@ jobs: sleep 10 - name: Run Tests - run: docker compose exec -T tests vendor/bin/phpunit /usr/src/code/tests/Queue/E2E/Adapter/${{matrix.adapter}}Test.php --debug + run: docker compose exec -T tests vendor/bin/phpunit /usr/local/src/tests/Queue/E2E/Adapter/${{matrix.adapter}}Test.php --debug From bd62e6e545408a5d5d0865f507890b4928d406a1 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 28 Mar 2025 18:37:17 +1300 Subject: [PATCH 8/8] Remove redunant unit test --- .github/workflows/tests.yml | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 78e5b7f..6391332 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,23 +7,6 @@ concurrency: on: [pull_request] jobs: - unit_test: - name: Unit Test - runs-on: ubuntu-latest - - steps: - - name: checkout - uses: actions/checkout@v4 - - - name: Load and Start Services - run: | - docker compose build - docker compose up -d - sleep 10 - - - name: Run Unit Tests - run: docker compose exec tests vendor/bin/phpunit /usr/src/code/tests/unit - adapter_test: name: Adapter Tests runs-on: ubuntu-latest