├── .gitignore ├── .travis.yml ├── CREDITS ├── ChangeLog ├── LICENSE ├── README ├── TODO ├── config.m4 ├── const_gen.sh ├── examples ├── image_thumbnail_client.php ├── image_thumbnail_client_bg.php ├── image_thumbnail_client_task.php ├── image_thumbnail_worker.php ├── reverse_client.php ├── reverse_client_bg.php ├── reverse_client_task.php └── reverse_worker.php ├── gearman.stub.php ├── gearman_arginfo.h ├── package.xml ├── php-gearman.ini ├── php_gearman.c ├── php_gearman.h ├── php_gearman_client.c ├── php_gearman_client.h ├── php_gearman_job.c ├── php_gearman_job.h ├── php_gearman_task.c ├── php_gearman_task.h ├── php_gearman_worker.c ├── php_gearman_worker.h ├── test_client.php ├── test_worker.php └── tests ├── connect.inc ├── gearman_001.phpt ├── gearman_002.phpt ├── gearman_client_001.phpt ├── gearman_client_002.phpt ├── gearman_client_003.phpt ├── gearman_client_004.phpt ├── gearman_client_005.phpt ├── gearman_client_006.phpt ├── gearman_client_007.phpt ├── gearman_client_008.phpt ├── gearman_client_009.phpt ├── gearman_client_010.phpt ├── gearman_client_011.phpt ├── gearman_client_012.phpt ├── gearman_client_013.phpt ├── gearman_client_014.phpt ├── gearman_client_015.phpt ├── gearman_client_016.phpt ├── gearman_client_017.phpt ├── gearman_client_018.phpt ├── gearman_client_019.phpt ├── gearman_client_020.phpt ├── gearman_client_021.phpt ├── gearman_client_022.phpt ├── gearman_client_integration_test_001.phpt ├── gearman_client_integration_test_002.phpt ├── gearman_job_001.phpt ├── gearman_job_002.phpt ├── gearman_job_003.phpt ├── gearman_job_integration_test_001.phpt ├── gearman_job_integration_test_002.phpt ├── gearman_job_integration_test_003.phpt ├── gearman_job_integration_test_004.phpt ├── gearman_job_integration_test_005.phpt ├── gearman_job_integration_test_006.phpt ├── gearman_job_integration_test_007.phpt ├── gearman_job_integration_test_008.phpt ├── gearman_job_integration_test_009.phpt ├── gearman_job_integration_test_010.phpt ├── gearman_task_001.phpt ├── gearman_task_002.phpt ├── gearman_task_003.phpt ├── gearman_task_004.phpt ├── gearman_task_005.phpt ├── gearman_task_006.phpt ├── gearman_tasks_integration_test_001.phpt ├── gearman_tasks_integration_test_002.phpt ├── gearman_worker_001.phpt ├── gearman_worker_002.phpt ├── gearman_worker_003.phpt ├── gearman_worker_004.phpt ├── gearman_worker_005.phpt ├── gearman_worker_006.phpt ├── gearman_worker_007.phpt ├── gearman_worker_008.phpt ├── gearman_worker_009.phpt ├── gearman_worker_010.phpt ├── gearman_worker_011.phpt ├── gearman_worker_012.phpt ├── gearman_worker_013.phpt ├── gearman_worker_014.phpt ├── gearman_worker_015.phpt ├── gearman_worker_016.phpt ├── gearman_worker_017.phpt ├── gearman_worker_018.phpt ├── gearman_worker_integration_test_001.phpt ├── skipif.inc ├── skipifconnect.inc └── skipifversion.inc /.gitignore: -------------------------------------------------------------------------------- 1 | .deps 2 | .libs/ 3 | Makefile 4 | Makefile.fragments 5 | Makefile.global 6 | Makefile.objects 7 | acinclude.m4 8 | aclocal.m4 9 | autom4te.cache/ 10 | build/ 11 | config.guess 12 | config.h 13 | config.h.in 14 | config.log 15 | config.nice 16 | config.status 17 | config.sub 18 | configure 19 | configure.in 20 | gearman.la 21 | install-sh 22 | libtool 23 | ltmain.sh 24 | missing 25 | mkinstalldirs 26 | modules/ 27 | php_gearman.lo 28 | run-tests.php 29 | *.loT 30 | *.lo 31 | *.la 32 | tests/*.sh 33 | tests/*.diff 34 | tests/*.exp 35 | tests/*.log 36 | tests/*.out 37 | tests/*.php 38 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | matrix: 4 | include: 5 | - os: linux 6 | dist: trusty 7 | sudo: required 8 | php: 7.0 9 | - os: linux 10 | dist: trusty 11 | sudo: required 12 | php: 7.1 13 | - os: osx 14 | sudo: required 15 | language: generic 16 | env: 17 | - _PHP: 'php70' 18 | - os: osx 19 | sudo: required 20 | language: generic 21 | env: 22 | - _PHP: 'php71' 23 | 24 | addons: 25 | apt: 26 | packages: 27 | - gearman-job-server 28 | - libgearman-dev 29 | - gearman-tools 30 | 31 | before_install: 32 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update ; fi 33 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew tap homebrew/dupes ; fi 34 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew tap homebrew/versions ; fi 35 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew tap homebrew/homebrew-php ; fi 36 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install gearman ; fi 37 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew services start gearman ; fi 38 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install $_PHP ; fi 39 | 40 | script: 41 | - phpize 42 | - ./configure 43 | - make 44 | - REPORT_EXIT_STATUS=1 make test 45 | -------------------------------------------------------------------------------- /CREDITS: -------------------------------------------------------------------------------- 1 | Authors: James M. Luedke 2 | Eric Day 3 | 4 | Big Thanks: Pierre Joye (http://blog.thepimp.net/) 5 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | 2 | Version 2.1.4 3 | ------------- 4 | - Bump min PHP version to 7.1 5 | - Fix stubs and arginfo 6 | 7 | Version 2.1.3 8 | ------------- 9 | - Bump max PHP version so pecl will install for PHP 8.4 10 | 11 | Version 2.1.2 12 | ------------- 13 | - Bump max PHP version so pecl will install for PHP 8.2 and 8.3 as well 14 | 15 | Version 2.1.1 16 | ------------- 17 | - Compatibility fix - remove ZVAL_NEW_ARR usage 18 | - add Param to skip exception handling setup in addServer/addServers in GearmanWorker class 19 | - fix memory leak in GearmanClient::doXXX methods 20 | - fix some proto and stubs types 21 | 22 | Version 2.1.0 23 | ------------- 24 | - PHP 8.0 support 25 | - Serialization segfault fix 26 | - Clean up tests 27 | 28 | Version 2.0.6 29 | ------------- 30 | - fixing issue with double calling of zval_dtor in set fail callback 31 | 32 | Version 2.0.5 33 | ------------- 34 | - fixing incorrect number of required parameters for GearmanClient::addServer 35 | and GearmanClient::addServers, along with proceduralequivalents 36 | 37 | Version 2.0.4 38 | ------------- 39 | - bug fix for issue #59, Param to skip exception handling setup in addServer/addServers 40 | in GearmanClient class 41 | 42 | Version 2.0.3 43 | ------------- 44 | - Including changes for PHP 7.x 45 | - smaller bug fixes 46 | - splitting up into several smaller files 47 | 48 | Version 0.6.0 49 | ------------- 50 | - Fixed build issue with 5.1.x 51 | - Merged eday changes 52 | - Added wait functions, uncommented other worker functions. 53 | - Added timeout functions. 54 | - Updated function list from C library and removed free methods (should use unset). 55 | - Added stubs for new functions, fixed tests, fixed some of the existing functions from changes. 56 | 57 | Version 0.5.0 58 | ------------- 59 | - Fixed a bug in the task callbacks, the addtional data arg will now work as expected. 60 | - Added gearman_client_add_servers. This was adde in a recent version of libgearman. 61 | - Updates to zts for php 5.3 62 | - Replaced errno with getErrno 63 | 64 | Version 0.4.0 65 | ------------- 66 | - Fixed memory leak in $task object 67 | - Regened constants off of libgearman v0.7 68 | - Removed gearman_task_take_data 69 | - Added exception to task job object when created without a geaman obj arg 70 | - Fixed a bunch of places where it was possible to use objects before verifying them 71 | - Other small bug fixes 72 | 73 | Version 0.3.1 74 | ------------- 75 | - Initial PECL import 76 | - Reworked client/worker/task/job objects. 77 | - Added $obj->return_code() to all objects 78 | - Fixed compile issues with PHP 5.1 and 5.3 79 | 80 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------- 2 | The PHP License, version 3.01 3 | Copyright (c) 1999 - 2008 The PHP Group. All rights reserved. 4 | -------------------------------------------------------------------- 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, is permitted provided that the following conditions 8 | are met: 9 | 10 | 1. Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | 13 | 2. Redistributions in binary form must reproduce the above copyright 14 | notice, this list of conditions and the following disclaimer in 15 | the documentation and/or other materials provided with the 16 | distribution. 17 | 18 | 3. The name "PHP" must not be used to endorse or promote products 19 | derived from this software without prior written permission. For 20 | written permission, please contact group@php.net. 21 | 22 | 4. Products derived from this software may not be called "PHP", nor 23 | may "PHP" appear in their name, without prior written permission 24 | from group@php.net. You may indicate that your software works in 25 | conjunction with PHP by saying "Foo for PHP" instead of calling 26 | it "PHP Foo" or "phpfoo" 27 | 28 | 5. The PHP Group may publish revised and/or new versions of the 29 | license from time to time. Each version will be given a 30 | distinguishing version number. 31 | Once covered code has been published under a particular version 32 | of the license, you may always continue to use it under the terms 33 | of that version. You may also choose to use such covered code 34 | under the terms of any subsequent version of the license 35 | published by the PHP Group. No one other than the PHP Group has 36 | the right to modify the terms applicable to covered code created 37 | under this License. 38 | 39 | 6. Redistributions of any form whatsoever must retain the following 40 | acknowledgment: 41 | "This product includes PHP software, freely available from 42 | ". 43 | 44 | THIS SOFTWARE IS PROVIDED BY THE PHP DEVELOPMENT TEAM ``AS IS'' AND 45 | ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 46 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 47 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PHP 48 | DEVELOPMENT TEAM OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 49 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 50 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 51 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 53 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 54 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 55 | OF THE POSSIBILITY OF SUCH DAMAGE. 56 | 57 | -------------------------------------------------------------------- 58 | 59 | This software consists of voluntary contributions made by many 60 | individuals on behalf of the PHP Group. 61 | 62 | The PHP Group can be contacted via Email at group@php.net. 63 | 64 | For more information on the PHP Group and the PHP project, 65 | please see . 66 | 67 | PHP includes the Zend Engine, freely available at 68 | . 69 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | The Gearman PHP Extension provides a wrapper to libgearman. This 2 | gives the user the ability to write fully featured Gearman clients 3 | and workers in PHP, allowing them to quickly develop distributed 4 | applications. 5 | 6 | For more information about Gearman, see: http://www.gearman.org/ 7 | 8 | Requirements 9 | * For the 0.8.* versions, libgearman v0.14 or greater 10 | * For the 1.0.* versions, libgearman v0.21 or greater 11 | * For the 1.1.* versions, libgearman v1.1.0 or greater 12 | * For the 2.0.* versions, testing is done against libgearman v1.1.8 and PHP 7.0-7.4 13 | * For the 2.1.* versions, testing is done against libgearman v1.1.18 and PHP 7.0-8.3 14 | 15 | The Gearman PHP Extension requires the Gearman C server and library package 16 | to be installed. You can download the latest from: 17 | 18 | https://github.com/gearman/gearmand/releases 19 | 20 | See the README file in that package for installation instructions. Once 21 | it is installed you can compile the Gearman PHP Extension. You'll need 22 | to make sure you have the PHP development packages installed first 23 | (if you have 'phpize' command you’re all set). You'll also probably 24 | want the PHP command line interface installed as well (usually named 25 | php-cli). After extracting the Gearman PHP tarball, just run: 26 | 27 | phpize 28 | ./configure 29 | make 30 | make install 31 | 32 | You then need to make PHP aware of the new extension by adding the 33 | following line to your php.ini: 34 | 35 | extension="gearman.so" 36 | 37 | You can then test if the module is configured correctly with the 38 | PHP cli: 39 | 40 | php --info | grep gearman 41 | 42 | To run a simple example 43 | 44 | cd examples 45 | 46 | 1. Start the gearmand server in a separate terminal: 47 | 48 | gearmand 49 | 50 | 51 | 2. In another terminal, change to this source directory and run: 52 | 53 | php examples/reverse_worker.php 54 | 55 | 56 | 3. In another terminal, change to this source directory and run: 57 | 58 | php examples/reverse_client.php 59 | 60 | 61 | You should see some output from both the reverse client and worker 62 | scripts about the status and then a final result. 63 | 64 | 65 | Have fun! 66 | 67 | http://pecl.php.net/package/gearman 68 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | Sat Mar 28 16:44:11 PDT 2009 2 | 1. Create generic add_task_*/client_do function so there is not so much 3 | code duplication. 4 | 2. Clean up test_[client|worker].php scripts. Check every return val etc. 5 | 3. Split code into more files php_gearman.c client.c worker.c task.c job.c 6 | -------------------------------------------------------------------------------- /config.m4: -------------------------------------------------------------------------------- 1 | dnl Gearman PHP Extension 2 | dnl 3 | dnl Copyright (C) 2008 James M. Luedke , 4 | dnl Eric Day 5 | dnl All rights reserved. 6 | dnl 7 | dnl Use and distribution licensed under the PHP license. See 8 | dnl the LICENSE file in this directory for full text. 9 | 10 | CFLAGS="$CFLAGS -Wall" 11 | 12 | PHP_ARG_WITH(gearman, whether to enable gearman support, 13 | [[ --with-gearman[=PATH] Include gearman support]]) 14 | 15 | if test "$PHP_GEARMAN" != "no"; then 16 | for i in $PHP_GEARMAN /usr/local /usr /opt/local; do 17 | if test -r $i/include/libgearman-1.0/gearman.h; then 18 | GEARMAN_LIB_DIR=$i/$PHP_LIBDIR 19 | GEARMAN_INC_DIR=$i/include 20 | AC_MSG_RESULT([found in $i]) 21 | break 22 | fi 23 | done 24 | 25 | if test -z "$GEARMAN_LIB_DIR" -o -z "$GEARMAN_INC_DIR"; then 26 | AC_MSG_RESULT([not found]) 27 | AC_MSG_ERROR([Please install libgearman]) 28 | fi 29 | 30 | PHP_CHECK_LIBRARY(gearman, gearman_client_set_context, 31 | [ 32 | PHP_ADD_LIBRARY_WITH_PATH(gearman, $GEARMAN_LIB_DIR, GEARMAN_SHARED_LIBADD) 33 | AC_DEFINE(HAVE_GEARMAN, 1, [Whether you have gearman]) 34 | ],[ 35 | AC_MSG_ERROR([libgearman version 0.10 or later required]) 36 | ],[ 37 | -L$GEARMAN_LIB_DIR -R$GEARMAN_LIB_DIR 38 | ]) 39 | 40 | PHP_CHECK_LIBRARY(gearman, gearman_worker_set_server_option, 41 | [ 42 | PHP_ADD_LIBRARY_WITH_PATH(gearman, $GEARMAN_LIB_DIR, GEARMAN_SHARED_LIBADD) 43 | AC_DEFINE(HAVE_GEARMAN, 1, [Whether you have gearman]) 44 | ],[ 45 | AC_MSG_ERROR([libgearman version 0.21 or later required]) 46 | ],[ 47 | -L$GEARMAN_LIB_DIR -R$GEARMAN_LIB_DIR 48 | ]) 49 | 50 | PHP_CHECK_LIBRARY(gearman, gearman_job_error, 51 | [ 52 | PHP_ADD_LIBRARY_WITH_PATH(gearman, $GEARMAN_LIB_DIR, GEARMAN_SHARED_LIBADD) 53 | AC_DEFINE(HAVE_GEARMAN, 1, [Whether you have gearman]) 54 | ],[ 55 | AC_MSG_ERROR([libgearman version 1.1.0 or later required]) 56 | ],[ 57 | -L$GEARMAN_LIB_DIR -R$GEARMAN_LIB_DIR 58 | ]) 59 | 60 | PHP_CHECK_LIBRARY(gearman, gearman_client_unique_status, 61 | [ 62 | PHP_ADD_LIBRARY_WITH_PATH(gearman, $GEARMAN_LIB_DIR, GEARMAN_SHARED_LIBADD) 63 | AC_DEFINE(HAVE_GEARMAN, 1, [Whether you have gearman]) 64 | ],[ 65 | AC_MSG_ERROR([libgearman version 1.1.0 or later required]) 66 | ],[ 67 | -L$GEARMAN_LIB_DIR -R$GEARMAN_LIB_DIR 68 | ]) 69 | 70 | PHP_SUBST(GEARMAN_SHARED_LIBADD) 71 | 72 | PHP_ADD_INCLUDE($GEARMAN_INC_DIR) 73 | PHP_NEW_EXTENSION(gearman, php_gearman.c php_gearman_client.c php_gearman_worker.c php_gearman_job.c php_gearman_task.c, $ext_shared) 74 | fi 75 | -------------------------------------------------------------------------------- /const_gen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Gearman PHP Extension 4 | # 5 | # Copyright (C) 2008 James M. Luedke , 6 | # Eric Day 7 | # All rights reserved. 8 | # 9 | # Use and distribution licensed under the PHP license. See 10 | # the LICENSE file in this directory for full text. 11 | 12 | if [ "$1" = "" ] 13 | then 14 | header=/usr/local/include/libgearman/constants.h 15 | else 16 | header=$1 17 | fi 18 | 19 | if [ ! -e $header ] 20 | then 21 | echo "$header does not exist" 22 | exit 1; 23 | fi 24 | 25 | if [ -e php_gearman.c.new ] 26 | then 27 | echo "php_gearman.c.new already exists" 28 | exit 1; 29 | fi 30 | 31 | awk 'BEGIN { p= 1; } \ 32 | /CONST_GEN_START/ { p= 0; print $0; } \ 33 | { if (p == 1) { print $0; } }' php_gearman.c >> php_gearman.c.new 34 | 35 | grep ' GEARMAN' $header | \ 36 | sed 's/.*\(GEARMAN[A-Z0-9_]*\).*/\1/' | \ 37 | sed 's/\(.*\)/ REGISTER_LONG_CONSTANT("\1",\ 38 | \1,\ 39 | CONST_CS | CONST_PERSISTENT);/' | \ 40 | sed 's/LONG\(.*GEARMAN_DEFAULT_TCP_HOST\)/STRING\1/' | \ 41 | sed 's/LONG\(.*GEARMAN_DEFAULT_UDS\)/STRING\1/' | \ 42 | sed 's/LONG\(.*GEARMAN_DEFAULT_USER\)/STRING\1/' >> php_gearman.c.new 43 | 44 | awk 'BEGIN { p= 0; } \ 45 | /CONST_GEN_STOP/ { p= 1; } \ 46 | { if (p == 1) { print $0; } }' php_gearman.c >> php_gearman.c.new 47 | 48 | echo "New source file can be found in: php_gearman.c.new" 49 | 50 | exit 0 51 | -------------------------------------------------------------------------------- /examples/image_thumbnail_client.php: -------------------------------------------------------------------------------- 1 | addServer(); 17 | 18 | $data['src']= $_SERVER['argv'][1]; 19 | $data['dest']= "small_" . $_SERVER['argv'][1]; 20 | $data['x']= 200; 21 | $data['y']= NULL; 22 | 23 | /* run reverse client */ 24 | do 25 | { 26 | $value = $gmc->do("shrink_image", serialize($data)); 27 | switch ($gmc->returnCode()) 28 | { 29 | case GEARMAN_WORK_DATA: 30 | echo "DATA: $value\n"; 31 | break; 32 | case GEARMAN_SUCCESS: 33 | echo "SUCCESS: $value\n"; 34 | break; 35 | case GEARMAN_WORK_STATUS: 36 | list($numerator, $denominator)= $gmc->doStatus(); 37 | echo "Status: $numerator/$denominator\n"; 38 | break; 39 | default: 40 | echo "ERR: " . $gmc->error() . "\n"; 41 | } 42 | } 43 | while($gmc->returnCode() != GEARMAN_SUCCESS); 44 | 45 | echo "DONE: $value\n"; 46 | 47 | -------------------------------------------------------------------------------- /examples/image_thumbnail_client_bg.php: -------------------------------------------------------------------------------- 1 | addServer(); 18 | 19 | for ($x=0; $x<20; $x++) 20 | { 21 | $data[$x]['src']= $_SERVER['argv'][1]; 22 | $data[$x]['dest']= "$x.jpg"; 23 | $data[$x]['x']= ((80+1)*($x+1)); 24 | $data[$x]['y']= NULL; 25 | } 26 | 27 | /* fire off each job */ 28 | foreach ($data as $img) 29 | { 30 | $job_handle[]= $gmc->doBackground("shrink_image", serialize($img)); 31 | if ($gmc->returnCode() != GEARMAN_SUCCESS) 32 | { 33 | echo "ERROR RET: " . $gmc->error() . "\n"; 34 | exit; 35 | } 36 | } 37 | 38 | echo "DONE\n"; 39 | -------------------------------------------------------------------------------- /examples/image_thumbnail_client_task.php: -------------------------------------------------------------------------------- 1 | addServer(); 18 | 19 | /* set a few callbacks */ 20 | $gmc->setCreatedCallback("thumb_created"); 21 | $gmc->setCompleteCallback("thumb_complete"); 22 | $gmc->setFailCallback("thumb_fail"); 23 | 24 | for ($x= 0; $x<20; $x++) 25 | { 26 | $data[$x]['src']= $_SERVER['argv'][1]; 27 | $data[$x]['dest']= "$x.jpg"; 28 | $data[$x]['x']= ((80+1)*($x+1)); 29 | $data[$x]['y']= NULL; 30 | } 31 | 32 | /* fire off each job */ 33 | foreach ($data as $img) 34 | { 35 | /* NOTE: if you want to asynchronously queue jobs use 36 | ** $task= $gmc->add_task_background("shrink_image", serialize($img)); 37 | ** however keep in mind that your complete callback will not get called */ 38 | if (! $gmc->addTask("shrink_image", serialize($img))) 39 | { 40 | echo "ERROR RET: " . $gmc->error() . "\n"; 41 | exit; 42 | } 43 | } 44 | 45 | if (! $gmc->runTasks()) 46 | { 47 | echo "ERROR RET:" . $gmc->error() . "\n"; 48 | exit; 49 | } 50 | echo "DONE\n"; 51 | exit; 52 | 53 | function thumb_created($task) 54 | { 55 | echo "CREATED -> job: " . $task->jobHandle() . "\n"; 56 | } 57 | 58 | function thumb_complete($task) 59 | { 60 | echo "COMPLETE -> job: " . $task->jobHandle() . 61 | " new_file: " . $task->data() . "\n"; 62 | } 63 | 64 | function thumb_fail($task) 65 | { 66 | echo "FAIL job: " . $task->jobHandle() . "\n"; 67 | } 68 | 69 | ?> 70 | -------------------------------------------------------------------------------- /examples/image_thumbnail_worker.php: -------------------------------------------------------------------------------- 1 | addServer(); 17 | 18 | # optional config paramsj 19 | $args; 20 | 21 | $gmw->addFunction("shrink_image", "resize_image", $args); 22 | 23 | while($gmw->work()) 24 | { 25 | switch ($gmw->returnCode()) 26 | { 27 | case GEARMAN_SUCCESS: 28 | break; 29 | default: 30 | echo "ERROR RET: " . $gmc->returnCode() . "\n"; 31 | exit; 32 | } 33 | } 34 | echo "DONE\n"; 35 | 36 | 37 | /* simple function to resize an image 38 | * Requires the Imagick extension */ 39 | function resize_image($job, $args) 40 | { 41 | $wrk= $job->workload(); 42 | $data= unserialize($wrk); 43 | if (! $data['src'] || ! $data['dest'] || ! $data['x']) 44 | { $job->sendFail(); print_r($data); return; } 45 | echo $job->handle() . " - creating: $data[dest] x:$data[x] y:$data[y]\n"; 46 | $im= new Imagick(); 47 | $im->readimage($data['src']); 48 | $im->thumbnailImage($data['x'], $data['y']); 49 | $im->writeImage($data['dest']); 50 | $im->destroy(); 51 | $job->sendStatus(1, 1); 52 | 53 | return $data['dest']; 54 | } 55 | ?> 56 | -------------------------------------------------------------------------------- /examples/reverse_client.php: -------------------------------------------------------------------------------- 1 | , 6 | * Eric Day 7 | * All rights reserved. 8 | * 9 | * Use and distribution licensed under the PHP license. See 10 | * the LICENSE file in this directory for full text. 11 | */ 12 | 13 | echo "Starting\n"; 14 | 15 | # Create our client object. 16 | $gmclient= new GearmanClient(); 17 | 18 | # Add default server (localhost). 19 | $gmclient->addServer(); 20 | 21 | echo "Sending job\n"; 22 | 23 | # Send reverse job 24 | do 25 | { 26 | $result = $gmclient->doNormal("reverse", "Hello!"); 27 | # Check for various return packets and errors. 28 | switch($gmclient->returnCode()) 29 | { 30 | case GEARMAN_WORK_DATA: 31 | echo "Data: $result\n"; 32 | break; 33 | case GEARMAN_WORK_STATUS: 34 | list($numerator, $denominator)= $gmclient->doStatus(); 35 | echo "Status: $numerator/$denominator complete\n"; 36 | break; 37 | case GEARMAN_SUCCESS: 38 | break; 39 | default: 40 | echo "RET: " . $gmclient->returnCode() . "\n"; 41 | exit; 42 | } 43 | } 44 | while($gmclient->returnCode() != GEARMAN_SUCCESS); 45 | echo "Success: $result\n"; 46 | 47 | ?> 48 | -------------------------------------------------------------------------------- /examples/reverse_client_bg.php: -------------------------------------------------------------------------------- 1 | , 6 | * Eric Day 7 | * All rights reserved. 8 | * 9 | * Use and distribution licensed under the PHP license. See 10 | * the LICENSE file in this directory for full text. 11 | */ 12 | 13 | /* create our object */ 14 | $gmclient= new GearmanClient(); 15 | 16 | /* add the default server */ 17 | $gmclient->addServer(); 18 | 19 | /* run reverse client */ 20 | $job_handle = $gmclient->doBackground("reverse", "this is a test"); 21 | 22 | if ($gmclient->returnCode() != GEARMAN_SUCCESS) 23 | { 24 | echo "bad return code\n"; 25 | exit; 26 | } 27 | exit; 28 | ?> 29 | -------------------------------------------------------------------------------- /examples/reverse_client_task.php: -------------------------------------------------------------------------------- 1 | addServer(); 4 | 5 | $gmc->setCreatedCallback("reverse_created"); 6 | $gmc->setStatusCallback("reverse_status"); 7 | $gmc->setCompleteCallback("reverse_complete"); 8 | $gmc->setFailCallback("reverse_fail"); 9 | $task= $gmc->addTask("reverse", "this is a test", NULL); 10 | 11 | if (! $gmc->runTasks()) 12 | { 13 | echo "ERROR " . $gmc->error() . "\n"; 14 | exit; 15 | } 16 | echo "DONE\n"; 17 | 18 | function reverse_created($task) 19 | { 20 | echo "CREATED: " . $task->jobHandle() . "\n"; 21 | } 22 | 23 | function reverse_status($task) 24 | { 25 | echo "STATUS: " . $task->jobHandle() . " - " . $task->taskNumerator() . 26 | "/" . $task->taskDenominator() . "\n"; 27 | } 28 | 29 | function reverse_complete($task) 30 | { 31 | echo "COMPLETE: " . $task->jobHandle() . "\n"; 32 | } 33 | 34 | function reverse_fail($task) 35 | { 36 | echo "FAILED: " . $task->jobHandle() . "\n"; 37 | } 38 | ?> 39 | -------------------------------------------------------------------------------- /examples/reverse_worker.php: -------------------------------------------------------------------------------- 1 | , 6 | * Eric Day 7 | * All rights reserved. 8 | * 9 | * Use and distribution licensed under the PHP license. See 10 | * the LICENSE file in this directory for full text. 11 | */ 12 | 13 | echo "Starting\n"; 14 | 15 | # Create our worker object. 16 | $gmworker= new GearmanWorker(); 17 | 18 | # Add default server (localhost). 19 | $gmworker->addServer(); 20 | 21 | # Register function "reverse" with the server. Change the worker function to 22 | # "reverse_fn_fast" for a faster worker with no output. 23 | $gmworker->addFunction("reverse", "reverse_fn"); 24 | 25 | print "Waiting for job...\n"; 26 | while($gmworker->work()) 27 | { 28 | if ($gmworker->returnCode() != GEARMAN_SUCCESS) 29 | { 30 | echo "return_code: " . $gmworker->returnCode() . "\n"; 31 | break; 32 | } 33 | } 34 | 35 | function reverse_fn($job) 36 | { 37 | echo "Received job: " . $job->handle() . "\n"; 38 | 39 | $workload= $job->workload(); 40 | $workload_size= $job->workloadSize(); 41 | 42 | echo "Workload: $workload ($workload_size)\n"; 43 | 44 | # This status loop is not needed, just showing how it works 45 | for ($x= 0; $x < $workload_size; $x++) 46 | { 47 | echo "Sending status: $x/$workload_size complete\n"; 48 | /* 49 | $job->sendStatus($x, $workload_size); 50 | sleep(1); 51 | */ 52 | } 53 | 54 | $result= strrev($workload); 55 | echo "Result: $result\n"; 56 | 57 | # Return what we want to send back to the client. 58 | return $result; 59 | } 60 | 61 | # A much simpler and less verbose version of the above function would be: 62 | function reverse_fn_fast($job) 63 | { 64 | return strrev($job->workload()); 65 | } 66 | 67 | 68 | 69 | 70 | ?> 71 | -------------------------------------------------------------------------------- /gearman.stub.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | gearman 4 | pecl.php.net 5 | PHP wrapper to libgearman 6 | This extension uses libgearman library to provide API for communicating with gearmand, and writing clients and workers. 7 | 8 | James M. Luedke 9 | jluedke 10 | contact@jamesluedke.com 11 | no 12 | 13 | 14 | Herman J. Radtke III 15 | hradtke 16 | hradtke@php.net 17 | yes 18 | 19 | 2025-01-07 20 | 21 | 22 | 2.1.4 23 | 2.1.4 24 | 25 | 26 | stable 27 | stable 28 | 29 | PHP 30 | 31 | PHP 8 is now supported 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 7.1.0 142 | 8.4.99 143 | 6.0.0 144 | 145 | 146 | 1.4.0b1 147 | 148 | 149 | 150 | gearman 151 | 152 | 153 | 154 | 155 | 156 | stable 157 | stable 158 | 159 | 160 | 2.1.4 161 | 2.1.4 162 | 163 | 2025-01-07 164 | 165 | - Bump min PHP version to PHP 7.1 166 | 167 | 168 | 169 | 170 | 171 | stable 172 | stable 173 | 174 | 175 | 2.1.3 176 | 2.1.3 177 | 178 | 2025-01-07 179 | 180 | - Bump max PHP version to support PHP 8.4 181 | 182 | 183 | 184 | 185 | 186 | stable 187 | stable 188 | 189 | 190 | 2.1.2 191 | 2.1.2 192 | 193 | 2024-04-03 194 | 195 | - Bump max PHP version to support PHP 8.2 and 8.3 196 | 197 | 198 | 199 | 200 | 201 | stable 202 | stable 203 | 204 | 205 | 2.1.1 206 | 2.1.1 207 | 208 | 2024-04-02 209 | 210 | - Compatibility fix - remove ZVAL_NEW_ARR usage 211 | - add Param to skip exception handling setup in addServer/addServers in GearmanWorker class 212 | - fix memory leak in GearmanClient::doXXX methods 213 | - fix some proto and stubs types 214 | 215 | 216 | 217 | 218 | 219 | stable 220 | stable 221 | 222 | 223 | 2.1.0 224 | 2.1.0 225 | 226 | 2021-01-16 227 | 228 | - PHP 8.0 Support 229 | - Fix a serialization segfault 230 | 231 | 232 | 233 | 234 | 235 | stable 236 | stable 237 | 238 | 239 | 1.1.2 240 | 1.1.2 241 | 242 | 2013-08-28 243 | 244 | - All callbacks should have a reference pointer (Special thanks to guilhermeblanco) 245 | 246 | 247 | 248 | 249 | 250 | stable 251 | stable 252 | 253 | 254 | 1.1.1 255 | 1.1.1 256 | 257 | 2013-01-08 258 | 259 | - Updated README with new libgearman requirements. 260 | - Add explicit check for >= libgearman-1.1.0 261 | - Fixed PECL Bug 63807 (Special thanks to kgovande) 262 | - Add support for gearman_client_unique_status() method 263 | 264 | 265 | 266 | 267 | 268 | stable 269 | stable 270 | 271 | 272 | 1.1.0 273 | 1.1.0 274 | 275 | 2012-09-22 276 | 277 | - libgearman v1.1.* is now required 278 | - Fixed PECL bug #60877 (no way to set client_id for a GearmanWorker) 279 | - Fixed BC breaks in libgearman v1.1.0 280 | - Make phpversion('gearman') return the real version 281 | 282 | 283 | 284 | 285 | 286 | stable 287 | stable 288 | 289 | 290 | 1.0.3 291 | 1.0.3 292 | 293 | 2012-08-04 294 | 295 | - Additional fix for PECL Bug #59423 (GearmanWorker::wait and GearmanWorker::work are spamming the log with warnings.) 296 | 297 | 298 | 299 | 300 | 301 | stable 302 | stable 303 | 304 | 305 | 1.0.2 306 | 1.0.2 307 | 308 | 2012-03-05 309 | 310 | - Updates for PHP 5.4 311 | 312 | 313 | 314 | 315 | 316 | stable 317 | stable 318 | 319 | 320 | 1.0.1 321 | 1.0.1 322 | 323 | 2011-12-05 324 | 325 | - Fixed PECL Bug #60438 (wrong version number in php_gearman.h) 326 | 327 | 328 | 329 | 330 | 331 | stable 332 | stable 333 | 334 | 335 | 1.0.0 336 | 1.0.0 337 | 338 | 2011-12-02 339 | 340 | - Fixed PECL Bug #18658 (GearmanWorker::wait and GearmanWorker::work are spamming the log with warnings.) 341 | - Fixed PECL Bug #22636 (Uncaught worker exception sends back GEARMAN_SUCCESS return code) 342 | - Fixed PECL Bug #16900 (Don't use reseved method names) 343 | - Fixed PECL Bug #59944 (Undefined symbol: gearman_client_set_server_option) 344 | - Now requires libgearman 0.21 or greater 345 | 346 | 347 | 348 | 349 | 350 | beta 351 | beta 352 | 353 | 354 | 0.8.0 355 | 0.8.0 356 | 357 | 2010-05-17 358 | 359 | - Fixed PECL Bug #17114 (Return values that are class private variables are null'd) 360 | - Fixed PECL Bug #22637 (GearmanJob::setReturn causes segfault) 361 | - Fixed PECL Bug #16883 (GearmanJob::sendWarning() segfaults) 362 | - Fixed PECL Bug #16884 (GearmanJob::sendData() crashes) 363 | - Fixed PECL Bug #16883 (GearmanWorker::addFunction segfaults when function name is non-string) 364 | - Fixed PECL Bug #17539 (Segmentation fault on calling error() of a fresh worker). Patch by jiayong. 365 | 366 | 367 | 368 | 369 | 370 | beta 371 | beta 372 | 373 | 374 | 0.7.0 375 | 0.7.0 376 | 377 | 2010-03-09 378 | 379 | - Now builds against gearmand 0.12 380 | - Had to remove some of the low level code I was working on. 381 | 382 | 383 | 384 | 385 | 386 | beta 387 | beta 388 | 389 | 390 | 0.6.0 391 | 0.6.0 392 | 393 | 2009-10-01 394 | 395 | - Fixed build issue with 5.1.x 396 | - Merged eday changes 397 | - Added wait functions, uncommented other worker functions. 398 | - Added timeout functions. 399 | - Updated function list from C library and removed free methods (should use unset). 400 | - Added stubs for new functions, fixed tests, fixed some of the existing functions from changes. 401 | 402 | 403 | 404 | 405 | 406 | beta 407 | beta 408 | 409 | 410 | 0.5.0 411 | 0.5.0 412 | 413 | 2009-07-27 414 | 415 | - Fixed a bug in the task callbacks, the addtional data arg will now work as expected. 416 | - Added gearman_client_add_servers. This was adde in a recent version of libgearman. 417 | - Updates to zts for php 5.3 418 | - Replaced errno with getErrno 419 | 420 | 421 | 422 | 423 | 424 | beta 425 | beta 426 | 427 | 428 | 0.4.0 429 | 0.4.0 430 | 431 | 2009-06-30 432 | 433 | - Fixed memory leak in $task object 434 | - Regened constants off of libgearman v0.7 435 | - Removed gearman_task_take_data 436 | - Added exception to task job object when created without a geaman obj arg 437 | - Fixed a bunch of places where it was possible to use objects before verifying them 438 | - Other small bug fixes 439 | 440 | 441 | 442 | 443 | 444 | beta 445 | beta 446 | 447 | 448 | 0.3 449 | 0.3 450 | 451 | 2009-05-20 452 | 453 | - Initial PECL import 454 | - Reworked client/worker/task/job objects. 455 | - Added $obj->return_code() to all objects 456 | - Fixed compile issues with PHP 5.1 and 5.3 457 | 458 | 459 | 460 | 461 | 462 | -------------------------------------------------------------------------------- /php-gearman.ini: -------------------------------------------------------------------------------- 1 | error_reporting = E_ALL 2 | 3 | display_startup_errors = On 4 | 5 | extension=modules/gearman.so 6 | -------------------------------------------------------------------------------- /php_gearman.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Gearman PHP Extension 3 | * 4 | * Copyright (C) 2008 James M. Luedke , 5 | * Eric Day 6 | * All rights reserved. 7 | * 8 | * Use and distribution licensed under the PHP license. See 9 | * the LICENSE file in this directory for full text. 10 | */ 11 | 12 | #ifndef __PHP_GEARMAN_H 13 | #define __PHP_GEARMAN_H 14 | 15 | #include "php.h" 16 | #include "php_ini.h" 17 | #include "ext/standard/info.h" 18 | 19 | #include "zend_exceptions.h" 20 | #include "zend_interfaces.h" 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | /* module version */ 27 | #define PHP_GEARMAN_VERSION "2.1.4" 28 | 29 | extern zend_module_entry gearman_module_entry; 30 | #define phpext_gearman_ptr &gearman_module_entry 31 | 32 | typedef enum { 33 | GEARMAN_OBJ_CREATED = (1 << 0) 34 | } gearman_obj_flags_t; 35 | 36 | extern zend_class_entry *gearman_exception_ce; 37 | 38 | #define GEARMAN_EXCEPTION(__error, __error_code) { \ 39 | zend_throw_exception(gearman_exception_ce, __error, __error_code); \ 40 | return; \ 41 | } 42 | 43 | void *_php_malloc(size_t size, void *arg); 44 | void _php_free(void *ptr, void *arg); 45 | 46 | /* backward compat macros */ 47 | 48 | #ifndef ZVAL_EMPTY_ARRAY 49 | #define ZVAL_EMPTY_ARRAY(value) array_init(value) 50 | #endif 51 | 52 | #ifndef RETVAL_EMPTY_ARRAY 53 | #define RETVAL_EMPTY_ARRAY() ZVAL_EMPTY_ARRAY(return_value) 54 | #endif 55 | 56 | #ifndef RETURN_EMPTY_ARRAY 57 | #define RETURN_EMPTY_ARRAY() do { RETVAL_EMPTY_ARRAY(); return; } while (0) 58 | #endif 59 | 60 | #ifndef IS_MIXED 61 | # define IS_MIXED 0 62 | #endif 63 | 64 | #ifndef ZEND_ARG_INFO_WITH_DEFAULT_VALUE 65 | #define ZEND_ARG_INFO_WITH_DEFAULT_VALUE(pass_by_ref, name, default_value) \ 66 | ZEND_ARG_INFO(pass_by_ref, name) 67 | #endif 68 | 69 | #if PHP_VERSION_ID < 70200 70 | #undef ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX 71 | #define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, class_name, allow_null) \ 72 | static const zend_internal_arg_info name[] = { \ 73 | { (const char*)(zend_uintptr_t)(required_num_args), ( #class_name ), 0, return_reference, allow_null, 0 }, 74 | #endif 75 | 76 | #ifndef ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX 77 | # define ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(name, return_reference, required_num_args, class_name, allow_null) \ 78 | ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, class_name, allow_null) 79 | #endif 80 | 81 | #ifndef ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX 82 | # define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(name, return_reference, num_args, type) \ 83 | ZEND_BEGIN_ARG_INFO_EX(name, 0, return_reference, num_args) 84 | #endif 85 | 86 | #ifndef ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX 87 | # define ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(name, return_reference, required_num_args, class_name, type) \ 88 | ZEND_BEGIN_ARG_INFO_EX(name, 0, return_reference, required_num_args) 89 | #endif 90 | 91 | #ifndef ZEND_ARG_TYPE_MASK 92 | # define ZEND_ARG_TYPE_MASK(pass_by_ref, name, type_mask, default_value) \ 93 | ZEND_ARG_TYPE_INFO(pass_by_ref, name, 0, 0) 94 | #endif 95 | 96 | #ifndef ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE 97 | # define ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(pass_by_ref, name, type_hint, allow_null, default_value) \ 98 | ZEND_ARG_TYPE_INFO(pass_by_ref, name, type_hint, allow_null) 99 | #endif 100 | 101 | #endif /* __PHP_GEARMAN_H */ 102 | -------------------------------------------------------------------------------- /php_gearman_client.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Gearman PHP Extension 3 | * 4 | * Copyright (C) 2008 James M. Luedke , 5 | * Eric Day 6 | * All rights reserved. 7 | * 8 | * Use and distribution licensed under the PHP license. See 9 | * the LICENSE file in this directory for full text. 10 | */ 11 | 12 | #ifndef __PHP_GEARMAN_CLIENT_H 13 | #define __PHP_GEARMAN_CLIENT_H 14 | 15 | #include "php.h" 16 | #include "php_ini.h" 17 | #include "ext/standard/info.h" 18 | 19 | #include "zend_exceptions.h" 20 | #include "zend_interfaces.h" 21 | 22 | #include "php_gearman.h" 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | extern zend_class_entry *gearman_client_ce; 29 | extern zend_object_handlers gearman_client_obj_handlers; 30 | 31 | void gearman_client_free_obj(zend_object *object); 32 | zend_object *gearman_client_obj_new(zend_class_entry *ce); 33 | 34 | typedef enum { 35 | GEARMAN_CLIENT_OBJ_CREATED = (1 << 0) 36 | } gearman_client_obj_flags_t; 37 | 38 | typedef struct { 39 | gearman_return_t ret; 40 | gearman_client_obj_flags_t flags; 41 | gearman_client_st client; 42 | /* used for keeping track of task interface callbacks */ 43 | zval zworkload_fn; 44 | zval zcreated_fn; 45 | zval zdata_fn; 46 | zval zwarning_fn; 47 | zval zstatus_fn; 48 | zval zcomplete_fn; 49 | zval zexception_fn; 50 | zval zfail_fn; 51 | 52 | zend_ulong created_tasks; 53 | zval task_list; 54 | 55 | zend_object std; 56 | } gearman_client_obj; 57 | 58 | gearman_client_obj *gearman_client_fetch_object(zend_object *obj); 59 | 60 | #define Z_GEARMAN_CLIENT_P(zv) gearman_client_fetch_object(Z_OBJ_P((zv))) 61 | 62 | /* NOTE: It seems kinda weird that GEARMAN_WORK_FAIL is a valid 63 | * return code, however it is required for a worker to pass status 64 | * back to the client about a failed job, other return codes can 65 | * be passed back but they will cause a docref Warning. Might 66 | * want to think of a better solution XXX */ 67 | #define PHP_GEARMAN_CLIENT_RET_OK(__ret) ((__ret) == GEARMAN_SUCCESS || \ 68 | (__ret) == GEARMAN_PAUSE || \ 69 | (__ret) == GEARMAN_IO_WAIT || \ 70 | (__ret) == GEARMAN_WORK_STATUS || \ 71 | (__ret) == GEARMAN_WORK_DATA || \ 72 | (__ret) == GEARMAN_WORK_EXCEPTION || \ 73 | (__ret) == GEARMAN_WORK_WARNING || \ 74 | (__ret) == GEARMAN_WORK_FAIL) 75 | 76 | #endif /* __PHP_GEARMAN_CLIENT_H */ 77 | -------------------------------------------------------------------------------- /php_gearman_job.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Gearman PHP Extension 3 | * 4 | * Copyright (C) 2008 James M. Luedke , 5 | * Eric Day 6 | * All rights reserved. 7 | * 8 | * Use and distribution licensed under the PHP license. See 9 | * the LICENSE file in this directory for full text. 10 | */ 11 | 12 | #include "php_gearman_job.h" 13 | 14 | inline gearman_job_obj *gearman_job_fetch_object(zend_object *obj) { 15 | return (gearman_job_obj *)((char*)(obj) - XtOffsetOf(gearman_job_obj, std)); 16 | } 17 | 18 | /* {{{ proto object GearmanJob::__destruct() 19 | cleans up GearmanJob object */ 20 | PHP_METHOD(GearmanJob, __destruct) { 21 | gearman_job_obj *intern = Z_GEARMAN_JOB_P(getThis()); 22 | if (!intern) { 23 | return; 24 | } 25 | 26 | if (intern->flags & GEARMAN_JOB_OBJ_CREATED) { 27 | gearman_job_free(intern->job); 28 | intern->flags &= ~GEARMAN_JOB_OBJ_CREATED; 29 | } 30 | } 31 | /* }}} */ 32 | 33 | zend_object *gearman_job_obj_new(zend_class_entry *ce) { 34 | gearman_job_obj *intern = ecalloc(1, 35 | sizeof(gearman_job_obj) + 36 | zend_object_properties_size(ce)); 37 | 38 | zend_object_std_init(&(intern->std), ce); 39 | object_properties_init(&intern->std, ce); 40 | 41 | intern->std.handlers = &gearman_job_obj_handlers; 42 | return &intern->std; 43 | } 44 | 45 | /* {{{ proto int gearman_job_return_code() 46 | get last gearman_return_t */ 47 | PHP_FUNCTION(gearman_job_return_code) { 48 | gearman_job_obj *obj; 49 | zval *zobj; 50 | 51 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) { 52 | RETURN_NULL(); 53 | } 54 | obj = Z_GEARMAN_JOB_P(zobj); 55 | 56 | RETURN_LONG(obj->ret); 57 | } 58 | /* }}} */ 59 | 60 | /* {{{ proto bool gearman_job_set_return(int gearman_return_t) 61 | This function will set a return value of a job */ 62 | PHP_FUNCTION(gearman_job_set_return) { 63 | zval *zobj; 64 | gearman_job_obj *obj; 65 | gearman_return_t ret; 66 | zend_long ret_val; 67 | 68 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_job_ce, &ret_val) == FAILURE) { 69 | RETURN_NULL(); 70 | } 71 | obj = Z_GEARMAN_JOB_P(zobj); 72 | 73 | ret = ret_val; 74 | /* make sure its a valid gearman_return_t */ 75 | if (ret < GEARMAN_SUCCESS || ret > GEARMAN_MAX_RETURN) { 76 | php_error_docref(NULL, E_WARNING, 77 | "Invalid gearman_return_t: %d", ret); 78 | RETURN_FALSE; 79 | } 80 | 81 | obj->ret = ret; 82 | RETURN_TRUE; 83 | } 84 | /* }}} */ 85 | 86 | /* {{{ proto bool gearman_job_send_data(object job, string data) 87 | Send data for a running job. */ 88 | PHP_FUNCTION(gearman_job_send_data) { 89 | zval *zobj; 90 | gearman_job_obj *obj; 91 | char *data; 92 | size_t data_len; 93 | 94 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_job_ce, 95 | &data, &data_len) == FAILURE) { 96 | RETURN_NULL(); 97 | } 98 | obj = Z_GEARMAN_JOB_P(zobj); 99 | 100 | /* make sure worker initialized a job */ 101 | if (obj->job == NULL) { 102 | RETURN_FALSE; 103 | } 104 | 105 | obj->ret = gearman_job_send_data(obj->job, data, data_len); 106 | if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) { 107 | php_error_docref(NULL, E_WARNING, "%s", 108 | gearman_job_error(obj->job)); 109 | RETURN_FALSE; 110 | } 111 | 112 | RETURN_TRUE; 113 | } 114 | /* }}} */ 115 | 116 | /* {{{ proto bool gearman_job_send_warning(object job, string warning) 117 | Send warning for a running job. */ 118 | PHP_FUNCTION(gearman_job_send_warning) { 119 | zval *zobj; 120 | gearman_job_obj *obj; 121 | char *warning = NULL; 122 | size_t warning_len = 0; 123 | 124 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_job_ce, 125 | &warning, &warning_len) == FAILURE) { 126 | RETURN_FALSE; 127 | } 128 | obj = Z_GEARMAN_JOB_P(zobj); 129 | 130 | /* make sure worker initialized a job */ 131 | if (obj->job == NULL) { 132 | RETURN_FALSE; 133 | } 134 | 135 | obj->ret = gearman_job_send_warning(obj->job, (void *) warning, 136 | (size_t) warning_len); 137 | if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) { 138 | php_error_docref(NULL, E_WARNING, "%s", 139 | gearman_job_error(obj->job)); 140 | RETURN_FALSE; 141 | } 142 | 143 | RETURN_TRUE; 144 | } 145 | /* }}} */ 146 | 147 | /* {{{ proto bool gearman_job_send_status(object job, int numerator, int denominator) 148 | Send status information for a running job. */ 149 | PHP_FUNCTION(gearman_job_send_status) { 150 | zval *zobj; 151 | gearman_job_obj *obj; 152 | zend_long numerator, denominator; 153 | 154 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll", &zobj, gearman_job_ce, 155 | &numerator, &denominator) == FAILURE) { 156 | RETURN_FALSE; 157 | } 158 | obj = Z_GEARMAN_JOB_P(zobj); 159 | 160 | obj->ret = gearman_job_send_status(obj->job, (uint32_t)numerator, 161 | (uint32_t)denominator); 162 | if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) { 163 | php_error_docref(NULL, E_WARNING, "%s", 164 | gearman_job_error(obj->job)); 165 | RETURN_FALSE; 166 | } 167 | 168 | RETURN_TRUE; 169 | } 170 | /* }}} */ 171 | /* {{{ proto bool gearman_job_send_complete(object job, string result) 172 | Send result and complete status for a job. */ 173 | PHP_FUNCTION(gearman_job_send_complete) { 174 | zval *zobj; 175 | gearman_job_obj *obj; 176 | char *result; 177 | size_t result_len; 178 | 179 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_job_ce, 180 | &result, &result_len) == FAILURE) { 181 | RETURN_FALSE; 182 | } 183 | obj = Z_GEARMAN_JOB_P(zobj); 184 | 185 | obj->ret = gearman_job_send_complete(obj->job, result, result_len); 186 | if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) { 187 | php_error_docref(NULL, E_WARNING, "%s", 188 | gearman_job_error(obj->job)); 189 | RETURN_FALSE; 190 | } 191 | 192 | RETURN_TRUE; 193 | } 194 | /* }}} */ 195 | 196 | /* {{{ proto bool gearman_job_send_exception(object job, string exception) 197 | Send exception for a running job. */ 198 | PHP_FUNCTION(gearman_job_send_exception) { 199 | zval *zobj; 200 | gearman_job_obj *obj; 201 | char *exception; 202 | size_t exception_len; 203 | 204 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_job_ce, 205 | &exception, &exception_len) == FAILURE) { 206 | RETURN_FALSE; 207 | } 208 | obj = Z_GEARMAN_JOB_P(zobj); 209 | 210 | obj->ret= gearman_job_send_exception(obj->job, exception, exception_len); 211 | if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) { 212 | php_error_docref(NULL, E_WARNING, "%s", 213 | gearman_job_error(obj->job)); 214 | RETURN_FALSE; 215 | } 216 | 217 | RETURN_TRUE; 218 | } 219 | /* }}} */ 220 | 221 | /* {{{ proto bool gearman_job_send_fail(object job) 222 | Send fail status for a job. */ 223 | PHP_FUNCTION(gearman_job_send_fail) { 224 | zval *zobj; 225 | gearman_job_obj *obj; 226 | 227 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) { 228 | RETURN_FALSE; 229 | } 230 | obj = Z_GEARMAN_JOB_P(zobj); 231 | 232 | obj->ret = gearman_job_send_fail(obj->job); 233 | if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) { 234 | php_error_docref(NULL, E_WARNING, "%s", 235 | gearman_job_error(obj->job)); 236 | RETURN_FALSE; 237 | } 238 | 239 | RETURN_TRUE; 240 | } 241 | /* }}} */ 242 | 243 | /* {{{ proto false|string gearman_job_handle(object job) 244 | Return job handle. */ 245 | PHP_FUNCTION(gearman_job_handle) { 246 | zval *zobj; 247 | gearman_job_obj *obj; 248 | 249 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) { 250 | RETURN_NULL(); 251 | } 252 | obj = Z_GEARMAN_JOB_P(zobj); 253 | 254 | /* make sure worker initialized a job */ 255 | if (obj->job == NULL) { 256 | RETURN_FALSE; 257 | } 258 | 259 | RETURN_STRING((char *)gearman_job_handle(obj->job)); 260 | } 261 | /* }}} */ 262 | 263 | /* {{{ proto false|string gearman_job_function_name(object job) 264 | Return the function name associated with a job. */ 265 | PHP_FUNCTION(gearman_job_function_name) { 266 | zval *zobj; 267 | gearman_job_obj *obj; 268 | 269 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) { 270 | RETURN_NULL(); 271 | } 272 | obj = Z_GEARMAN_JOB_P(zobj); 273 | 274 | /* make sure worker initialized a job */ 275 | if (obj->job == NULL) { 276 | RETURN_FALSE; 277 | } 278 | 279 | RETURN_STRING((char *)gearman_job_function_name(obj->job)); 280 | } 281 | /* }}} */ 282 | 283 | /* {{{ proto false|string gearman_job_unique(object job) 284 | Get the unique ID associated with a job. */ 285 | PHP_FUNCTION(gearman_job_unique) { 286 | zval *zobj; 287 | gearman_job_obj *obj; 288 | 289 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) { 290 | RETURN_NULL(); 291 | } 292 | obj = Z_GEARMAN_JOB_P(zobj); 293 | 294 | /* make sure worker initialized a job */ 295 | if (obj->job == NULL) { 296 | RETURN_FALSE; 297 | } 298 | 299 | RETURN_STRING((char *)gearman_job_unique(obj->job)); 300 | } 301 | /* }}} */ 302 | 303 | /* {{{ proto string gearman_job_workload(object job) 304 | Returns the workload for a job. */ 305 | PHP_FUNCTION(gearman_job_workload) { 306 | zval *zobj; 307 | gearman_job_obj *obj; 308 | const uint8_t *workload; 309 | size_t workload_len; 310 | 311 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) { 312 | RETURN_NULL(); 313 | } 314 | obj = Z_GEARMAN_JOB_P(zobj); 315 | 316 | workload = gearman_job_workload(obj->job); 317 | workload_len = gearman_job_workload_size(obj->job); 318 | 319 | RETURN_STRINGL((char *)workload, (long) workload_len); 320 | } 321 | /* }}} */ 322 | 323 | /* {{{ proto int gearman_job_workload_size(object job) 324 | Returns size of the workload for a job. */ 325 | PHP_FUNCTION(gearman_job_workload_size) { 326 | zval *zobj; 327 | gearman_job_obj *obj; 328 | size_t workload_len; 329 | 330 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) { 331 | RETURN_NULL(); 332 | } 333 | obj = Z_GEARMAN_JOB_P(zobj); 334 | 335 | workload_len = gearman_job_workload_size(obj->job); 336 | 337 | RETURN_LONG((long) workload_len); 338 | } 339 | /* }}} */ 340 | 341 | -------------------------------------------------------------------------------- /php_gearman_job.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Gearman PHP Extension 3 | * 4 | * Copyright (C) 2008 James M. Luedke , 5 | * Eric Day 6 | * All rights reserved. 7 | * 8 | * Use and distribution licensed under the PHP license. See 9 | * the LICENSE file in this directory for full text. 10 | */ 11 | 12 | #ifndef __PHP_GEARMAN_JOB_H 13 | #define __PHP_GEARMAN_JOB_H 14 | 15 | #include "php.h" 16 | #include "php_ini.h" 17 | #include "ext/standard/info.h" 18 | 19 | #include "zend_exceptions.h" 20 | #include "zend_interfaces.h" 21 | 22 | #include "php_gearman.h" 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | extern zend_class_entry *gearman_job_ce; 29 | extern zend_object_handlers gearman_job_obj_handlers; 30 | 31 | zend_object *gearman_job_obj_new(zend_class_entry *ce); 32 | 33 | typedef enum { 34 | GEARMAN_JOB_OBJ_CREATED = (1 << 0) 35 | } gearman_job_obj_flags_t; 36 | 37 | typedef struct { 38 | gearman_return_t ret; 39 | gearman_job_obj_flags_t flags; 40 | gearman_job_st *job; 41 | 42 | zend_object std; 43 | } gearman_job_obj; 44 | 45 | gearman_job_obj *gearman_job_fetch_object(zend_object *obj); 46 | #define Z_GEARMAN_JOB_P(zv) gearman_job_fetch_object(Z_OBJ_P((zv))) 47 | 48 | #endif /* __PHP_GEARMAN_JOB_H */ 49 | -------------------------------------------------------------------------------- /php_gearman_task.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Gearman PHP Extension 3 | * 4 | * Copyright (C) 2008 James M. Luedke , 5 | * Eric Day 6 | * All rights reserved. 7 | * 8 | * Use and distribution licensed under the PHP license. See 9 | * the LICENSE file in this directory for full text. 10 | */ 11 | 12 | #include "php_gearman_task.h" 13 | 14 | inline gearman_task_obj *gearman_task_fetch_object(zend_object *obj) { 15 | return (gearman_task_obj *)((char*)(obj) - XtOffsetOf(gearman_task_obj, std)); 16 | } 17 | 18 | inline zend_object *gearman_task_obj_new(zend_class_entry *ce) { 19 | gearman_task_obj *intern = ecalloc(1, 20 | sizeof(gearman_task_obj) + 21 | zend_object_properties_size(ce)); 22 | 23 | zend_object_std_init(&(intern->std), ce); 24 | object_properties_init(&intern->std, ce); 25 | intern->task_id = 0; 26 | 27 | intern->std.handlers = &gearman_task_obj_handlers; 28 | return &intern->std; 29 | } 30 | 31 | /* this function will be used to call our user defined task callbacks */ 32 | gearman_return_t _php_task_cb_fn(gearman_task_obj *task, gearman_client_obj *client, zval zcall) { 33 | gearman_return_t ret; 34 | 35 | zval ztask, argv[2], retval; 36 | uint32_t param_count; 37 | 38 | ZVAL_OBJ(&ztask, &task->std); 39 | ZVAL_COPY_VALUE(&argv[0], &ztask); 40 | 41 | if (Z_ISUNDEF(task->zdata)) { 42 | param_count = 1; 43 | } else { 44 | ZVAL_COPY_VALUE(&argv[1], &task->zdata); 45 | param_count = 2; 46 | } 47 | 48 | if (call_user_function(EG(function_table), NULL, &zcall, &retval, param_count, argv) != SUCCESS) { 49 | php_error_docref(NULL, 50 | E_WARNING, 51 | "Could not call the function %s", 52 | ( Z_ISUNDEF(zcall) || Z_TYPE(zcall) != IS_STRING) ? "[undefined]" : Z_STRVAL(zcall) 53 | ); 54 | ret = 0; 55 | } else { 56 | if (Z_ISUNDEF(retval)) { 57 | ret = 0; 58 | } else { 59 | if (Z_TYPE(retval) != IS_LONG) { 60 | convert_to_long(&retval); 61 | } 62 | ret = Z_LVAL(retval); 63 | } 64 | } 65 | 66 | return ret; 67 | } 68 | 69 | void _php_task_free(gearman_task_st *task, void *context) { 70 | gearman_task_obj *task_obj= (gearman_task_obj *) context; 71 | gearman_client_obj *cli_obj = Z_GEARMAN_CLIENT_P(&task_obj->zclient); 72 | task_obj->flags &= ~GEARMAN_TASK_OBJ_CREATED; 73 | zend_hash_index_del(Z_ARRVAL(cli_obj->task_list), task_obj->task_id); 74 | } 75 | 76 | /* TODO: clean this up a bit, Macro? */ 77 | gearman_return_t _php_task_workload_fn(gearman_task_st *task) { 78 | gearman_task_obj *task_obj = (gearman_task_obj *) gearman_task_context(task); 79 | gearman_client_obj *client_obj = Z_GEARMAN_CLIENT_P(&task_obj->zclient); 80 | return _php_task_cb_fn(task_obj, client_obj, client_obj->zworkload_fn); 81 | } 82 | 83 | gearman_return_t _php_task_created_fn(gearman_task_st *task) { 84 | gearman_task_obj *task_obj = (gearman_task_obj *) gearman_task_context(task); 85 | gearman_client_obj *client_obj = Z_GEARMAN_CLIENT_P(&task_obj->zclient); 86 | return _php_task_cb_fn(task_obj, client_obj, client_obj->zcreated_fn); 87 | } 88 | 89 | gearman_return_t _php_task_data_fn(gearman_task_st *task) { 90 | gearman_task_obj *task_obj = (gearman_task_obj *) gearman_task_context(task); 91 | gearman_client_obj *client_obj = Z_GEARMAN_CLIENT_P(&task_obj->zclient); 92 | return _php_task_cb_fn(task_obj, client_obj, client_obj->zdata_fn); 93 | } 94 | 95 | gearman_return_t _php_task_warning_fn(gearman_task_st *task) { 96 | gearman_task_obj *task_obj = (gearman_task_obj *) gearman_task_context(task); 97 | gearman_client_obj *client_obj = Z_GEARMAN_CLIENT_P(&task_obj->zclient); 98 | return _php_task_cb_fn(task_obj, client_obj, client_obj->zwarning_fn); 99 | } 100 | 101 | gearman_return_t _php_task_status_fn(gearman_task_st *task) { 102 | gearman_task_obj *task_obj = (gearman_task_obj *) gearman_task_context(task); 103 | gearman_client_obj *client_obj = Z_GEARMAN_CLIENT_P(&task_obj->zclient); 104 | return _php_task_cb_fn(task_obj, client_obj, client_obj->zstatus_fn); 105 | } 106 | 107 | gearman_return_t _php_task_complete_fn(gearman_task_st *task) { 108 | gearman_task_obj *task_obj = (gearman_task_obj *) gearman_task_context(task); 109 | gearman_client_obj *client_obj = Z_GEARMAN_CLIENT_P(&task_obj->zclient); 110 | return _php_task_cb_fn(task_obj, client_obj, client_obj->zcomplete_fn); 111 | } 112 | 113 | gearman_return_t _php_task_exception_fn(gearman_task_st *task) { 114 | gearman_task_obj *task_obj = (gearman_task_obj *) gearman_task_context(task); 115 | gearman_client_obj *client_obj = Z_GEARMAN_CLIENT_P(&task_obj->zclient); 116 | return _php_task_cb_fn(task_obj, client_obj, client_obj->zexception_fn); 117 | } 118 | 119 | gearman_return_t _php_task_fail_fn(gearman_task_st *task) { 120 | gearman_task_obj *task_obj = (gearman_task_obj *) gearman_task_context(task); 121 | gearman_client_obj *client_obj = Z_GEARMAN_CLIENT_P(&task_obj->zclient); 122 | return _php_task_cb_fn(task_obj, client_obj, client_obj->zfail_fn); 123 | } 124 | 125 | /* {{{ proto object GearmanTask::__construct() 126 | Returns a task object */ 127 | PHP_METHOD(GearmanTask, __construct) { 128 | } 129 | 130 | void gearman_task_free_obj(zend_object *object) { 131 | gearman_task_obj *intern = gearman_task_fetch_object(object); 132 | if (!intern) { 133 | return; 134 | } 135 | 136 | zval_dtor(&intern->zworkload); 137 | zval_dtor(&intern->zdata); 138 | zval_dtor(&intern->zclient); 139 | 140 | zend_object_std_dtor(&intern->std); 141 | } 142 | 143 | /* {{{ proto int gearman_task_return_code() 144 | get last gearman_return_t */ 145 | PHP_FUNCTION(gearman_task_return_code) { 146 | zval *zobj; 147 | gearman_task_obj *obj; 148 | 149 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_task_ce) == FAILURE) { 150 | RETURN_NULL(); 151 | } 152 | obj = Z_GEARMAN_TASK_P(zobj); 153 | 154 | RETURN_LONG(obj->ret); 155 | } 156 | /* }}} */ 157 | 158 | /* {{{ proto false|string gearman_task_function_name(object task) 159 | Returns function name associated with a task. */ 160 | PHP_FUNCTION(gearman_task_function_name) { 161 | zval *zobj; 162 | gearman_task_obj *obj; 163 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_task_ce) == FAILURE) { 164 | RETURN_NULL(); 165 | } 166 | obj = Z_GEARMAN_TASK_P(zobj); 167 | 168 | if (obj->flags & GEARMAN_TASK_OBJ_CREATED) { 169 | RETURN_STRING((char *)gearman_task_function_name(obj->task)); 170 | } 171 | RETURN_FALSE; 172 | } 173 | /* }}} */ 174 | 175 | /* {{{ proto false|string gearman_task_unique(object task) 176 | Returns unique identifier for a task. */ 177 | PHP_FUNCTION(gearman_task_unique) { 178 | zval *zobj; 179 | gearman_task_obj *obj; 180 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_task_ce) == FAILURE) { 181 | RETURN_NULL(); 182 | } 183 | obj = Z_GEARMAN_TASK_P(zobj); 184 | 185 | if (obj->flags & GEARMAN_TASK_OBJ_CREATED) { 186 | RETURN_STRING((char *)gearman_task_unique(obj->task)); 187 | } 188 | RETURN_FALSE; 189 | } 190 | /* }}} */ 191 | 192 | /* {{{ proto false|string gearman_task_job_handle(object task) 193 | Returns job handle for a task. */ 194 | PHP_FUNCTION(gearman_task_job_handle) { 195 | zval *zobj; 196 | gearman_task_obj *obj; 197 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_task_ce) == FAILURE) { 198 | RETURN_NULL(); 199 | } 200 | obj = Z_GEARMAN_TASK_P(zobj); 201 | 202 | if (obj->flags & GEARMAN_TASK_OBJ_CREATED) { 203 | RETURN_STRING((char *)gearman_task_job_handle(obj->task)); 204 | } 205 | RETURN_FALSE; 206 | } 207 | /* }}} */ 208 | /* {{{ proto bool gearman_task_is_known(object task) 209 | Get status on whether a task is known or not */ 210 | PHP_FUNCTION(gearman_task_is_known) { 211 | zval *zobj; 212 | gearman_task_obj *obj; 213 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_task_ce) == FAILURE) { 214 | RETURN_NULL(); 215 | } 216 | obj = Z_GEARMAN_TASK_P(zobj); 217 | 218 | if (obj->flags & GEARMAN_TASK_OBJ_CREATED) { 219 | RETURN_BOOL(gearman_task_is_known(obj->task)); 220 | } 221 | RETURN_FALSE; 222 | } 223 | /* }}} */ 224 | 225 | 226 | /* {{{ proto bool gearman_task_is_running(object task) 227 | Get status on whether a task is running or not */ 228 | PHP_FUNCTION(gearman_task_is_running) { 229 | zval *zobj; 230 | gearman_task_obj *obj; 231 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_task_ce) == FAILURE) { 232 | RETURN_NULL(); 233 | } 234 | obj = Z_GEARMAN_TASK_P(zobj); 235 | 236 | if (obj->flags & GEARMAN_TASK_OBJ_CREATED) { 237 | RETURN_BOOL(gearman_task_is_running(obj->task)); 238 | } 239 | RETURN_FALSE; 240 | } 241 | /* }}} */ 242 | 243 | 244 | /* {{{ proto false|int gearman_task_numerator(object task) 245 | Returns the numerator of percentage complete for a task. */ 246 | PHP_FUNCTION(gearman_task_numerator) { 247 | zval *zobj; 248 | gearman_task_obj *obj; 249 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_task_ce) == FAILURE) { 250 | RETURN_NULL(); 251 | } 252 | obj = Z_GEARMAN_TASK_P(zobj); 253 | 254 | if (obj->flags & GEARMAN_TASK_OBJ_CREATED) { 255 | RETURN_LONG(gearman_task_numerator(obj->task)); 256 | } 257 | RETURN_FALSE; 258 | } 259 | /* }}} */ 260 | 261 | 262 | /* {{{ proto false|int gearman_task_denominator(object task) 263 | Returns the denominator of percentage complete for a task. */ 264 | PHP_FUNCTION(gearman_task_denominator) { 265 | zval *zobj; 266 | gearman_task_obj *obj; 267 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_task_ce) == FAILURE) { 268 | RETURN_NULL(); 269 | } 270 | obj = Z_GEARMAN_TASK_P(zobj); 271 | 272 | if (obj->flags & GEARMAN_TASK_OBJ_CREATED) { 273 | RETURN_LONG(gearman_task_denominator(obj->task)); 274 | } 275 | RETURN_FALSE; 276 | } 277 | /* }}} */ 278 | /* {{{ proto false|string gearman_task_data(object task) 279 | Get data being returned for a task. */ 280 | PHP_FUNCTION(gearman_task_data) { 281 | zval *zobj; 282 | gearman_task_obj *obj; 283 | const uint8_t *data; 284 | size_t data_len; 285 | 286 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_task_ce) == FAILURE) { 287 | RETURN_NULL(); 288 | } 289 | obj = Z_GEARMAN_TASK_P(zobj); 290 | 291 | if (obj->flags & GEARMAN_TASK_OBJ_CREATED && 292 | !gearman_client_has_option(&Z_GEARMAN_CLIENT_P(&obj->zclient)->client, GEARMAN_CLIENT_UNBUFFERED_RESULT)) { 293 | data = gearman_task_data(obj->task); 294 | data_len = gearman_task_data_size(obj->task); 295 | 296 | RETURN_STRINGL((char *)data, (long) data_len); 297 | } 298 | RETURN_FALSE; 299 | } 300 | /* }}} */ 301 | 302 | 303 | /* {{{ proto false|int gearman_task_data_size(object task) 304 | Get data size being returned for a task. */ 305 | PHP_FUNCTION(gearman_task_data_size) { 306 | zval *zobj; 307 | gearman_task_obj *obj; 308 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_task_ce) == FAILURE) { 309 | RETURN_FALSE; 310 | } 311 | obj = Z_GEARMAN_TASK_P(zobj); 312 | 313 | if (obj->flags & GEARMAN_TASK_OBJ_CREATED) { 314 | RETURN_LONG(gearman_task_data_size(obj->task)); 315 | } 316 | RETURN_FALSE; 317 | } 318 | /* }}} */ 319 | 320 | 321 | /* {{{ proto false|int gearman_task_send_workload(object task, string data) 322 | NOT-TESTED Send packet data for a task. */ 323 | PHP_FUNCTION(gearman_task_send_workload) { 324 | zval *zobj; 325 | gearman_task_obj *obj; 326 | char *data; 327 | size_t data_len; 328 | 329 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_task_ce, 330 | &data, &data_len) == FAILURE) { 331 | RETURN_FALSE; 332 | } 333 | obj = Z_GEARMAN_TASK_P(zobj); 334 | 335 | 336 | if (!(obj->flags & GEARMAN_TASK_OBJ_CREATED)) { 337 | RETURN_FALSE; 338 | } 339 | 340 | /* XXX verify that i am doing this correctly */ 341 | data_len = gearman_task_send_workload(obj->task, data, data_len, &obj->ret); 342 | if (obj->ret != GEARMAN_SUCCESS) 343 | { 344 | php_error_docref(NULL, E_WARNING, "%s", 345 | gearman_client_error(&Z_GEARMAN_CLIENT_P(&obj->zclient)->client)); 346 | RETURN_FALSE; 347 | } 348 | 349 | RETURN_LONG(data_len); 350 | } 351 | /* }}} */ 352 | 353 | 354 | /* {{{ proto false|array gearman_task_recv_data(object task, long buffer_size) 355 | NOT-TESTED Read work or result data into a buffer for a task. */ 356 | PHP_FUNCTION(gearman_task_recv_data) { 357 | zval *zobj; 358 | gearman_task_obj *obj; 359 | char *data_buffer; 360 | zend_long data_buffer_size; 361 | size_t data_len; 362 | 363 | if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_task_ce, 364 | &data_buffer_size) == FAILURE) { 365 | RETURN_NULL(); 366 | } 367 | obj = Z_GEARMAN_TASK_P(zobj); 368 | 369 | if (!(obj->flags & GEARMAN_TASK_OBJ_CREATED)) { 370 | RETURN_FALSE; 371 | } 372 | 373 | data_buffer= (char *) emalloc(data_buffer_size); 374 | 375 | data_len= gearman_task_recv_data(obj->task, data_buffer, data_buffer_size, 376 | &obj->ret); 377 | if (obj->ret != GEARMAN_SUCCESS && 378 | !gearman_client_has_option(&Z_GEARMAN_CLIENT_P(&obj->zclient)->client, GEARMAN_CLIENT_UNBUFFERED_RESULT)) { 379 | php_error_docref(NULL, E_WARNING, "%s", 380 | gearman_client_error(&Z_GEARMAN_CLIENT_P(&obj->zclient)->client)); 381 | RETURN_FALSE; 382 | } 383 | 384 | array_init(return_value); 385 | add_next_index_long(return_value, (long)data_len); 386 | add_next_index_stringl(return_value, (char *)data_buffer, 387 | (long)data_len); 388 | } 389 | /* }}} */ 390 | -------------------------------------------------------------------------------- /php_gearman_task.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Gearman PHP Extension 3 | * 4 | * Copyright (C) 2008 James M. Luedke , 5 | * Eric Day 6 | * All rights reserved. 7 | * 8 | * Use and distribution licensed under the PHP license. See 9 | * the LICENSE file in this directory for full text. 10 | */ 11 | 12 | #ifndef __PHP_GEARMAN_TASK_H 13 | #define __PHP_GEARMAN_TASK_H 14 | 15 | #include "php.h" 16 | #include "php_ini.h" 17 | #include "ext/standard/info.h" 18 | 19 | #include "zend_exceptions.h" 20 | #include "zend_interfaces.h" 21 | 22 | #include "php_gearman.h" 23 | #include "php_gearman_client.h" 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | void gearman_task_free_obj(zend_object *object); 30 | zend_object *gearman_task_obj_new(zend_class_entry *ce); 31 | extern zend_class_entry *gearman_task_ce; 32 | extern zend_object_handlers gearman_task_obj_handlers; 33 | 34 | typedef enum { 35 | GEARMAN_TASK_OBJ_CREATED = (1 << 0), 36 | } gearman_task_obj_flags_t; 37 | 38 | typedef struct { 39 | gearman_return_t ret; 40 | gearman_task_obj_flags_t flags; 41 | gearman_task_st *task; 42 | zval zclient; 43 | zval zdata; 44 | zval zworkload; 45 | zend_ulong task_id; 46 | 47 | zend_object std; 48 | } gearman_task_obj; 49 | 50 | gearman_task_obj *gearman_task_fetch_object(zend_object *obj); 51 | #define Z_GEARMAN_TASK_P(zv) gearman_task_fetch_object(Z_OBJ_P((zv))) 52 | 53 | gearman_return_t _php_task_cb_fn(gearman_task_obj *task, gearman_client_obj *client, zval zcall); 54 | void _php_task_free(gearman_task_st *task, void *context); 55 | 56 | gearman_return_t _php_task_workload_fn(gearman_task_st *task); 57 | gearman_return_t _php_task_created_fn(gearman_task_st *task); 58 | gearman_return_t _php_task_data_fn(gearman_task_st *task); 59 | gearman_return_t _php_task_warning_fn(gearman_task_st *task); 60 | gearman_return_t _php_task_status_fn(gearman_task_st *task); 61 | gearman_return_t _php_task_complete_fn(gearman_task_st *task); 62 | gearman_return_t _php_task_exception_fn(gearman_task_st *task); 63 | gearman_return_t _php_task_fail_fn(gearman_task_st *task); 64 | 65 | #endif /* __PHP_GEARMAN_TASK_H */ 66 | -------------------------------------------------------------------------------- /php_gearman_worker.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Gearman PHP Extension 3 | * 4 | * Copyright (C) 2008 James M. Luedke , 5 | * Eric Day 6 | * All rights reserved. 7 | * 8 | * Use and distribution licensed under the PHP license. See 9 | * the LICENSE file in this directory for full text. 10 | */ 11 | 12 | #ifndef __PHP_GEARMAN_WORKER_H 13 | #define __PHP_GEARMAN_WORKER_H 14 | 15 | #include "php.h" 16 | #include "php_ini.h" 17 | #include "ext/standard/info.h" 18 | 19 | #include "zend_exceptions.h" 20 | #include "zend_interfaces.h" 21 | 22 | #include "php_gearman.h" 23 | #include "php_gearman_job.h" 24 | #include "php_gearman_client.h" 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | extern zend_class_entry *gearman_worker_ce; 31 | extern zend_object_handlers gearman_worker_obj_handlers; 32 | 33 | zend_object *gearman_worker_obj_new(zend_class_entry *ce); 34 | 35 | typedef struct { 36 | zval zname; /* name associated with callback */ 37 | zval zcall; /* name of callback */ 38 | zval zdata; /* data passed to callback via worker */ 39 | } gearman_worker_cb_obj; 40 | 41 | typedef enum { 42 | GEARMAN_WORKER_OBJ_CREATED = (1 << 0) 43 | } gearman_worker_obj_flags_t; 44 | 45 | typedef struct { 46 | gearman_return_t ret; 47 | gearman_worker_obj_flags_t flags; 48 | gearman_worker_st worker; 49 | zval cb_list; 50 | 51 | zend_object std; 52 | } gearman_worker_obj; 53 | 54 | gearman_worker_obj *gearman_worker_fetch_object(zend_object *obj); 55 | #define Z_GEARMAN_WORKER_P(zv) gearman_worker_fetch_object(Z_OBJ_P((zv))) 56 | 57 | #endif /* __PHP_GEARMAN_WORKER_H */ 58 | -------------------------------------------------------------------------------- /test_client.php: -------------------------------------------------------------------------------- 1 | , 6 | * Eric Day 7 | * All rights reserved. 8 | * 9 | * Use and distribution licensed under the PHP license. See 10 | * the LICENSE file in this directory for full text. 11 | */ 12 | 13 | 14 | /* client */ 15 | 16 | echo "# TESTING CLIENT INTERFACE\n"; 17 | $client = gearman_client_create(); 18 | if (! is_object($client)) 19 | { 20 | echo "gearman_client_create() FAILED\n"; 21 | exit(0); 22 | } 23 | else 24 | echo "gearman_client_create() pass\n"; 25 | 26 | 27 | /* 28 | $client_new = gearman_client_clone($client); 29 | if (! is_object($client_new)) 30 | { 31 | echo "gearman_client_clone() FAILED\n"; 32 | exit(0); 33 | } 34 | else 35 | echo "gearman_client_clone() pass\n"; 36 | 37 | unset($client_new); 38 | echo "unset client pass\n"; 39 | */ 40 | 41 | $ret= gearman_client_error($client); 42 | if ($ret != GEARMAN_SUCCESS) 43 | { 44 | echo "gearman_client_error() FAILED\n"; 45 | exit(0); 46 | } 47 | else 48 | echo "gearman_client_error() pass\n"; 49 | 50 | $ret= gearman_client_get_errno($client); 51 | if ($ret != 0) 52 | { 53 | echo "gearman_client_get_errno() FAILED\n"; 54 | exit(0); 55 | } 56 | else 57 | echo "gearman_client_get_errno() pass\n"; 58 | 59 | if (! gearman_client_add_options($client, GEARMAN_CLIENT_UNBUFFERED_RESULT)) 60 | { 61 | echo "gearman_client_set_options() FAILED\n"; 62 | exit(0); 63 | } 64 | gearman_client_remove_options($client, GEARMAN_CLIENT_UNBUFFERED_RESULT); 65 | echo "gearman_client_set_options() pass\n"; 66 | 67 | /* 68 | if (! gearman_client_add_server($client)) 69 | { 70 | echo "gearman_client_add_server() FAILED\n"; 71 | exit(0); 72 | } 73 | echo "gearman_client_add_server() pass\n"; 74 | */ 75 | 76 | if (! gearman_client_add_servers($client, "127.0.0.1:4730")) 77 | { 78 | echo "gearman_client_add_servers() FAILED\n"; 79 | exit(0); 80 | } 81 | echo "gearman_client_add_servers() pass\n"; 82 | 83 | $value = gearman_client_do_normal($client, "test_worker", "gearman_client_do"); 84 | if ($client->returnCode() != GEARMAN_SUCCESS) 85 | { 86 | echo "gearman_client_do_normal() FAILED\n"; 87 | exit(0); 88 | } 89 | else 90 | echo "gearman_client_do_normal() pass -> $value\n"; 91 | 92 | 93 | $job_handle = gearman_client_do_background($client, "test_worker", "gearman_client_do_background"); 94 | if ($client->returnCode() != GEARMAN_SUCCESS) 95 | { 96 | echo "gearman_client_do_background() FAILED\n"; 97 | exit(0); 98 | } 99 | else 100 | echo "gearman_client_do_background() pass -> $job_handle\n"; 101 | 102 | $value = gearman_client_do_high($client, "test_worker", "gearman_client_do_high"); 103 | if ($client->returnCode() != GEARMAN_SUCCESS) 104 | { 105 | echo "gearman_client_do_high() FAILED\n"; 106 | exit(0); 107 | } 108 | else 109 | echo "gearman_client_do_high() pass -> $value\n"; 110 | 111 | $value = gearman_client_do_low($client, "test_worker", "gearman_client_do_low"); 112 | if ($client->returnCode() != GEARMAN_SUCCESS) 113 | { 114 | echo "gearman_client_do_low() FAILED\n"; 115 | exit(0); 116 | } 117 | else 118 | echo "gearman_client_do_low() pass -> $value\n"; 119 | 120 | $value = gearman_client_do_high_background($client, "test_worker", "gearman_client_do_high_background"); 121 | if ($client->returnCode() != GEARMAN_SUCCESS) 122 | { 123 | echo "gearman_client_do_high_background() FAILED\n"; 124 | exit(0); 125 | } 126 | else 127 | echo "gearman_client_do_high_background() pass -> $value\n"; 128 | 129 | $value = gearman_client_do_low_background($client, "test_worker", "gearman_client_do_low_background"); 130 | if ($client->returnCode() != GEARMAN_SUCCESS) 131 | { 132 | echo "gearman_client_do_low_background() FAILED\n"; 133 | exit(0); 134 | } 135 | else 136 | echo "gearman_client_do_low_background() pass -> $value\n"; 137 | 138 | $job_handle = gearman_client_do_background($client, "test_gearman_job_status", "test_gearman_job_status"); 139 | if ($client->returnCode() != GEARMAN_SUCCESS) 140 | { 141 | echo "gearman_client_do_background() FAILED -> while checking status\n"; 142 | exit(0); 143 | } 144 | sleep(1); 145 | list($is_known, $is_running, $numerator, $denominator) = gearman_client_job_status($client, $job_handle); 146 | if ($client->returnCode() != GEARMAN_SUCCESS || ! $is_known || ! $is_running || ! $numerator || ! $denominator) 147 | { 148 | echo "gearman_client_job_status() FAILED\n"; 149 | } 150 | else 151 | echo "gearman_client_job_status() pass -> is_known: $is_known is_running: $is_running $numerator / $denominator\n"; 152 | if ($job_handle != gearman_client_do_job_handle($client)) 153 | { 154 | echo "gearman_client_do_job_handle() FAILED\n"; 155 | exit(0); 156 | } 157 | else 158 | echo "gearman_client_do_job_handle() pass -> $job_handle\n"; 159 | 160 | gearman_client_ping($client, "gearman_client_ping"); 161 | echo "gearman_client_ping() pass\n"; 162 | 163 | gearman_client_add_task($client, "test_gearman_job", "test_gearman_job", "test_gearman_job"); 164 | echo "gearman_client_add_task() pass\n"; 165 | /* run a task see if they all work */ 166 | $res = gearman_client_run_tasks($client); 167 | echo "gearman_client_run_tasks() pass\n"; 168 | 169 | /* clear all the callbacks so they dont mess with other test */ 170 | gearman_client_clear_callbacks($client); 171 | echo "gearman_client_clear_callbacks() pass\n"; 172 | 173 | 174 | /* set all of our callback functions */ 175 | /* 176 | gearman_client_set_workload_fn($client, "test_gearman_client_set_workload_fn"); 177 | echo "gearman_client_set_workload_fn() pass\n"; 178 | */ 179 | gearman_client_set_created_callback($client, "test_gearman_client_set_created_callback"); 180 | // XXX gearman_client_set_data_fn($client, "test_gearman_client_set_data_fn"); 181 | gearman_client_set_warning_callback($client, "test_gearman_client_set_warning_callback"); 182 | gearman_client_set_status_callback($client, "test_gearman_client_set_status_callback"); 183 | gearman_client_set_complete_callback($client, "test_gearman_client_set_complete_callback"); 184 | gearman_client_set_exception_callback($client, "test_gearman_client_set_exception_callback"); 185 | gearman_client_set_fail_callback($client, "test_gearman_client_set_fail_callback"); 186 | gearman_client_add_task($client, "test_set_callback_callback", "test_set_callback_callback", "test_set_callback_callback"); 187 | /* run a task see if they all work */ 188 | $res = gearman_client_run_tasks($client); 189 | // gearman_client_clear_fn($client); 190 | 191 | /* test tasks interface */ 192 | gearman_client_clear_callback($client); 193 | gearman_client_set_complete_callback($client, "test_gearman_tasks"); 194 | gearman_client_add_task($client, "test_tasks", "test_tasks", "test_tasks"); 195 | $res = gearman_client_run_tasks($client); 196 | gearman_client_clear_callback($client); 197 | 198 | # You can turn off auto task destruction by unsetting this flag on a gearman client. 199 | gearman_client_remove_options($client, GEARMAN_CLIENT_FREE_TASKS); 200 | $task = gearman_client_add_task_background($client, "test_tasks_background", "test_tasks_background", "test_tasks_background"); 201 | gearman_client_set_status_callback($client, "test_gearman_task_status"); 202 | $res = gearman_client_run_tasks($client); 203 | $job_handle = gearman_task_job_handle($task); 204 | gearman_client_add_task_status($client, $job_handle); 205 | echo "\tgearman_client_add_task_status() pass\n"; 206 | $res = gearman_client_run_tasks($client); 207 | 208 | function test_gearman_task_status($task) 209 | { 210 | $is_known = gearman_task_is_known($task); 211 | if ($is_known) 212 | echo "\tgearman_task_is_known() pass\n"; 213 | else 214 | echo "\tgearman_task_is_known() FAILED\n"; 215 | sleep(1); 216 | $is_running = gearman_task_is_running($task); 217 | if ($is_running) 218 | echo "\tgearman_task_is_running() pass\n"; 219 | else 220 | echo "\tgearman_task_is_running() pass\n"; 221 | } 222 | 223 | echo "gearman_client_add_task_background() pass\n"; 224 | 225 | 226 | gearman_client_clear_callback($client); 227 | gearman_client_add_task_high($client, "test_tasks_high", "test_tasks_high", "test_tasks_high"); 228 | echo "gearman_client_add_task_high() pass\n"; 229 | 230 | gearman_client_add_task_low($client, "test_tasks_low", "test_tasks_low", "test_tasks_low"); 231 | echo "gearman_client_add_task_low() pass\n"; 232 | 233 | gearman_client_add_task_high_background($client, "test_tasks_high_background", "test_tasks_high_background", "test_tasks_high_background"); 234 | echo "gearman_client_add_task_high_background() pass\n"; 235 | gearman_client_add_task_low_background($client, "test_tasks_low_background", "test_tasks_low_background", "test_tasks_low_background"); 236 | echo "gearman_client_add_task_low_background() pass\n"; 237 | 238 | $res = gearman_client_run_tasks($client); 239 | /* XXX add task high seems to break things 240 | */ 241 | 242 | $value = gearman_client_do($client, "exit_test", "exit_test"); 243 | echo "DONE $ret\n"; 244 | 245 | 246 | /* XXX does not seem to call cb as i would expect 247 | $data = "gearman_job_exception"; 248 | gearman_client_add_task($client, "test_gearman_job_exception", $data, NULL); 249 | $data = "gearman_job_fail"; 250 | gearman_client_add_task($client, "test_gearman_job_fail", $data, NULL); 251 | */ 252 | 253 | 254 | /* XXX can not test till I have the gearman low level functions done 255 | $task_new = gearman_task_create(); 256 | gearman_task_fn_arg($task_new); 257 | echo "gearman_task_fn_arg() pass\n"; 258 | */ 259 | 260 | 261 | /* 262 | * Test Functions 263 | */ 264 | 265 | function test_gearman_client_set_fail_callback($task) 266 | { 267 | echo "\tgearman_client_set_fail_callback() pass\n"; 268 | } 269 | 270 | function test_gearman_client_set_exception_callback($task) 271 | { 272 | echo "\tgearman_client_set_exception_callback() pass\n"; 273 | } 274 | 275 | function test_gearman_client_set_complete_callback($task) 276 | { 277 | echo "\tgearman_client_set_complete_callback() pass\n"; 278 | } 279 | 280 | function test_gearman_client_set_status_callback($task) 281 | { 282 | echo "\tgearman_client_set_status_callback() pass\n"; 283 | } 284 | 285 | function test_gearman_client_set_warning_callback($task) 286 | { 287 | echo "\tgearman_client_set_warning_callback() pass\n"; 288 | } 289 | 290 | function test_gearman_client_set_data_callback($task) 291 | { 292 | echo "\tgearman_client_set_data_callback() pass\n"; 293 | } 294 | function test_gearman_client_set_created_callback($task) 295 | { 296 | echo "\tgearman_client_set_created_callback() pass\n"; 297 | } 298 | 299 | function test_gearman_client_set_workload_callback($task) 300 | { 301 | echo "\tgearman_client_set_workload_callback() pass\n"; 302 | } 303 | 304 | function test_gearman_tasks($task) 305 | { 306 | $is_known= false; 307 | $is_running= false; 308 | 309 | echo "Testing Task Functions\n"; 310 | $func = gearman_task_function_name($task); 311 | echo "\tgearman_task_function_name() pass - $func\n"; 312 | $uuid = gearman_task_unique($task); 313 | echo "\tgearman_task_unique() pass - $uuid\n"; 314 | $job_handle = gearman_task_job_handle($task); 315 | echo "\tgearman_task_job_handle() pass - $job_handle\n"; 316 | 317 | 318 | $numerator = gearman_task_numerator($task); 319 | echo "\tgearman_task_numerator() pass - $numerator\n"; 320 | $denominator = gearman_task_denominator($task); 321 | echo "\tgearman_task_denominator() pass - $denominator\n"; 322 | $data = gearman_task_data($task); 323 | echo "\tgearman_task_data() pass - $data\n"; 324 | $data_size = gearman_task_data_size($task); 325 | echo "\tgearman_task_data_size() pass - $data_size\n"; 326 | /* XXX Not sure how to test task data yet 327 | gearman_task_take_data($task); 328 | echo "gearman_task_take_data() pass\n"; 329 | gearman_task_send_data() 330 | gearman_task_recv_data() 331 | */ 332 | 333 | } 334 | 335 | ?> 336 | -------------------------------------------------------------------------------- /test_worker.php: -------------------------------------------------------------------------------- 1 | , 6 | * Eric Day 7 | * All rights reserved. 8 | * 9 | * Use and distribution licensed under the PHP license. See 10 | * the LICENSE file in this directory for full text. 11 | */ 12 | 13 | /* worker */ 14 | $worker = gearman_worker_create(); 15 | if (! is_object($worker)) 16 | { 17 | echo "gearman_worker_create() FAILED\n"; 18 | exit(0); 19 | } 20 | else 21 | echo "gearman_worker_create() pass\n"; 22 | 23 | /* 24 | if(! gearman_worker_add_server($worker)) 25 | { 26 | echo "gearman_worker_add_server() FAILED\n"; 27 | exit(0); 28 | } 29 | echo "gearman_worker_add_server() pass\n"; 30 | */ 31 | 32 | if(! gearman_worker_add_servers($worker, "127.0.0.1:4730")) 33 | { 34 | echo "gearman_worker_add_servers() FAILED\n"; 35 | exit(0); 36 | } 37 | echo "gearman_worker_add_servers() pass\n"; 38 | 39 | 40 | /* 41 | $worker_new = gearman_worker_clone($worker); 42 | if (! is_object($worker_new)) 43 | { 44 | echo "gearman_worker_clone() FAILED\n"; 45 | exit(0); 46 | } 47 | else 48 | echo "gearman_worker_clone() pass\n"; 49 | 50 | unset($worker_new); 51 | echo "unset worker pass\n"; 52 | 53 | $ret = gearman_worker_error($worker); 54 | if ($ret != GEARMAN_SUCCESS) 55 | { 56 | echo "gearman_worker_error() FAILED\n"; 57 | exit(0); 58 | } 59 | else 60 | echo "gearman_worker_error() pass\n"; 61 | 62 | if(! gearman_worker_set_options($worker, GEARMAN_WORKER_NON_BLOCKING, 1)) 63 | { 64 | echo "gearman_worker_set_options() FAILED\n"; 65 | exit(0); 66 | } 67 | gearman_worker_set_options($worker, GEARMAN_WORKER_NON_BLOCKING, 0); 68 | echo "gearman_worker_set_options() pass\n"; 69 | 70 | if(! gearman_worker_register($worker, "test_one")) 71 | { 72 | echo "gearman_worker_register() FAILED\n"; 73 | exit(0); 74 | } 75 | gearman_worker_register($worker, "test_two"); 76 | gearman_worker_register($worker, "test_three"); 77 | echo "gearman_worker_register() pass\n"; 78 | 79 | if(! gearman_worker_unregister($worker, "test_three")) 80 | { 81 | echo "gearman_worker_unregister() FAILED\n"; 82 | exit(0); 83 | } 84 | echo "gearman_worker_unregister() pass\n"; 85 | 86 | if(! gearman_worker_unregister_all($worker)) 87 | { 88 | echo "gearman_worker_unregister_all() FAILED\n"; 89 | exit(0); 90 | } 91 | echo "gearman_worker_unregister_all() pass\n"; 92 | 93 | gearman_worker_echo($worker, "gearman_worker_echo"); 94 | echo "gearman_worker_echo() pass\n"; 95 | */ 96 | 97 | if (! gearman_worker_add_function($worker, "test_worker", "test_worker", "test_worker")) 98 | { 99 | echo "gearman_worker_add_function() FAILED\n"; 100 | exit(0); 101 | } 102 | gearman_worker_add_function($worker, "exit_test", "exit_test", "exit_test"); 103 | 104 | gearman_worker_add_function($worker, "test_gearman_job", "test_gearman_job", "test_gearman_job"); 105 | gearman_worker_add_function($worker, "test_set_callback_fn", "test_set_callback_fn", "test_set_callback_fn"); 106 | gearman_worker_add_function($worker, "test_tasks", "test_tasks", "test_tasks"); 107 | gearman_worker_add_function($worker, "test_tasks_background", "test_tasks_background", "test_tasks_background"); 108 | gearman_worker_add_function($worker, "test_tasks_high", "test_tasks_high", "test_tasks_high"); 109 | gearman_worker_add_function($worker, "test_tasks_low", "test_tasks_low", "test_tasks_low"); 110 | gearman_worker_add_function($worker, "test_tasks_high_background", "test_tasks_high_background", "test_tasks_high_background"); 111 | gearman_worker_add_function($worker, "test_tasks_low_background", "test_tasks_low_background", "test_tasks_low_background"); 112 | gearman_worker_add_function($worker, "test_gearman_job_status", "test_gearman_job_status", "test_status"); 113 | gearman_worker_add_function($worker, "test_gearman_job_complete", "test_gearman_job_complete", "test_complete"); 114 | gearman_worker_add_function($worker, "test_gearman_job_exception", "test_gearman_job_exception", "test_exception"); 115 | gearman_worker_add_function($worker, "test_gearman_job_fail", "test_gearman_job_fail", "test_fail"); 116 | echo "gearman_worker_add_function() pass\n"; 117 | 118 | echo "Starting Worker...\n"; 119 | $numb_test = 5; 120 | $do_loop= true; 121 | while ($do_loop) 122 | { 123 | if (! gearman_worker_work($worker)) 124 | { 125 | echo "gearman_worker_work: FAILED\n"; 126 | exit(0); 127 | } 128 | } 129 | echo "gearman_worker_work() pass\n"; 130 | 131 | 132 | /* 133 | * Job Functions 134 | */ 135 | 136 | /* XXX finish this up once the gearman objects are added 137 | $job = gearman_job_create(); 138 | if (! is_object($job)) 139 | { 140 | echo "gearman_job_create() FAILED\n"; 141 | exit(0); 142 | } 143 | else 144 | echo "gearman_job_create() pass\n"; 145 | 146 | $data = "job_data"; 147 | gearman_job_data($job, $data); 148 | echo "gearman_job_data() pass\n"; 149 | 150 | gearman_job_warning($job, "gearman_job_warning"); 151 | echo "gearman_job_warning() pass\n"; 152 | 153 | gearman_job_status($job, 1, 2); 154 | gearman_job_status($job, 2, 2); 155 | echo "gearman_job_status() pass\n"; 156 | 157 | gearman_job_complete($job, "gearman_job_complete"); 158 | echo "gearman_job_complete() pass\n"; 159 | 160 | gearman_job_exception($job, "gearman_job_exception"); 161 | echo "gearman_job_exception() pass\n"; 162 | 163 | geaman_job_fail($job, "gearman_job_fail"); 164 | echo "gearman_job_fail() pass\n"; 165 | 166 | $job_handle= gearman_job_handle($job); 167 | if (! is_string($job_handle)) 168 | { 169 | echo "gearman_job_handle() FAILED\n"; 170 | exit(0); 171 | } 172 | else 173 | echo "gearman_job_handle() pass\n"; 174 | 175 | */ 176 | 177 | /* 178 | * Test Functions 179 | */ 180 | 181 | function exit_test($job, $data) 182 | { 183 | global $do_loop; 184 | echo "Done Working\n"; 185 | $do_loop=false; 186 | } 187 | 188 | function test_gearman_job_fail($job, $data) 189 | { 190 | gearman_job_fail($job); 191 | echo "gearman_job_fail() pass\n"; 192 | } 193 | 194 | function test_gearman_job_exception($job, $data) 195 | { 196 | gearman_job_exception($job, "test_exception"); 197 | echo "gearman_job_exception() pass\n"; 198 | } 199 | 200 | function test_gearman_job_complete($job, $data) 201 | { 202 | gearman_job_complete($job, "test_complete"); 203 | echo "gearman_job_complete() pass\n"; 204 | } 205 | 206 | function test_gearman_job_status($job, $data) 207 | { 208 | gearman_job_send_data($job, "test data"); 209 | gearman_job_send_status($job, 1, 4); 210 | sleep(1); 211 | gearman_job_send_status($job, 2, 4); 212 | sleep(1); 213 | gearman_job_send_status($job, 3, 4); 214 | sleep(1); 215 | gearman_job_send_status($job, 4, 4); 216 | sleep(1); 217 | echo "gearman_send_job_status() pass\n"; 218 | } 219 | 220 | function test_worker($job, $data=NULL) 221 | { 222 | return $job->workload(); 223 | } 224 | 225 | function test_gearman_job($job, $data) 226 | { 227 | echo "Testing Job Functions\n"; 228 | $job_handle = gearman_job_handle($job); 229 | echo "\tgearman_job_handle() pass - $job_handle\n"; 230 | $workload = gearman_job_workload($job); 231 | echo "\tgearman_job_workload() pass - $workload\n"; 232 | $workload_size = gearman_job_workload_size($job); 233 | echo "\tgearman_job_workload_size() pass - $workload_size\n"; 234 | $fname = gearman_job_function_name($job); 235 | echo "\tgearman_job_function_name() pass - $fname\n"; 236 | return "test_set_callback_fn"; 237 | } 238 | 239 | function test_set_callback_fn($job, $data) 240 | { 241 | gearman_job_send_status($job, 1, 1); 242 | echo "\tgearman_job_send_status() pass\n"; 243 | sleep(1); 244 | gearman_job_send_warning($job, "test_set_callback_fn warning"); 245 | echo "\tgearman_job_send_warning() pass\n"; 246 | sleep(1); 247 | gearman_job_send_exception($job, "test_set_callback_fn exception"); 248 | echo "\tgearman_job_send_exception() pass\n"; 249 | sleep(1); 250 | /* $job->set_return(GEARMAN_WORK_FAIL); == $job->fail() == */ 251 | if(! gearman_job_send_fail($job)) 252 | echo "\tgearman_job_send_fail() FAILED\n"; 253 | else 254 | echo "\tgearman_job_send_fail() pass\n"; 255 | sleep(1); 256 | } 257 | 258 | function test_tasks($job, $data) 259 | { 260 | $job->sendData("foobar"); 261 | gearman_job_send_status($job, 1, 1); 262 | sleep(2); 263 | return "test_tasks"; 264 | } 265 | 266 | function test_tasks_background($job, $data) 267 | { 268 | echo "\ttest_tasks_background() pass\n"; 269 | gearman_job_send_status($job, 1, 2); 270 | sleep(4); 271 | gearman_job_send_status($job, 2, 2); 272 | return "done"; 273 | } 274 | 275 | function test_tasks_high($job, $data) 276 | { 277 | echo "\ttest_task_high() pass\n"; 278 | sleep(1); 279 | } 280 | 281 | function test_tasks_low($job, $data) 282 | { 283 | echo "\ttest_task_low() pass\n"; 284 | sleep(1); 285 | } 286 | 287 | function test_tasks_high_background($job, $data) 288 | { 289 | echo "\ttest_task_high_background() pass\n"; 290 | sleep(1); 291 | } 292 | 293 | function test_tasks_low_background($job, $data) 294 | { 295 | echo "\ttest_task_low_background() pass\n"; 296 | sleep(1); 297 | } 298 | 299 | 300 | ?> 301 | -------------------------------------------------------------------------------- /tests/connect.inc: -------------------------------------------------------------------------------- 1 | 5 | --FILE-- 6 | 9 | --EXPECT-- 10 | gearman extension is available 11 | -------------------------------------------------------------------------------- /tests/gearman_002.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_set_id() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | setId('test'); 10 | 11 | $worker = gearman_worker_create(); 12 | gearman_worker_set_id($worker, 'test'); 13 | 14 | echo "OK"; 15 | ?> 16 | --EXPECT-- 17 | OK 18 | -------------------------------------------------------------------------------- /tests/gearman_client_001.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | new GearmanClient(), gearman_client_create() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 16 | --EXPECT-- 17 | GearmanClient() (OO) class: GearmanClient 18 | gearman_client_create() (Procedural) class: GearmanClient 19 | OK 20 | -------------------------------------------------------------------------------- /tests/gearman_client_002.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::returnCode(), gearman_client_return_code() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | returnCode() . PHP_EOL; 10 | 11 | $client2 = gearman_client_create(); 12 | print "gearman_client_return_code (Procedural): " . gearman_client_return_code($client2) . PHP_EOL; 13 | 14 | print "OK"; 15 | ?> 16 | --EXPECT-- 17 | GearmanClient::returnCode (OO): 0 18 | gearman_client_return_code (Procedural): 0 19 | OK 20 | -------------------------------------------------------------------------------- /tests/gearman_client_003.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::error(), gearman_client_error() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | error() . "'\n"; 10 | 11 | $client2 = gearman_client_create(); 12 | print "gearman_client_error (Procedural): '" . gearman_client_error($client2) . "'\n"; 13 | 14 | print "OK"; 15 | ?> 16 | --EXPECT-- 17 | GearmanClient::error (OO): '' 18 | gearman_client_error (Procedural): '' 19 | OK 20 | -------------------------------------------------------------------------------- /tests/gearman_client_004.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::getErrno(), gearman_client_get_errno() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | getErrno() . PHP_EOL; 10 | 11 | $client2 = gearman_client_create(); 12 | print "gearman_client_get_errno (Procedural): " . gearman_client_get_errno($client2) . PHP_EOL; 13 | 14 | print "OK"; 15 | ?> 16 | --EXPECT-- 17 | GearmanClient::getErrno (OO): 0 18 | gearman_client_get_errno (Procedural): 0 19 | OK 20 | -------------------------------------------------------------------------------- /tests/gearman_client_005.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::options(), gearman_client_options() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | setOptions(32); 10 | print "GearmanClient::options (OO): " . $client->options() . PHP_EOL; 11 | 12 | $client2 = gearman_client_create(); 13 | gearman_client_set_options($client2, 32); 14 | print "gearman_client_options (Procedural): " . gearman_client_options($client2) . PHP_EOL; 15 | 16 | print "OK"; 17 | ?> 18 | --EXPECT-- 19 | GearmanClient::options (OO): 32 20 | gearman_client_options (Procedural): 32 21 | OK 22 | -------------------------------------------------------------------------------- /tests/gearman_client_006.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::setOptions(), gearman_client_set_options() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | setOptions(32); 10 | print "GearmanClient::options (OO): " . $client->options() . PHP_EOL; 11 | print "GearmanClient::setOptions (OO): " . ($client->setOptions(GEARMAN_CLIENT_NON_BLOCKING) ? 'Success' : 'Failure') . PHP_EOL; 12 | print "GearmanClient::options (OO): " . $client->options() . PHP_EOL; 13 | 14 | $client2 = gearman_client_create(); 15 | gearman_client_set_options($client2, 32); 16 | print "gearman_client_options (Procedural): " . gearman_client_options($client2) . PHP_EOL; 17 | print "gearman_client_set_options (Procedural): " . (gearman_client_set_options($client2, GEARMAN_CLIENT_NON_BLOCKING) ? 'Success' : 'Failure') . PHP_EOL; 18 | print "gearman_client_options (Procedural): " . gearman_client_options($client2) . PHP_EOL; 19 | 20 | print "OK"; 21 | ?> 22 | --EXPECT-- 23 | GearmanClient::options (OO): 32 24 | GearmanClient::setOptions (OO): Success 25 | GearmanClient::options (OO): 2 26 | gearman_client_options (Procedural): 32 27 | gearman_client_set_options (Procedural): Success 28 | gearman_client_options (Procedural): 2 29 | OK 30 | -------------------------------------------------------------------------------- /tests/gearman_client_007.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::addOptions(), gearman_client_add_options() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | setOptions(GEARMAN_CLIENT_NON_BLOCKING); 10 | print "GearmanClient::options (OO): " . $client->options() . PHP_EOL; 11 | print "GearmanClient::addOptions (OO): " . ($client->addOptions(GEARMAN_CLIENT_FREE_TASKS) ? 'Success' : 'Failure') . PHP_EOL; 12 | print "GearmanClient::options (OO): " . $client->options() . PHP_EOL; 13 | 14 | $client2 = gearman_client_create(); 15 | gearman_client_set_options($client2, GEARMAN_CLIENT_NON_BLOCKING); 16 | print "gearman_client_options (Procedural): " . gearman_client_options($client2) . PHP_EOL; 17 | print "gearman_client_add_options (Procedural): " . (gearman_client_add_options($client2, GEARMAN_CLIENT_FREE_TASKS) ? 'Success' : 'Failure') . PHP_EOL; 18 | print "gearman_client_options (Procedural): " . gearman_client_options($client2) . PHP_EOL; 19 | 20 | print "OK"; 21 | ?> 22 | --EXPECT-- 23 | GearmanClient::options (OO): 2 24 | GearmanClient::addOptions (OO): Success 25 | GearmanClient::options (OO): 34 26 | gearman_client_options (Procedural): 2 27 | gearman_client_add_options (Procedural): Success 28 | gearman_client_options (Procedural): 34 29 | OK 30 | -------------------------------------------------------------------------------- /tests/gearman_client_008.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::removeOptions(), gearman_client_remove_options() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | setOptions(32); 10 | print "GearmanClient::options (OO): " . $client->options() . PHP_EOL; 11 | print "GearmanClient::removeOptions (OO): " . ($client->removeOptions(GEARMAN_CLIENT_FREE_TASKS) ? 'Success' : 'Failure') . PHP_EOL; 12 | print "GearmanClient::options (OO): " . $client->options() . PHP_EOL; 13 | 14 | $client2 = gearman_client_create(); 15 | gearman_client_set_options($client2, 32); 16 | print "gearman_client_options (Procedural): " . gearman_client_options($client2) . PHP_EOL; 17 | print "gearman_client_remove_options (Procedural): " . (gearman_client_remove_options($client2, GEARMAN_CLIENT_FREE_TASKS) ? 'Success' : 'Failure') . PHP_EOL; 18 | print "gearman_client_options (Procedural): " . gearman_client_options($client2) . PHP_EOL; 19 | 20 | print "OK"; 21 | ?> 22 | --EXPECT-- 23 | GearmanClient::options (OO): 32 24 | GearmanClient::removeOptions (OO): Success 25 | GearmanClient::options (OO): 0 26 | gearman_client_options (Procedural): 32 27 | gearman_client_remove_options (Procedural): Success 28 | gearman_client_options (Procedural): 0 29 | OK 30 | -------------------------------------------------------------------------------- /tests/gearman_client_009.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::timeout(), gearman_client_timeout() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | timeout() . PHP_EOL; 10 | 11 | $client2 = gearman_client_create(); 12 | print "gearman_client_timeout (Procedural): " . gearman_client_timeout($client2) . PHP_EOL; 13 | 14 | print "OK"; 15 | ?> 16 | --EXPECT-- 17 | GearmanClient::timeout (OO): -1 18 | gearman_client_timeout (Procedural): -1 19 | OK 20 | -------------------------------------------------------------------------------- /tests/gearman_client_010.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::setTimeout(), gearman_client_set_timeout() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | timeout() . PHP_EOL; 10 | print "GearmanClient::setTimeout (OO): " . ($client->setTimeout(3) ? 'Success' : 'Failure') . PHP_EOL; 11 | print "GearmanClient::timeout (OO): " . $client->timeout() . PHP_EOL; 12 | 13 | $client2 = gearman_client_create(); 14 | print "gearman_client_timeout (Procedural): " . gearman_client_timeout($client2) . PHP_EOL; 15 | print "gearman_client_set_timeout (Procedural): " . (gearman_client_set_timeout($client2, 3) ? 'Success' : 'Failure') . PHP_EOL; 16 | print "gearman_client_timeout (Procedural): " . gearman_client_timeout($client2) . PHP_EOL; 17 | 18 | print "OK"; 19 | ?> 20 | --EXPECT-- 21 | GearmanClient::timeout (OO): -1 22 | GearmanClient::setTimeout (OO): Success 23 | GearmanClient::timeout (OO): 3 24 | gearman_client_timeout (Procedural): -1 25 | gearman_client_set_timeout (Procedural): Success 26 | gearman_client_timeout (Procedural): 3 27 | OK 28 | -------------------------------------------------------------------------------- /tests/gearman_client_011.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::context(), GearmanClient::setContext(), gearman_client_context(), gearman_client_set_context() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | setContext('context1_context1_context1_context1') ? 'Success' : 'Failure') . PHP_EOL; 10 | print "GearmanClient::context (OO): " . $client->context() . PHP_EOL; 11 | 12 | $client2 = gearman_client_create(); 13 | print "gearman_client_set_context (Procedural): " . (gearman_client_set_context($client2, 'context2_context2') ? 'Success' : 'Failure') . PHP_EOL; 14 | print "gearman_client_context (Procedural): " . gearman_client_context($client2) . PHP_EOL; 15 | 16 | print "OK"; 17 | ?> 18 | --EXPECT-- 19 | GearmanClient::setContext(OO): Success 20 | GearmanClient::context (OO): context1_context1_context1_context1 21 | gearman_client_set_context (Procedural): Success 22 | gearman_client_context (Procedural): context2_context2 23 | OK 24 | -------------------------------------------------------------------------------- /tests/gearman_client_012.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::addServer(), gearman_client_add_server() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer('localhost') ? 'Success' : 'Failure') . PHP_EOL; 12 | print "GearmanClient::addServer(OO), explicit port: " . ($client->addServer('localhost', 4730) ? 'Success' : 'Failure') . PHP_EOL; 13 | 14 | $client2 = gearman_client_create(); 15 | print "gearman_client_add_server (Procedural), implicit port: " . (gearman_client_add_server($client2, 'localhost') ? 'Success' : 'Failure') . PHP_EOL; 16 | print "gearman_client_add_server (Procedural): explicit port" . (gearman_client_add_server($client2, 'localhost', 4730) ? 'Success' : 'Failure') . PHP_EOL; 17 | 18 | print "OK"; 19 | ?> 20 | --EXPECT-- 21 | GearmanClient::addServer(OO), implicit port: Success 22 | GearmanClient::addServer(OO), explicit port: Success 23 | gearman_client_add_server (Procedural), implicit port: Success 24 | gearman_client_add_server (Procedural): explicit portSuccess 25 | OK 26 | -------------------------------------------------------------------------------- /tests/gearman_client_013.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::addServers(), gearman_client_add_servers() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServers('localhost,localhost:4730') ? 'Success' : 'Failure') . PHP_EOL; 12 | 13 | $client2 = gearman_client_create(); 14 | print "gearman_client_add_servers (Procedural): " . (gearman_client_add_servers($client2, 'localhost,localhost:4730') ? 'Success' : 'Failure') . PHP_EOL; 15 | 16 | print "OK"; 17 | ?> 18 | --EXPECT-- 19 | GearmanClient::addServer(OO): Success 20 | gearman_client_add_servers (Procedural): Success 21 | OK 22 | -------------------------------------------------------------------------------- /tests/gearman_client_014.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::wait(), gearman_client_wait() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer('localhost', 4730); 12 | print "Placeholder for GearmanClient::wait function" . PHP_EOL; 13 | /* 14 | print "GearmanClient::wait (OO): " . ($client->wait() ? 'Success' : 'Failure') . PHP_EOL; 15 | */ 16 | 17 | $client2 = gearman_client_create(); 18 | /* 19 | print "gearman_client_wait (Procedural): " . (gearman_client_wait($client2) ? 'Success' : 'Failure') . PHP_EOL; 20 | */ 21 | 22 | print "OK"; 23 | ?> 24 | --EXPECT-- 25 | Placeholder for GearmanClient::wait function 26 | OK 27 | -------------------------------------------------------------------------------- /tests/gearman_client_015.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::doJobHandle(), gearman_client_do_job_handle() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer('localhost', 4730); 12 | 13 | $job_handle = $client->doBackground("test_doJobHandle", "test_doJobHandle"); 14 | print "GearmanClient::doJobHandle() (OO): " 15 | . ($job_handle == $client->doJobHandle() ? 'Success' : 'Failure') . PHP_EOL; 16 | 17 | 18 | 19 | $client2 = gearman_client_create(); 20 | gearman_client_add_server($client2, 'localhost', 4730); 21 | 22 | $job_handle = gearman_client_do_background($client2, "test_do_job_handle", "test_do_job_handle"); 23 | 24 | print "gearman_client_do_job_handle() (Procedural): " 25 | . ($job_handle == gearman_client_do_job_handle($client2) ? 'Success' : 'Failure') . PHP_EOL; 26 | 27 | print "OK"; 28 | ?> 29 | --EXPECT-- 30 | GearmanClient::doJobHandle() (OO): Success 31 | gearman_client_do_job_handle() (Procedural): Success 32 | OK 33 | -------------------------------------------------------------------------------- /tests/gearman_client_016.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::doStatus(), gearman_client_do_status() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addOptions(GEARMAN_CLIENT_NON_BLOCKING); 14 | $client->addServer('localhost', 4730); 15 | 16 | # Note: Still need to figure out why doNormal is blocking despite 17 | # GEARMAN_CLIENT_NON_BLOCKING 18 | #$job_handle = $client->doNormal($job_name . "_OO", "test_doStatus"); 19 | list($numerator, $denominator) = $client->doStatus(); 20 | 21 | print "GearmanClient::doStatus() (OO): " . PHP_EOL 22 | . " Numerator is 0: " . ($numerator == 0 ? 'Success' : 'Failure') . 23 | PHP_EOL 24 | . " Denominator is 0: " . ($denominator == 0 ? 'Success' : 'Failure') . 25 | PHP_EOL; 26 | 27 | $client2 = gearman_client_create(); 28 | gearman_client_add_options($client2, GEARMAN_CLIENT_NON_BLOCKING); 29 | gearman_client_add_server($client2, 'localhost', 4730); 30 | 31 | # Note: Still need to figure out why doNormal is blocking despite 32 | # GEARMAN_CLIENT_NON_BLOCKING 33 | #$job_handle = gearman_client_do_normal->($client2, $job_name . "_procedural", "test_doStatus"); 34 | list($numerator, $denominator) = gearman_client_do_status($client2); 35 | 36 | print "gearman_client_do_status() (Procedural): " . PHP_EOL 37 | . " Numerator is 0: " . ($numerator == 0 ? 'Success' : 'Failure') . 38 | PHP_EOL 39 | . " Denominator is 0: " . ($denominator == 0 ? 'Success' : 'Failure') . 40 | PHP_EOL; 41 | 42 | print "OK"; 43 | ?> 44 | --EXPECT-- 45 | GearmanClient::doStatus() (OO): 46 | Numerator is 0: Success 47 | Denominator is 0: Success 48 | gearman_client_do_status() (Procedural): 49 | Numerator is 0: Success 50 | Denominator is 0: Success 51 | OK 52 | -------------------------------------------------------------------------------- /tests/gearman_client_017.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::jobStatus(), gearman_client_job_status() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer('localhost', 4730); 14 | 15 | $job_handle = $client->doBackground($job_name . "_OO", "test_jobStatus"); 16 | list($is_known, $is_running, $numerator, $denominator) = $client->jobStatus($job_handle); 17 | 18 | print "GearmanClient::jobStatus() (OO): " . PHP_EOL 19 | . " is_known is true: " . ($is_known === true ? 'Success' : 'Failure') . 20 | PHP_EOL 21 | . " is_running is false: " . ($is_running=== false ? 'Success' : 'Failure') . 22 | PHP_EOL 23 | . " Numerator is 0: " . ($numerator == 0 ? 'Success' : 'Failure') . 24 | PHP_EOL 25 | . " Denominator is 0: " . ($denominator == 0 ? 'Success' : 'Failure') . 26 | PHP_EOL; 27 | 28 | $client2 = gearman_client_create(); 29 | gearman_client_add_server($client2, 'localhost', 4730); 30 | 31 | $job_handle = gearman_client_do_background($client2, $job_name . 32 | "_procedural", "test_jobStatus"); 33 | list($is_known, $is_running, $numerator, $denominator) = 34 | gearman_client_job_status($client2, $job_handle); 35 | 36 | print "gearman_client_job_status() (Procedural): " . PHP_EOL 37 | . " is_known is true: " . ($is_known === true ? 'Success' : 'Failure') . 38 | PHP_EOL 39 | . " is_running is false: " . ($is_running === false ? 'Success' : 'Failure') . 40 | PHP_EOL 41 | . " Numerator is 0: " . ($numerator == 0 ? 'Success' : 'Failure') . 42 | PHP_EOL 43 | . " Denominator is 0: " . ($denominator == 0 ? 'Success' : 'Failure') . 44 | PHP_EOL; 45 | 46 | print "OK"; 47 | ?> 48 | --EXPECT-- 49 | GearmanClient::jobStatus() (OO): 50 | is_known is true: Success 51 | is_running is false: Success 52 | Numerator is 0: Success 53 | Denominator is 0: Success 54 | gearman_client_job_status() (Procedural): 55 | is_known is true: Success 56 | is_running is false: Success 57 | Numerator is 0: Success 58 | Denominator is 0: Success 59 | OK 60 | -------------------------------------------------------------------------------- /tests/gearman_client_018.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::jobStatusByUniqueKey(), gearman_client_job_status_by_unique_key() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer('localhost', 4730); 14 | 15 | $oo_key = $job_name . "_oo"; 16 | $client->doBackground($job_name . "_OO", "test_jobStatusByUniqueKey"); 17 | list($is_known, $is_running, $numerator, $denominator) = 18 | $client->jobStatusByUniqueKey($oo_key); 19 | 20 | print "GearmanClient::doStatus() (OO): " . PHP_EOL 21 | /* 22 | Note: This is returning falso, while jobStatus returns true. 23 | Looks to be across multiple versions of libgearman (1.0.2 and 1.1.2 checked) 24 | . " is_known is true: " . ($is_known === true ? 'Success' : 'Failure') . 25 | PHP_EOL 26 | */ 27 | . " is_running is false: " . ($is_running=== false ? 'Success' : 'Failure') . 28 | PHP_EOL 29 | . " Numerator is 0: " . ($numerator == 0 ? 'Success' : 'Failure') . 30 | PHP_EOL 31 | . " Denominator is 0: " . ($denominator == 0 ? 'Success' : 'Failure') . 32 | PHP_EOL; 33 | 34 | $client2 = gearman_client_create(); 35 | gearman_client_add_server($client2, 'localhost', 4730); 36 | 37 | $procedural_key = $job_name . "_procedural"; 38 | $job_handle = gearman_client_do_background($client2, $job_name . 39 | "_procedural", "test_jobStatusByUniqueKey"); 40 | list($is_known, $is_running, $numerator, $denominator) = 41 | gearman_client_job_status_by_unique_key($client2, $procedural_key); 42 | 43 | print "gearman_client_job_status_by_unique_key() (Procedural): " . PHP_EOL 44 | /* 45 | Note: This is returning falso, while jobStatus returns true. 46 | Looks to be across multiple versions of libgearman (1.0.2 and 1.1.2 checked) 47 | . " is_known is true: " . ($is_known === true ? 'Success' : 'Failure') . 48 | PHP_EOL 49 | */ 50 | . " is_running is false: " . ($is_running === false ? 'Success' : 'Failure') . 51 | PHP_EOL 52 | . " Numerator is 0: " . ($numerator == 0 ? 'Success' : 'Failure') . 53 | PHP_EOL 54 | . " Denominator is 0: " . ($denominator == 0 ? 'Success' : 'Failure') . 55 | PHP_EOL; 56 | 57 | print "OK"; 58 | ?> 59 | --EXPECT-- 60 | GearmanClient::doStatus() (OO): 61 | is_running is false: Success 62 | Numerator is 0: Success 63 | Denominator is 0: Success 64 | gearman_client_job_status_by_unique_key() (Procedural): 65 | is_running is false: Success 66 | Numerator is 0: Success 67 | Denominator is 0: Success 68 | OK 69 | -------------------------------------------------------------------------------- /tests/gearman_client_019.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::ping(), gearman_client_ping() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer('localhost', 4730); 12 | print "GearmanClient::ping (OO): " . ($client->ping('oo data') ? 'Success' : 'Failure') . PHP_EOL; 13 | 14 | $client2 = gearman_client_create(); 15 | gearman_client_add_server($client2, 'localhost', 4730); 16 | print "gearman_client_ping (Procedural): " 17 | . (gearman_client_ping($client2,'procedural data') ? 'Success' : 'Failure') . PHP_EOL; 18 | 19 | print "OK"; 20 | ?> 21 | --EXPECT-- 22 | GearmanClient::ping (OO): Success 23 | gearman_client_ping (Procedural): Success 24 | OK 25 | -------------------------------------------------------------------------------- /tests/gearman_client_020.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::clearCallbacks(), gearman_client_clear_callbacks() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer('localhost', 4730); 12 | print "GearmanClient::clearCallbacks (OO) (No callbacks set): " . ($client->clearCallbacks() ? 'Success' : 'Failure') . PHP_EOL; 13 | $client->setWorkloadCallback(function($task, $context){}); 14 | $client->setCreatedCallback(function($task, $context){}); 15 | $client->setDataCallback(function($task, $context){}); 16 | $client->setWarningCallback(function($task, $context){}); 17 | $client->setStatusCallback(function($task, $context){}); 18 | $client->setCompleteCallback(function($task, $context){}); 19 | $client->setExceptionCallback(function($task, $context){}); 20 | $client->setFailCallback(function($task, $context){}); 21 | print "GearmanClient::clearCallbacks (OO) (Callbacks set): " . ($client->clearCallbacks() ? 'Success' : 'Failure') . PHP_EOL; 22 | 23 | $client2 = gearman_client_create(); 24 | gearman_client_add_server($client2, 'localhost', 4730); 25 | print "gearman_client_clear_callbacks (Procedural) (No callbacks set: " 26 | . (gearman_client_clear_callbacks($client2) ? 'Success' : 'Failure') . PHP_EOL; 27 | gearman_client_set_workload_callback($client2, function($task, $context){}); 28 | gearman_client_set_created_callback($client2, function($task, $context){}); 29 | gearman_client_set_data_callback($client2, function($task, $context){}); 30 | gearman_client_set_warning_callback($client2, function($task, $context){}); 31 | gearman_client_set_status_callback($client2, function($task, $context){}); 32 | gearman_client_set_complete_callback($client2, function($task, $context){}); 33 | gearman_client_set_exception_callback($client2, function($task, $context){}); 34 | gearman_client_set_fail_callback($client2, function($task, $context){}); 35 | print "gearman_client_clear_callbacks (Procedural) (Callbacks set: " 36 | . (gearman_client_clear_callbacks($client2) ? 'Success' : 'Failure') . PHP_EOL; 37 | 38 | print "OK"; 39 | ?> 40 | --EXPECT-- 41 | GearmanClient::clearCallbacks (OO) (No callbacks set): Success 42 | GearmanClient::clearCallbacks (OO) (Callbacks set): Success 43 | gearman_client_clear_callbacks (Procedural) (No callbacks set: Success 44 | gearman_client_clear_callbacks (Procedural) (Callbacks set: Success 45 | OK 46 | -------------------------------------------------------------------------------- /tests/gearman_client_021.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::enableExceptionHandler(),gearman_client_enable_exception_handler() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServers('localhost:4731,localhost', false); 15 | 16 | // Enabling the exception handler, which will attempt to connect to 17 | // the server and in doing so throw an exception since we can't 18 | // connect to a server that doesn't exist 19 | try { 20 | $client->enableExceptionHandler(); 21 | } catch (Exception $e) { 22 | print "Exception 1 caught: " . $e->getMessage() . PHP_EOL; 23 | } 24 | 25 | // Test 2: GearmanClient::addServers, Exception callback enabled (by default). 26 | // Here, we don't give the second param, so the exception handler is enabled 27 | // upon calling addServers instead of later in enableExceptionHandler 28 | $client2 = new GearmanClient(); 29 | 30 | try { 31 | $client2->addServers('localhost:4731,localhost'); 32 | } catch (Exception $e) { 33 | print "Exception 2 caught: " . $e->getMessage() . PHP_EOL; 34 | } 35 | 36 | // Test 3: GearmanClient::addServers, Also, when we explicitly enable in addServers 37 | $client3 = new GearmanClient(); 38 | 39 | try { 40 | $client3->addServers('localhost:4731,localhost', true); 41 | } catch (Exception $e) { 42 | print "Exception 3 caught: " . $e->getMessage() . PHP_EOL; 43 | } 44 | 45 | // Now, do the same as above but with addServer (singular) 46 | // Test 4: GearmanClient::addServer, Exception callback disabled 47 | $client4 = new GearmanClient(); 48 | $client4->addServer('localhost', 4731, false); 49 | 50 | try { 51 | $client4->enableExceptionHandler(); 52 | } catch (Exception $e) { 53 | print "Exception 4 caught: " . $e->getMessage() . PHP_EOL; 54 | } 55 | 56 | // Test 5: GearmanClient::addServer, default 57 | $client5 = new GearmanClient(); 58 | 59 | try { 60 | $client5->addServer('localhost', 4731); 61 | } catch (Exception $e) { 62 | print "Exception 5 caught: " . $e->getMessage() . PHP_EOL; 63 | } 64 | 65 | // Test 6: GearmanClient::addServer, explicitly set enableExceptionHandler 66 | $client6 = new GearmanClient(); 67 | 68 | try { 69 | $client6->addServer('localhost', 4731, true); 70 | } catch (Exception $e) { 71 | print "Exception 6 caught: " . $e->getMessage() . PHP_EOL; 72 | } 73 | 74 | print "OK"; 75 | ?> 76 | --EXPECTF-- 77 | Exception 1 caught: Failed to set exception option 78 | Exception 2 caught: Failed to set exception option 79 | Exception 3 caught: Failed to set exception option 80 | Exception 4 caught: Failed to set exception option 81 | Exception 5 caught: Failed to set exception option 82 | Exception 6 caught: Failed to set exception option 83 | OK 84 | -------------------------------------------------------------------------------- /tests/gearman_client_022.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | unserialize(serialize(GearmanClient)) 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 19 | --EXPECT-- 20 | 012345 21 | OK 22 | -------------------------------------------------------------------------------- /tests/gearman_client_integration_test_001.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | 3 | --SKIPIF-- 4 | 8 | --FILE-- 9 | 0) { 20 | // Parent. This should be the Worker 21 | $worker = new GearmanWorker(); 22 | $worker->addServer($host, $port); 23 | 24 | print "addFunction: " . var_export( 25 | $worker->addFunction( 26 | $job_name, 27 | function($job) { 28 | print "workload: " . var_export($job->workload(), true) . PHP_EOL; 29 | } 30 | ), 31 | true 32 | ) . PHP_EOL; 33 | 34 | for($i=0; $i<6; $i++) { 35 | $worker->work(); 36 | } 37 | 38 | print "unregister: " . var_export($worker->unregister($job_name), true) . PHP_EOL; 39 | 40 | // Wait for child 41 | $exit_status = 0; 42 | if (pcntl_wait($exit_status) <= 0) { 43 | print "pcntl_wait exited with error" . PHP_EOL; 44 | } else if (!pcntl_wifexited($exit_status)) { 45 | print "child exited with error" . PHP_EOL; 46 | } 47 | 48 | print "OK" . PHP_EOL; 49 | } else { 50 | // Child. This is the Client 51 | $client = new GearmanClient(); 52 | $client->addServer($host, $port); 53 | 54 | $job_types = ['doNormal', 'doHigh', 'doLow']; 55 | foreach ($job_types as $job_type) { 56 | $unique_key = "{$job_name}_{$job_type}"; 57 | $workload = "Workload for $job_type"; 58 | $handle = $client->$job_type($job_name, $workload, $unique_key); 59 | } 60 | 61 | // Background jobs can run into a race condition if they complete out of 62 | // order 63 | $job_types = ['doBackground', 'doHighBackground', 'doLowBackground']; 64 | foreach ($job_types as $job_type) { 65 | $unique_key = "{$job_name}_{$job_type}"; 66 | $workload = "Workload for $job_type"; 67 | $handle = $client->$job_type($job_name, $workload, $unique_key); 68 | 69 | do { 70 | usleep(10000); 71 | list($is_known, $is_running, $numerator, $denominator) = 72 | $client->jobStatus($handle); 73 | } while ($is_known === true || $is_running === true); 74 | } 75 | } 76 | ?> 77 | --EXPECT-- 78 | addFunction: true 79 | workload: 'Workload for doNormal' 80 | workload: 'Workload for doHigh' 81 | workload: 'Workload for doLow' 82 | workload: 'Workload for doBackground' 83 | workload: 'Workload for doHighBackground' 84 | workload: 'Workload for doLowBackground' 85 | unregister: true 86 | OK 87 | -------------------------------------------------------------------------------- /tests/gearman_client_integration_test_002.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanClient::setStatusCallback(), gearman_client_set_status_callback(), 3 | GearmanClient::addTaskStatus(), gearman_client_add_task_status(), 4 | GearmanClient::runTasks(), gearman_client_run_tasks() 5 | --SKIPIF-- 6 | 9 | --FILE-- 10 | addServer('localhost'); 20 | 21 | $handle = $client->doBackground("reverse", "Hello World!"); 22 | 23 | $client->setStatusCallback("reverse_status"); 24 | 25 | $oo_context = "context passed in through OO"; 26 | 27 | $client->addTaskStatus($handle, $oo_context); 28 | 29 | // Should print within reverse_status 30 | $client->runTasks(); 31 | 32 | $client2 = gearman_client_create(); 33 | gearman_client_add_server($client2, 'localhost', 4730); 34 | 35 | $handle = gearman_client_do_background($client2, "reverse", "Hello World!"); 36 | 37 | gearman_client_set_status_callback($client2, "reverse_status"); 38 | 39 | $procedural_context = "context passed in through procedural"; 40 | 41 | gearman_client_add_task_status($client2, $handle, $procedural_context); 42 | 43 | gearman_client_run_tasks($client2); 44 | 45 | print "OK"; 46 | ?> 47 | --EXPECT-- 48 | In reverse_status context is 'context passed in through OO' 49 | In reverse_status context is 'context passed in through procedural' 50 | OK 51 | -------------------------------------------------------------------------------- /tests/gearman_job_001.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | new GearmanJob() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 13 | --EXPECT-- 14 | GearmanJob() (OO) class: GearmanJob 15 | OK 16 | -------------------------------------------------------------------------------- /tests/gearman_job_002.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanJob::returnCode(), gearman_job_return_code(), 3 | GearmanJob::setReturn(), gearman_job_set_return() 4 | --SKIPIF-- 5 | 6 | --FILE-- 7 | returnCode() . PHP_EOL; 11 | print "GearmanJob::setReturn with value GEARMAN_TIMEOUT (OO): " 12 | . ($job->setReturn(GEARMAN_TIMEOUT) ? 'Success' : 'Failure') 13 | . PHP_EOL; 14 | print "GearmanJob::returnCode matches GEARMAN_TIMEOUT (OO): " 15 | . ($job->returnCode() == GEARMAN_TIMEOUT ? 'Success' : 'Failure') . PHP_EOL; 16 | 17 | 18 | $job2 = new GearmanJob(); 19 | print "gearman_job_return_code (Procedural): " . gearman_job_return_code($job2) . PHP_EOL; 20 | print "gearman_job_set_return with value GEARMAN_TIMEOUT (Procedural): " 21 | . (gearman_job_set_return($job2, GEARMAN_TIMEOUT) ? 'Success' : 'Failure') 22 | . PHP_EOL; 23 | print "gearman_job_return_code matches GEARMAN_TIMEOUT (OO): " 24 | . (gearman_job_return_code($job2) == GEARMAN_TIMEOUT ? 'Success' : 'Failure') . PHP_EOL; 25 | 26 | print "OK"; 27 | ?> 28 | --EXPECT-- 29 | GearmanJob::returnCode (OO): 0 30 | GearmanJob::setReturn with value GEARMAN_TIMEOUT (OO): Success 31 | GearmanJob::returnCode matches GEARMAN_TIMEOUT (OO): Success 32 | gearman_job_return_code (Procedural): 0 33 | gearman_job_set_return with value GEARMAN_TIMEOUT (Procedural): Success 34 | gearman_job_return_code matches GEARMAN_TIMEOUT (OO): Success 35 | OK 36 | -------------------------------------------------------------------------------- /tests/gearman_job_003.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | unserialize(serialize(GearmanJob)) 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 19 | --EXPECT-- 20 | 012345 21 | OK 22 | -------------------------------------------------------------------------------- /tests/gearman_job_integration_test_001.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test GearmanJob::sendData() 3 | --SKIPIF-- 4 | 8 | --FILE-- 9 | 0) { 20 | // Parent. This is the worker 21 | $worker = new GearmanWorker(); 22 | $worker->addServer($host, $port); 23 | $worker->addFunction( 24 | $job_name, 25 | function($job, $data) { 26 | print "GearmanJob::sendData (OO): " 27 | . ($job->sendData("{'foo': 'bar'}") === true ? 'Success' : 'Failure') 28 | . PHP_EOL; 29 | } 30 | ); 31 | 32 | $worker->work(); 33 | 34 | $worker->unregister($job_name); 35 | 36 | // Wait for child 37 | $exit_status = 0; 38 | if (pcntl_wait($exit_status) <= 0) { 39 | print "pcntl_wait exited with error" . PHP_EOL; 40 | } else if (!pcntl_wifexited($exit_status)) { 41 | print "child exited with error" . PHP_EOL; 42 | } 43 | } else { 44 | //Child. This is the client. Don't echo anything here 45 | $client = new GearmanClient(); 46 | if ($client->addServer($host, $port) !== true) { 47 | exit(1); // error 48 | }; 49 | 50 | $tasks = []; 51 | $tasks[] = $client->addTask($job_name, "normal"); 52 | $client->runTasks(); 53 | if ($client->returnCode() != GEARMAN_SUCCESS) { 54 | exit(2); // error 55 | } 56 | exit(0); 57 | } 58 | 59 | print "Done"; 60 | --EXPECTF-- 61 | Start 62 | GearmanJob::sendData (OO): Success 63 | Done 64 | -------------------------------------------------------------------------------- /tests/gearman_job_integration_test_002.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test GearmanJob::sendWarning() 3 | --SKIPIF-- 4 | 8 | --FILE-- 9 | 0) { 20 | // Parent. This is the worker 21 | $worker = new GearmanWorker(); 22 | $worker->addServer($host, $port); 23 | $worker->addFunction( 24 | $job_name, 25 | function($job, $data) { 26 | print "GearmanJob::sendWarning (OO): " 27 | . ($job->sendWarning("Warning string") === true ? 'Success' : 'Failure') 28 | . PHP_EOL; 29 | } 30 | ); 31 | 32 | $worker->work(); 33 | 34 | $worker->unregister($job_name); 35 | 36 | // Wait for child 37 | $exit_status = 0; 38 | if (pcntl_wait($exit_status) <= 0) { 39 | print "pcntl_wait exited with error" . PHP_EOL; 40 | } else if (!pcntl_wifexited($exit_status)) { 41 | print "child exited with error" . PHP_EOL; 42 | } 43 | } else { 44 | //Child. This is the client. Don't echo anything here 45 | $client = new GearmanClient(); 46 | if ($client->addServer($host, $port) !== true) { 47 | exit(1); // error 48 | }; 49 | 50 | $tasks = []; 51 | $tasks[] = $client->addTask($job_name, "normal"); 52 | $client->runTasks(); 53 | if ($client->returnCode() != GEARMAN_SUCCESS) { 54 | exit(2); // error 55 | } 56 | exit(0); 57 | } 58 | 59 | print "Done"; 60 | --EXPECTF-- 61 | Start 62 | GearmanJob::sendWarning (OO): Success 63 | Done 64 | -------------------------------------------------------------------------------- /tests/gearman_job_integration_test_003.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test GearmanJob::sendStatus 3 | --SKIPIF-- 4 | 8 | --FILE-- 9 | 0) { 20 | // Parent. This is the worker 21 | $worker = new GearmanWorker(); 22 | $worker->addServer($host, $port); 23 | $worker->addFunction( 24 | $job_name, 25 | function($job, $data) { 26 | print "GearmanJob::sendStatus (OO): " 27 | . ($job->sendStatus(1,2) === true ? 'Success' : 'Failure') 28 | . PHP_EOL; 29 | } 30 | ); 31 | 32 | $worker->work(); 33 | 34 | $worker->unregister($job_name); 35 | 36 | // Wait for child 37 | $exit_status = 0; 38 | if (pcntl_wait($exit_status) <= 0) { 39 | print "pcntl_wait exited with error" . PHP_EOL; 40 | } else if (!pcntl_wifexited($exit_status)) { 41 | print "child exited with error" . PHP_EOL; 42 | } 43 | } else { 44 | //Child. This is the client. Don't echo anything here 45 | $client = new GearmanClient(); 46 | if ($client->addServer($host, $port) !== true) { 47 | exit(1); // error 48 | }; 49 | 50 | $tasks = []; 51 | $tasks[] = $client->addTask($job_name, "normal"); 52 | $client->runTasks(); 53 | if ($client->returnCode() != GEARMAN_SUCCESS) { 54 | exit(2); // error 55 | } 56 | exit(0); 57 | } 58 | 59 | print "Done"; 60 | --EXPECTF-- 61 | Start 62 | GearmanJob::sendStatus (OO): Success 63 | Done 64 | -------------------------------------------------------------------------------- /tests/gearman_job_integration_test_004.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test GearmanJob::sendComplete() 3 | --SKIPIF-- 4 | 8 | --FILE-- 9 | 0) { 20 | // Parent. This is the worker 21 | $worker = new GearmanWorker(); 22 | $worker->addServer($host, $port); 23 | $worker->addFunction( 24 | $job_name, 25 | function($job, $data) { 26 | print "GearmanJob::sendComplete (OO): " 27 | . ($job->sendComplete("Job is complete.") === true ? 'Success' : 'Failure') 28 | . PHP_EOL; 29 | } 30 | ); 31 | 32 | $worker->work(); 33 | 34 | $worker->unregister($job_name); 35 | 36 | // Wait for child 37 | $exit_status = 0; 38 | if (pcntl_wait($exit_status) <= 0) { 39 | print "pcntl_wait exited with error" . PHP_EOL; 40 | } else if (!pcntl_wifexited($exit_status)) { 41 | print "child exited with error" . PHP_EOL; 42 | } 43 | } else { 44 | //Child. This is the client. Don't echo anything here 45 | $client = new GearmanClient(); 46 | if ($client->addServer($host, $port) !== true) { 47 | exit(1); // error 48 | }; 49 | 50 | $tasks = []; 51 | $tasks[] = $client->addTask($job_name, "normal"); 52 | $client->runTasks(); 53 | if ($client->returnCode() != GEARMAN_SUCCESS) { 54 | exit(2); // error 55 | } 56 | exit(0); 57 | } 58 | 59 | print "Done"; 60 | --EXPECTF-- 61 | Start 62 | GearmanJob::sendComplete (OO): Success 63 | Done 64 | -------------------------------------------------------------------------------- /tests/gearman_job_integration_test_005.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test GearmanJob::sendException() 3 | --SKIPIF-- 4 | 8 | --FILE-- 9 | 0) { 20 | // Parent. This is the worker 21 | $worker = new GearmanWorker(); 22 | $worker->addServer($host, $port); 23 | $worker->addFunction( 24 | $job_name, 25 | function($job, $data) { 26 | print "GearmanJob::sendException (OO): " 27 | . ($job->sendException("Sending exception from job.") === true ? 'Success' : 'Failure') 28 | . PHP_EOL; 29 | } 30 | ); 31 | 32 | $worker->work(); 33 | 34 | $worker->unregister($job_name); 35 | 36 | // Wait for child 37 | $exit_status = 0; 38 | if (pcntl_wait($exit_status) <= 0) { 39 | print "pcntl_wait exited with error" . PHP_EOL; 40 | } else if (!pcntl_wifexited($exit_status)) { 41 | print "child exited with error" . PHP_EOL; 42 | } 43 | } else { 44 | //Child. This is the client. Don't echo anything here 45 | $client = new GearmanClient(); 46 | if ($client->addServer($host, $port) !== true) { 47 | exit(1); // error 48 | }; 49 | 50 | $tasks = []; 51 | $tasks[] = $client->addTask($job_name, "normal"); 52 | $client->runTasks(); 53 | if ($client->returnCode() != GEARMAN_SUCCESS) { 54 | exit(2); // error 55 | } 56 | exit(0); 57 | } 58 | 59 | print "Done"; 60 | --EXPECTF-- 61 | Start 62 | GearmanJob::sendException (OO): Success 63 | Done 64 | -------------------------------------------------------------------------------- /tests/gearman_job_integration_test_006.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test GearmanJob::sendFail() 3 | --SKIPIF-- 4 | 8 | --FILE-- 9 | 0) { 20 | // Parent. This is the worker 21 | $worker = new GearmanWorker(); 22 | $worker->addServer($host, $port); 23 | $worker->addFunction( 24 | $job_name, 25 | function($job, $data) { 26 | print "GearmanJob::sendFail (OO): " 27 | . ($job->sendFail() === true ? 'Success' : 'Failure') 28 | . PHP_EOL; 29 | } 30 | ); 31 | 32 | $worker->work(); 33 | 34 | $worker->unregister($job_name); 35 | 36 | // Wait for child 37 | $exit_status = 0; 38 | if (pcntl_wait($exit_status) <= 0) { 39 | print "pcntl_wait exited with error" . PHP_EOL; 40 | } else if (!pcntl_wifexited($exit_status)) { 41 | print "child exited with error" . PHP_EOL; 42 | } 43 | } else { 44 | //Child. This is the client. Don't echo anything here 45 | $client = new GearmanClient(); 46 | if ($client->addServer($host, $port) !== true) { 47 | exit(1); // error 48 | }; 49 | 50 | $tasks = []; 51 | $tasks[] = $client->addTask($job_name, "normal"); 52 | $client->runTasks(); 53 | if ($client->returnCode() != GEARMAN_SUCCESS) { 54 | exit(2); // error 55 | } 56 | exit(0); 57 | } 58 | 59 | print "Done"; 60 | --EXPECTF-- 61 | Start 62 | GearmanJob::sendFail (OO): Success 63 | Done 64 | -------------------------------------------------------------------------------- /tests/gearman_job_integration_test_007.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test GearmanJob::handle() 3 | --SKIPIF-- 4 | 8 | --FILE-- 9 | 0) { 20 | // Parent. This is the worker 21 | $worker = new GearmanWorker(); 22 | $worker->addServer($host, $port); 23 | $worker->addFunction( 24 | $job_name, 25 | function($job, $data) { 26 | print "GearmanJob::handle (OO): " 27 | . (preg_match('/^(.*):(.*):(.*)$/',$job->handle()) ? 'Success' : 'Failure') 28 | . PHP_EOL; 29 | } 30 | ); 31 | 32 | $worker->work(); 33 | 34 | $worker->unregister($job_name); 35 | 36 | // Wait for child 37 | $exit_status = 0; 38 | if (pcntl_wait($exit_status) <= 0) { 39 | print "pcntl_wait exited with error" . PHP_EOL; 40 | } else if (!pcntl_wifexited($exit_status)) { 41 | print "child exited with error" . PHP_EOL; 42 | } 43 | } else { 44 | //Child. This is the client. Don't echo anything here 45 | $client = new GearmanClient(); 46 | if ($client->addServer($host, $port) !== true) { 47 | exit(1); // error 48 | }; 49 | 50 | $tasks = []; 51 | $tasks[] = $client->addTask($job_name, "normal"); 52 | $client->runTasks(); 53 | if ($client->returnCode() != GEARMAN_SUCCESS) { 54 | exit(2); // error 55 | } 56 | exit(0); 57 | } 58 | 59 | print "Done"; 60 | --EXPECTF-- 61 | Start 62 | GearmanJob::handle (OO): Success 63 | Done 64 | -------------------------------------------------------------------------------- /tests/gearman_job_integration_test_008.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test GearmanJob::functionName() 3 | --SKIPIF-- 4 | 8 | --FILE-- 9 | 0) { 20 | // Parent. This is the worker 21 | $worker = new GearmanWorker(); 22 | $worker->addServer($host, $port); 23 | $worker->addFunction( 24 | $job_name, 25 | function($job, $data) { 26 | global $job_name; 27 | print "GearmanJob::functionName (OO): " 28 | . ($job->functionName() == $job_name ? 'Success' : 'Failure') 29 | . PHP_EOL; 30 | } 31 | ); 32 | 33 | $worker->work(); 34 | 35 | $worker->unregister($job_name); 36 | 37 | // Wait for child 38 | $exit_status = 0; 39 | if (pcntl_wait($exit_status) <= 0) { 40 | print "pcntl_wait exited with error" . PHP_EOL; 41 | } else if (!pcntl_wifexited($exit_status)) { 42 | print "child exited with error" . PHP_EOL; 43 | } 44 | } else { 45 | //Child. This is the client. Don't echo anything here 46 | $client = new GearmanClient(); 47 | if ($client->addServer($host, $port) !== true) { 48 | exit(1); // error 49 | }; 50 | 51 | $tasks = []; 52 | $tasks[] = $client->addTask($job_name, "normal"); 53 | $client->runTasks(); 54 | if ($client->returnCode() != GEARMAN_SUCCESS) { 55 | exit(2); // error 56 | } 57 | exit(0); 58 | } 59 | 60 | print "Done"; 61 | --EXPECTF-- 62 | Start 63 | GearmanJob::functionName (OO): Success 64 | Done 65 | -------------------------------------------------------------------------------- /tests/gearman_job_integration_test_009.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test GearmanJob::unique() 3 | --SKIPIF-- 4 | 8 | --FILE-- 9 | 0) { 20 | // Parent. This is the worker 21 | $worker = new GearmanWorker(); 22 | $worker->addServer($host, $port); 23 | $worker->addFunction( 24 | $job_name, 25 | function($job, $data) { 26 | print "GearmanJob::unique (OO): " 27 | . (preg_match('/^(.*)-(.*)-(.*)-(.*)-(.*)$/',$job->unique()) ? 'Success' : 'Failure') 28 | . PHP_EOL; 29 | } 30 | ); 31 | 32 | $worker->work(); 33 | 34 | $worker->unregister($job_name); 35 | 36 | // Wait for child 37 | $exit_status = 0; 38 | if (pcntl_wait($exit_status) <= 0) { 39 | print "pcntl_wait exited with error" . PHP_EOL; 40 | } else if (!pcntl_wifexited($exit_status)) { 41 | print "child exited with error" . PHP_EOL; 42 | } 43 | } else { 44 | //Child. This is the client. Don't echo anything here 45 | $client = new GearmanClient(); 46 | if ($client->addServer($host, $port) !== true) { 47 | exit(1); // error 48 | }; 49 | 50 | $tasks = []; 51 | $tasks[] = $client->addTask($job_name, "normal"); 52 | $client->runTasks(); 53 | if ($client->returnCode() != GEARMAN_SUCCESS) { 54 | exit(2); // error 55 | } 56 | exit(0); 57 | } 58 | 59 | print "Done"; 60 | --EXPECTF-- 61 | Start 62 | GearmanJob::unique (OO): Success 63 | Done 64 | -------------------------------------------------------------------------------- /tests/gearman_job_integration_test_010.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test GearmanJob::workload(), GearmanJob::workloadSize() 3 | --SKIPIF-- 4 | 8 | --FILE-- 9 | 0) { 20 | // Parent. This is the worker 21 | $worker = new GearmanWorker(); 22 | $worker->addServer($host, $port); 23 | $worker->addFunction( 24 | $job_name, 25 | function($job, $data) { 26 | print "GearmanJob::workload (OO): " 27 | . $job->workload() 28 | . PHP_EOL; 29 | 30 | print "GearmanJob::workloadSize (OO): " 31 | . $job->workloadSize() 32 | . PHP_EOL; 33 | } 34 | ); 35 | 36 | $worker->work(); 37 | 38 | $worker->unregister($job_name); 39 | 40 | // Wait for child 41 | $exit_status = 0; 42 | if (pcntl_wait($exit_status) <= 0) { 43 | print "pcntl_wait exited with error" . PHP_EOL; 44 | } else if (!pcntl_wifexited($exit_status)) { 45 | print "child exited with error" . PHP_EOL; 46 | } 47 | } else { 48 | //Child. This is the client. Don't echo anything here 49 | $client = new GearmanClient(); 50 | if ($client->addServer($host, $port) !== true) { 51 | exit(1); // error 52 | }; 53 | 54 | $tasks = []; 55 | $tasks[] = $client->addTask($job_name, "normal"); 56 | $client->runTasks(); 57 | if ($client->returnCode() != GEARMAN_SUCCESS) { 58 | exit(2); // error 59 | } 60 | exit(0); 61 | } 62 | 63 | print "Done"; 64 | --EXPECTF-- 65 | Start 66 | GearmanJob::workload (OO): normal 67 | GearmanJob::workloadSize (OO): 6 68 | Done 69 | -------------------------------------------------------------------------------- /tests/gearman_task_001.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanTask::functionName, gearman_task_function_name 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer('localhost', 4730); 12 | $task = $client->addTask("GearmanTaskFunction", "normal"); 13 | print "GearmanTask::functionName (OO): " . $task->functionName() . PHP_EOL; 14 | 15 | $client2 = gearman_client_create(); 16 | gearman_client_add_server($client2, 'localhost', 4730); 17 | $task2 = gearman_client_add_task($client2, "GearmanTaskFunction2", "normal"); 18 | print "gearman_client_function_name (Procedural): " 19 | . gearman_task_function_name($task2) . PHP_EOL; 20 | 21 | print "OK"; 22 | ?> 23 | --EXPECT-- 24 | GearmanTask::functionName (OO): GearmanTaskFunction 25 | gearman_client_function_name (Procedural): GearmanTaskFunction2 26 | OK 27 | -------------------------------------------------------------------------------- /tests/gearman_task_002.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanTask::unique, gearman_task_unique 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer('localhost', 4730); 12 | $task = $client->addTask("GearmanTaskFunction", "normal"); 13 | print "GearmanTask::unique (OO): " 14 | . (preg_match('/^(.*)-(.*)-(.*)-(.*)-(.*)$/', $task->unique()) 15 | ? 'String matches' : 'String does not match') 16 | . PHP_EOL; 17 | 18 | $client2 = gearman_client_create(); 19 | gearman_client_add_server($client2, 'localhost', 4730); 20 | $task2 = gearman_client_add_task($client2, "GearmanTaskFunction2", "normal"); 21 | print "gearman_client_unique (Procedural): " 22 | . (preg_match('/^(.*)-(.*)-(.*)-(.*)-(.*)$/', gearman_task_unique($task)) 23 | ? 'String matches' : 'String does not match') 24 | . PHP_EOL; 25 | 26 | print "OK"; 27 | ?> 28 | --EXPECT-- 29 | GearmanTask::unique (OO): String matches 30 | gearman_client_unique (Procedural): String matches 31 | OK 32 | -------------------------------------------------------------------------------- /tests/gearman_task_003.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanTask::jobHandle(), gearman_task_job_handle() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer('localhost', 4730); 12 | $client->setCreatedCallback(function ($task) { 13 | print "GearmanTask::jobHandle (OO): " 14 | . (preg_match('/^(.*):(.*):(.*)$/', $task->jobHandle()) 15 | ? 'String matches' : 'String does not match') 16 | . PHP_EOL; 17 | }); 18 | 19 | $client->addTaskBackground("GearmanTaskFunction", "normal"); 20 | $client->runTasks(); 21 | 22 | 23 | $client2 = gearman_client_create(); 24 | gearman_client_add_server($client2, 'localhost', 4730); 25 | gearman_client_set_created_callback($client2, function ($task) { 26 | print "gearman_task_job_handle (Procedural): " 27 | . (preg_match('/^(.*):(.*):(.*)$/', $task->jobHandle()) 28 | ? 'String matches' : 'String does not match') 29 | . PHP_EOL; 30 | }); 31 | gearman_client_add_task_background($client2, "GearmanTaskFunction2", "normal"); 32 | gearman_client_run_tasks($client2); 33 | 34 | print "OK"; 35 | ?> 36 | --EXPECT-- 37 | GearmanTask::jobHandle (OO): String matches 38 | gearman_task_job_handle (Procedural): String matches 39 | OK 40 | -------------------------------------------------------------------------------- /tests/gearman_task_004.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanTask::is_known(), gearman_task_is_known(), 3 | GearmanTask::is_running(), gearman_task_is_running(), 4 | GearmanTask::numerator(), gearman_task_numerator(), 5 | GearmanTask::denominator(), gearman_task_denominator() 6 | --SKIPIF-- 7 | 10 | --FILE-- 11 | addServer('localhost', 4730); 15 | $client->setCreatedCallback(function ($task) { 16 | print "GearmanTask::isKnown (OO): " 17 | . ($task->isKnown() === true ? 'Success' : 'Failure') . PHP_EOL; 18 | print "GearmanTask::isRunning (OO): " 19 | . ($task->isRunning() === false ? 'Success' : 'Failure') . PHP_EOL; 20 | print "GearmanTask::taskNumerator (OO): " 21 | . ($task->taskNumerator() == 0 ? 'Success' : 'Failure') . PHP_EOL; 22 | print "GearmanTask::taskDenominator (OO): " 23 | . ($task->taskDenominator() == 0 ? 'Success' : 'Failure') . PHP_EOL; 24 | }); 25 | 26 | $client->addTaskBackground("GearmanTaskFunction", "normal"); 27 | $client->runTasks(); 28 | 29 | 30 | $client2 = gearman_client_create(); 31 | gearman_client_add_server($client2, 'localhost', 4730); 32 | gearman_client_set_created_callback($client2, function ($task) { 33 | print "gearman_task_is_known (Procedural): " 34 | . (gearman_task_is_known($task) === true ? 'Success' : 'Failure') . PHP_EOL; 35 | print "gearman_task_is_running (Procedural): " 36 | . (gearman_task_is_running($task) === false ? 'Success' : 'Failure') . PHP_EOL; 37 | print "gearman_task_numerator (Procedural): " 38 | . (gearman_task_numerator($task) == 0 ? 'Success' : 'Failure') . PHP_EOL; 39 | print "gearman_task_denominator (Procedural): " 40 | . (gearman_task_denominator($task) == 0 ? 'Success' : 'Failure') . PHP_EOL; 41 | }); 42 | gearman_client_add_task_background($client2, "GearmanTaskFunction2", "normal"); 43 | gearman_client_run_tasks($client2); 44 | 45 | print "OK"; 46 | ?> 47 | --EXPECT-- 48 | GearmanTask::isKnown (OO): Success 49 | GearmanTask::isRunning (OO): Success 50 | GearmanTask::taskNumerator (OO): Success 51 | GearmanTask::taskDenominator (OO): Success 52 | gearman_task_is_known (Procedural): Success 53 | gearman_task_is_running (Procedural): Success 54 | gearman_task_numerator (Procedural): Success 55 | gearman_task_denominator (Procedural): Success 56 | OK 57 | -------------------------------------------------------------------------------- /tests/gearman_task_005.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanTask::data(), gearman_task_data(), 3 | GearmanTask::dataSize(), gearman_task_data_size() 4 | --SKIPIF-- 5 | 8 | --FILE-- 9 | addServer('localhost', 4730); 13 | $client->setCreatedCallback(function ($task) { 14 | print "GearmanTask::data (OO): " 15 | . ($task->data() === '' ? 'Success' : 'Failure') . PHP_EOL; 16 | print "GearmanTask::dataSize (OO): " 17 | . ($task->dataSize() == 0 ? 'Success' : 'Failure') . PHP_EOL; 18 | }); 19 | 20 | $client->addTaskBackground("GearmanTaskFunction", "normal", "OO data"); 21 | $client->runTasks(); 22 | 23 | 24 | $client2 = gearman_client_create(); 25 | gearman_client_add_server($client2, 'localhost', 4730); 26 | gearman_client_set_created_callback($client2, function ($task) { 27 | print "gearman_task_data (Procedural): " 28 | . (gearman_task_data($task) === '' ? 'Success' : 'Failure') . PHP_EOL; 29 | print "gearman_task_data_size (Procedural): " 30 | . (gearman_task_data_size($task) == 0 ? 'Success' : 'Failure') . PHP_EOL; 31 | }); 32 | gearman_client_add_task_background($client2, "GearmanTaskFunction2", "normal"); 33 | gearman_client_run_tasks($client2); 34 | 35 | print "OK"; 36 | ?> 37 | --EXPECT-- 38 | GearmanTask::data (OO): Success 39 | GearmanTask::dataSize (OO): Success 40 | gearman_task_data (Procedural): Success 41 | gearman_task_data_size (Procedural): Success 42 | OK 43 | -------------------------------------------------------------------------------- /tests/gearman_task_006.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | unserialize(serialize(GearmanTask)) 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 18 | --EXPECT-- 19 | 012345 20 | OK 21 | -------------------------------------------------------------------------------- /tests/gearman_tasks_integration_test_001.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test Gearman worker methods 3 | --SKIPIF-- 4 | 8 | --FILE-- 9 | 0) { 20 | // Parent. This is the worker 21 | $worker = new GearmanWorker(); 22 | print "addServer: " . var_export($worker->addServer($host, $port), true) . PHP_EOL; 23 | print "addFunction: " . var_export( 24 | $worker->addFunction( 25 | $job_name, 26 | function($job) { 27 | print "workload: " . var_export($job->workload(), true) . PHP_EOL; 28 | } 29 | ), 30 | true 31 | ) . PHP_EOL; 32 | 33 | for ($i = 0; $i < 6; $i++) { 34 | $worker->work(); 35 | } 36 | 37 | print "unregister: " . var_export($worker->unregister($job_name), true) . PHP_EOL; 38 | 39 | // Wait for child 40 | $exit_status = 0; 41 | if (pcntl_wait($exit_status) <= 0) { 42 | print "pcntl_wait exited with error" . PHP_EOL; 43 | } else if (!pcntl_wifexited($exit_status)) { 44 | print "child exited with error" . PHP_EOL; 45 | } 46 | } else { 47 | //Child. This is the client. Don't echo anything here 48 | $client = new GearmanClient(); 49 | if ($client->addServer($host, $port) !== true) { 50 | exit(1); // error 51 | }; 52 | 53 | $tasks = []; 54 | $tasks[] = $client->addTask($job_name, "normal"); 55 | $tasks[] = $client->addTaskBackground($job_name, "normalbg"); 56 | $tasks[] = $client->addTaskHigh($job_name, 1); 57 | $tasks[] = $client->addTaskHighBackground($job_name, 2.0); 58 | $tasks[] = $client->addTaskLow($job_name, "low"); 59 | $tasks[] = $client->addTaskLowBackground($job_name, true); 60 | $client->runTasks(); 61 | if ($client->returnCode() != GEARMAN_SUCCESS) { 62 | exit(2); // error 63 | } 64 | exit(0); 65 | } 66 | 67 | print "Done"; 68 | --EXPECTF-- 69 | Start 70 | addServer: true 71 | addFunction: true 72 | workload: '2' 73 | workload: '1' 74 | workload: 'normalbg' 75 | workload: 'normal' 76 | workload: '1' 77 | workload: 'low' 78 | unregister: true 79 | Done 80 | -------------------------------------------------------------------------------- /tests/gearman_tasks_integration_test_002.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test Gearman worker methods 3 | --SKIPIF-- 4 | 10 | --FILE-- 11 | addServer($host, $port); 32 | $worker->addFunction( 33 | $job_name, 34 | function($job) { 35 | if ($job->workload() == "success") { 36 | return "done"; 37 | } else if ($job->workload() == "exception") { 38 | $job->sendException("unhandled"); 39 | return "exception"; 40 | } else if ($job->workload() == "fail") { 41 | $job->sendFail(); 42 | return "fail"; 43 | } 44 | } 45 | ); 46 | 47 | for ($i = 0; $i < count($contexts); $i++) { 48 | $worker->work(); 49 | } 50 | 51 | $worker->unregister($job_name); 52 | exit(0); 53 | } else { 54 | //Parent. This is the client. 55 | $client = new GearmanClient(); 56 | if ($client->addServer($host, $port) !== true) { 57 | exit(1); // error 58 | }; 59 | 60 | $client->setCompleteCallback(function($task) { 61 | print "Complete: " . $task->data() . PHP_EOL; 62 | }); 63 | $client->setDataCallback(function($task) { 64 | print "Data: " . $task->data() . PHP_EOL; 65 | }); 66 | $client->setExceptionCallback(function($task) { 67 | print "Exception: " . $task->data() . PHP_EOL; 68 | }); 69 | $client->setFailCallback(function($task) { 70 | print "Fail" . PHP_EOL; 71 | }); 72 | 73 | $tasks = []; 74 | foreach ($contexts as $c) { 75 | $tasks[] = $client->addTask($job_name, $c); 76 | } 77 | $client->runTasks(); 78 | print "returnCode: " . var_export($client->returnCode(), true) . PHP_EOL; 79 | print "clearCallbacks: " . var_export($client->clearCallbacks(), true) . PHP_EOL; 80 | 81 | // Wait for child 82 | $exit_status = 0; 83 | if (pcntl_wait($exit_status) <= 0) { 84 | print "pcntl_wait exited with error" . PHP_EOL; 85 | } else if (!pcntl_wifexited($exit_status)) { 86 | print "child exited with error" . PHP_EOL; 87 | } 88 | } 89 | 90 | print "Done"; 91 | --EXPECTF-- 92 | Start 93 | Exception: unhandled 94 | Fail 95 | Complete: done 96 | returnCode: 0 97 | clearCallbacks: true 98 | Done 99 | -------------------------------------------------------------------------------- /tests/gearman_worker_001.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_return_code() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | returnCode() . PHP_EOL; 10 | 11 | $worker2 = gearman_worker_create(); 12 | print "gearman_worker_return_code (Procedural): " . gearman_worker_return_code($worker2) . PHP_EOL; 13 | 14 | print "OK"; 15 | ?> 16 | --EXPECT-- 17 | GearmanWorker::returnCode (OO): 0 18 | gearman_worker_return_code (Procedural): 0 19 | OK 20 | -------------------------------------------------------------------------------- /tests/gearman_worker_002.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_return_code() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | returnCode() . PHP_EOL; 10 | 11 | $worker2 = gearman_worker_create(); 12 | print "gearman_worker_return_code (Procedural): " . gearman_worker_return_code($worker2) . PHP_EOL; 13 | 14 | print "OK"; 15 | ?> 16 | --EXPECT-- 17 | GearmanWorker::returnCode (OO): 0 18 | gearman_worker_return_code (Procedural): 0 19 | OK 20 | -------------------------------------------------------------------------------- /tests/gearman_worker_003.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_error() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | error() . "'\n"; 10 | 11 | $worker2 = gearman_worker_create(); 12 | print "gearman_worker_error (Procedural): '" . gearman_worker_error($worker2) . "'\n"; 13 | 14 | print "OK"; 15 | ?> 16 | --EXPECT-- 17 | GearmanWorker::error (OO): '' 18 | gearman_worker_error (Procedural): '' 19 | OK 20 | -------------------------------------------------------------------------------- /tests/gearman_worker_004.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_errno() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | getErrno() . PHP_EOL; 10 | 11 | $worker2 = gearman_worker_create(); 12 | print "gearman_worker_errno (Procedural): " . gearman_worker_errno($worker2) . PHP_EOL; 13 | 14 | print "OK"; 15 | ?> 16 | --EXPECT-- 17 | GearmanWorker::getErrno (OO): 0 18 | gearman_worker_errno (Procedural): 0 19 | OK 20 | -------------------------------------------------------------------------------- /tests/gearman_worker_005.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_options() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | options() . PHP_EOL; 10 | 11 | $worker2 = gearman_worker_create(); 12 | print "gearman_worker_options (Procedural): " . gearman_worker_options($worker2) . PHP_EOL; 13 | 14 | print "OK"; 15 | ?> 16 | --EXPECT-- 17 | GearmanWorker::options (OO): 644 18 | gearman_worker_options (Procedural): 644 19 | OK 20 | -------------------------------------------------------------------------------- /tests/gearman_worker_006.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_set_options() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | setOptions(GEARMAN_WORKER_NON_BLOCKING) === true ? 'Success' : 'Failure') . PHP_EOL; 10 | print "GearmanWorker::options() (OO): " . $worker->options() . PHP_EOL; 11 | 12 | $worker2 = gearman_worker_create(); 13 | print "gearman_worker_set_options (Procedural): " . (gearman_worker_set_options($worker, GEARMAN_WORKER_NON_BLOCKING) === true ? 'Success' : 'Failure') . PHP_EOL; 14 | print "gearman_worker_options() (Procedural): " . gearman_worker_options($worker) . PHP_EOL; 15 | 16 | print "OK"; 17 | ?> 18 | --EXPECT-- 19 | GearmanWorker::setOptions (OO): Success 20 | GearmanWorker::options() (OO): 6 21 | gearman_worker_set_options (Procedural): Success 22 | gearman_worker_options() (Procedural): 6 23 | OK 24 | -------------------------------------------------------------------------------- /tests/gearman_worker_007.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_add_options() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | addOptions(GEARMAN_WORKER_NON_BLOCKING) === true ? 'Success' : 'Failure') . PHP_EOL; 10 | print "GearmanWorker::options() (OO): " . $worker->options() . PHP_EOL; 11 | 12 | $worker2 = gearman_worker_create(); 13 | print "gearman_worker_add_options() (Procedural): " . (gearman_worker_add_options($worker2, GEARMAN_WORKER_NON_BLOCKING) === true ? 'Success' : 'Failure') . PHP_EOL; 14 | print "gearman_worker_options() (OO): " . gearman_worker_options($worker2) . PHP_EOL; 15 | 16 | print "OK"; 17 | ?> 18 | --EXPECT-- 19 | GearmanWorker::addOptions() (OO): Success 20 | GearmanWorker::options() (OO): 646 21 | gearman_worker_add_options() (Procedural): Success 22 | gearman_worker_options() (OO): 646 23 | OK 24 | -------------------------------------------------------------------------------- /tests/gearman_worker_008.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_timeout(), gearman_worker_set_timeout() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | timeout() . PHP_EOL; 12 | print "GearmanWorker::setTimeout() (OO): " .($worker->setTimeout($timeout) === true ? "Success" : "Failure") . PHP_EOL; 13 | print "GearmanWorker::timeout() (OO, after setting): " . $worker->timeout() . PHP_EOL; 14 | 15 | $worker2 = gearman_worker_create(); 16 | print "gearman_worker_timeout() (Procedural, before setting): " . gearman_worker_timeout($worker2) . PHP_EOL; 17 | print "gearman_worker_set_timeout() (Procedural): " . (gearman_worker_set_timeout($worker2, $timeout) === true ? "Success" : "Failure") . PHP_EOL; 18 | print "gearman_worker_timeout() (Procedural, after setting): " . gearman_worker_timeout($worker2) . PHP_EOL; 19 | 20 | print "OK"; 21 | ?> 22 | --EXPECT-- 23 | GearmanWorker::timeout() (OO, before setting): -1 24 | GearmanWorker::setTimeout() (OO): Success 25 | GearmanWorker::timeout() (OO, after setting): 5 26 | gearman_worker_timeout() (Procedural, before setting): -1 27 | gearman_worker_set_timeout() (Procedural): Success 28 | gearman_worker_timeout() (Procedural, after setting): 5 29 | OK 30 | -------------------------------------------------------------------------------- /tests/gearman_worker_009.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_set_id() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | setId($id) === true ? "Success" : "Failure").PHP_EOL; 12 | 13 | $worker2 = gearman_worker_create(); 14 | print "gearman_worker_set_id() (Procedural): " . (gearman_worker_set_id($worker, $id) === true ? "Success" : "Failure").PHP_EOL; 15 | 16 | print "OK"; 17 | ?> 18 | --EXPECT-- 19 | GearmanWorker::setId() (OO): Success 20 | gearman_worker_set_id() (Procedural): Success 21 | OK 22 | -------------------------------------------------------------------------------- /tests/gearman_worker_010.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_add_server(), gearman_worker_add_servers() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer($host, $port) === true ? "Success" : "Failure").PHP_EOL; 15 | print "GearmanWorker::addServers() (OO): " . ($worker->addServers("$host:$port") === true ? "Success" : "Failure").PHP_EOL; 16 | 17 | $worker2 = gearman_worker_create(); 18 | print "gearman_worker_add_server() (Procedural): " . (gearman_worker_add_server($worker, $host, $port) === true ? "Success" : "Failure").PHP_EOL; 19 | print "gearman_worker_add_servers() (Procedural): " . (gearman_worker_add_servers($worker, "$host:$port") === true ? "Success" : "Failure").PHP_EOL; 20 | 21 | print "OK"; 22 | ?> 23 | --EXPECT-- 24 | GearmanWorker::addServer() (OO):Success 25 | GearmanWorker::addServers() (OO): Success 26 | gearman_worker_add_server() (Procedural): Success 27 | gearman_worker_add_servers() (Procedural): Success 28 | OK 29 | -------------------------------------------------------------------------------- /tests/gearman_worker_011.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_wait() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer($host, $port); 15 | $worker->setTimeout(1); 16 | /* 17 | Still need to figure out how to test wait here... 18 | $worker->wait(); 19 | */ 20 | 21 | print "OK"; 22 | ?> 23 | --EXPECT-- 24 | OK 25 | -------------------------------------------------------------------------------- /tests/gearman_worker_012.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_register(), gearman_worker_unregister(), gearman_worker_unregister_all() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer($host, $port); 16 | 17 | print "GearmanWorker::register() (OO): " . ($worker->register($registered_func) === true ? "Success" : "Failure") . PHP_EOL; 18 | print "GearmanWorker::unregister() (OO): " . ($worker->unregister($registered_func) === true ? "Success" : "Failure") . PHP_EOL; 19 | print "GearmanWorker::register() (OO): " . ($worker->register($registered_func) === true ? "Success" : "Failure") . PHP_EOL; 20 | print "GearmanWorker::unregisterAll() (OO): " . ($worker->unregisterAll() === true ? "Success" : "Failure") . PHP_EOL; 21 | 22 | $worker2 = gearman_worker_create(); 23 | gearman_worker_add_server($worker, $host, $port); 24 | 25 | print "gearman_worker_register() (Procedural): " . (gearman_worker_register($worker, $registered_func) === true ? "Success" : "Failure") . PHP_EOL; 26 | print "gearman_worker_unregister() (Procedural): " . (gearman_worker_unregister($worker, $registered_func) === true ? "Success" : "Failure") . PHP_EOL; 27 | print "gearman_worker_register() (Procedural): " . (gearman_worker_register($worker, $registered_func) === true ? "Success" : "Failure") . PHP_EOL; 28 | print "gearman_worker_unregister_all() (Procedural): " . (gearman_worker_unregister_all($worker) === true ? "Success" : "Failure") . PHP_EOL; 29 | 30 | print "OK"; 31 | 32 | function registered_function() { 33 | print "I'm in ".__FUNCTION__.PHP_EOL; 34 | } 35 | ?> 36 | --EXPECT-- 37 | GearmanWorker::register() (OO): Success 38 | GearmanWorker::unregister() (OO): Success 39 | GearmanWorker::register() (OO): Success 40 | GearmanWorker::unregisterAll() (OO): Success 41 | gearman_worker_register() (Procedural): Success 42 | gearman_worker_unregister() (Procedural): Success 43 | gearman_worker_register() (Procedural): Success 44 | gearman_worker_unregister_all() (Procedural): Success 45 | OK 46 | 47 | -------------------------------------------------------------------------------- /tests/gearman_worker_013.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_add_function() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer($host, $port); 17 | print "GearmanWorker::addFunction() (OO): ".($worker->addFunction($job, $func) ? "Success" : "Failure").PHP_EOL; 18 | 19 | $worker2 = gearman_worker_create(); 20 | gearman_worker_add_server($worker, $host, $port); 21 | print "gearman_worker_add_function() (Procedural): ".(gearman_worker_add_function($worker, $job, $func) ? "Success" : "Failure").PHP_EOL; 22 | 23 | print "OK"; 24 | 25 | function do_work() { 26 | print "I'm in ".__FUNCTION__.PHP_EOL; 27 | } 28 | ?> 29 | --EXPECT-- 30 | GearmanWorker::addFunction() (OO): Success 31 | gearman_worker_add_function() (Procedural): Success 32 | OK 33 | -------------------------------------------------------------------------------- /tests/gearman_worker_014.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_work() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer($host, $port); 19 | $handle = $client->doBackground($job, $workload); 20 | $client->doBackground($job, $workload); 21 | 22 | print "GearmanWorker::doBackground() (OO): ".(preg_match('/^H:'.gethostname().':\d+$/', $handle) === 1? 'Success' : 'Failure').PHP_EOL; 23 | 24 | $worker = new GearmanWorker(); 25 | $worker->addServer($host, $port); 26 | $worker->addFunction($job, $func); 27 | 28 | print "GearmanWorker::work() (OO): ".($worker->work() === true ? 'Success' : 'Failure') . PHP_EOL; 29 | 30 | $worker2 = gearman_worker_create(); 31 | gearman_worker_add_server($worker, $host, $port); 32 | gearman_worker_add_function($worker, $job, $func); 33 | 34 | print "gearman_worker_work() (Procedural): ".(gearman_worker_work($worker) === true ? 'Success' : 'Failure') . PHP_EOL; 35 | 36 | print "OK"; 37 | 38 | function do_work($job) { 39 | print "Calling function ".__FUNCTION__.PHP_EOL; 40 | } 41 | ?> 42 | --EXPECT-- 43 | GearmanWorker::doBackground() (OO): Success 44 | Calling function do_work 45 | GearmanWorker::work() (OO): Success 46 | Calling function do_work 47 | gearman_worker_work() (Procedural): Success 48 | OK 49 | -------------------------------------------------------------------------------- /tests/gearman_worker_015.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | gearman_worker_ping() 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer($host, $port); 17 | echo "GearmanWorker::server() (OO): ".($worker->ping($json_workload) ? "Success" : "Failure").PHP_EOL; 18 | 19 | $worker2 = gearman_worker_create(); 20 | gearman_worker_add_server($worker, $host, $port); 21 | echo "gearman_worker_work() (Procedural): ".($worker->ping($json_workload) ? "Success" : "Failure").PHP_EOL; 22 | 23 | print "OK"; 24 | ?> 25 | --EXPECT-- 26 | GearmanWorker::server() (OO): Success 27 | gearman_worker_work() (Procedural): Success 28 | OK 29 | -------------------------------------------------------------------------------- /tests/gearman_worker_016.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanWorker::addFunction(), context param 3 | --SKIPIF-- 4 | 7 | --FILE-- 8 | addServer($host, $port); 16 | $handle = $client->doBackground($job, $workload); 17 | $client->doBackground($job, $workload); 18 | $client->doBackground($job, $workload); 19 | $client->doBackground($job, $workload); 20 | 21 | print "GearmanWorker::doBackground() (OO): ".(preg_match('/^H:'.gethostname().':\d+$/', $handle) === 1? 'Success' : 'Failure').PHP_EOL; 22 | 23 | $worker = new TestWorker(); 24 | print "GearmanWorker::work() (OO, array ctx): " .($worker->work() === true ? 'Success' : 'Failure') . PHP_EOL; 25 | print "GearmanWorker::work() (OO, array ctx): " .($worker->work() === true ? 'Success' : 'Failure') . PHP_EOL; 26 | print "GearmanWorker::work() (OO, array ctx): " .($worker->work() === true ? 'Success' : 'Failure') . PHP_EOL; 27 | 28 | 29 | print "OK"; 30 | 31 | class TestWorker extends \GearmanWorker 32 | { 33 | public function __construct() 34 | { 35 | global $job; 36 | parent::__construct(); 37 | $this->addServer(); 38 | $this->addFunction($job, [$this, 'test'], ['firstArg' => 'firstValue']); 39 | } 40 | 41 | public function test($job, $context) 42 | { 43 | echo "Starting job {$job->workload()}". PHP_EOL; 44 | $firstArg = $context['firstArg']; 45 | echo "FirstArg is $firstArg" . PHP_EOL; 46 | } 47 | } 48 | 49 | ?> 50 | --EXPECT-- 51 | GearmanWorker::doBackground() (OO): Success 52 | Starting job {"workload":"test"} 53 | FirstArg is firstValue 54 | GearmanWorker::work() (OO, array ctx): Success 55 | Starting job {"workload":"test"} 56 | FirstArg is firstValue 57 | GearmanWorker::work() (OO, array ctx): Success 58 | Starting job {"workload":"test"} 59 | FirstArg is firstValue 60 | GearmanWorker::work() (OO, array ctx): Success 61 | OK 62 | -------------------------------------------------------------------------------- /tests/gearman_worker_017.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | unserialize(serialize(GearmanWorker)) 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 19 | --EXPECT-- 20 | 012345 21 | OK 22 | -------------------------------------------------------------------------------- /tests/gearman_worker_018.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | GearmanWorker::enableExceptionHandler(),gearman_worker_enable_exception_handler() 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | addServers('localhost:4731,localhost', false); 13 | 14 | // Enabling the exception handler, which will attempt to connect to 15 | // the server and in doing so throw an exception since we can't 16 | // connect to a server that doesn't exist 17 | try { 18 | $worker->enableExceptionHandler(); 19 | } catch (Exception $e) { 20 | print "Exception 1 caught: " . $e->getMessage() . PHP_EOL; 21 | } 22 | 23 | // Test 2: GearmanWorker::addServers, Exception callback enabled (by default). 24 | // Here, we don't give the second param, so the exception handler is enabled 25 | // upon calling addServers instead of later in enableExceptionHandler 26 | $worker2 = new GearmanWorker(); 27 | 28 | try { 29 | $worker2->addServers('localhost:4731,localhost'); 30 | } catch (Exception $e) { 31 | print "Exception 2 caught: " . $e->getMessage() . PHP_EOL; 32 | } 33 | 34 | // Test 3: GearmanWorker::addServers, Also, when we explicitly enable in addServers 35 | $worker3 = new GearmanWorker(); 36 | 37 | try { 38 | $worker3->addServers('localhost:4731,localhost', true); 39 | } catch (Exception $e) { 40 | print "Exception 3 caught: " . $e->getMessage() . PHP_EOL; 41 | } 42 | 43 | // Now, do the same as above but with addServer (singular) 44 | // Test 4: GearmanWorker::addServer, Exception callback disabled 45 | $worker4 = new GearmanWorker(); 46 | $worker4->addServer('localhost', 4731, false); 47 | 48 | try { 49 | $worker4->enableExceptionHandler(); 50 | } catch (Exception $e) { 51 | print "Exception 4 caught: " . $e->getMessage() . PHP_EOL; 52 | } 53 | 54 | // Test 5: GearmanWorker::addServer, default 55 | $worker5 = new GearmanWorker(); 56 | 57 | try { 58 | $worker5->addServer('localhost', 4731); 59 | } catch (Exception $e) { 60 | print "Exception 5 caught: " . $e->getMessage() . PHP_EOL; 61 | } 62 | 63 | // Test 6: GearmanWorker::addServer, explicitly set enableExceptionHandler 64 | $worker6 = new GearmanWorker(); 65 | 66 | try { 67 | $worker6->addServer('localhost', 4731, true); 68 | } catch (Exception $e) { 69 | print "Exception 6 caught: " . $e->getMessage() . PHP_EOL; 70 | } 71 | 72 | // Test 7: GearmanWorker::addServer, default (positive case) 73 | $worker7 = new GearmanWorker(); 74 | $worker7->addServer('localhost', 4730); 75 | 76 | // Test 8: GearmanWorker::addServer, explicitly set enableExceptionHandler (positive case) 77 | $worker8 = new GearmanWorker(); 78 | $worker8->addServer('localhost', 4730, true); 79 | 80 | // Test 9: GearmanWorker::addServer, call enableExceptionHandler (positive case) 81 | $worker9 = new GearmanWorker(); 82 | $worker9->addServer('localhost', 4730, false); 83 | $worker9->enableExceptionHandler(); 84 | 85 | print "OK"; 86 | ?> 87 | --EXPECTF-- 88 | Exception 1 caught: Failed to set exception option 89 | Exception 2 caught: Failed to set exception option 90 | Exception 3 caught: Failed to set exception option 91 | Exception 4 caught: Failed to set exception option 92 | Exception 5 caught: Failed to set exception option 93 | Exception 6 caught: Failed to set exception option 94 | OK 95 | -------------------------------------------------------------------------------- /tests/gearman_worker_integration_test_001.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test Gearman worker methods 3 | --SKIPIF-- 4 | 8 | --FILE-- 9 | 0) { 18 | // Parent. This is the worker 19 | $worker = new GearmanWorker(); 20 | print "addServer: " . var_export($worker->addServer($host, $port), true) . PHP_EOL; 21 | print "setTimeout: " . var_export($worker->setTimeout(100), true) . PHP_EOL; 22 | print "register: " . var_export($worker->register($job_name, 5), true) . PHP_EOL; 23 | print "register: " . var_export($worker->register($job_name . "1", 5), true) . PHP_EOL; 24 | print "register: " . var_export($worker->register($job_name . "2", 5), true) . PHP_EOL; 25 | print "register: " . var_export($worker->register($job_name . "3", 5), true) . PHP_EOL; 26 | print "addFunction: " . var_export( 27 | $worker->addFunction( 28 | $job_name, 29 | function($job) { 30 | print "workload: " . var_export($job->workload(), true) . PHP_EOL; 31 | } 32 | ), true 33 | ) . PHP_EOL; 34 | print "work: " . var_export($worker->work(), true) . PHP_EOL; 35 | print "unregister: " . var_export($worker->unregister($job_name), true) . PHP_EOL; 36 | print "unregisterAll: " . var_export($worker->unregisterAll(), true) . PHP_EOL; 37 | 38 | // Wait for child 39 | $exit_status = 0; 40 | if (pcntl_wait($exit_status) <= 0) { 41 | print "pcntl_wait exited with error" . PHP_EOL; 42 | } else if (!pcntl_wifexited($exit_status)) { 43 | print "child exited with error" . PHP_EOL; 44 | } 45 | } else { 46 | //Child. This is the client. Don't echo anything here 47 | $client = new GearmanClient(); 48 | if ($client->addServer($host, $port) !== true) { 49 | exit(1); // error 50 | }; 51 | $client->doBackground($job_name, "nothing"); 52 | if ($client->returnCode() != GEARMAN_SUCCESS) { 53 | exit(2); // error 54 | } 55 | exit(0); 56 | } 57 | 58 | print "Done"; 59 | --EXPECTF-- 60 | Start 61 | addServer: true 62 | setTimeout: true 63 | register: true 64 | register: true 65 | register: true 66 | register: true 67 | addFunction: true 68 | workload: 'nothing' 69 | work: true 70 | unregister: true 71 | unregisterAll: true 72 | Done 73 | -------------------------------------------------------------------------------- /tests/skipif.inc: -------------------------------------------------------------------------------- 1 | = {$min_ver}, found version {$version}\n"); 9 | } 10 | 11 | 12 | --------------------------------------------------------------------------------