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.



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

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

Instance Attribute Details

#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.



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

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/iron_response/batch.rb', line 86

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

    case runtime
    when "ruby"
      prepare_ruby_code(@code)
    when "node"
      prepare_node_code(@code)
    end
  end

  @code
end

#create_code!(options = {}) ⇒ Object



107
108
109
# File 'lib/iron_response/batch.rb', line 107

def create_code!(options={})
  @client.codes.create(code, options)
end

#get_aws_s3_response(task_id) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/iron_response/batch.rb', line 47

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



60
61
62
63
64
65
66
67
68
69
# File 'lib/iron_response/batch.rb', line 60

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



38
39
40
41
42
43
44
45
# File 'lib/iron_response/batch.rb', line 38

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



103
104
105
# File 'lib/iron_response/batch.rb', line 103

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

#prepare_node_code(code) ⇒ Object



80
81
82
83
84
# File 'lib/iron_response/batch.rb', line 80

def prepare_node_code(code)
  code.runtime = "node"
  code.dir     = "node_modules"
  code.file    = "package.json"
end

#prepare_ruby_code(code) ⇒ Object



76
77
78
# File 'lib/iron_response/batch.rb', line 76

def prepare_ruby_code(code)
  code.runtime = "ruby"
end

#run!Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/iron_response/batch.rb', line 26

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

#runtimeObject



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

def runtime
  return "ruby" if @worker.end_with?(".rb")
  return "node" if @worker.end_with?(".js")
end

#worker_languageObject



22
23
24
# File 'lib/iron_response/batch.rb', line 22

def worker_language
  is_ruby 
end

#worker_nameObject



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

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