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

#initializeBatch

Returns a new instance of Batch.



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

def initialize
  @results = []
  @config  = {}
end

Instance Attribute Details

#auto_update_workerObject

Returns the value of attribute auto_update_worker.



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

def auto_update_worker
  @auto_update_worker
end

#configObject

Returns the value of attribute config.



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

def config
  @config
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.



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

def results
  @results
end

#workerObject

Returns the value of attribute worker.



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

def worker
  @worker
end

Instance Method Details

#codeObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/iron_response/batch.rb', line 75

def code
  if @code.nil?
    @code = IronWorkerNG::Code::Ruby.new(exec: @worker)
    @code.name = worker_name
    @code.merge_gem("iron_response", IronResponse::VERSION) # bootstraps the current version with the worker
    @code.runtime = "ruby"
  end

  @code
end

#create_code!Object



90
91
92
# File 'lib/iron_response/batch.rb', line 90

def create_code!
  @client.codes.create(code)
end

#get_aws_s3_response(task_id) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/iron_response/batch.rb', line 51

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].value

  JSON.parse(response)
end

#get_iron_cache_response(task_id) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/iron_response/batch.rb', line 64

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)
  value = cache.get(key).value

  JSON.parse(value)
end

#get_response_from_task_id(task_id) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/iron_response/batch.rb', line 42

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!Object



86
87
88
# File 'lib/iron_response/batch.rb', line 86

def patch_code!
  @client.codes.patch(code)
end

#run!Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/iron_response/batch.rb', line 23

def run!
  @client = IronWorkerNG::Client.new(@config[:iron_io])

  if @auto_update_worker
    create_code!
  end

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

  task_ids.each do |task_id|
    @results << get_response_from_task_id(@client.tasks.wait_for(task_id)._id)
  end

  @results
end

#worker_nameObject



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

def worker_name
  @worker.split("/").last.split(".rb").first
end