~cgb-cs/appscale/appscale-main

« back to all changes in this revision

Viewing changes to AppController/doc/TaskQueueSQS.html

  • Committer: Chris Bunch
  • Date: 2012-02-27 07:19:48 UTC
  • Revision ID: cgb@cs.ucsb.edu-20120227071948-3uic3g1s3ph6cklo
added json validation for sqs interface, and regenerated coverage/rdoc

Show diffs side-by-side

added added

removed removed

Lines of Context:
203
203
 
204
204
    <div id="description">
205
205
      
 
206
<p><a href="TaskQueueSQS.html">TaskQueueSQS</a> provides a simple interface
 
207
(implementing <a href="TaskQueue.html">TaskQueue</a>) to Amazon’s Simple
 
208
Queue Service (SQS). SQS is externally managed and hosted by Amazon, so we
 
209
don’t have to worry about deploying it. TODO(cgb): SQS is eventually
 
210
consistent, so it is possible that we could receive the same message twice,
 
211
and thus execute the same task twice.</p>
 
212
 
206
213
    </div>
207
214
 
208
215
    <!-- Constants -->
213
220
      
214
221
        <dt><a name="NAME">NAME</a></dt>
215
222
        
216
 
        <dd class="description"></dd>
 
223
        <dd class="description"><p>The name of this queue. Since we always use our Executor to run tasks, and
 
224
store task info in SQS, we call it ‘executor-sqs’.</p></dd>
217
225
        
218
226
      
219
227
      </dl>
238
246
 
239
247
        <div class="method-description">
240
248
        
241
 
        
 
249
        <p>The access key, provided by AWS, that is required to access SQS.</p>
242
250
        
243
251
        </div>
244
252
      </div>
255
263
 
256
264
        <div class="method-description">
257
265
        
258
 
        
 
266
        <p>The secret key, provided by AWS, that is required to access SQS.</p>
259
267
        
260
268
        </div>
261
269
      </div>
282
290
 
283
291
        <div class="method-description">
284
292
          
285
 
          <p>creates a new SQS queue via the AWS RubyGem</p>
 
293
          <p>Creates a new SQS queue connection and queue to store / retrieve tasks
 
294
with, via the AWS RubyGem. We pull in the AWS RubyGem instead of the
 
295
RightScale AWS RubyGem since the RightScale gem doesn’t work with the
 
296
current version of SQS.</p>
286
297
          
287
298
 
288
299
          
289
300
          <div class="method-source-code"
290
301
            id="new-source">
291
302
<pre>
292
 
<span class="ruby-comment"># File ../Neptune/task_queue_sqs.rb, line 23</span>
 
303
<span class="ruby-comment"># File ../Neptune/task_queue_sqs.rb, line 36</span>
293
304
def initialize(credentials)
294
305
  if credentials.class != <span class="ruby-constant">Hash</span>
295
306
    raise <span class="ruby-constant">BadConfigurationException</span>.new
338
349
 
339
350
        <div class="method-description">
340
351
          
341
 
          <p>returns all the credentials needed to access this queue for SQS, it’s
 
352
          <p>Returns all the credentials needed to access this queue. For SQS, it’s 
342
353
just the access key and secret key</p>
343
354
          
344
355
 
346
357
          <div class="method-source-code"
347
358
            id="get_creds-source">
348
359
<pre>
349
 
<span class="ruby-comment"># File ../Neptune/task_queue_sqs.rb, line 67</span>
 
360
<span class="ruby-comment"># File ../Neptune/task_queue_sqs.rb, line 93</span>
350
361
def get_creds()
351
362
  return {<span class="ruby-string">'@EC2_ACCESS_KEY'</span> =&gt; <span class="ruby-ivar">@EC2_ACCESS_KEY</span>,
352
363
    <span class="ruby-string">'@EC2_SECRET_KEY'</span> =&gt; <span class="ruby-ivar">@EC2_SECRET_KEY</span>}
374
385
 
375
386
        <div class="method-description">
376
387
          
377
 
          
 
388
          <p>Retrieves an item from the queue, returning the Hash version of the
 
389
JSON-dumped data. We should be the only ones writing to the queue, but just
 
390
in case we aren’t, we need to validate that the data we’re receiving
 
391
from the queue is data that we can load via JSON. TODO(cgb): Receiving and
 
392
deleting the message isn’t an atomic operation, and since SQS is
 
393
eventually consistent, could this result in two readers getting the same
 
394
item? If so, would using a lock on the queue (making the operation atomic)
 
395
solve the problem?</p>
378
396
          
379
397
 
380
398
          
381
399
          <div class="method-source-code"
382
400
            id="pop-source">
383
401
<pre>
384
 
<span class="ruby-comment"># File ../Neptune/task_queue_sqs.rb, line 55</span>
 
402
<span class="ruby-comment"># File ../Neptune/task_queue_sqs.rb, line 78</span>
385
403
def pop()
386
404
  json_item = <span class="ruby-ivar">@queue</span>.receive_message()
387
405
  return nil if json_item.nil?  <span class="ruby-comment"># occurs when the queue is empty</span>
388
 
  json_item.delete  <span class="ruby-comment"># TODO(cgb) - maybe delete messages after the task is done</span>
 
406
  json_item.delete()
389
407
 
390
 
  item = <span class="ruby-constant">JSON</span>.load(json_item.body)
391
 
  return item
 
408
  begin
 
409
    return <span class="ruby-constant">JSON</span>.load(json_item.body())
 
410
  rescue <span class="ruby-constant">JSON</span>::<span class="ruby-constant">ParserError</span>  <span class="ruby-comment"># if somebody else wrote a non-JSON item</span>
 
411
    return pop()
 
412
  end
392
413
end</pre>
393
414
          </div>
394
415
          
413
434
 
414
435
        <div class="method-description">
415
436
          
416
 
          <p>stores a hash in the queue for later processing, by converting it to JSON</p>
 
437
          <p>Stores a Hash in the queue for later processing. Since SQS can’t store
 
438
items in Hash format, we use the JSON library to dump it to a string, which
 
439
SQS can store.</p>
417
440
          
418
441
 
419
442
          
420
443
          <div class="method-source-code"
421
444
            id="push-source">
422
445
<pre>
423
 
<span class="ruby-comment"># File ../Neptune/task_queue_sqs.rb, line 45</span>
 
446
<span class="ruby-comment"># File ../Neptune/task_queue_sqs.rb, line 60</span>
424
447
def push(item)
425
448
  if item.class != <span class="ruby-constant">Hash</span>
426
449
    raise <span class="ruby-constant">BadConfigurationException</span>.new
452
475
 
453
476
        <div class="method-description">
454
477
          
455
 
          <p>returns the number of messages in the queue</p>
 
478
          <p>Returns the number of messages in the queue, which in SQS is the number of
 
479
visible messages (there should be no invisible messages since we always
 
480
immediately delete messages upon receiving them).</p>
456
481
          
457
482
 
458
483
          
459
484
          <div class="method-source-code"
460
485
            id="size-source">
461
486
<pre>
462
 
<span class="ruby-comment"># File ../Neptune/task_queue_sqs.rb, line 74</span>
 
487
<span class="ruby-comment"># File ../Neptune/task_queue_sqs.rb, line 102</span>
463
488
def size()
464
489
  return <span class="ruby-ivar">@queue</span>.visible_messages
465
490
end</pre>
495
520
          <div class="method-source-code"
496
521
            id="to_s-source">
497
522
<pre>
498
 
<span class="ruby-comment"># File ../Neptune/task_queue_sqs.rb, line 83</span>
 
523
<span class="ruby-comment"># File ../Neptune/task_queue_sqs.rb, line 111</span>
499
524
def to_s()
500
525
  access_key = <span class="ruby-constant">HelperFunctions</span>.obscure_string(<span class="ruby-ivar">@EC2_ACCESS_KEY</span>)
501
526
  secret_key = <span class="ruby-constant">HelperFunctions</span>.obscure_string(<span class="ruby-ivar">@EC2_SECRET_KEY</span>)