~yolanda.robla/charms/precise/gearman-server/trunk

« back to all changes in this revision

Viewing changes to README

  • Committer: Juan L. Negron
  • Date: 2012-03-09 17:27:02 UTC
  • Revision ID: juan.negron@canonical.com-20120309172702-wse8u3cjwyz87jjz
Adding Testing section to README

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
 
45
45
    juju add-relation gearman-server gearman-worker
46
46
 
 
47
Testing
 
48
-------
 
49
Simple test
 
50
 
 
51
ssh into one ( or each ) of your workers and type the following:
 
52
 
 
53
    gearman -h <gearman-job-server-fqdn> -w -f wc -- wc -l
 
54
 
 
55
ssh into any other instance/machine that has access to the gearman-job-server and type:
 
56
 
 
57
    gearman -h <gearman-job-server-fqdn> -f wc < /etc/passwd
 
58
 
 
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.
 
60
 
 
61
Simple PHP test
 
62
 
 
63
Consider the following php test. Ssh into one ( or each ) of your workers and enter the following php code:
 
64
 
 
65
<?php
 
66
 
 
67
echo "Starting\n";
 
68
 
 
69
# Create our worker object.
 
70
$gmworker= new GearmanWorker();
 
71
 
 
72
# Add job server (gearman-job-server).
 
73
$gmworker->addServer("gearman-job-server");
 
74
 
 
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");
 
78
 
 
79
print "Waiting for job...\n";
 
80
while($gmworker->work())
 
81
{
 
82
  if ($gmworker->returnCode() != GEARMAN_SUCCESS)
 
83
  {
 
84
    echo "return_code: " . $gmworker->returnCode() . "\n";
 
85
    break;
 
86
  }
 
87
}
 
88
 
 
89
function reverse_fn($job)
 
90
{
 
91
  echo "Received job: " . $job->handle() . "\n";
 
92
 
 
93
  $workload = $job->workload();
 
94
  $workload_size = $job->workloadSize();
 
95
 
 
96
  echo "Workload: $workload ($workload_size)\n";
 
97
 
 
98
  # This status loop is not needed, just showing how it works
 
99
  for ($x= 0; $x < $workload_size; $x++)
 
100
  {
 
101
    echo "Sending status: " . ($x + 1) . "/$workload_size complete\n";
 
102
    $job->sendStatus($x, $workload_size);
 
103
    sleep(1);
 
104
  }
 
105
 
 
106
  $result= strrev($workload);
 
107
  echo "Result: $result\n";
 
108
 
 
109
  # Return what we want to send back to the client.
 
110
  return $result;
 
111
}
 
112
 
 
113
# A much simpler and less verbose version of the above function would be:
 
114
function reverse_fn_fast($job)
 
115
{
 
116
  return strrev($job->workload());
 
117
}
 
118
 
 
119
?>
 
120
 
 
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:
 
122
 
 
123
    php /tmp/reverse_worker.php
 
124
 
 
125
The above will connect to the gearman-job-server and wait for a job. This is a syncronous job.
 
126
 
 
127
From a machine with access to the gearman-job-server, run the following code:
 
128
 
 
129
<?php
 
130
 
 
131
# Create our client object.
 
132
$gmclient= new GearmanClient();
 
133
 
 
134
# Add job server (gearman-job-server).
 
135
$gmworker->addServer("gearman-job-server");
 
136
 
 
137
echo "Sending job\n";
 
138
 
 
139
# Send reverse job
 
140
do
 
141
{
 
142
  $result = $gmclient->do("reverse", "Hello!");
 
143
 
 
144
  # Check for various return packets and errors.
 
145
  switch($gmclient->returnCode())
 
146
  {
 
147
    case GEARMAN_WORK_DATA:
 
148
      echo "Data: $result\n";
 
149
      break;
 
150
    case GEARMAN_WORK_STATUS:
 
151
      list($numerator, $denominator)= $gmclient->doStatus();
 
152
      echo "Status: $numerator/$denominator complete\n";
 
153
      break;
 
154
    case GEARMAN_WORK_FAIL:
 
155
      echo "Failed\n";
 
156
      exit;
 
157
    case GEARMAN_SUCCESS:
 
158
      break;
 
159
    default:
 
160
      echo "RET: " . $gmclient->returnCode() . "\n";
 
161
      exit;
 
162
  }
 
163
}
 
164
while($gmclient->returnCode() != GEARMAN_SUCCESS);
 
165
 
 
166
?>
 
167
 
 
168
Save the above code as /tmp/reverse_client.php and run:
 
169
 
 
170
    php reverse_client.php
 
171
 
 
172
The above example will output something similar to:
 
173
 
 
174
php reverse_worker.php
 
175
Starting
 
176
Waiting for job...
 
177
Received job: H:gearman-job-server:36
 
178
Workload: Hello! (6)
 
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
 
185
Result: !olleH
 
186
 
 
187
php reverse_client.php
 
188
Starting
 
189
Sending job
 
190
Status: 1/6 complete
 
191
Status: 2/6 complete
 
192
Status: 3/6 complete
 
193
Status: 4/6 complete
 
194
Status: 5/6 complete
 
195
Status: 6/6 complete
 
196
Success: !olleH
 
197
 
 
198
A simple Python test
 
199
 
 
200
On a worker ( or all of them ) save the following file as /tmp/reverse_worker.py:
 
201
 
 
202
gm_worker = gearman.GearmanWorker(['localhost:4730'])
 
203
 
 
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)
 
208
 
 
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)
 
212
 
 
213
# Enter our work loop and call gm_worker.after_poll() after each time we timeout/see socket activity
 
214
gm_worker.work()
 
215
 
 
216
Run the above code:
 
217
 
 
218
python /tmp/reverse_worker.py
 
219
 
 
220
On a client machine save the following code as /tmp/reverse_client.py:
 
221
 
 
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
 
229
 
 
230
gm_client = gearman.GearmanClient(['localhost:4730', 'otherhost:4730'])
 
231
 
 
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)
 
236
 
 
237
 
 
238
Run the above code:
 
239
 
 
240
python /tmp/reverse_client.py