iron_worker_ng 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,47 +1,184 @@
1
- # Basic Usage
1
+ # Introduction
2
2
 
3
- Visit http://iron.io for more details.
3
+ To run your code in cloud you need to do two things - upload code package to be executed and queue or schedule it for execution. While you can use REST APIs for that, it's always better to use IronWorker library created specificaly for your language of choice such as IronWorkerNG.
4
4
 
5
- ## Create Worker
5
+ # Preparing Environment
6
6
 
7
- You can just put any code into worker or can create class with name matching file name (e.g MyWorker class in my_worker.rb) and run method.
7
+ You'll need to register at http://iron.io and get your credintials to use IronWorkerNG. Each account can have unlimited number of project, so take advantage of it by creating separate projects for development, testing and production. Each project is identified by unique project_id and requiers access token to do any actions on it like uploading or queueing workers.
8
8
 
9
- ```ruby
10
- require 'active_record' # just in case
9
+ Also you'll need working ruby 1.9 interpreter and IronWorkerNG gem. Install it using following command.
10
+
11
+ ```sh
12
+ gem install iron_worker_ng
13
+ ```
14
+
15
+ It's recommended that you'll install typhoeus gem as well for faster API interaction.
11
16
 
12
- # do something fun
13
- puts params[:foo]
17
+ ```sh
18
+ gem install typhoeus
19
+ ```
20
+
21
+ # Creating Worker
22
+
23
+ IronWorkerNG ruby worker is common ruby code. It can be as simple as show below and as complex as you want.
24
+
25
+ ```ruby
26
+ puts "I'm worker"
27
+ puts "My task_id is #{@iron_worker_task_id}"
28
+ puts "I'm executing inside #{@iron_io_project_id} and was queued using #{@iron_io_token} token"
29
+ puts "I got '#{params}' parameters"
14
30
  ```
15
31
 
16
- ## Create Runner
32
+ Everything your worker will output to stdout will be logged and available for your review when worker will finish execution.
33
+
34
+ # Create Code Package
35
+
36
+ As this code will be executed on the cloud, you'll need to supply it with all necessary gems and supplementary data. IronWorkerNG::Code::Ruby will help you to do this.
17
37
 
18
38
  ```ruby
19
- require 'iron_worker_ng'
39
+ code = IronWorkerNG::Code::Ruby.new
40
+
41
+ code.merge_worker 'my_worker.rb'
42
+ code.merge_file '../lib/utils.rb'
43
+ code.merge_dir '../config'
44
+ code.merge_gem 'activerecord'
45
+ ```
46
+
47
+ ## IronWorkerNG::Code::Ruby API
20
48
 
21
- client = IronWorkerNG::Client.new('IRON_IO_TOKEN', 'IRON_IO_PROJECT_ID')
49
+ Please note that this API will help you to create code package but to upload it to IronWorker servers you'll need to use IronWorkerNG::Client API.
22
50
 
51
+ ### initialize(name = nil)
52
+
53
+ Will create new code package with specified name. If name is omited, camel-cased worker's file name will be used.
54
+
55
+ ```ruby
23
56
  code = IronWorkerNG::Code::Ruby.new
24
- code.merge_worker 'path/to/my_worker.rb'
25
- code.merge_gem 'activerecord' # we are using it in our worker
57
+ code_with_name = IronWorkerNG::Code::Ruby.new('CoolWorker')
58
+ ```
59
+
60
+ ### name()
26
61
 
27
- # you can use hash_string to check if you need to reupload code
28
- # note that hash_string check is fast while code upload can take a while (depends on how much things you merged)
62
+ Will return code package name.
63
+
64
+ ```ruby
65
+ puts code.name
66
+ ```
67
+
68
+ ### hash_string()
69
+
70
+ ```
29
71
  puts code.hash_string
72
+ ```
73
+
74
+ Will return code package hash string. If you want prevent uploading unchanged code packages, you can use it to check if any changes were made. As it's verty efficient, it shouldn't cause any performace impact.
75
+
76
+ ### merge_file(path, dest = '')
77
+
78
+ ```ruby
79
+ code.merge_file '../config/database.yml' # will be in the same directory as worker
80
+ code.merge_file 'clients.csv', 'information/clients' # will be in information/clients subdirectory
81
+ ```
82
+
83
+ Merges file located at path into the code package. You can use optional dest to set destination directory which will be automatically created.
84
+
85
+ ### merge_dir(path, dest = '')
86
+
87
+ Recursively merges directory located at path into the code package.
88
+
89
+ ```ruby
90
+ code.merge_dir '../config' # will be in the same directory as worker
91
+ code.merge_dir 'lib', 'utils' # will be in utils subdirectory, accessible as utils/lib
92
+ ```
93
+
94
+ ### merge_worker(path, name = nil)
95
+
96
+ Merges worker located at path. If name is omited, camel-cased file name will be used. You can have only one worker merged per code package.
97
+
98
+ ```ruby
99
+ code.merge_worker 'my_worker.rb' # name will be MyWorker
100
+ ```
101
+
102
+ ### merge_gem(name, version = '>= 0')
103
+
104
+ Merges gem with dependencies. Please note that gems which contains binary extensions will not be merged at the moment, however we have sane set of such gems preinstalled at IronWorker servers. You can use version constrains if you need specific gem version.
105
+
106
+ ```ruby
107
+ code.merge_gem 'activerecord'
108
+ code.merge_gem 'paperclip', '< 3.0.0,>= 2.1.0'
109
+ ```
110
+
111
+ ### merge_gemfile(path, *groups)
112
+
113
+ Merges all gems from specified groups in Gemfile. Please note that it'll just merge gems and not auto require them when executing worker.
114
+
115
+ ```ruby
116
+ code.merge_gemfile '../Gemfile', 'common', 'worker' # merges gems from common and worker groups
117
+ ```
118
+
119
+ # Using IronWorker
120
+
121
+ When you have your code package you are ready to run it on IronWorker servers.
122
+
123
+ ```ruby
124
+ client = IronWorkerNG::Client.new(:token => IRON_IO_TOKEN', :project_id => 'IRON_IO_PROJECT_ID')
30
125
 
31
126
  client.codes.create(code)
127
+ client.tasks.create('MyWorker', {:client => 'Joe'})
128
+ ```
129
+
130
+ ## IronWorker::Client API
32
131
 
33
- client.tasks.create('MyWorker', :foo => 'bar')
132
+ You can use IronWorkerNG::Client API to upload code packages, queue tasks, created schedules and more.
133
+
134
+ ### initialize(options = {})
135
+
136
+ Creates client object used for all interactions with IronWorker servers.
137
+
138
+ ```ruby
139
+ client = IronWorkerNG::Client.new(:token => 'IRON_IO_TOKEN', :project_id => 'IRON_IO_PROJECT_ID')
34
140
  ```
35
141
 
36
- ## CLI
142
+ ### codes.list(options = {})
37
143
 
38
- Iron Worker NG got nice CLI tool bundled. Here is small example how to get your code running in cloud in few seconds.
144
+ Returns array of information about uploaded codes. Visit http://dev.iron.io/worker/reference/api/#list_code_packages for more information about options and code object format.
39
145
 
40
- ```sh
41
- % cat my_worker.rb
42
- puts "my name is #{params[:name]} and it is #{params[:it]}"
43
- % iron_worker_ng codes.create --ruby-merge-worker my_worker.rb
44
- % TASK_ID=`iron_worker_ng tasks.create -n MyWorker -p name,worker -p it,fun --print-id`
45
- % iron_worker_ng tasks.log -t $TASK_ID --live
46
- my name is worker and it is fun
146
+ ```ruby
147
+ client.codes.list.each do |code|
148
+ puts code.inspect
149
+ end
150
+ ```
151
+
152
+ ### codes.get(code_id)
153
+
154
+ Returns information about uploaded code with specified code_id. Viist http://dev.iron.io/worker/reference/api/#get_info_about_a_code_package for more information about code object format.
155
+
156
+ ```ruby
157
+ puts client.codes.get('1234567890').name
158
+ ```
159
+
160
+ ### codes.create(code)
161
+
162
+ Uploads IronWorkerNG::Code::Ruby object to IronWorker servers.
163
+
164
+ ```ruby
165
+ client.codes.create(code)
166
+ ```
167
+
168
+ ### codes.delete(code_id)
169
+
170
+ Deletes code with specified code_id from IronWorker servers.
171
+
172
+ ```ruby
173
+ client.codes.delete('1234567890')
174
+ ```
175
+
176
+ ### codes.revisions(code_id, options = {})
177
+
178
+ Returns array of revision information for code package with specified code_id. Visit http://dev.iron.io/worker/reference/api/#list_code_package_revisions for more information about options and revision information object.
179
+
180
+ ```ruby
181
+ client.codes.revisions('1234567890').each do |revision|
182
+ puts revision.inspect
183
+ end
47
184
  ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.8
1
+ 0.1.9
@@ -17,15 +17,15 @@ module IronWorkerNG
17
17
  attr_accessor :api_version
18
18
  attr_accessor :user_agent
19
19
 
20
- def initialize(token, project_id, params = {})
21
- @token = token
22
- @project_id = project_id
20
+ def initialize(options = {})
21
+ @token = options[:token] || options['token']
22
+ @project_id = options[:project_id] || options['project_id']
23
23
 
24
- @scheme = params[:scheme] || 'https'
25
- @host = params[:host] || IronWorkerNG::APIClient::AWS_US_EAST_HOST
26
- @port = params[:port] || 443
27
- @api_version = params[:api_version] || 2
28
- @user_agent = params[:user_agent] || 'iron_worker_ng-' + IronWorkerNG.version
24
+ @scheme = options[:scheme] || 'https'
25
+ @host = options[:host] || IronWorkerNG::APIClient::AWS_US_EAST_HOST
26
+ @port = options[:port] || 443
27
+ @api_version = options[:api_version] || 2
28
+ @user_agent = options[:user_agent] || 'iron_worker_ng-' + IronWorkerNG.version
29
29
 
30
30
  @url = "#{scheme}://#{host}:#{port}/#{api_version}/"
31
31
 
@@ -81,8 +81,8 @@ module IronWorkerNG
81
81
  JSON.parse(response.body)
82
82
  end
83
83
 
84
- def codes_list(params = {})
85
- parse_response(get("projects/#{@project_id}/codes", params))
84
+ def codes_list(options = {})
85
+ parse_response(get("projects/#{@project_id}/codes", options))
86
86
  end
87
87
 
88
88
  def codes_get(id)
@@ -97,24 +97,24 @@ module IronWorkerNG
97
97
  parse_response(delete("projects/#{@project_id}/codes/#{id}"))
98
98
  end
99
99
 
100
- def codes_revisions(id, params = {})
101
- parse_response(get("projects/#{@project_id}/codes/#{id}/revisions", params))
100
+ def codes_revisions(id, options = {})
101
+ parse_response(get("projects/#{@project_id}/codes/#{id}/revisions", options))
102
102
  end
103
103
 
104
- def codes_download(id, params = {})
105
- parse_response(get("projects/#{@project_id}/codes/#{id}/download", params), false)
104
+ def codes_download(id, options = {})
105
+ parse_response(get("projects/#{@project_id}/codes/#{id}/download", options), false)
106
106
  end
107
107
 
108
- def tasks_list(params = {})
109
- parse_response(get("projects/#{@project_id}/tasks", params))
108
+ def tasks_list(options = {})
109
+ parse_response(get("projects/#{@project_id}/tasks", options))
110
110
  end
111
111
 
112
112
  def tasks_get(id)
113
113
  parse_response(get("projects/#{@project_id}/tasks/#{id}"))
114
114
  end
115
115
 
116
- def tasks_create(code_name, payload, params = {})
117
- parse_response(post("projects/#{@project_id}/tasks", {:tasks => [{:code_name => code_name, :payload => payload}.merge(params)]}))
116
+ def tasks_create(code_name, payload, options = {})
117
+ parse_response(post("projects/#{@project_id}/tasks", {:tasks => [{:code_name => code_name, :payload => payload}.merge(options)]}))
118
118
  end
119
119
 
120
120
  def tasks_cancel(id)
@@ -129,23 +129,23 @@ module IronWorkerNG
129
129
  parse_response(get("projects/#{@project_id}/tasks/#{id}/log"), false)
130
130
  end
131
131
 
132
- def tasks_set_progress(id, params = {})
133
- parse_response(post("projects/#{@project_id}/tasks/#{id}/progress", params))
132
+ def tasks_set_progress(id, options = {})
133
+ parse_response(post("projects/#{@project_id}/tasks/#{id}/progress", options))
134
134
  end
135
135
 
136
- def schedules_list(params = {})
137
- parse_response(get("projects/#{@project_id}/schedules", params))
136
+ def schedules_list(options = {})
137
+ parse_response(get("projects/#{@project_id}/schedules", options))
138
138
  end
139
139
 
140
140
  def schedules_get(id)
141
141
  parse_response(get("projects/#{@project_id}/schedules/#{id}"))
142
142
  end
143
143
 
144
- def schedules_create(code_name, payload, params = {})
145
- params[:start_at] = params[:start_at].iso8601 if (not params[:start_at].nil?) && params[:start_at].class == Time
146
- params[:end_at] = params[:end_at].iso8601 if (not params[:end_at].nil?) && params[:end_at].class == Time
144
+ def schedules_create(code_name, payload, options = {})
145
+ options[:start_at] = options[:start_at].iso8601 if (not options[:start_at].nil?) && options[:start_at].class == Time
146
+ options[:end_at] = options[:end_at].iso8601 if (not options[:end_at].nil?) && options[:end_at].class == Time
147
147
 
148
- parse_response(post("projects/#{@project_id}/schedules", {:schedules => [{:code_name => code_name, :payload => payload}.merge(params)]}))
148
+ parse_response(post("projects/#{@project_id}/schedules", {:schedules => [{:code_name => code_name, :payload => payload}.merge(options)]}))
149
149
  end
150
150
 
151
151
  def schedules_cancel(id)
@@ -22,8 +22,8 @@ module IronWorkerNG
22
22
  class Client
23
23
  attr_reader :api
24
24
 
25
- def initialize(token, project_id, params = {})
26
- @api = IronWorkerNG::APIClient.new(token, project_id, params)
25
+ def initialize(options = {})
26
+ @api = IronWorkerNG::APIClient.new(options)
27
27
  end
28
28
 
29
29
  def method_missing(name, *args, &block)
@@ -34,8 +34,8 @@ module IronWorkerNG
34
34
  end
35
35
  end
36
36
 
37
- def codes_list(params = {})
38
- @api.codes_list(params)['codes'].map { |c| OpenStruct.new(c) }
37
+ def codes_list(options = {})
38
+ @api.codes_list(options)['codes'].map { |c| OpenStruct.new(c) }
39
39
  end
40
40
 
41
41
  def codes_get(code_id)
@@ -56,20 +56,16 @@ module IronWorkerNG
56
56
  true
57
57
  end
58
58
 
59
- def codes_revisions(code_id, params = {})
60
- @api.codes_revisions(code_id, params)['revisions'].map { |c| OpenStruct.new(c) }
59
+ def codes_revisions(code_id, options = {})
60
+ @api.codes_revisions(code_id, options)['revisions'].map { |c| OpenStruct.new(c) }
61
61
  end
62
62
 
63
- def codes_download(code_id, params = {})
64
- @api.codes_download(code_id, params)
63
+ def codes_download(code_id, options = {})
64
+ @api.codes_download(code_id, options)
65
65
  end
66
66
 
67
- def tasks_get(task_id)
68
- OpenStruct.new(@api.tasks_get(task_id))
69
- end
70
-
71
- def tasks_list(params = {})
72
- @api.tasks_list(params)['tasks'].map { |t| OpenStruct.new(t) }
67
+ def tasks_list(options = {})
68
+ @api.tasks_list(options)['tasks'].map { |t| OpenStruct.new(t) }
73
69
  end
74
70
 
75
71
  def tasks_get(task_id)
@@ -77,7 +73,7 @@ module IronWorkerNG
77
73
  end
78
74
 
79
75
  def tasks_create(code_name, params = {}, options = {})
80
- res = @api.tasks_create(code_name, {:project_id => @api.project_id, :token => @api.token, :params => params}.to_json, options)
76
+ res = @api.tasks_create(code_name, {:token => @api.token, :project_id => @api.project_id, :params => params}.to_json, options)
81
77
 
82
78
  OpenStruct.new(res['tasks'][0])
83
79
  end
@@ -98,25 +94,25 @@ module IronWorkerNG
98
94
  @api.tasks_log(task_id)
99
95
  end
100
96
 
101
- def tasks_set_progress(task_id, params = {})
102
- @api.tasks_set_progress(task_id, params)
97
+ def tasks_set_progress(task_id, options = {})
98
+ @api.tasks_set_progress(task_id, options)
103
99
 
104
100
  true
105
101
  end
106
102
 
107
- def tasks_wait_for(task_id, params = {})
108
- params[:sleep] ||= 5
103
+ def tasks_wait_for(task_id, options = {})
104
+ options[:sleep] ||= 5
109
105
 
110
106
  task = tasks_get(task_id)
111
107
  while task.status == 'queued' || task.status == 'running'
112
108
  yield task if block_given?
113
- sleep params[:sleep]
109
+ sleep options[:sleep]
114
110
  task = tasks_get(task_id)
115
111
  end
116
112
  end
117
113
 
118
- def schedules_list(params = {})
119
- @api.schedules_list(params)['schedules'].map { |s| OpenStruct.new(s) }
114
+ def schedules_list(options = {})
115
+ @api.schedules_list(options)['schedules'].map { |s| OpenStruct.new(s) }
120
116
  end
121
117
 
122
118
  def schedules_get(schedule_id)
@@ -124,7 +120,7 @@ module IronWorkerNG
124
120
  end
125
121
 
126
122
  def schedules_create(code_name, params = {}, options = {})
127
- res = @api.schedules_create(code_name, {:project_id => @api.project_id, :token => @api.token, :params => params}.to_json, options)
123
+ res = @api.schedules_create(code_name, {:token => @api.token, :project_id => @api.project_id, :params => params}.to_json, options)
128
124
 
129
125
  OpenStruct.new(res['schedules'][0])
130
126
  end
@@ -12,7 +12,7 @@ module IronWorkerNG
12
12
  end
13
13
 
14
14
  def hash_string
15
- Digest::MD5.hexdigest(@path + @klass)
15
+ Digest::MD5.hexdigest(@path + @klass + File.mtime(@path).to_i.to_s)
16
16
  end
17
17
 
18
18
  def bundle(zip)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iron_worker_ng
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-03-21 00:00:00.000000000 Z
13
+ date: 2012-03-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: zip
@@ -139,7 +139,7 @@ files:
139
139
  - lib/iron_worker_ng/feature/ruby/merge_gemfile.rb
140
140
  - lib/iron_worker_ng/feature/ruby/merge_worker.rb
141
141
  - lib/iron_worker_ng/version.rb
142
- homepage: http://www.iron.io
142
+ homepage: https://github.com/iron-io/iron_worker_ruby_ng
143
143
  licenses: []
144
144
  post_install_message:
145
145
  rdoc_options: []
@@ -153,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
153
153
  version: '0'
154
154
  segments:
155
155
  - 0
156
- hash: 148197165
156
+ hash: 534011215
157
157
  required_rubygems_version: !ruby/object:Gem::Requirement
158
158
  none: false
159
159
  requirements: