45
45
juju add-relation gearman-server gearman-worker
51
ssh into one ( or each ) of your workers and type the following:
53
gearman -h <gearman-job-server-fqdn> -w -f wc -- wc -l
55
ssh into any other instance/machine that has access to the gearman-job-server and type:
57
gearman -h <gearman-job-server-fqdn> -f wc < /etc/passwd
59
This should send a request for the number of lines in /etc/passwd to the gearman-job-server who will in turn ask the worker. The worker will pass the answer back through the job-server to the client. If it returns a number, it works.
63
Consider the following php test. Ssh into one ( or each ) of your workers and enter the following php code:
69
# Create our worker object.
70
$gmworker= new GearmanWorker();
72
# Add job server (gearman-job-server).
73
$gmworker->addServer("gearman-job-server");
75
# Register function "reverse" with the server. Change the worker function to
76
# "reverse_fn_fast" for a faster worker with no output.
77
$gmworker->addFunction("reverse", "reverse_fn");
79
print "Waiting for job...\n";
80
while($gmworker->work())
82
if ($gmworker->returnCode() != GEARMAN_SUCCESS)
84
echo "return_code: " . $gmworker->returnCode() . "\n";
89
function reverse_fn($job)
91
echo "Received job: " . $job->handle() . "\n";
93
$workload = $job->workload();
94
$workload_size = $job->workloadSize();
96
echo "Workload: $workload ($workload_size)\n";
98
# This status loop is not needed, just showing how it works
99
for ($x= 0; $x < $workload_size; $x++)
101
echo "Sending status: " . ($x + 1) . "/$workload_size complete\n";
102
$job->sendStatus($x, $workload_size);
106
$result= strrev($workload);
107
echo "Result: $result\n";
109
# Return what we want to send back to the client.
113
# A much simpler and less verbose version of the above function would be:
114
function reverse_fn_fast($job)
116
return strrev($job->workload());
121
Save the above code somewhere in your worker instance. For the sake of this example, we'll save it on /tmp/reverse_worker.php. Run the following:
123
php /tmp/reverse_worker.php
125
The above will connect to the gearman-job-server and wait for a job. This is a syncronous job.
127
From a machine with access to the gearman-job-server, run the following code:
131
# Create our client object.
132
$gmclient= new GearmanClient();
134
# Add job server (gearman-job-server).
135
$gmworker->addServer("gearman-job-server");
137
echo "Sending job\n";
142
$result = $gmclient->do("reverse", "Hello!");
144
# Check for various return packets and errors.
145
switch($gmclient->returnCode())
147
case GEARMAN_WORK_DATA:
148
echo "Data: $result\n";
150
case GEARMAN_WORK_STATUS:
151
list($numerator, $denominator)= $gmclient->doStatus();
152
echo "Status: $numerator/$denominator complete\n";
154
case GEARMAN_WORK_FAIL:
157
case GEARMAN_SUCCESS:
160
echo "RET: " . $gmclient->returnCode() . "\n";
164
while($gmclient->returnCode() != GEARMAN_SUCCESS);
168
Save the above code as /tmp/reverse_client.php and run:
170
php reverse_client.php
172
The above example will output something similar to:
174
php reverse_worker.php
177
Received job: H:gearman-job-server:36
179
Sending status: 1/6 complete
180
Sending status: 2/6 complete
181
Sending status: 3/6 complete
182
Sending status: 4/6 complete
183
Sending status: 5/6 complete
184
Sending status: 6/6 complete
187
php reverse_client.php
200
On a worker ( or all of them ) save the following file as /tmp/reverse_worker.py:
202
gm_worker = gearman.GearmanWorker(['localhost:4730'])
204
# See gearman/job.py to see attributes on the GearmanJob
205
# Send back a reversed version of the 'data' string
206
def task_listener_reverse(gearman_worker, gearman_job):
207
return reversed(gearman_job.data)
209
# gm_worker.set_client_id is optional
210
gm_worker.set_client_id('your_worker_client_id_name')
211
gm_worker.register_task('reverse', task_listener_reverse)
213
# Enter our work loop and call gm_worker.after_poll() after each time we timeout/see socket activity
218
python /tmp/reverse_worker.py
220
On a client machine save the following code as /tmp/reverse_client.py:
222
def check_request_status(job_request):
223
if job_request.complete:
224
print "Job %s finished! Result: %s - %s" % (job_request.job.unique, job_request.state, job_request.result)
225
elif job_request.timed_out:
226
print "Job %s timed out!" % job_request.unique
227
elif job_request.state == JOB_UNKNOWN:
228
print "Job %s connection failed!" % job_request.unique
230
gm_client = gearman.GearmanClient(['localhost:4730', 'otherhost:4730'])
232
# See gearman/job.py to see attributes on the GearmanJobRequest
233
# Defaults to PRIORITY_NONE, background=False (synchronous task), wait_until_complete=True
234
completed_job_request = gm_client.submit_job("task_name", "arbitrary binary data")
235
check_request_status(completed_job_request)
240
python /tmp/reverse_client.py