1
/* Copyright (C) 2009 Sun Microsystems
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
18
#include "function_map.h"
24
static void *_do_malloc(size_t size, void *arg)
26
Item_func_gman_do *item= (Item_func_gman_do *)arg;
27
return item->realloc(size);
31
Item_func_gman_do::~Item_func_gman_do()
33
if (options & GMAN_DO_OPTIONS_CLIENT)
34
gearman_client_free(&client);
37
String *Item_func_gman_do::val_str(String *str)
46
char job_handle[GEARMAN_JOB_HANDLE_SIZE];
48
if (arg_count < 1 || arg_count > 3 || !(function= args[0]->val_str(str)))
54
if (arg_count > 1 && (res= args[1]->val_str(str)) != NULL)
57
workload_size= res->length();
65
if (arg_count == 3 && (res= args[2]->val_str(str)) != NULL)
70
if (!(options & GMAN_DO_OPTIONS_CLIENT))
72
if (!GetFunctionMap().get(string(function->ptr()), &client))
78
gearman_client_set_workload_malloc_fn(&client, _do_malloc, this);
79
options= (gman_do_options_t)(options | GMAN_DO_OPTIONS_CLIENT);
82
if (options & GMAN_DO_OPTIONS_BACKGROUND)
84
if (options & GMAN_DO_OPTIONS_HIGH)
86
ret= gearman_client_do_high_background(&client, function->ptr(), unique,
87
workload, workload_size,
90
else if (options & GMAN_DO_OPTIONS_LOW)
92
ret= gearman_client_do_low_background(&client, function->ptr(), unique,
93
workload, workload_size,
98
ret= gearman_client_do_background(&client, function->ptr(), unique,
99
workload, workload_size, job_handle);
102
if (ret == GEARMAN_SUCCESS)
104
result_size= strlen(job_handle);
105
buffer.realloc(result_size);
106
buffer.length(result_size);
107
memcpy(buffer.ptr(), job_handle, result_size);
112
if (options & GMAN_DO_OPTIONS_HIGH)
114
(void) gearman_client_do_high(&client, function->ptr(), unique, workload,
115
workload_size, &result_size, &ret);
117
else if (options & GMAN_DO_OPTIONS_LOW)
119
(void) gearman_client_do_low(&client, function->ptr(), unique, workload,
120
workload_size, &result_size, &ret);
124
(void) gearman_client_do(&client, function->ptr(), unique, workload,
125
workload_size, &result_size, &ret);
129
if (ret != GEARMAN_SUCCESS)
139
void *Item_func_gman_do::realloc(size_t size)
141
buffer.realloc(size);