1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
/*
* Gearman PHP Extension
*
* Copyright (C) 2008 James M. Luedke <contact@jamesluedke.com>,
* Eric Day <eday@oddments.org>
* All rights reserved.
*
* Use and distribution licensed under the PHP license. See
* the LICENSE file in this directory for full text.
*/
#include "php_gearman_job.h"
gearman_job_obj *gearman_job_fetch_object(zend_object *obj) {
return (gearman_job_obj *)((char*)(obj) - XtOffsetOf(gearman_job_obj, std));
}
/* {{{ proto object GearmanJob::__destruct()
cleans up GearmanJob object */
PHP_METHOD(GearmanJob, __destruct) {
gearman_job_obj *intern = Z_GEARMAN_JOB_P(getThis());
if (!intern) {
return;
}
if (intern->flags & GEARMAN_JOB_OBJ_CREATED) {
gearman_job_free(intern->job);
}
zend_object_std_dtor(&intern->std);
}
zend_object *gearman_job_obj_new(zend_class_entry *ce) {
gearman_job_obj *intern = ecalloc(1,
sizeof(gearman_job_obj) +
zend_object_properties_size(ce));
zend_object_std_init(&(intern->std), ce);
object_properties_init(&intern->std, ce);
intern->std.handlers = &gearman_job_obj_handlers;
return &intern->std;
}
/* {{{ proto int gearman_job_return_code()
get last gearman_return_t */
PHP_FUNCTION(gearman_job_return_code) {
gearman_job_obj *obj;
zval *zobj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_JOB_P(zobj);
RETURN_LONG(obj->ret);
}
/* }}} */
/* {{{ proto bool gearman_job_set_return(int gearman_return_t)
This function will set a return value of a job */
PHP_FUNCTION(gearman_job_set_return) {
zval *zobj;
gearman_job_obj *obj;
gearman_return_t ret;
zend_long ret_val;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &zobj, gearman_job_ce, &ret_val) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_JOB_P(zobj);
ret = ret_val;
/* make sure its a valid gearman_return_t */
if (ret < GEARMAN_SUCCESS || ret > GEARMAN_MAX_RETURN) {
php_error_docref(NULL, E_WARNING,
"Invalid gearman_return_t: %d", ret);
RETURN_FALSE;
}
obj->ret = ret;
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_job_send_data(object job, string data)
Send data for a running job. */
PHP_FUNCTION(gearman_job_send_data) {
zval *zobj;
gearman_job_obj *obj;
char *data;
size_t data_len;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_job_ce,
&data, &data_len) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_JOB_P(zobj);
/* make sure worker initialized a job */
if (obj->job == NULL) {
RETURN_FALSE;
}
obj->ret = gearman_job_send_data(obj->job, data, data_len);
if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_job_error(obj->job));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_job_send_warning(object job, string warning)
Send warning for a running job. */
PHP_FUNCTION(gearman_job_send_warning) {
zval *zobj;
gearman_job_obj *obj;
char *warning = NULL;
size_t warning_len = 0;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_job_ce,
&warning, &warning_len) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_JOB_P(zobj);
/* make sure worker initialized a job */
if (obj->job == NULL) {
RETURN_FALSE;
}
obj->ret = gearman_job_send_warning(obj->job, (void *) warning,
(size_t) warning_len);
if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_job_error(obj->job));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_job_send_status(object job, int numerator, int denominator)
Send status information for a running job. */
PHP_FUNCTION(gearman_job_send_status) {
zval *zobj;
gearman_job_obj *obj;
zend_long numerator, denominator;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll", &zobj, gearman_job_ce,
&numerator, &denominator) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_JOB_P(zobj);
obj->ret = gearman_job_send_status(obj->job, (uint32_t)numerator,
(uint32_t)denominator);
if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_job_error(obj->job));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_job_send_complete(object job, string result)
Send result and complete status for a job. */
PHP_FUNCTION(gearman_job_send_complete) {
zval *zobj;
gearman_job_obj *obj;
char *result;
size_t result_len;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_job_ce,
&result, &result_len) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_JOB_P(zobj);
obj->ret = gearman_job_send_complete(obj->job, result, result_len);
if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_job_error(obj->job));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_job_send_exception(object job, string exception)
Send exception for a running job. */
PHP_FUNCTION(gearman_job_send_exception) {
zval *zobj;
gearman_job_obj *obj;
char *exception;
size_t exception_len;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &zobj, gearman_job_ce,
&exception, &exception_len) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_JOB_P(zobj);
obj->ret= gearman_job_send_exception(obj->job, exception, exception_len);
if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_job_error(obj->job));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool gearman_job_send_fail(object job)
Send fail status for a job. */
PHP_FUNCTION(gearman_job_send_fail) {
zval *zobj;
gearman_job_obj *obj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) {
RETURN_FALSE;
}
obj = Z_GEARMAN_JOB_P(zobj);
obj->ret = gearman_job_send_fail(obj->job);
if (obj->ret != GEARMAN_SUCCESS && obj->ret != GEARMAN_IO_WAIT) {
php_error_docref(NULL, E_WARNING, "%s",
gearman_job_error(obj->job));
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto string gearman_job_handle(object job)
Return job handle. */
PHP_FUNCTION(gearman_job_handle) {
zval *zobj;
gearman_job_obj *obj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_JOB_P(zobj);
/* make sure worker initialized a job */
if (obj->job == NULL) {
RETURN_FALSE;
}
RETURN_STRING((char *)gearman_job_handle(obj->job))
}
/* }}} */
/* {{{ proto string gearman_job_function_name(object job)
Return the function name associated with a job. */
PHP_FUNCTION(gearman_job_function_name) {
zval *zobj;
gearman_job_obj *obj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_JOB_P(zobj);
/* make sure worker initialized a job */
if (obj->job == NULL) {
RETURN_FALSE;
}
RETURN_STRING((char *)gearman_job_function_name(obj->job))
}
/* }}} */
/* {{{ proto string gearman_job_unique(object job)
Get the unique ID associated with a job. */
PHP_FUNCTION(gearman_job_unique) {
zval *zobj;
gearman_job_obj *obj;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_JOB_P(zobj);
/* make sure worker initialized a job */
if (obj->job == NULL) {
RETURN_FALSE;
}
RETURN_STRING((char *)gearman_job_unique(obj->job))
}
/* }}} */
/* {{{ proto string gearman_job_workload(object job)
Returns the workload for a job. */
PHP_FUNCTION(gearman_job_workload) {
zval *zobj;
gearman_job_obj *obj;
const uint8_t *workload;
size_t workload_len;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_JOB_P(zobj);
workload = gearman_job_workload(obj->job);
workload_len = gearman_job_workload_size(obj->job);
RETURN_STRINGL((char *)workload, (long) workload_len);
}
/* }}} */
/* {{{ proto int gearman_job_workload_size(object job)
Returns size of the workload for a job. */
PHP_FUNCTION(gearman_job_workload_size) {
zval *zobj;
gearman_job_obj *obj;
size_t workload_len;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &zobj, gearman_job_ce) == FAILURE) {
RETURN_NULL();
}
obj = Z_GEARMAN_JOB_P(zobj);
workload_len = gearman_job_workload_size(obj->job);
RETURN_LONG((long) workload_len);
}
/* }}} */
|