iron_response 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/iron_response.rb CHANGED
@@ -1,156 +1,7 @@
1
1
  require "iron_response/version"
2
- require "iron_worker_ng"
3
- require "aws/s3"
4
- require "iron_cache"
5
- require "json"
2
+ require "iron_response/common"
3
+ require "iron_response/batch"
4
+ require "iron_response/worker"
6
5
 
7
6
  module IronResponse
8
- module Common
9
- S3_PATH = "tasks"
10
-
11
- DEFAULT_S3_BUCKET = "iron_response"
12
- DEFAULT_IRON_CACHE_CACHE_NAME = "iron_response"
13
-
14
- def Common.s3_path(task_id)
15
- "#{S3_PATH}/#{task_id}.json"
16
- end
17
-
18
- def Common.s3_bucket_name(config)
19
- config[:aws_s3][:bucket].nil? ? DEFAULT_S3_BUCKET : @config[:aws_s3][:bucket]
20
- end
21
-
22
- def Common.iron_cache_key(task_id)
23
- task_id
24
- end
25
-
26
- def Common.iron_cache_cache_name(config)
27
- config[:iron_io][:cache].nil? ? DEFAULT_IRON_CACHE_CACHE_NAME : @config[:iron_io][:cache]
28
- end
29
-
30
- def Common.response_provider(config)
31
- config[:aws_s3].nil? ? :iron_cache : :aws_s3
32
- end
33
- end
34
-
35
- class Worker
36
- def initialize(binding, &block)
37
- task_id = eval("@iron_task_id", binding)
38
- params = eval("params", binding)
39
- @config = params[:config]
40
-
41
- case IronResponse::Common.response_provider(@config)
42
- when :iron_cache
43
- send_data_to_iron_cache(params, task_id, block.call)
44
- when :aws_s3
45
- send_data_to_s3(params, task_id, block.call)
46
- end
47
- end
48
-
49
- def send_data_to_iron_cache(params, task_id, data)
50
- cache_client = IronCache::Client.new(@config[:iron_io])
51
- cache_name = IronResponse::Common.iron_cache_cache_name(@config)
52
- cache = cache_client.cache(cache_name)
53
-
54
- key = IronResponse::Common.iron_cache_key(task_id)
55
- value = data.to_json
56
-
57
- cache.put(key, value)
58
- end
59
-
60
- def send_data_to_s3(params, task_id, data)
61
- aws_s3 = @config[:aws_s3]
62
- AWS::S3::Base.establish_connection! access_key_id: aws_s3[:access_key_id],
63
- secret_access_key: aws_s3[:secret_access_key]
64
-
65
- path = IronResponse::Common.s3_path(task_id)
66
- bucket_name = IronResponse::Common.s3_bucket_name(@config)
67
- value = data.to_json
68
-
69
- AWS::S3::S3Object.store(path, value, bucket_name)
70
- end
71
- end
72
-
73
- class Batch
74
- attr_accessor :config
75
- attr_accessor :worker
76
- attr_accessor :params_array
77
- attr_accessor :auto_update_worker
78
-
79
- def initialize
80
- @config = {}
81
- end
82
-
83
- def worker_name
84
- @worker.split("/").last.split(".rb").first
85
- end
86
-
87
- def run!
88
- @client = IronWorkerNG::Client.new(@config[:iron_io])
89
-
90
- if @auto_update_worker
91
- create_code!
92
- end
93
-
94
- task_ids = params_array.map do |params|
95
- params[:config] = @config
96
- @client.tasks.create(worker_name, params)._id
97
- end
98
-
99
- task_ids.map do |task_id|
100
- get_response_from_task_id(@client.tasks.wait_for(task_id)._id)
101
- end
102
- end
103
-
104
- def get_response_from_task_id(task_id)
105
- case IronResponse::Common.response_provider(@config)
106
- when :iron_cache
107
- get_iron_cache_response(task_id)
108
- when :aws_s3
109
- get_aws_s3_response(task_id)
110
- end
111
- end
112
-
113
- def get_aws_s3_response(task_id)
114
- aws_s3 = @config[:aws_s3]
115
- AWS::S3::Base.establish_connection! access_key_id: aws_s3[:access_key_id],
116
- secret_access_key: aws_s3[:secret_access_key]
117
-
118
- bucket_name = IronResponse::Common.s3_bucket_name(@config)
119
- bucket = AWS::S3::Bucket.find(bucket_name)
120
- path = IronResponse::Common.s3_path(task_id)
121
- response = bucket[path].value
122
-
123
- JSON.parse(response)
124
- end
125
-
126
- def get_iron_cache_response(task_id)
127
- cache_client = IronCache::Client.new(@config[:iron_io])
128
- cache_name = IronResponse::Common.iron_cache_cache_name(@config)
129
- cache = cache_client.cache(cache_name)
130
-
131
- key = IronResponse::Common.iron_cache_key(task_id)
132
- value = cache.get(key).value
133
-
134
- JSON.parse(value)
135
- end
136
-
137
- def code
138
- if @code.nil?
139
- @code = IronWorkerNG::Code::Ruby.new(exec: @worker)
140
- @code.name = worker_name
141
- @code.merge_gem("iron_response", IronResponse::VERSION) # bootstraps the current version with the worker
142
- @code.runtime = "ruby"
143
- end
144
-
145
- @code
146
- end
147
-
148
- def patch_code!
149
- @client.codes.patch(code)
150
- end
151
-
152
- def create_code!
153
- @client.codes.create(code)
154
- end
155
- end
156
7
  end
@@ -0,0 +1,95 @@
1
+ require "iron_cache"
2
+ require "iron_worker_ng"
3
+ require "aws/s3"
4
+ require "json"
5
+
6
+ module IronResponse
7
+ class Batch
8
+ attr_accessor :config
9
+ attr_accessor :worker
10
+ attr_accessor :params_array
11
+ attr_accessor :auto_update_worker
12
+ attr_accessor :results
13
+
14
+ def initialize
15
+ @results = []
16
+ @config = {}
17
+ end
18
+
19
+ def worker_name
20
+ @worker.split("/").last.split(".rb").first
21
+ end
22
+
23
+ def run!
24
+ @client = IronWorkerNG::Client.new(@config[:iron_io])
25
+
26
+ if @auto_update_worker
27
+ create_code!
28
+ end
29
+
30
+ task_ids = params_array.map do |params|
31
+ params[:config] = @config
32
+ @client.tasks.create(worker_name, params)._id
33
+ end
34
+
35
+ task_ids.each do |task_id|
36
+ @results << get_response_from_task_id(@client.tasks.wait_for(task_id)._id)
37
+ end
38
+
39
+ @results
40
+ end
41
+
42
+ def get_response_from_task_id(task_id)
43
+ case IronResponse::Common.response_provider(@config)
44
+ when :iron_cache
45
+ get_iron_cache_response(task_id)
46
+ when :aws_s3
47
+ get_aws_s3_response(task_id)
48
+ end
49
+ end
50
+
51
+ def get_aws_s3_response(task_id)
52
+ aws_s3 = @config[:aws_s3]
53
+ AWS::S3::Base.establish_connection! access_key_id: aws_s3[:access_key_id],
54
+ secret_access_key: aws_s3[:secret_access_key]
55
+
56
+ bucket_name = IronResponse::Common.s3_bucket_name(@config)
57
+ bucket = AWS::S3::Bucket.find(bucket_name)
58
+ path = IronResponse::Common.s3_path(task_id)
59
+ response = bucket[path].value
60
+
61
+ JSON.parse(response)
62
+ end
63
+
64
+ def get_iron_cache_response(task_id)
65
+ cache_client = IronCache::Client.new(@config[:iron_io])
66
+ cache_name = IronResponse::Common.iron_cache_cache_name(@config)
67
+ cache = cache_client.cache(cache_name)
68
+
69
+ key = IronResponse::Common.iron_cache_key(task_id)
70
+ value = cache.get(key).value
71
+
72
+ JSON.parse(value)
73
+ end
74
+
75
+ def code
76
+ if @code.nil?
77
+ @code = IronWorkerNG::Code::Ruby.new(exec: @worker)
78
+ @code.name = worker_name
79
+ @code.merge_gem("iron_response", IronResponse::VERSION) # bootstraps the current version with the worker
80
+ @code.runtime = "ruby"
81
+ end
82
+
83
+ @code
84
+ end
85
+
86
+ def patch_code!
87
+ @client.codes.patch(code)
88
+ end
89
+
90
+ def create_code!
91
+ @client.codes.create(code)
92
+ end
93
+ end
94
+
95
+ end
@@ -0,0 +1,28 @@
1
+ module IronResponse
2
+ module Common
3
+ S3_PATH = "tasks"
4
+
5
+ DEFAULT_S3_BUCKET = "iron_response"
6
+ DEFAULT_IRON_CACHE_CACHE_NAME = "iron_response"
7
+
8
+ def Common.s3_path(task_id)
9
+ "#{S3_PATH}/#{task_id}.json"
10
+ end
11
+
12
+ def Common.s3_bucket_name(config)
13
+ config[:aws_s3][:bucket].nil? ? DEFAULT_S3_BUCKET : @config[:aws_s3][:bucket]
14
+ end
15
+
16
+ def Common.iron_cache_key(task_id)
17
+ task_id
18
+ end
19
+
20
+ def Common.iron_cache_cache_name(config)
21
+ config[:iron_io][:cache].nil? ? DEFAULT_IRON_CACHE_CACHE_NAME : @config[:iron_io][:cache]
22
+ end
23
+
24
+ def Common.response_provider(config)
25
+ config[:aws_s3].nil? ? :iron_cache : :aws_s3
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module IronResponse
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -0,0 +1,43 @@
1
+ require "aws/s3"
2
+ require "iron_cache"
3
+ require "json"
4
+
5
+ module IronResponse
6
+ class Worker
7
+ def initialize(binding, &block)
8
+ task_id = eval("@iron_task_id", binding)
9
+ params = eval("params", binding)
10
+ @config = params[:config]
11
+
12
+ case IronResponse::Common.response_provider(@config)
13
+ when :iron_cache
14
+ send_data_to_iron_cache(params, task_id, block.call)
15
+ when :aws_s3
16
+ send_data_to_s3(params, task_id, block.call)
17
+ end
18
+ end
19
+
20
+ def send_data_to_iron_cache(params, task_id, data)
21
+ cache_client = IronCache::Client.new(@config[:iron_io])
22
+ cache_name = IronResponse::Common.iron_cache_cache_name(@config)
23
+ cache = cache_client.cache(cache_name)
24
+
25
+ key = IronResponse::Common.iron_cache_key(task_id)
26
+ value = data.to_json
27
+
28
+ cache.put(key, value)
29
+ end
30
+
31
+ def send_data_to_s3(params, task_id, data)
32
+ aws_s3 = @config[:aws_s3]
33
+ AWS::S3::Base.establish_connection! access_key_id: aws_s3[:access_key_id],
34
+ secret_access_key: aws_s3[:secret_access_key]
35
+
36
+ path = IronResponse::Common.s3_path(task_id)
37
+ bucket_name = IronResponse::Common.s3_bucket_name(@config)
38
+ value = data.to_json
39
+
40
+ AWS::S3::S3Object.store(path, value, bucket_name)
41
+ end
42
+ end
43
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iron_response
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-17 00:00:00.000000000 Z
12
+ date: 2013-08-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: iron_worker_ng
@@ -121,7 +121,10 @@ files:
121
121
  - Rakefile
122
122
  - iron_response.gemspec
123
123
  - lib/iron_response.rb
124
+ - lib/iron_response/batch.rb
125
+ - lib/iron_response/common.rb
124
126
  - lib/iron_response/version.rb
127
+ - lib/iron_response/worker.rb
125
128
  - test/configuration.rb.example
126
129
  - test/gem_dependency_test.rb
127
130
  - test/synopsis_test.rb