Class: IronResponse::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_response/batch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Batch

Returns a new instance of Batch.



14
15
16
17
# File 'lib/iron_response/batch.rb', line 14

def initialize(config)
  @config = config
  @client = IronWorkerNG::Client.new(@config[:iron_io])
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



12
13
14
# File 'lib/iron_response/batch.rb', line 12

def code
  @code
end

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/iron_response/batch.rb', line 8

def config
  @config
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/iron_response/batch.rb', line 9

def name
  @name
end

#params_arrayObject

Returns the value of attribute params_array.



10
11
12
# File 'lib/iron_response/batch.rb', line 10

def params_array
  @params_array
end

#resultsObject

Returns the value of attribute results.



11
12
13
# File 'lib/iron_response/batch.rb', line 11

def results
  @results
end

Instance Method Details

#create_code!(options = {}) ⇒ Object



72
73
74
75
# File 'lib/iron_response/batch.rb', line 72

def create_code!(options={})
  @code.merge_gem("iron_response", IronResponse::VERSION) if @code.runtime == "ruby" # bootstraps the current version with the worker
  @client.codes.create(@code, options)
end

#get_aws_s3_response(task_id) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/iron_response/batch.rb', line 44

def get_aws_s3_response(task_id)
  aws_s3 = @config[:aws_s3]
  AWS::S3::Base.establish_connection! access_key_id:     aws_s3[:access_key_id],
                                      secret_access_key: aws_s3[:secret_access_key]

  bucket_name = IronResponse::Common.s3_bucket_name(@config)
  bucket      = AWS::S3::Bucket.find(bucket_name)
  path        = IronResponse::Common.s3_path(task_id)
  response    = bucket[path]

  IronResponse::Common.handle_response(response, task_id, @client)
end

#get_iron_cache_response(task_id) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/iron_response/batch.rb', line 57

def get_iron_cache_response(task_id)
  cache_client = IronCache::Client.new(@config[:iron_io])
  cache_name   = IronResponse::Common.iron_cache_cache_name(@config)
  cache        = cache_client.cache(cache_name)
  
  key          = IronResponse::Common.iron_cache_key(task_id)
  response     = cache.get(key)

  IronResponse::Common.handle_response(response, task_id, @client)
end

#get_response_from_task_id(task_id) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/iron_response/batch.rb', line 35

def get_response_from_task_id(task_id)
  case IronResponse::Common.response_provider(@config)
  when :iron_cache
    get_iron_cache_response(task_id)
  when :aws_s3
    get_aws_s3_response(task_id)
  end
end

#patch_code!(options = {}) ⇒ Object



68
69
70
# File 'lib/iron_response/batch.rb', line 68

def patch_code!(options={})
  @client.codes.patch(@code, options)
end

#run!Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/iron_response/batch.rb', line 23

def run!
  task_ids = params_array.map do |params|
    params[:config] = @config
    @client.tasks.create(worker_name, params)._id
  end

  task_ids.map do |task_id|
    p "Fetching response for IronWorker task #{task_id}"
    get_response_from_task_id(@client.tasks.wait_for(task_id)._id)
  end
end

#worker_nameObject



19
20
21
# File 'lib/iron_response/batch.rb', line 19

def worker_name
  @code.name
end