From 6aacf5b254a7b981a1c8f743555b0b1ae6aba525 Mon Sep 17 00:00:00 2001 From: Marius Cristea Date: Sat, 14 Jul 2018 23:23:20 +0300 Subject: [PATCH 1/6] Adds basic SQS compatibility. Implements SQS Worker, Client and Connection class. Adds global variable $aws_credentials as well as sqs for WP_MINIONS_BACKEND. --- composer.json | 3 +- composer.lock | 732 -------------------------- includes/WpMinions/Plugin.php | 4 + includes/WpMinions/SQS/Client.php | 82 +++ includes/WpMinions/SQS/Connection.php | 61 +++ includes/WpMinions/SQS/Worker.php | 145 +++++ 6 files changed, 294 insertions(+), 733 deletions(-) delete mode 100644 composer.lock create mode 100644 includes/WpMinions/SQS/Client.php create mode 100644 includes/WpMinions/SQS/Connection.php create mode 100644 includes/WpMinions/SQS/Worker.php diff --git a/composer.json b/composer.json index 1df355c..d6e4f36 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ ], "require": { "composer/installers": "~1.0", - "php-amqplib/php-amqplib": ">=2.6.1" + "php-amqplib/php-amqplib": ">=2.6.1", + "aws/aws-sdk-php": "^3.63" }, "require-dev": { "phpunit/phpunit": "~3.7", diff --git a/composer.lock b/composer.lock deleted file mode 100644 index f710ec0..0000000 --- a/composer.lock +++ /dev/null @@ -1,732 +0,0 @@ -{ - "_readme": [ - "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", - "This file is @generated automatically" - ], - "content-hash": "3da539add4bc42dcecdaa80e0c670adc", - "packages": [ - { - "name": "composer/installers", - "version": "v1.3.0", - "source": { - "type": "git", - "url": "https://github.com/composer/installers.git", - "reference": "79ad876c7498c0bbfe7eed065b8651c93bfd6045" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/composer/installers/zipball/79ad876c7498c0bbfe7eed065b8651c93bfd6045", - "reference": "79ad876c7498c0bbfe7eed065b8651c93bfd6045", - "shasum": "" - }, - "require": { - "composer-plugin-api": "^1.0" - }, - "replace": { - "roundcube/plugin-installer": "*", - "shama/baton": "*" - }, - "require-dev": { - "composer/composer": "1.0.*@dev", - "phpunit/phpunit": "4.1.*" - }, - "type": "composer-plugin", - "extra": { - "class": "Composer\\Installers\\Plugin", - "branch-alias": { - "dev-master": "1.0-dev" - } - }, - "autoload": { - "psr-4": { - "Composer\\Installers\\": "src/Composer/Installers" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Kyle Robinson Young", - "email": "kyle@dontkry.com", - "homepage": "https://github.com/shama" - } - ], - "description": "A multi-framework Composer library installer", - "homepage": "https://composer.github.io/installers/", - "keywords": [ - "Craft", - "Dolibarr", - "Eliasis", - "Hurad", - "ImageCMS", - "Kanboard", - "MODX Evo", - "Mautic", - "Maya", - "OXID", - "Plentymarkets", - "Porto", - "RadPHP", - "SMF", - "Thelia", - "WolfCMS", - "agl", - "aimeos", - "annotatecms", - "attogram", - "bitrix", - "cakephp", - "chef", - "cockpit", - "codeigniter", - "concrete5", - "croogo", - "dokuwiki", - "drupal", - "elgg", - "expressionengine", - "fuelphp", - "grav", - "installer", - "itop", - "joomla", - "kohana", - "laravel", - "lavalite", - "lithium", - "magento", - "mako", - "mediawiki", - "modulework", - "moodle", - "phpbb", - "piwik", - "ppi", - "puppet", - "reindex", - "roundcube", - "shopware", - "silverstripe", - "sydes", - "symfony", - "typo3", - "wordpress", - "yawik", - "zend", - "zikula" - ], - "time": "2017-04-24T06:37:16+00:00" - }, - { - "name": "php-amqplib/php-amqplib", - "version": "v2.6.3", - "source": { - "type": "git", - "url": "https://github.com/php-amqplib/php-amqplib.git", - "reference": "fa2f0d4410a11008cb36b379177291be7ee9e4f6" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/php-amqplib/php-amqplib/zipball/fa2f0d4410a11008cb36b379177291be7ee9e4f6", - "reference": "fa2f0d4410a11008cb36b379177291be7ee9e4f6", - "shasum": "" - }, - "require": { - "ext-bcmath": "*", - "ext-mbstring": "*", - "php": ">=5.3.0" - }, - "replace": { - "videlalvaro/php-amqplib": "self.version" - }, - "require-dev": { - "phpunit/phpunit": "^4.8", - "scrutinizer/ocular": "^1.1", - "squizlabs/php_codesniffer": "^2.5" - }, - "suggest": { - "ext-sockets": "Use AMQPSocketConnection" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.7-dev" - } - }, - "autoload": { - "psr-4": { - "PhpAmqpLib\\": "PhpAmqpLib/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "LGPL-2.1" - ], - "authors": [ - { - "name": "Alvaro Videla", - "role": "Original Maintainer" - }, - { - "name": "John Kelly", - "email": "johnmkelly86@gmail.com", - "role": "Maintainer" - }, - { - "name": "Raúl Araya", - "email": "nubeiro@gmail.com", - "role": "Maintainer" - } - ], - "description": "Formerly videlalvaro/php-amqplib. This library is a pure PHP implementation of the AMQP protocol. It's been tested against RabbitMQ.", - "homepage": "https://github.com/php-amqplib/php-amqplib/", - "keywords": [ - "message", - "queue", - "rabbitmq" - ], - "time": "2016-04-11T14:30:01+00:00" - } - ], - "packages-dev": [ - { - "name": "hamcrest/hamcrest-php", - "version": "v1.2.2", - "source": { - "type": "git", - "url": "https://github.com/hamcrest/hamcrest-php.git", - "reference": "b37020aa976fa52d3de9aa904aa2522dc518f79c" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/hamcrest/hamcrest-php/zipball/b37020aa976fa52d3de9aa904aa2522dc518f79c", - "reference": "b37020aa976fa52d3de9aa904aa2522dc518f79c", - "shasum": "" - }, - "require": { - "php": ">=5.3.2" - }, - "replace": { - "cordoval/hamcrest-php": "*", - "davedevelopment/hamcrest-php": "*", - "kodova/hamcrest-php": "*" - }, - "require-dev": { - "phpunit/php-file-iterator": "1.3.3", - "satooshi/php-coveralls": "dev-master" - }, - "type": "library", - "autoload": { - "classmap": [ - "hamcrest" - ], - "files": [ - "hamcrest/Hamcrest.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD" - ], - "description": "This is the PHP port of Hamcrest Matchers", - "keywords": [ - "test" - ], - "time": "2015-05-11T14:41:42+00:00" - }, - { - "name": "mockery/mockery", - "version": "0.9.9", - "source": { - "type": "git", - "url": "https://github.com/mockery/mockery.git", - "reference": "6fdb61243844dc924071d3404bb23994ea0b6856" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/mockery/mockery/zipball/6fdb61243844dc924071d3404bb23994ea0b6856", - "reference": "6fdb61243844dc924071d3404bb23994ea0b6856", - "shasum": "" - }, - "require": { - "hamcrest/hamcrest-php": "~1.1", - "lib-pcre": ">=7.0", - "php": ">=5.3.2" - }, - "require-dev": { - "phpunit/phpunit": "~4.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "0.9.x-dev" - } - }, - "autoload": { - "psr-0": { - "Mockery": "library/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Pádraic Brady", - "email": "padraic.brady@gmail.com", - "homepage": "http://blog.astrumfutura.com" - }, - { - "name": "Dave Marshall", - "email": "dave.marshall@atstsolutions.co.uk", - "homepage": "http://davedevelopment.co.uk" - } - ], - "description": "Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a test double framework with a succinct API capable of clearly defining all possible object operations and interactions using a human readable Domain Specific Language (DSL). Designed as a drop in alternative to PHPUnit's phpunit-mock-objects library, Mockery is easy to integrate with PHPUnit and can operate alongside phpunit-mock-objects without the World ending.", - "homepage": "http://github.com/padraic/mockery", - "keywords": [ - "BDD", - "TDD", - "library", - "mock", - "mock objects", - "mockery", - "stub", - "test", - "test double", - "testing" - ], - "time": "2017-02-28T12:52:32+00:00" - }, - { - "name": "phpunit/php-code-coverage", - "version": "1.2.18", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "fe2466802556d3fe4e4d1d58ffd3ccfd0a19be0b" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/fe2466802556d3fe4e4d1d58ffd3ccfd0a19be0b", - "reference": "fe2466802556d3fe4e4d1d58ffd3ccfd0a19be0b", - "shasum": "" - }, - "require": { - "php": ">=5.3.3", - "phpunit/php-file-iterator": ">=1.3.0@stable", - "phpunit/php-text-template": ">=1.2.0@stable", - "phpunit/php-token-stream": ">=1.1.3,<1.3.0" - }, - "require-dev": { - "phpunit/phpunit": "3.7.*@dev" - }, - "suggest": { - "ext-dom": "*", - "ext-xdebug": ">=2.0.5" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.2.x-dev" - } - }, - "autoload": { - "classmap": [ - "PHP/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "" - ], - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", - "homepage": "https://github.com/sebastianbergmann/php-code-coverage", - "keywords": [ - "coverage", - "testing", - "xunit" - ], - "time": "2014-09-02T10:13:14+00:00" - }, - { - "name": "phpunit/php-file-iterator", - "version": "1.4.2", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-file-iterator.git", - "reference": "3cc8f69b3028d0f96a9078e6295d86e9bf019be5" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/3cc8f69b3028d0f96a9078e6295d86e9bf019be5", - "reference": "3cc8f69b3028d0f96a9078e6295d86e9bf019be5", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.4.x-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "FilterIterator implementation that filters files based on a list of suffixes.", - "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", - "keywords": [ - "filesystem", - "iterator" - ], - "time": "2016-10-03T07:40:28+00:00" - }, - { - "name": "phpunit/php-text-template", - "version": "1.2.1", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-text-template.git", - "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/31f8b717e51d9a2afca6c9f046f5d69fc27c8686", - "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "type": "library", - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "lead" - } - ], - "description": "Simple template engine.", - "homepage": "https://github.com/sebastianbergmann/php-text-template/", - "keywords": [ - "template" - ], - "time": "2015-06-21T13:50:34+00:00" - }, - { - "name": "phpunit/php-timer", - "version": "1.0.9", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-timer.git", - "reference": "3dcf38ca72b158baf0bc245e9184d3fdffa9c46f" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/3dcf38ca72b158baf0bc245e9184d3fdffa9c46f", - "reference": "3dcf38ca72b158baf0bc245e9184d3fdffa9c46f", - "shasum": "" - }, - "require": { - "php": "^5.3.3 || ^7.0" - }, - "require-dev": { - "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.0-dev" - } - }, - "autoload": { - "classmap": [ - "src/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "Utility class for timing", - "homepage": "https://github.com/sebastianbergmann/php-timer/", - "keywords": [ - "timer" - ], - "time": "2017-02-26T11:10:40+00:00" - }, - { - "name": "phpunit/php-token-stream", - "version": "1.2.2", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "ad4e1e23ae01b483c16f600ff1bebec184588e32" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/ad4e1e23ae01b483c16f600ff1bebec184588e32", - "reference": "ad4e1e23ae01b483c16f600ff1bebec184588e32", - "shasum": "" - }, - "require": { - "ext-tokenizer": "*", - "php": ">=5.3.3" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.2-dev" - } - }, - "autoload": { - "classmap": [ - "PHP/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "" - ], - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "Wrapper around PHP's tokenizer extension.", - "homepage": "https://github.com/sebastianbergmann/php-token-stream/", - "keywords": [ - "tokenizer" - ], - "time": "2014-03-03T05:10:30+00:00" - }, - { - "name": "phpunit/phpunit", - "version": "3.7.38", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "38709dc22d519a3d1be46849868aa2ddf822bcf6" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/38709dc22d519a3d1be46849868aa2ddf822bcf6", - "reference": "38709dc22d519a3d1be46849868aa2ddf822bcf6", - "shasum": "" - }, - "require": { - "ext-ctype": "*", - "ext-dom": "*", - "ext-json": "*", - "ext-pcre": "*", - "ext-reflection": "*", - "ext-spl": "*", - "php": ">=5.3.3", - "phpunit/php-code-coverage": "~1.2", - "phpunit/php-file-iterator": "~1.3", - "phpunit/php-text-template": "~1.1", - "phpunit/php-timer": "~1.0", - "phpunit/phpunit-mock-objects": "~1.2", - "symfony/yaml": "~2.0" - }, - "require-dev": { - "pear-pear.php.net/pear": "1.9.4" - }, - "suggest": { - "phpunit/php-invoker": "~1.1" - }, - "bin": [ - "composer/bin/phpunit" - ], - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.7.x-dev" - } - }, - "autoload": { - "classmap": [ - "PHPUnit/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "", - "../../symfony/yaml/" - ], - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sebastian@phpunit.de", - "role": "lead" - } - ], - "description": "The PHP Unit Testing framework.", - "homepage": "http://www.phpunit.de/", - "keywords": [ - "phpunit", - "testing", - "xunit" - ], - "time": "2014-10-17T09:04:17+00:00" - }, - { - "name": "phpunit/phpunit-mock-objects", - "version": "1.2.3", - "source": { - "type": "git", - "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", - "reference": "5794e3c5c5ba0fb037b11d8151add2a07fa82875" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/5794e3c5c5ba0fb037b11d8151add2a07fa82875", - "reference": "5794e3c5c5ba0fb037b11d8151add2a07fa82875", - "shasum": "" - }, - "require": { - "php": ">=5.3.3", - "phpunit/php-text-template": ">=1.1.1@stable" - }, - "suggest": { - "ext-soap": "*" - }, - "type": "library", - "autoload": { - "classmap": [ - "PHPUnit/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "" - ], - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Sebastian Bergmann", - "email": "sb@sebastian-bergmann.de", - "role": "lead" - } - ], - "description": "Mock Object library for PHPUnit", - "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", - "keywords": [ - "mock", - "xunit" - ], - "time": "2013-01-13T10:24:48+00:00" - }, - { - "name": "symfony/yaml", - "version": "v2.8.24", - "source": { - "type": "git", - "url": "https://github.com/symfony/yaml.git", - "reference": "4c29dec8d489c4e37cf87ccd7166cd0b0e6a45c5" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/4c29dec8d489c4e37cf87ccd7166cd0b0e6a45c5", - "reference": "4c29dec8d489c4e37cf87ccd7166cd0b0e6a45c5", - "shasum": "" - }, - "require": { - "php": ">=5.3.9" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.8-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Component\\Yaml\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony Yaml Component", - "homepage": "https://symfony.com", - "time": "2017-06-01T20:52:29+00:00" - } - ], - "aliases": [], - "minimum-stability": "stable", - "stability-flags": [], - "prefer-stable": false, - "prefer-lowest": false, - "platform": [], - "platform-dev": [] -} diff --git a/includes/WpMinions/Plugin.php b/includes/WpMinions/Plugin.php index f0c25bd..ad79eb7 100644 --- a/includes/WpMinions/Plugin.php +++ b/includes/WpMinions/Plugin.php @@ -187,6 +187,8 @@ function build_client() { return new \WpMinions\Gearman\Client(); } elseif ( 'rabbitmq' === strtolower( $backend ) ) { return new \WpMinions\RabbitMQ\Client(); + } elseif ( 'sqs' === strtolower( $backend ) ) { + return new \WpMinions\SQS\Client(); } else { return new \WpMinions\Cron\Client(); } @@ -217,6 +219,8 @@ function build_worker() { return new \WpMinions\Gearman\Worker(); } elseif ( 'rabbitmq' === strtolower( $backend ) ) { return new \WpMinions\RabbitMQ\Worker(); + } elseif ( 'sqs' === strtolower( $backend ) ) { + return new \WpMinions\SQS\Worker(); } else { return new \WpMinions\Cron\Worker(); } diff --git a/includes/WpMinions/SQS/Client.php b/includes/WpMinions/SQS/Client.php new file mode 100644 index 0000000..7f63a7d --- /dev/null +++ b/includes/WpMinions/SQS/Client.php @@ -0,0 +1,82 @@ +connect() ) { + return false; + } + + $job_data = array( + 'hook' => $hook, + 'args' => $args, + 'blog_id' => get_current_blog_id(), + ); + try { + $this->connection->get_connection()->sendMessage( array( + 'QueueUrl' => $this->connection->get_channel(), + 'MessageBody' => json_encode( $job_data ) + ) ); + + return true; + } catch ( \Exception $e ) { + error_log( + 'SQSClient->add failed: ' . $e->getMessage() + ); + + return false; + } + } + + /** + * Connect to host and channel. + */ + private function connect() { + if ( null !== $this->connection ) { + return $this->connection; + } + + try { + $this->connection = new Connection(); + } catch ( \Exception $e ) { + error_log( + 'SQSClient->connect failed: ' . $e->getMessage() + ); + + return false; + } + + return $this->connection; + } +} diff --git a/includes/WpMinions/SQS/Connection.php b/includes/WpMinions/SQS/Connection.php new file mode 100644 index 0000000..e92e123 --- /dev/null +++ b/includes/WpMinions/SQS/Connection.php @@ -0,0 +1,61 @@ +queue_name = 'fleet'; + $this->aws_credentials = $aws_credentials; + $this->connection = new \Aws\Sqs\SqsClient( $this->aws_credentials ); + + } else { + throw new \Exception( 'Could not create connection.' ); + } + } + + /** + * Return queue url. + * + * @return mixed|null Queue url. + */ + public function get_channel() { + return $this->connection->getQueueUrl( array( 'QueueName' => $this->queue_name ) )->get( 'QueueUrl' ); + } + + /** + * Return connection client. + * + * @return \Aws\Sqs\SqsClient Client. + */ + public function get_connection() { + return $this->connection; + } + +} diff --git a/includes/WpMinions/SQS/Worker.php b/includes/WpMinions/SQS/Worker.php new file mode 100644 index 0000000..be0d12f --- /dev/null +++ b/includes/WpMinions/SQS/Worker.php @@ -0,0 +1,145 @@ +connect() ) { + return false; + } + $switched = false; + + $message = $this->get_message(); + if ( empty( $message ) ) { + return false; + } + + try { + $job_data = json_decode( $message['Body'], true ); + $hook = $job_data['hook']; + $args = $job_data['args']; + + if ( function_exists( 'is_multisite' ) && is_multisite() && $job_data['blog_id'] ) { + $blog_id = $job_data['blog_id']; + + if ( get_current_blog_id() !== $blog_id ) { + switch_to_blog( $blog_id ); + $switched = true; + } else { + $switched = false; + } + } else { + $switched = false; + } + + do_action( 'wp_async_task_before_job', $hook, $message ); + do_action( 'wp_async_task_before_job_' . $hook, $message ); + + do_action( $hook, $args, $message ); + + do_action( 'wp_async_task_after_job', $hook, $message ); + do_action( 'wp_async_task_after_job_' . $hook, $message ); + + //Delete message from the queue as we have procesessed. + $this->connection->get_connection()->deleteMessage( array( + 'QueueUrl' => $this->connection->get_channel(), + 'ReceiptHandle' => $message['ReceiptHandle'] + ) ); + $result = true; + } catch ( \Exception $e ) { + error_log( + 'SQSWorker->do_job failed: ' . $e->getMessage() + ); + //Make the message available back into the queue right away. + $this->connection->get_connection()->changeMessageVisibility( array( + 'QueueUrl' => $this->connection->get_channel(), + 'ReceiptHandle' => $message['ReceiptHandle'], + 'VisibilityTimeout' => 0 + ) ); + + $result = false; + } + + if ( $switched ) { + restore_current_blog(); + } + //Wait 3s to avoid too many requests to AWS. + sleep( 3 ); + + return $result; + } + + /** + * Connect to Aws SQS queue. + */ + private function connect() { + if ( null !== $this->connection ) { + return $this->connection; + } + + try { + $this->connection = new Connection(); + } catch ( \Exception $e ) { + return false; + } + + return $this->connection; + } + + /** + * Get next message from the queue. + * + * @return \Aws\Result|bool|mixed + */ + private function get_message() { + if ( empty( $this->connection ) ) { + return false; + } + try { + // Receive a message from the queue + $message = $this->connection->get_connection()->receiveMessage( array( + 'QueueUrl' => $this->connection->get_channel() + ) ); + + if ( $message['Messages'] == null ) { + // No message to process + return false; + } + // Get the message and return it + $message = array_pop( $message['Messages'] ); + + return $message; + } catch ( \Exception $e ) { + error_log( + 'SQSWorker->get_message failed: ' . $e->getMessage() + ); + + return false; + } + } +} From 4b6628cec210923c205c247bba24de1f4a4fbe34 Mon Sep 17 00:00:00 2001 From: selu91 Date: Mon, 16 Jul 2018 17:11:51 +0300 Subject: [PATCH 2/6] Adds system tests for SQS. Remove testing queue name, adds wordpress, following the rabbitmq example. --- includes/WpMinions/SQS/Connection.php | 2 +- system-tests/sqs/test-client.php | 21 +++++++++++ system-tests/sqs/test-worker.php | 52 +++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 system-tests/sqs/test-client.php create mode 100644 system-tests/sqs/test-worker.php diff --git a/includes/WpMinions/SQS/Connection.php b/includes/WpMinions/SQS/Connection.php index e92e123..98d3bba 100644 --- a/includes/WpMinions/SQS/Connection.php +++ b/includes/WpMinions/SQS/Connection.php @@ -31,7 +31,7 @@ public function __construct() { $aws_credentials = array(); } - $this->queue_name = 'fleet'; + $this->queue_name = 'wordpress'; $this->aws_credentials = $aws_credentials; $this->connection = new \Aws\Sqs\SqsClient( $this->aws_credentials ); diff --git a/system-tests/sqs/test-client.php b/system-tests/sqs/test-client.php new file mode 100644 index 0000000..2c00b69 --- /dev/null +++ b/system-tests/sqs/test-client.php @@ -0,0 +1,21 @@ + 'us-east-1', + 'version' => 'latest', + 'credentials' => array( + 'key' => '', //Testing AWS key. + 'secret' => ' ' //Testing AWS secret. + ) +); + +$connection = new Aws\Sqs\SqsClient( $aws_credentials ); + +$queue_url = $connection->getQueueUrl( array( 'QueueName' => 'wordpress' ) )->get( 'QueueUrl' ); + +$connection->sendMessage( array( + 'QueueUrl' => $queue_url, + 'MessageBody' => 'Hello World!' +) ); \ No newline at end of file diff --git a/system-tests/sqs/test-worker.php b/system-tests/sqs/test-worker.php new file mode 100644 index 0000000..b3dccb8 --- /dev/null +++ b/system-tests/sqs/test-worker.php @@ -0,0 +1,52 @@ + 'us-east-1', + 'version' => 'latest', + 'credentials' => array( + 'key' => '', //Testing AWS key. + 'secret' => ' ' //Testing AWS secret. + ) +); + +$connection = new Aws\Sqs\SqsClient( $aws_credentials ); + +$queue_url = $connection->getQueueUrl( array( 'QueueName' => 'wordpress' ) )->get( 'QueueUrl' ); + +echo ' [*] Waiting for messages. To exit press CTRL+C', "\n"; + +$callback = function ( $msg ) { + echo " [x] Received ", $msg, "\n"; +}; + +while ( true ) { + $message = $connection->receiveMessage( array( + 'QueueUrl' => $queue_url + ) ); + if ( ! empty( $message ) && ! empty( $message['Messages'] ) ) { + + $message = array_pop( $message['Messages'] ); + + try { + + call_user_func( $callback, $message['Body'] ); + $connection->deleteMessage( array( + 'QueueUrl' => $queue_url, + 'ReceiptHandle' => $message['ReceiptHandle'] + ) ); + } catch ( Exception $e ) { + $connection->changeMessageVisibility( array( + 'QueueUrl' => $queue_url, + 'ReceiptHandle' => $message['ReceiptHandle'], + 'VisibilityTimeout' => 0 + ) ); + echo $e->getMessage(); + } + + } + + sleep( 3 ); +} From df7a3dc0f33d24847e52d23eea8055c36df26b39 Mon Sep 17 00:00:00 2001 From: selu91 Date: Mon, 16 Jul 2018 17:16:22 +0300 Subject: [PATCH 3/6] Adds config file for system tests. --- system-tests/sqs/config.php | 11 +++++++++++ system-tests/sqs/test-client.php | 11 ++--------- system-tests/sqs/test-worker.php | 11 +---------- 3 files changed, 14 insertions(+), 19 deletions(-) create mode 100644 system-tests/sqs/config.php diff --git a/system-tests/sqs/config.php b/system-tests/sqs/config.php new file mode 100644 index 0000000..d2c15e6 --- /dev/null +++ b/system-tests/sqs/config.php @@ -0,0 +1,11 @@ + 'us-east-1', + 'version' => 'latest', + 'credentials' => array( + 'key' => '', //Testing AWS key. + 'secret' => ' ' //Testing AWS secret. + ) +); diff --git a/system-tests/sqs/test-client.php b/system-tests/sqs/test-client.php index 2c00b69..18a5fe8 100644 --- a/system-tests/sqs/test-client.php +++ b/system-tests/sqs/test-client.php @@ -1,15 +1,8 @@ 'us-east-1', - 'version' => 'latest', - 'credentials' => array( - 'key' => '', //Testing AWS key. - 'secret' => ' ' //Testing AWS secret. - ) -); +require_once __DIR__ . '/config.php'; + $connection = new Aws\Sqs\SqsClient( $aws_credentials ); diff --git a/system-tests/sqs/test-worker.php b/system-tests/sqs/test-worker.php index b3dccb8..fce2af9 100644 --- a/system-tests/sqs/test-worker.php +++ b/system-tests/sqs/test-worker.php @@ -1,16 +1,7 @@ 'us-east-1', - 'version' => 'latest', - 'credentials' => array( - 'key' => '', //Testing AWS key. - 'secret' => ' ' //Testing AWS secret. - ) -); +require_once __DIR__ . '/config.php'; $connection = new Aws\Sqs\SqsClient( $aws_credentials ); From 461445dcda0322b9d2926ad37a01732dec9dfb52 Mon Sep 17 00:00:00 2001 From: selul Date: Wed, 25 Sep 2019 19:15:29 +0300 Subject: [PATCH 4/6] fix conflicts --- composer.lock | 469 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 443 insertions(+), 26 deletions(-) diff --git a/composer.lock b/composer.lock index d310b80..15dd05b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,20 +4,103 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "579b825ca4a8572eec781af18d9ab14c", + "content-hash": "9b1232c657fcb82858c213a9ef437b29", "packages": [ + { + "name": "aws/aws-sdk-php", + "version": "3.112.7", + "source": { + "type": "git", + "url": "https://github.com/aws/aws-sdk-php.git", + "reference": "5167704e39f4e139152906b3d962aa15692bff07" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/5167704e39f4e139152906b3d962aa15692bff07", + "reference": "5167704e39f4e139152906b3d962aa15692bff07", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-pcre": "*", + "ext-simplexml": "*", + "guzzlehttp/guzzle": "^5.3.3|^6.2.1", + "guzzlehttp/promises": "~1.0", + "guzzlehttp/psr7": "^1.4.1", + "mtdowling/jmespath.php": "~2.2", + "php": ">=5.5" + }, + "require-dev": { + "andrewsville/php-token-reflection": "^1.4", + "aws/aws-php-sns-message-validator": "~1.0", + "behat/behat": "~3.0", + "doctrine/cache": "~1.4", + "ext-dom": "*", + "ext-openssl": "*", + "ext-pcntl": "*", + "ext-sockets": "*", + "nette/neon": "^2.3", + "phpunit/phpunit": "^4.8.35|^5.4.3", + "psr/cache": "^1.0", + "psr/simple-cache": "^1.0" + }, + "suggest": { + "aws/aws-php-sns-message-validator": "To validate incoming SNS notifications", + "doctrine/cache": "To use the DoctrineCacheAdapter", + "ext-curl": "To send requests using cURL", + "ext-openssl": "Allows working with CloudFront private distributions and verifying received SNS messages", + "ext-sockets": "To use client-side monitoring" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, + "autoload": { + "psr-4": { + "Aws\\": "src/" + }, + "files": [ + "src/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Amazon Web Services", + "homepage": "http://aws.amazon.com" + } + ], + "description": "AWS SDK for PHP - Use Amazon Web Services in your PHP project", + "homepage": "http://aws.amazon.com/sdkforphp", + "keywords": [ + "amazon", + "aws", + "cloud", + "dynamodb", + "ec2", + "glacier", + "s3", + "sdk" + ], + "time": "2019-09-24T18:13:32+00:00" + }, { "name": "composer/installers", - "version": "v1.6.0", + "version": "v1.7.0", "source": { "type": "git", "url": "https://github.com/composer/installers.git", - "reference": "cfcca6b1b60bc4974324efb5783c13dca6932b5b" + "reference": "141b272484481432cda342727a427dc1e206bfa0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/installers/zipball/cfcca6b1b60bc4974324efb5783c13dca6932b5b", - "reference": "cfcca6b1b60bc4974324efb5783c13dca6932b5b", + "url": "https://api.github.com/repos/composer/installers/zipball/141b272484481432cda342727a427dc1e206bfa0", + "reference": "141b272484481432cda342727a427dc1e206bfa0", "shasum": "" }, "require": { @@ -73,6 +156,7 @@ "RadPHP", "SMF", "Thelia", + "Whmcs", "WolfCMS", "agl", "aimeos", @@ -95,6 +179,7 @@ "installer", "itop", "joomla", + "known", "kohana", "laravel", "lavalite", @@ -124,7 +209,249 @@ "zend", "zikula" ], - "time": "2018-08-27T06:10:37+00:00" + "time": "2019-08-12T15:00:31+00:00" + }, + { + "name": "guzzlehttp/guzzle", + "version": "6.3.3", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "407b0cb880ace85c9b63c5f9551db498cb2d50ba" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/407b0cb880ace85c9b63c5f9551db498cb2d50ba", + "reference": "407b0cb880ace85c9b63c5f9551db498cb2d50ba", + "shasum": "" + }, + "require": { + "guzzlehttp/promises": "^1.0", + "guzzlehttp/psr7": "^1.4", + "php": ">=5.5" + }, + "require-dev": { + "ext-curl": "*", + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.4 || ^7.0", + "psr/log": "^1.0" + }, + "suggest": { + "psr/log": "Required for using the Log middleware" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.3-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle is a PHP HTTP client library", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2018-04-22T15:46:56+00:00" + }, + { + "name": "guzzlehttp/promises", + "version": "v1.3.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "time": "2016-12-20T10:07:11+00:00" + }, + { + "name": "guzzlehttp/psr7", + "version": "1.6.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "239400de7a173fe9901b9ac7c06497751f00727a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/239400de7a173fe9901b9ac7c06497751f00727a", + "reference": "239400de7a173fe9901b9ac7c06497751f00727a", + "shasum": "" + }, + "require": { + "php": ">=5.4.0", + "psr/http-message": "~1.0", + "ralouphie/getallheaders": "^2.0.5 || ^3.0.0" + }, + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "ext-zlib": "*", + "phpunit/phpunit": "~4.8.36 || ^5.7.27 || ^6.5.8" + }, + "suggest": { + "zendframework/zend-httphandlerrunner": "Emit PSR-7 responses" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.6-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Tobias Schultze", + "homepage": "https://github.com/Tobion" + } + ], + "description": "PSR-7 message implementation that also provides common utility methods", + "keywords": [ + "http", + "message", + "psr-7", + "request", + "response", + "stream", + "uri", + "url" + ], + "time": "2019-07-01T23:21:34+00:00" + }, + { + "name": "mtdowling/jmespath.php", + "version": "2.4.0", + "source": { + "type": "git", + "url": "https://github.com/jmespath/jmespath.php.git", + "reference": "adcc9531682cf87dfda21e1fd5d0e7a41d292fac" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/jmespath/jmespath.php/zipball/adcc9531682cf87dfda21e1fd5d0e7a41d292fac", + "reference": "adcc9531682cf87dfda21e1fd5d0e7a41d292fac", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "bin": [ + "bin/jp.php" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "autoload": { + "psr-4": { + "JmesPath\\": "src/" + }, + "files": [ + "src/JmesPath.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Declaratively specify how to extract elements from a JSON document", + "keywords": [ + "json", + "jsonpath" + ], + "time": "2016-12-03T22:08:25+00:00" }, { "name": "php-amqplib/php-amqplib", @@ -195,6 +522,96 @@ "rabbitmq" ], "time": "2018-10-23T18:48:24+00:00" + }, + { + "name": "psr/http-message", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "time": "2016-08-06T14:39:51+00:00" + }, + { + "name": "ralouphie/getallheaders", + "version": "3.0.3", + "source": { + "type": "git", + "url": "https://github.com/ralouphie/getallheaders.git", + "reference": "120b605dfeb996808c31b6477290a714d356e822" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822", + "reference": "120b605dfeb996808c31b6477290a714d356e822", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "require-dev": { + "php-coveralls/php-coveralls": "^2.1", + "phpunit/phpunit": "^5 || ^6.5" + }, + "type": "library", + "autoload": { + "files": [ + "src/getallheaders.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ralph Khattar", + "email": "ralph.khattar@gmail.com" + } + ], + "description": "A polyfill for getallheaders.", + "time": "2019-03-08T08:55:37+00:00" } ], "packages-dev": [ @@ -245,16 +662,16 @@ }, { "name": "mockery/mockery", - "version": "0.9.9", + "version": "0.9.11", "source": { "type": "git", "url": "https://github.com/mockery/mockery.git", - "reference": "6fdb61243844dc924071d3404bb23994ea0b6856" + "reference": "be9bf28d8e57d67883cba9fcadfcff8caab667f8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/mockery/mockery/zipball/6fdb61243844dc924071d3404bb23994ea0b6856", - "reference": "6fdb61243844dc924071d3404bb23994ea0b6856", + "url": "https://api.github.com/repos/mockery/mockery/zipball/be9bf28d8e57d67883cba9fcadfcff8caab667f8", + "reference": "be9bf28d8e57d67883cba9fcadfcff8caab667f8", "shasum": "" }, "require": { @@ -306,7 +723,7 @@ "test double", "testing" ], - "time": "2017-02-28T12:52:32+00:00" + "time": "2019-02-12T16:07:13+00:00" }, { "name": "phpunit/php-code-coverage", @@ -680,16 +1097,16 @@ }, { "name": "symfony/polyfill-ctype", - "version": "v1.10.0", + "version": "v1.12.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-ctype.git", - "reference": "e3d826245268269cd66f8326bd8bc066687b4a19" + "reference": "550ebaac289296ce228a706d0867afc34687e3f4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/e3d826245268269cd66f8326bd8bc066687b4a19", - "reference": "e3d826245268269cd66f8326bd8bc066687b4a19", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/550ebaac289296ce228a706d0867afc34687e3f4", + "reference": "550ebaac289296ce228a706d0867afc34687e3f4", "shasum": "" }, "require": { @@ -701,7 +1118,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.9-dev" + "dev-master": "1.12-dev" } }, "autoload": { @@ -717,13 +1134,13 @@ "MIT" ], "authors": [ - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - }, { "name": "Gert de Pagter", "email": "BackEndTea@gmail.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" } ], "description": "Symfony polyfill for ctype functions", @@ -734,20 +1151,20 @@ "polyfill", "portable" ], - "time": "2018-08-06T14:22:27+00:00" + "time": "2019-08-06T08:03:45+00:00" }, { "name": "symfony/yaml", - "version": "v2.8.47", + "version": "v2.8.50", "source": { "type": "git", "url": "https://github.com/symfony/yaml.git", - "reference": "0e16589861f192dbffb19b06683ce3ef58f7f99d" + "reference": "02c1859112aa779d9ab394ae4f3381911d84052b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/0e16589861f192dbffb19b06683ce3ef58f7f99d", - "reference": "0e16589861f192dbffb19b06683ce3ef58f7f99d", + "url": "https://api.github.com/repos/symfony/yaml/zipball/02c1859112aa779d9ab394ae4f3381911d84052b", + "reference": "02c1859112aa779d9ab394ae4f3381911d84052b", "shasum": "" }, "require": { @@ -784,7 +1201,7 @@ ], "description": "Symfony Yaml Component", "homepage": "https://symfony.com", - "time": "2018-10-02T16:27:16+00:00" + "time": "2018-11-11T11:18:13+00:00" } ], "aliases": [], From 6c2aa4be9b1fc39b4efdf3aced4c6dc54b0e2624 Mon Sep 17 00:00:00 2001 From: selul Date: Sat, 21 Mar 2020 13:58:36 +0200 Subject: [PATCH 5/6] adds fifo queue compatibility, using extra args for SQS message --- includes/WpMinions/SQS/Client.php | 29 +++++++++++++++++++++------ includes/WpMinions/SQS/Connection.php | 2 +- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/includes/WpMinions/SQS/Client.php b/includes/WpMinions/SQS/Client.php index 7f63a7d..5a3ad6b 100644 --- a/includes/WpMinions/SQS/Client.php +++ b/includes/WpMinions/SQS/Client.php @@ -27,8 +27,8 @@ public function register() { /** * Adds a Job to the SQS Client's Queue. * - * @param string $hook The action hook name for the job - * @param array $args Optional arguments for the job + * @param string $hook The action hook name for the job + * @param array $args Optional arguments for the job * @param string $priority Optional priority of the job * * @return bool true or false depending on the Client @@ -37,17 +37,34 @@ public function add( $hook, $args = array(), $priority = 'normal' ) { if ( ! $this->connect() ) { return false; } - + $group_id = null; + $message_id = null; + if ( isset( $args['sqs_extra'] ) && ! empty( $args['sqs_extra'] ) ) { + if ( isset( $args['sqs_extra']['group_id'] ) ) { + $group_id = $args['sqs_extra']['group_id']; + } + if ( isset( $args['sqs_extra']['id'] ) ) { + $message_id = $args['sqs_extra']['id']; + } + unset( $args['sqs_extra'] ); + } $job_data = array( 'hook' => $hook, 'args' => $args, 'blog_id' => get_current_blog_id(), ); try { - $this->connection->get_connection()->sendMessage( array( + $job_args = array( 'QueueUrl' => $this->connection->get_channel(), - 'MessageBody' => json_encode( $job_data ) - ) ); + 'MessageBody' => json_encode( $job_data ), + ); + if ( ! empty( $group_id ) ) { + $job_args['MessageGroupId'] = $group_id; + } + if ( ! empty( $message_id ) ) { + $job_args['MessageDeduplicationId'] = $message_id; + } + $this->connection->get_connection()->sendMessage( $job_args ); return true; } catch ( \Exception $e ) { diff --git a/includes/WpMinions/SQS/Connection.php b/includes/WpMinions/SQS/Connection.php index 98d3bba..f8f17d8 100644 --- a/includes/WpMinions/SQS/Connection.php +++ b/includes/WpMinions/SQS/Connection.php @@ -31,7 +31,7 @@ public function __construct() { $aws_credentials = array(); } - $this->queue_name = 'wordpress'; + $this->queue_name = defined( 'WP_ASYNC_SQS_QUEUE' ) ? WP_ASYNC_SQS_QUEUE : 'wordpress'; $this->aws_credentials = $aws_credentials; $this->connection = new \Aws\Sqs\SqsClient( $this->aws_credentials ); From 4573a43b682fee9317e7d491bab46efac20622a6 Mon Sep 17 00:00:00 2001 From: selul Date: Mon, 23 Mar 2020 22:51:20 +0200 Subject: [PATCH 6/6] return true when there is no message, avoiding worker restart due to error --- includes/WpMinions/SQS/Worker.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/includes/WpMinions/SQS/Worker.php b/includes/WpMinions/SQS/Worker.php index be0d12f..9748374 100644 --- a/includes/WpMinions/SQS/Worker.php +++ b/includes/WpMinions/SQS/Worker.php @@ -36,7 +36,10 @@ public function work() { $message = $this->get_message(); if ( empty( $message ) ) { - return false; + //Wait 3s to avoid too many requests to AWS. + sleep( 3 ); + + return true; } try { @@ -82,7 +85,7 @@ public function work() { 'VisibilityTimeout' => 0 ) ); - $result = false; + $result = true; } if ( $switched ) {